Search code examples
c++openglglm-mathglew

opengl draw tringle with projection and view projection


I am trying to draw a tringle with OpenGL by using projection view and model matrices.

this is my shader

gl_Position = view * projection * model * vec4(pos, 1.0);       

and this is my code

glm::mat4 view = glm::translate(view, glm::vec3(0.0f, -0.5f, -2.0f));
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / height, 0.1f, 100.0f);

    float tringleVertices2[] = {
       0.0f,  0.5f, 0.0f, // Vertex 1 (X, Y)
      0.5f, -0.5f, 0.0f, // Vertex 2 (X, Y)
       -0.5f, -0.5f, 0.0f, // Vertex 3 (X, Y)
    };

But the weird thing is that when I remove the view and the projection matrix and my shader look like this

model * vec4(pos, 1.0);   

the code works

I would like to know if there is something wrong with my matrices or there are a problem with the other part of the code


Solution

  • The correct MVP matrix multiplication order should be:

    gl_Position = projection * view * model * vec4(pos, 1.0);

    Think about matrix-vector multiplication as it associates right-to-left: first you transform from model to world space, then from world to view space (via inverse camera matrix) and finally you project from view space to clip space.