feat(OpenGL-hello): rotate

This commit is contained in:
iridiumR 2023-05-24 15:51:39 +08:00
parent 10e78fa03f
commit 7a570f83d2
3 changed files with 20 additions and 4 deletions

View file

@ -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.
// ------------------------------------------------------------------------

View file

@ -4,6 +4,9 @@
#include <GLFW/glfw3.h>
#include <cmath>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// 四边形顶点
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);

View file

@ -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;
}