diff --git a/headers/shader.hpp b/headers/shader.hpp index 74691ae..7271488 100644 --- a/headers/shader.hpp +++ b/headers/shader.hpp @@ -1,6 +1,7 @@ #ifndef SHADER_H #define SHADER_H #include +#include #include class Shader { @@ -10,6 +11,7 @@ public: void use(); void setInt(const std::string &name, int value); void setFloat(const std::string &name, float value); + void setMat4(const std::string &name, glm::mat4 value); private: unsigned int ID; diff --git a/shaders/MVPTexVert.glsl b/shaders/MVPTexVert.glsl new file mode 100644 index 0000000..c96031d --- /dev/null +++ b/shaders/MVPTexVert.glsl @@ -0,0 +1,18 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec3 ourColor; +out vec2 TexCoord; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = aTexCoord; +} diff --git a/src/main.cpp b/src/main.cpp index ff7d785..6aa2357 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include #include #include @@ -54,7 +57,7 @@ int main(void) { GL_STATIC_DRAW); Shader *shaderProgram = - new Shader("./shaders/TexVert.glsl", "./shaders/TexFrag.glsl"); + new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl"); unsigned int texture; glGenTextures(1, &texture); @@ -80,6 +83,12 @@ int main(void) { stbi_image_free(data); while (!glfwWindowShouldClose(window)) { + glm::mat4 model = glm::mat4(1.0f); /*move model*/ + glm::mat4 view = glm::mat4(1.0f); /*move camera*/ + view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); + model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); /*camera settings*/ + processInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); @@ -87,6 +96,10 @@ int main(void) { shaderProgram->use(); shaderProgram->setInt("Tex0",0); + shaderProgram->setMat4("model",model); + shaderProgram->setMat4("view",view); + shaderProgram->setMat4("projection",projection); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); diff --git a/src/shader.cpp b/src/shader.cpp index 608d134..e9aa33b 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -44,6 +45,11 @@ void Shader::setFloat(const std::string &name, float value) { glUniform1f(glGetUniformLocation(ID, name.c_str()), value); } +void Shader::setMat4(const std::string &name, glm::mat4 value) { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(value)); + +} + /*priv*/ unsigned int Shader::shaderCMPL(GLenum type, const char *src) { unsigned int shader;