Igor Lihachev
Igor Lihachev

Reputation: 25

3D mesh is projecting to 2D plane

When I try to draw my mesh, I got something like this:

that

But when I rotate my mesh, it look like this:

this.

This is how it should be:

This

Here's my code:

// Generate MVP matrix.
glm::mat4 Projection = glm::perspective(glm::radians(45.0f),
                                      4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(glm::vec3(4,0,4),
                             glm::vec3(0,0,0),
                             glm::vec3(0,0,1));
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 mvp = Projection * View * Model;
GLint matrixId = glGetUniformLocation(shader.getProgramId(), "MVP");

// Generate VBO and VAO.
float triangleVertices[] = {
    -0.5f, -0.5f, 0.0f, 1.0f,
    0.5f, 0.5f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.0f, 1.0f,
    1.0f, 1.0f, 0.0f, 1.0f,
    1.0f, -1.0f, 0.0f, 1.0f,
    1.0f, -1.0f, 1.0f, 1.0f,
};
uint un_vertecesNum = sizeof(triangleVertices);
GLuint vbo;
glGenBuffers(1, &triangleBufferObject);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertecesNum, 
             triangleVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Begin main loop.
// Event handling here (Look for code below)
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &mvp[0][0]);

// Mesh display stuff.
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, un_vertecesNum);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

shader.UnUse();
SDL_GL_SwapWindow(win);
// End main loop.

Here's event handle and how I rotate my mesh:

SDL_Event event;
while ( SDL_PollEvent(&event) )
{
    switch(event.type)
    {
    case SDL_KEYDOWN:
        switch(event.key.keysym.sym)
        {
            case SDLK_a:
                Model = glm::rotate(Model, 0.1f, 
                        glm::vec3(0.0f, 1.0f, 0.0f));
            case SDLK_d:
                Model = glm::rotate(Model, 0.1f,
                        glm::vec3(0.0f, -1.0f, 0.0f));
                break;
        }
        break;
     }
}
mvp = Projection * View * Model;

Vert shader:

#version 130
uniform mat4 MVP;
attribute vec4 coord4d;
void main(void)
{
    vec4 temp = vec4(coord4d);
    gl_Position = temp*(MVP);
}

Frag shader:

#version 130
void main(void)
{
    gl_FragColor[0] = gl_FragCoord.x/640.0;
    gl_FragColor[1] = 0.2;
    gl_FragColor[2] = gl_FragCoord.y/480.0;
    gl_FragColor[3] = 1.0f;
}

Here is how Suzanne look like if you wonder:

img1 img1

I tried to google it, read something about it on "typical mistakes" page or find on stackoverflow, but nothing.

Upvotes: 0

Views: 323

Answers (1)

user3235832
user3235832

Reputation:

The vertex shader should be MVP * temp, not the other way around. Just like in the real world, in GLSL matrix-vector multiplication is non-commutative (this would be the "typical mistake" you were looking for)

Upvotes: 2

Related Questions