2
votes

I'm following the OpenGL tutorials on opengl-tutorial.org, and in tutorial 4 the author proposed, as a side "project", to send two "objects" to OpenGL and we have only ever rendered one object before. Specifically, he asks this:

Draw the cube AND the triangle, at different locations. You will need to generate 2 MVP matrices, to make 2 draw calls in the main loop, but only 1 shader is required.

We define the MVP matrix like this:

GLuint MatrixID = glGetUniformLocation(programID, "MVP");
glm::mat4 Projection = glm::perspective(90.0f, 4.0f / 3.0f, 0.1f, 100.0f);

// Camera matrix
glm::mat4 View = glm::lookAt(
                            glm::vec3(0,0,3), // Camera is at (4,3,3), in World Space
                            glm::vec3(0,0,0), // and looks at the origin
                            glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                       );
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * View * Model; 

Now I understand that only one MVP matrix is used per object. But I have a few questions.

  1. How do I create a MVP matrix for each object? Do I have to re-create the View and Model matrices for every object (IE specify a new one for each object)?

  2. I thought glm::lookAt was used as the "camera" of sorts, so I shouldn't have to create a new "Camera" for each object, should I?

  3. What does the Model matrix do? The author says it is a identity matrix right now, but if I change that to say 100, nothing happens! I thought that the Model matrix was the matrix that defined where the model was rendered, but I can't seem to change where the model is rendered without modifying the View matrix (which makes sense).

1

1 Answers

2
votes

Typically, one uses one projection and one view matrix per "camera". The view matrix basically represents the position and orientation of the camera, and the projection matrix represents the properties of the camera (field of view, aspect, etc. in some way like a "lens" of a real camera, but don't stretch that analogy too far).

A model matrix typically is used to place the model in some global "world cooridnate system". So, you need a model matrix per object, so each object can be placed independently.

The world coordiante system is not strictly needed at all. What matters to the resulting picture is how the objects are placed relative to the camera - that is there ModelView enters the picture - as the composition of the model and the view transform, directly going from object space to some camera-relative eye space, bypassing any "world space" completely.

In your case, you even further combine this with the projection matrix, so you go directly from object space to clip space.

As you can see, the MVP matrix needs to change if any of the distinct matrices it is composed of does change. You could in theory precalculate an MVP per object - but that is typically not done, since the camera is moveable and all those MVP matrices would have to be recalculated anyways - there is not much to save here. So one typically keeps these matrices separate and multiplies them together directly before the draw call.

What does the Model matrix do? The author says it is a identity matrix right now, but if I change that to say 100, nothing happens!

What does this even mean, "changing a matrix to 100"? From the above code snippets it can be seen that your model matrix is indeed used and affects the MVP matrix. So changing it should have an effect - depending on what you change, of course. If you just changed the line

glm::mat4 Model = glm::mat4(1.0f);

to

glm::mat4 Model = glm::mat4(100.0f);

then it would actually not lead to any observeable change. You would get different clip space coords, but the differences would cancel each other out and the produced image would have been the same. That would be due to how homogenous coordinates work, and is a different matter entirely.