From 56ca8c2766c0298677eb62002dbe5623c03afba7 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Wed, 24 May 2023 16:39:49 +0800 Subject: [PATCH] feat(OpenGL-hello): projection --- OpenGL/hello/include/shader.h | 65 +++++++++++++++++++++++++---------- OpenGL/hello/src/main.cpp | 26 ++++++++++---- OpenGL/hello/src/tex.fs | 2 +- OpenGL/hello/src/tex.vs | 19 +++++----- 4 files changed, 77 insertions(+), 35 deletions(-) diff --git a/OpenGL/hello/include/shader.h b/OpenGL/hello/include/shader.h index 061a22b..0d777c4 100644 --- a/OpenGL/hello/include/shader.h +++ b/OpenGL/hello/include/shader.h @@ -1,14 +1,16 @@ + + #ifndef SHADER_H #define SHADER_H -#include "glad/glad.h" // 包含glad来获取所有的必须OpenGL头文件 +#include +#include #include #include #include #include - class Shader { public: @@ -33,13 +35,13 @@ public: std::stringstream vShaderStream, fShaderStream; // read file's buffer contents into streams vShaderStream << vShaderFile.rdbuf(); - fShaderStream << fShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); // close file handlers vShaderFile.close(); fShaderFile.close(); // convert stream into string - vertexCode = vShaderStream.str(); - fragmentCode = fShaderStream.str(); + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); } catch (std::ifstream::failure& e) { @@ -68,10 +70,11 @@ public: // delete the shaders as they're linked into our program now and no longer necessary glDeleteShader(vertex); glDeleteShader(fragment); + } // activate the shader // ------------------------------------------------------------------------ - void use() + void use() const { glUseProgram(ID); } @@ -87,37 +90,60 @@ public: glUniform1i(glGetUniformLocation(ID, name.c_str()), value); } // ------------------------------------------------------------------------ - void setFloat1(const std::string &name, float value) const + void setFloat(const std::string &name, float value) const { glUniform1f(glGetUniformLocation(ID, name.c_str()), value); } // ------------------------------------------------------------------------ - void setFloat2(const std::string &name, float value1, float value2) const + void setVec2(const std::string &name, const glm::vec2 &value) const { - glUniform2f(glGetUniformLocation(ID, name.c_str()), value1, value2); + glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec2(const std::string &name, float x, float y) const + { + glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y); } // ------------------------------------------------------------------------ - void setFloat3(const std::string &name, float value1, float value2, float value3) const + void setVec3(const std::string &name, const glm::vec3 &value) const { - glUniform3f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3); + glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec3(const std::string &name, float x, float y, float z) const + { + glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z); } // ------------------------------------------------------------------------ - void setFloat4(const std::string &name, float value1, float value2, float value3, float value4) const + void setVec4(const std::string &name, const glm::vec4 &value) const { - glUniform4f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3, value4); + glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec4(const std::string &name, float x, float y, float z, float w) const + { + glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w); } // ------------------------------------------------------------------------ - void setMat4(const std::string &name, float* value) const - { - glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, value); + void setMat2(const std::string &name, const glm::mat2 &mat) const + { + glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); } + // ------------------------------------------------------------------------ + void setMat3(const std::string &name, const glm::mat3 &mat) const + { + glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + // ------------------------------------------------------------------------ + void setMat4(const std::string &name, const glm::mat4 &mat) const + { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + private: // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ - void checkCompileErrors(unsigned int shader, std::string type) + void checkCompileErrors(GLuint shader, std::string type) { - int success; - char infoLog[1024]; + GLint success; + GLchar infoLog[1024]; if (type != "PROGRAM") { glGetShaderiv(shader, GL_COMPILE_STATUS, &success); @@ -139,3 +165,4 @@ private: } }; #endif + diff --git a/OpenGL/hello/src/main.cpp b/OpenGL/hello/src/main.cpp index b9a8048..f399271 100644 --- a/OpenGL/hello/src/main.cpp +++ b/OpenGL/hello/src/main.cpp @@ -3,10 +3,10 @@ #include "stb_image.h" #include #include -#include #include #include #include +#include // 四边形顶点 float vertices[] = { @@ -40,7 +40,8 @@ int main() { // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 创建窗口 - GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); + GLFWwindow *window = + glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); @@ -147,7 +148,18 @@ int main() { // 绑定纹理 shader.setInt("texture1", 0); shader.setInt("texture2", 1); - // 设置变换矩阵 + + // create transformations + glm::mat4 model = glm::mat4(1.0f); + glm::mat4 view = glm::mat4(1.0f); + glm::mat4 projection = glm::mat4(1.0f); + model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); + projection = glm::perspective( + glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); + shader.setMat4("model", model); + shader.setMat4("view", view); + shader.setMat4("projection", projection); // 循环渲染 while (!glfwWindowShouldClose(window)) { @@ -167,11 +179,11 @@ int main() { // 绘制三角形 shader.use(); + // 设置变换矩阵 glm::mat4 trans(1.0f); - trans = glm::translate(trans,glm::vec3(0.5,-0.5,0.0)); - trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.5, 0.5)); - trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5)); - shader.setMat4("trans",glm::value_ptr(trans)); + trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, + 0.0)); + shader.setMat4("trans", trans); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/OpenGL/hello/src/tex.fs b/OpenGL/hello/src/tex.fs index b762a3a..a1c165d 100644 --- a/OpenGL/hello/src/tex.fs +++ b/OpenGL/hello/src/tex.fs @@ -9,5 +9,5 @@ uniform sampler2D texture2; void main() { - FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2) * vec4(ourColor, 1.0); } \ No newline at end of file diff --git a/OpenGL/hello/src/tex.vs b/OpenGL/hello/src/tex.vs index 9d1251e..a491db1 100644 --- a/OpenGL/hello/src/tex.vs +++ b/OpenGL/hello/src/tex.vs @@ -1,16 +1,19 @@ #version 330 core -layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -layout (location = 2) in vec2 aTexCoord; +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; uniform mat4 trans; -void main() -{ - gl_Position = trans * vec4(aPos, 1.0); - ourColor = aColor; - TexCoord = aTexCoord; +void main() { + gl_Position = projection * view * model * trans * vec4(aPos, 1.0); +// gl_Position = trans * vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = aTexCoord; }