Cilenco
Cilenco

Reputation: 7117

OpenGL multiply ProjectionMatrix and ModelMatrix

I'm learning OpenGL programming on Android at the moment and now I'm at the point where I have to use a ModelMatrix and a ProjectionMatrix. If I understand it right I have three different matrix in OpenGL:

  1. ProjectionMatrix which creates the 3D efect (does not change while running the app)
  2. ModelMatrix with this I can move objects around
  3. ViewMatrix with which I can move all objects around (Camera)

Now in my tutorial book OpenGL ES 2 for Androud: A Quick-Start Guide) the ProjectionMatrix and the ModelMatrix are multiplied to one single matrix. But I do not understand why. Is that the correct way? With the ModelMatrix I can move objects around so why should I multiplay it with an other matrix? Would be great if you can help me to better understand the different matrix in OpenGL.

Upvotes: 1

Views: 585

Answers (1)

newprogrammer
newprogrammer

Reputation: 2604

Right now, you're used to taking your vertex, left-multiplying it by a model matrix to rotate it/scale it/move it around in the world, then left-multiplying it by a view matrix to get its position relative to the camera, then left-multiplying it by a projection matrix to get that "3d effect".

In other words, you're taking your vertex v, then doing this to it: v' = P * V * M * v, where in v', x and y is the pixel location on the screen, and z is the "depth" of the vertex.

But matrix multiplication (whether its a 4x4 matrix or a 4x1 matrix/vector) is associative, meaning:

A * B * v is the same thing as (A * B) * v, where A and B are matrices and v is some vector.

So instead of taking a thousand or so vertices and then multiplying it by M, then V, then P, why not pre-multiply M, V, and P once to create an MVP matrix then multiply every vertex by that? That way, when your vertex shader runs on each vertex, it only has to do one matrix multiplication rather than three.

Though I am not sure why your tutorial would be multiplying only the model matrix and the perspective matrix. Is there a view matrix present?

Upvotes: 1

Related Questions