diff --git a/OpenGL/cube/CMakeLists.txt b/OpenGL/cube/CMakeLists.txt index 8d6195b..ddb2525 100644 --- a/OpenGL/cube/CMakeLists.txt +++ b/OpenGL/cube/CMakeLists.txt @@ -12,6 +12,7 @@ set(IncludeDIR ./include) find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) find_package(PNG REQUIRED) +find_package(JPEG REQUIRED) set(THREADS_PREFER_PTHREAD_FLAD ON) find_package(Threads REQUIRED) @@ -22,6 +23,7 @@ include_directories(${executable} ${SDL2_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR} ${PNG_INCLUDE_DIR} + ${JPEG_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS} ${IncludeDIR} ) @@ -38,10 +40,10 @@ set_target_properties(${executable} PROPERTIES ) target_link_libraries(${executable} - ${PNG_LIBRARY} SDL2 SDL2_image PNG::PNG + JPEG::JPEG Threads::Threads OpenGL::OpenGL OpenGL::GL diff --git a/OpenGL/cube/cube.a b/OpenGL/cube/cube.a index 9fa95db..2d73e0b 100755 Binary files a/OpenGL/cube/cube.a and b/OpenGL/cube/cube.a differ diff --git a/OpenGL/cube/resources/shaders/simple.vert b/OpenGL/cube/resources/shaders/simple.vert index ec64491..1f6c1d4 100644 --- a/OpenGL/cube/resources/shaders/simple.vert +++ b/OpenGL/cube/resources/shaders/simple.vert @@ -1,15 +1,18 @@ #version 130 -in vec2 position; +in vec3 position; in vec2 texcoord; in vec3 colour; out vec3 Colour; out vec2 TexCoord; +uniform mat4 model; +uniform mat4 view; +uniform mat4 proj; + void main() { Colour = colour; TexCoord = texcoord; - gl_Position = vec4(position, 0.0, 1.0); - // Equivilent to vec4(position.x, position.y, 0.0, 1.0) + gl_Position = proj * view * model * vec4(position, 1.0); } diff --git a/OpenGL/cube/resources/textures/cobble.jpg b/OpenGL/cube/resources/textures/cobble.jpg deleted file mode 100644 index af329ac..0000000 Binary files a/OpenGL/cube/resources/textures/cobble.jpg and /dev/null differ diff --git a/OpenGL/cube/resources/textures/dirt.png b/OpenGL/cube/resources/textures/dirt.png new file mode 100644 index 0000000..617d353 Binary files /dev/null and b/OpenGL/cube/resources/textures/dirt.png differ diff --git a/OpenGL/cube/resources/textures/dirtside.png b/OpenGL/cube/resources/textures/dirtside.png deleted file mode 100644 index 574dfd2..0000000 Binary files a/OpenGL/cube/resources/textures/dirtside.png and /dev/null differ diff --git a/OpenGL/cube/resources/textures/grass_side.png b/OpenGL/cube/resources/textures/grass_side.png new file mode 100644 index 0000000..9ceef3b Binary files /dev/null and b/OpenGL/cube/resources/textures/grass_side.png differ diff --git a/OpenGL/cube/resources/textures/grass_top.png b/OpenGL/cube/resources/textures/grass_top.png new file mode 100644 index 0000000..9c4366c Binary files /dev/null and b/OpenGL/cube/resources/textures/grass_top.png differ diff --git a/OpenGL/cube/src/main.cpp b/OpenGL/cube/src/main.cpp index b2d3208..8eb17c3 100644 --- a/OpenGL/cube/src/main.cpp +++ b/OpenGL/cube/src/main.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -70,13 +72,50 @@ int main(int argc, char** argv) { glClearColor(0.1f, 0.45f, 0.9f, 1.0f); // GL Screen coordinates of a 2D triangle followed by the colour and the texture coordinates - GLint numOfVerticies = 28; + GLint numOfVerticies = 288; float vertices[] = { - // Positions Colour Texture - -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right - -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left + // Positions Colour Texture + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1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 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }; // Element array - order of verticies drawn @@ -111,10 +150,14 @@ int main(int argc, char** argv) { Shader simpleShader; simpleShader.load("./resources/shaders/simple").attatch().link().use(); + // Load texture from file - SDL_Surface* sur = IMG_Load("./resources/textures/dirtside.png"); - if (sur == NULL) + SDL_Surface* sur = IMG_Load("./resources/textures/dirt.png"); + if (sur == NULL) { std::cerr << "Failed to load image: " << IMG_GetError() << std::endl; + } else { + std::cout << "Loaded texture 'dirt.png'" << std::endl; + } // Set up a GL texture GLuint tex; glBindTexture(GL_TEXTURE_2D, tex); @@ -126,21 +169,42 @@ int main(int argc, char** argv) { GLint posAttrib = glGetAttribLocation(simpleShader.getProgram(), "position"); glEnableVertexAttribArray(posAttrib); - glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, - 7*sizeof(float), 0); + glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, + 8*sizeof(float), 0); GLint colAttrib = glGetAttribLocation(simpleShader.getProgram(), "colour"); glEnableVertexAttribArray(colAttrib); glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, - 7*sizeof(float), (void*)(2*sizeof(float))); + 8*sizeof(float), (void*)(3*sizeof(float))); GLint texAttrib = glGetAttribLocation(simpleShader.getProgram(), "texcoord"); glEnableVertexAttribArray(texAttrib); glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, - 7*sizeof(float), (void*)(5*sizeof(float))); + 8*sizeof(float), (void*)(6*sizeof(float))); - GLint triangleUniColour = glGetUniformLocation(simpleShader.getProgram(), "triangleColour"); - glUniform3f(triangleUniColour, 0.2f, 0.8f, 0.3f); + + // Model matrice + glm::mat4 model = glm::mat4(1.0f); + // Gets uniform for model matrice, to be used later + GLint uniTrans = glGetUniformLocation(simpleShader.getProgram(), "model"); + + // View matrice + glm::mat4 view = glm::lookAt( + glm::vec3(1.2f, 1.2f, 1.2f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 1.0f) + ); + // Get uniform and send it to the GPU + GLint uniView = glGetUniformLocation(simpleShader.getProgram(), "view"); + glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view)); + + // Projection matrice + glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1.0f, 1.0f, 10.0f); + // Get uniform and send it to the GPU + GLint uniProj = glGetUniformLocation(simpleShader.getProgram(), "proj"); + glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj)); + + glEnable(GL_DEPTH_TEST); SDL_Event event; while (!game->isWindowClosed) { @@ -149,10 +213,18 @@ int main(int argc, char** argv) { if (event.key.keysym.sym == SDLK_ESCAPE || event.type == SDL_QUIT) game->isWindowClosed = true; + // Rotate cube + model = glm::rotate(model, glm::radians(1.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + model = glm::rotate(model, glm::radians(1.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + // model = glm::rotate(model, glm::radians(1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); + glm::vec4 result = model * glm::vec4(1.0f, 0.0f, 0.0f, 1.0f); + glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model)); + // Clear the screen - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw a triangle from the 3 vertices - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, 36); // Swap buffers SDL_GL_SwapWindow(game->window); } diff --git a/OpenGL/cube/src/texture.cpp b/OpenGL/cube/src/texture.cpp new file mode 100644 index 0000000..32d5def --- /dev/null +++ b/OpenGL/cube/src/texture.cpp @@ -0,0 +1,2 @@ +#include "texture.hpp" + diff --git a/OpenGL/cube/src/texture.hpp b/OpenGL/cube/src/texture.hpp new file mode 100644 index 0000000..416af14 --- /dev/null +++ b/OpenGL/cube/src/texture.hpp @@ -0,0 +1,6 @@ +#ifndef _TEXTURE_H_ +#define _TEXTURE_H_ + + + +#endif