The purpose of Model View Projection Matrix
Asked Answered
C

2

121

For what purposes are we using Model View Projection Matrix? Why do shaders require Model View Projection Matrix?

Cavin answered 5/4, 2011 at 10:49 Comment(3)
Just check out this link: songho.ca/opengl/gl_transform.htmlMinuend
I suggested opening a Stack Exchange site dedicated to Computer Graphics, and selected this question as an example question. Follow computergraphics.stackexchange.com if you're interested in seeing the site being launched!Stalnaker
codinglabs.net/article_world_view_projection_matrix.aspxCompander
R
219

The model, view and projection matrices are three separate matrices. Model maps from an object's local coordinate space into world space, view from world space to camera space, projection from camera to screen.

If you compose all three, you can use the one result to map all the way from object space to screen space, making you able to work out what you need to pass on to the next stage of a programmable pipeline from the incoming vertex positions.

In the fixed functionality pipelines of old, you'd apply model and view together, then work out lighting using another result derived from them (with some fixes so that e.g. normals are still unit length even if you've applied some scaling to the object), then apply projection. You can see that reflected in OpenGL, which never separates the model and view matrices — keeping them as a single modelview matrix stack. You therefore also sometimes see that reflected in shaders.

So: the composed model view projection matrix is often used by shaders to map from the vertices you loaded for each model to the screen. It's not required, there are lots of ways of achieving the same thing, it's just usual because it allows all possible linear transforms. Because of that, a lesser composed version of it was also the norm in ye olde fixed pipeline world.

Rhinoplasty answered 5/4, 2011 at 15:12 Comment(5)
so for several objects (meshes) we need several modelView matrixes, needn't we?Cavin
Assuming they may move independently or have been positioned separately then yes. So that's far and away the most common way to proceed — pass modelView or projectionModelView as a uniform to the shader program, having set it up for the current model on the CPU.Rhinoplasty
Hi Tommy, can u suggest any sample code for pan functionality in Opengl Es2.0 in Android using this Model View Projection Matrix if Possible. I refereed more links, i could not get any clear idea.if any sample code means, its easy to understand for me..Bernadettebernadina
I have a question. If openGL using MV matrix as one and if we went to go from camera space to world space we would need to inverse(projection matrix*view matrix) * (cursor position) but if model-view is one composed matrix . how do we separate model and view matrix so that I can use the view matrix for that calculation. So I need to keep them separate ?Sam
@EvrenBingøl Have you gotten your answer yet? I kept my model and view matrices separate, but I would like to know the actual answer to your question if I should continue making it separate or not.Introgression
M
10

Because matrices are convenient. Matrices help to convert locations/directions with respect to different spaces (A space can be defined by 3 perpendicular axes and an origin).

Here is an example from a book specified by @legends2k in comments.

The residents of Cartesia use a map of their city with the origin centered quite sensibly at the center of town and axes directed along the cardinal points of the compass. The residents of Dyslexia use a map of their city with the coordinates centered at an arbitrary point and the axes running in some arbitrary directions that probably seemed a good idea at the time. The citizens of both cities are quite happy with their respective maps, but the State Transportation Engineer assigned a task of running up a budget for the first highway between Cartesia and Dyslexia needs a map showing the details of both cities, which therefore introduces a third coordinate system that is superior to him, though not necessarily to anybody else.

Here is another example,

Assume that you have created a car object in a game with it's vertex positions using world's co-ordinates. Suppose you have to use this same car in some other game in an entirely different world, you have to define the positions again and the calculations will go complex. This is because you again have to calculate the positions of window, hood, headlight, wheels etc., in the car with respect to new world.

See this video to understand the concepts of model, view and projection. (highly recommended)

Then see this to understand how the vertices in the world are represented as Matrices and how they are transformed.

Muth answered 15/3, 2014 at 10:13 Comment(5)
If you think of it deeply, you'll realise that eventhough the transformation is based on an object's origin (i.e. single point), all the vertices of the object (mesh/model formally) will undergo the transformation i.e. all 1000 vertices will be multiplied by the model matrix. Hence your point isn't well-formed. The model matrix is simply the matrix which transforms vertices in model space to the world space. There's no performance benefit here, it's just convenience.Helium
If so, why don't they just represent every point in a single 3D space? There should be benefits when you use the output of a scene to another scene which could be an input to another movable scene :)Muth
They don't represent all vertices in a single space since it's convenient to work in relative space than in a greater space, like the world. Say a robot, when commanded to move 2 meters forward, doing it w.r.t its eye is more appropriate for it than locating the world's centre, then calculating the correct resultant coordinates.Helium
I think you are right.. Anyhow fragment shader will be called for every pixel and we'll be performing matrix multiplications for every pixel in case of complex meshes. :|Muth
Understood. Still, MVP is multiplied for every vertex's position and transpose(inverse(MV)) is multiplied for every normal in each pixel!Muth

© 2022 - 2024 — McMap. All rights reserved.