翻译过程中奇怪的MVP行为

问题描述 投票:0回答:1

我正在学习OpenGL的基础知识,并尝试使用正射投影平移三角形。我没看到我的期望,我在做什么错?

我的顶点缓冲区:

Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(-1.0f, 1.0f, 0.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

顶点着色器:

#version 330

layout (location = 0) in vec3 Position;

uniform mat4 gMVP;

void main()
{
    gl_Position = gMVP * vec4(Position, 1.0);
}

MVP计算和渲染:

    glm::mat4 p = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
    glm::mat4 v = glm::lookAt(glm::vec3(0,0,1), glm::vec3(0,0,0), glm::vec3(0,1,0)); 
    glm::mat4 m = glm::translate(glm::mat4(1.0f), glm::vec3(0.5f, 0.0f, 0.0f));
    glm::mat4 MVP = p * v * m; 

    glUniformMatrix4fv(gMVPLocation, 1, GL_TRUE, &MVP[0][0]);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);

    glutSwapBuffers();

结果:

enter image description here

它不应该沿着X轴向右移动吗?

opengl 3d glm vertex-shader vertex-buffer
1个回答
0
投票

glUniformMatrix4fv的第3个参数导致将矩阵设置为统一矩阵时将对其进行转置。将矩阵设置为统一矩阵后,不必对其进行转置:

glUniformMatrix4fv

glUniformMatrix4fv(gMVPLocation, 1, GL_TRUE, &MVP[0][0]);

在GLSL中,矢量必须从右边乘以矩阵。参见glUniformMatrix4fv(gMVPLocation, 1, GL_FALSE, &MVP[0][0]);

© www.soinside.com 2019 - 2024. All rights reserved.