diff --git a/CMakeLists.txt b/CMakeLists.txt index 764ade4..09f2e61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.21 FATAL_ERROR) project(Game) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c diff --git a/data/shaders/vertex.glsl b/data/shaders/vertex.glsl index ab367cb..7570847 100644 --- a/data/shaders/vertex.glsl +++ b/data/shaders/vertex.glsl @@ -2,10 +2,12 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; +uniform mat4 MVP; + out vec2 ourTexCoord; void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = MVP * vec4(aPos, 1.0); ourTexCoord = aTexCoord; }; diff --git a/src/ShaderLoader.cpp b/src/ShaderLoader.cpp index fc34daa..6129119 100644 --- a/src/ShaderLoader.cpp +++ b/src/ShaderLoader.cpp @@ -88,3 +88,7 @@ void ShaderLoader::setInt(const std::string &name, int value) const { void ShaderLoader::setFloat(const std::string &name, float value) const { glUniform1f(glGetUniformLocation(ID, name.c_str()), value); } +void ShaderLoader::setMat4(const std::string &name, glm::mat4 value) const { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, + &value[0][0]); +} diff --git a/src/ShaderLoader.h b/src/ShaderLoader.h index 38a9d01..580b356 100644 --- a/src/ShaderLoader.h +++ b/src/ShaderLoader.h @@ -5,6 +5,7 @@ // File reader #include "Error.h" #include +#include #include #include #include @@ -25,4 +26,5 @@ public: void setBool(const std::string &name, bool value) const; void setInt(const std::string &name, int value) const; void setFloat(const std::string &name, float value) const; + void setMat4(const std::string &name, glm::mat4 value) const; }; diff --git a/src/main.cpp b/src/main.cpp index 8716c97..04c55e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,8 @@ #include #include // Not used yet -//#include +#include +#include // File reader #include #include @@ -67,9 +68,9 @@ int main(int argc, char **argv) { // Create event handling struct SDL_Event input; - ShaderLoader shader("../data/shaders" + ShaderLoader shader("./data/shaders" "/vertex.glsl", - "../data/shaders" + "./data/shaders" "/fragment.glsl"); float vertices[] = { @@ -125,7 +126,7 @@ int main(int argc, char **argv) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Load texture image - SDL_Surface *image = IMG_Load("../data" + SDL_Surface *image = IMG_Load("./data" "/container.jpg"); if (image == nullptr) { error.crash("SDL2_image was unable to load a texture", IMG_GetError()); @@ -151,6 +152,28 @@ int main(int argc, char **argv) { SDL_FreeSurface(image); image = nullptr; + // Mess with perspective + // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 + // unit <-> 100 units + float width = 4; + float height = 3; + glm::mat4 Projection = glm::perspective( + glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); + + // Camera matrix + glm::mat4 View = glm::lookAt( + glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space + glm::vec3(0, 0, 0), // and looks at the origin + glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) + ); + // Model matrix : an identity matrix (model will be at the origin) + glm::mat4 Model = glm::mat4(1.0f); + + // Our ModelViewProjection : multiplication of our 3 matrices + glm::mat4 mvp = + Projection * View * + Model; // Remember, matrix multiplication is the other way around + // Game loop bool running = true; while (running) { @@ -168,6 +191,8 @@ int main(int argc, char **argv) { // Make every shader/rendering call from this point on use our shader // glUseProgram(shaderProgram); + // Send our glsl shader our mvp + shader.setMat4("MVP", mvp); shader.use(); // I think this is meant to be here but it breaks...