109

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

0xCursor
  • 2,224
  • 4
  • 13
  • 30
Yuriy Vikulov
  • 2,487
  • 5
  • 20
  • 29
  • 14
    Just check out this link: http://www.songho.ca/opengl/gl_transform.html – iKushal Feb 27 '12 at 11:13
  • 4
    I suggested opening a Stack Exchange site dedicated to Computer Graphics, and selected this question as an [example question](http://area51.stackexchange.com/proposals/62667/computer-graphics/62670#62670). Follow [computergraphics.stackexchange.com](http://area51.stackexchange.com/proposals/62667/computer-graphics) if you're interested in seeing the site being launched! – wip Dec 10 '13 at 04:44
  • 1
    http://www.codinglabs.net/article_world_view_projection_matrix.aspx – Björn Hallström Mar 22 '17 at 11:05

2 Answers2

204

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.

Tommy
  • 97,164
  • 12
  • 174
  • 193
  • 3
    so for several objects (meshes) we need several modelView matrixes, needn't we? – Yuriy Vikulov Apr 06 '11 at 01:53
  • 6
    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. – Tommy Apr 06 '11 at 10:50
  • 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.. – harikrishnan Jun 28 '13 at 13:29
  • 1
    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 ? – Evren Bingøl Jul 26 '13 at 00:16
  • @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. – tom_mai78101 Aug 07 '16 at 00:49
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.

cegprakash
  • 2,317
  • 26
  • 52
  • 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. – legends2k Mar 21 '14 at 07:28
  • 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 :) – cegprakash Mar 21 '14 at 08:11
  • 1
    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. – legends2k Mar 21 '14 at 08:18
  • 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. :| – cegprakash Mar 21 '14 at 08:28
  • 2
    In fragment shader only lighting/colouring calculations are done, by the time data reaches fragment shader, all position calculations are complete (i.e. in vertex shader); [a better explanation on why use multiple coordinate systems is here](http://books.google.com/books?id=X3hmuhBoFF0C&pg=PA80&lpg=PA80&dq=why+bother+with+multiple+coordinate+spaces). This book is an excellent 3D game math resource. The [same chapter as a sample PDF from the book's website](http://gamemath.com/3d_math_primer_2e_chapter3_multiple_coordinate_spaces.pdf). – legends2k Mar 21 '14 at 08:31
  • Understood. Still, MVP is multiplied for every vertex's position and transpose(inverse(MV)) is multiplied for every normal in each pixel! – cegprakash Mar 21 '14 at 08:50
  • Edited my answer :) Thank you @legends2k – cegprakash Mar 27 '14 at 00:14