Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Why one MVP matrix rather than seperate M, V, P?

Hybrid View

  1. #1
    Intern Contributor
    Join Date
    Jun 2012
    Posts
    63

    Why one MVP matrix rather than seperate M, V, P?

    Most OpenGL 3.0+ tutorials I look at multiply the model and view matrices together before sending them to the shader. Sometimes they even multiply the projection matrix together with the other two as well, and have only pass 1 matrix to GLSL. But if I change the view or scale a model, I'm only changing the view or model matrices. Why should I resend the entire MVP matrix every time I change just M, or just V, or just P? What's the reason against using 3 matrix uniforms, and sending to the shader just the matrix you changed, and then carrying out the multiplication in the shader?

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    24
    Matrices are multiplied before sending to a shader because of efficiency. Vertex shader is executed for each vertex so if you do multiplication (ProjectionMatrix * ViewMatrix * ModelMatrix) in the shader that can cause some performance penalty when you have many vertices. Better is do it once on the cpu side and send a result to the shader. In vertex shader you might need use only one of these matrices, for example ModelMatrix when calculating a vertex world position. Then you should have separate uniforms which represent mvp matrix, viewmatrix nad modelmatrix. You can use this way:
    Code :
    vec4 vertexWorldSpacePosition =  ModelMatrix * vec4(position, 1.0);
    vec4 vertexCameraSpacePosition = ViewMatrix * vertexWorldSpacePosition;
    gl_Position = MVP_Matrix * vec4(position, 1.0);
    Also take in mind opengl transformation order, it is important thing and you should read about it. For separate matrices gl_Position output should looks like this:
    Code :
    gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.0);
    Last edited by Triangle; 10-17-2012 at 05:56 AM.

  3. #3
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,067
    Even more important reason is precisio. On the CPU side, calculation is usually done in double precision.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •