简体   繁体   English

使用openGL GLM和着色器旋转不正确

[英]Incorrect rotations using openGL GLM and shaders

The goal of my program is to display a simple colored triangle rotating on the Y axis without any translation. 我的程序的目标是显示一个在Y轴上旋转而没有任何平移的简单彩色三角形。 I'm using the GLM library. 我正在使用GLM库。 The problem is that the transformations of my triangle are not correct : it has a strange and not logical behaviour. 问题是我的三角形的变换是不正确的:它有一个奇怪而不合逻辑的行为。 Here's my c++ code plus the vertex shader and fragment shader code : 这是我的c ++代码加上顶点着色器和片段着色器代码:

C++ code : 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);
}

Vertex shader code : 顶点着色器代码:

#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);
}

Fragment shader code : 片段着色器代码:

#version 330

in vec3 Color;
out vec4 FragColor;

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

When I declare just a simple matrix rotation for the model, I send it to the vertex shader and I use it as MVP and it works. 当我为模型声明一个简单的矩阵旋转时,我将它发送到顶点着色器并将其用作MVP并且它可以工作。 But when I add the projection matrix + the view matrix it does not work correctly. 但是当我添加投影矩阵+视图矩阵时,它无法正常工作。 Here's a picture : 这是一张图片:

在此输入图像描述

Normally, the triangle should be at the center of the screen. 通常,三角形应位于屏幕的中心。

Does anyone can help me? 有人能帮帮我吗?

This line: 这一行:

glm::mat4 ModelViewMatrix = model * view;

Should probably read: 应该读一下:

glm::mat4 ModelViewMatrix = view * model;

Matrix calculations are done in reverse, and you want to first multiply your vertex by model matrix, not by view. 矩阵计算是相反的,您希望首先按模型矩阵而不是视图乘以顶点。

You can also take a look at my other answer , explaining these concepts a bit. 您还可以查看我的其他答案 ,稍微解释一下这些概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM