View Full Version : Why one MVP matrix rather than seperate M, V, P?

fiodis

10-17-2012, 05:11 AM

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?

Triangle

10-17-2012, 05:45 AM

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:

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:

gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.0);

Aleksandar

10-17-2012, 08:27 AM

Even more important reason is precisio. On the CPU side, calculation is usually done in double precision.

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.