【问题标题】:Incorrect rotations using openGL GLM and shaders使用 openGL GLM 和着色器的不正确旋转
【发布时间】:2013-03-27 05:53:00
【问题描述】:

我的程序的目标是显示一个简单的彩色三角形,在 Y 轴上旋转,没有任何平移。我正在使用 GLM 库。问题是我的三角形的变换不正确:它有一个奇怪且不合逻辑的行为。这是我的 C++ 代码加上顶点着色器和片段着色器代码:

C++ 代码:

float angle = 0.0f;

struct Vertex
{
    float x, y, z;
};

static Vertex vertices[9] =
{
    -1.000000f, 0.000000f, 0.000000f,
    0.000000f, 1.000000f, 0.000000f,
    1.000000f, 0.000000f, 0.000000f
};

static Vertex colors[9] =
{
    1.000000f, 0.000000f, 0.000000f,
    0.000000f, 1.000000f, 0.000000f,
    0.000000f, 0.000000f, 1.000000f
};

[...]

int                 
main(int ac, char **av)
{
    bool            continuer = true;
    SDL_Event       event;
    GLuint          vboID[2];
    GLuint          programID = 0;

    //SDL window initialization

    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("VBO tests",NULL);
    SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);

    //Viewport initialization

    glViewport(0, 0, WIDTH, HEIGHT);

    //Projection initialization

    glm::mat4 projection = glm::mat4(1.0f);
    projection = glm::perspective<GLfloat>(60.0f, (float)(WIDTH/HEIGHT), 1.0f, 1000.0f);

    //View initialization

    glm::mat4 view = glm::lookAt<GLfloat>(glm::vec3(0.0f, 0.0f, 8.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

    glewInit();

    //Shaders initialization

    programID = initShaders("triangle.vert", "triangle.frag");

    //Main loop

    while (continuer)
    {
        eventListener(&event, &continuer);

        glClearDepth(1.0f);
        glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glUseProgram(programID);

        //Model transformations

        glm::mat4 model = glm::mat4(1.0f);
        model *= glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
        model *= glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f));

        glm::mat4 ModelViewMatrix = model * view;
        glm::mat4 ModelViewProjectionMatrix = projection * ModelViewMatrix;

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, colors);

        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glUniformMatrix4fv(glGetUniformLocation(programID, "ModelView"), 1, GL_TRUE, glm::value_ptr(ModelViewMatrix));
        glUniformMatrix4fv(glGetUniformLocation(programID, "MVP"), 1, GL_TRUE, glm::value_ptr(ModelViewProjectionMatrix));

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(0);

        glUseProgram(0);

        angle += 0.020f;

        glFlush();
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return (0);
}

顶点着色器代码:

#version 330

in vec3 VertexPosition;
in vec3 VertexColor;

uniform mat4 ModelViewMatrix;
uniform mat4 MVP;

out vec3 Color;

void main()
{
    Color = VertexColor;

    gl_Position = MVP * vec4(VertexPosition, 1.0f);
}

片段着色器代码:

#version 330

in vec3 Color;
out vec4 FragColor;

void main() {
    FragColor = vec4(Color, 1.0);
}

当我为模型声明一个简单的矩阵旋转时,我将它发送到顶点着色器并将它用作 MVP 并且它可以工作。但是当我添加投影矩阵 + 视图矩阵时,它不能正常工作。这是一张图片:

通常,三角形应该位于屏幕的中心。

有人可以帮助我吗?

【问题讨论】:

  • 当你说“当我添加投影矩阵 + 视图矩阵时它不能正常工作”是什么意思?添加到什么?什么不能正常工作?你在屏幕上看到了什么?
  • 我想说,当我将视图、投影和模型矩阵相乘时,我的图片上面的渲染效果不好。但是当我删除代码中的视图和投影时,我只保留模型转换矩阵,它可以工作。三角形更接近当然是因为我没有包含视图和投影,但它正确地围绕 Y 轴转动。我希望具有相同的行为,但使用投影和视图矩阵。我的代码对您来说似乎正确吗? (c++ 和顶点着色器)
  • 您能否将代码发布在您创建视图和投影矩阵以及将矩阵相乘的位置?

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


【解决方案1】:

这一行:

glm::mat4 ModelViewMatrix = model * view;

应该读作:

glm::mat4 ModelViewMatrix = view * model;

矩阵计算是反向进行的,您希望先将顶点乘以模型矩阵,而不是视图。

你也可以看看my other answer,稍微解释一下这些概念。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多