【问题标题】:OpenGL and GLM problem with cube rotation立方体旋转的OpenGL和GLM问题
【发布时间】:2021-05-25 11:28:44
【问题描述】:

我正在从http://learnopengl.com 学习 OpenGL,我在基于本章 Coordinate Systems 的转换时遇到问题... 我想渲染类似Movie 的东西,但我有类似Movie2 的东西在 5 秒内回到屏幕上。很抱歉有很多链接,但我认为通过视频显示它更容易。 这是我的渲染循环:

const auto projection = glm::perspectiveFov(glm::radians(45.0f), 800.0f, 600.0f, 0.1f, 100.0f);
const auto view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));

while (!glfwWindowShouldClose(window))
{
    processInput(window);

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    flatColorShader->bind();
    flatColorShader->setMat4("u_Projection", projection);
    flatColorShader->setMat4("u_View", view);

    auto model = glm::mat4(1.0f);
    model = glm::rotate(model, static_cast<float>(glfwGetTime()) * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
    flatColorShader->setMat4("u_Model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(vao);

    glfwSwapBuffers(window);
    glfwPollEvents();
}

Vaertex 着色器:

#version 460 core
layout (location = 0) in vec3 a_Pos;
layout (location = 1) in vec2 a_TexCoord;

out vec2 v_TexCoord;

uniform mat4 u_Projection;
uniform mat4 u_Model;
uniform mat4 u_View;

void main()
{
    v_TexCoord = vec2(a_TexCoord.x, 1.0f - a_TexCoord.y);
    gl_Position = u_Projection * u_Model * u_View * vec4(a_Pos, 1.0);
}

片段着色器:

#version 460 core

in vec2 v_TexCoord;

out vec4 color;

uniform sampler2D u_Texture;

void main()
{
    color = texture(u_Texture, v_TexCoord);
}

我想这是模型矩阵的问题,但我不知道是什么。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c++ opengl glsl shader glm-math


    【解决方案1】:

    顶点着色器中顶点变换的顺序不正确:

    gl_Position = u_Projection * u_Model * u_View * vec4(a_Pos, 1.0);

    gl_Position = u_Projection * u_View * u_Model * vec4(a_Pos, 1.0);
    

    顺序很重要,因为矩阵乘法不是commutative

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多