From 7a570f83d2785c2326f33ec8a204e52cea8c7156 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Wed, 24 May 2023 15:51:39 +0800 Subject: [PATCH] feat(OpenGL-hello): rotate --- OpenGL/hello/include/shader.h | 6 +++++- OpenGL/hello/src/main.cpp | 14 ++++++++++++-- OpenGL/hello/src/tex.vs | 4 +++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/OpenGL/hello/include/shader.h b/OpenGL/hello/include/shader.h index c3de973..061a22b 100644 --- a/OpenGL/hello/include/shader.h +++ b/OpenGL/hello/include/shader.h @@ -106,7 +106,11 @@ public: { glUniform4f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3, value4); } - + // ------------------------------------------------------------------------ + void setMat4(const std::string &name, float* value) const + { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, value); + } private: // utility function for checking shader compilation/linking errors. // ------------------------------------------------------------------------ diff --git a/OpenGL/hello/src/main.cpp b/OpenGL/hello/src/main.cpp index ed53e3b..b9a8048 100644 --- a/OpenGL/hello/src/main.cpp +++ b/OpenGL/hello/src/main.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include // 四边形顶点 float vertices[] = { @@ -24,7 +27,7 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height); void processInput(GLFWwindow *window); // settings -const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_WIDTH = 600; const unsigned int SCR_HEIGHT = 600; int main() { @@ -37,7 +40,7 @@ int main() { // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 创建窗口 - GLFWwindow *window = glfwCreateWindow(800, 600, "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(); @@ -144,6 +147,7 @@ int main() { // 绑定纹理 shader.setInt("texture1", 0); shader.setInt("texture2", 1); + // 设置变换矩阵 // 循环渲染 while (!glfwWindowShouldClose(window)) { @@ -162,6 +166,12 @@ 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)); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/OpenGL/hello/src/tex.vs b/OpenGL/hello/src/tex.vs index 22041cd..9d1251e 100644 --- a/OpenGL/hello/src/tex.vs +++ b/OpenGL/hello/src/tex.vs @@ -6,9 +6,11 @@ layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; +uniform mat4 trans; + void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = trans * vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; }