Transform matrix application order

Started by
4 comments, last by scniton 11 years, 9 months ago
I'm building my first OpenGL demo to incorporate into the game I'm writing. I wonder if you gentlefolk might help clarify the transformation process for me. I have a basic vertex and fragment shaders up and running already.

Here's what I see as the method for getting 3D objects perspective-transformed to screen:

(pseudocode)



On update:

Create the camera matrix: c = camRotation * camTranslation
Create the view matrix by inverting the camera matrix: v = c^-1
Create the projection matrix using the standard perspective projection matrix terms
Create view-projection matrix: vp = p * v

For each entity
Create a model matrix using entity world position, rotation, and scale values
Multiply this specific entity's world transform matrix (the model matrix): mvp = vp * m
Set mvp as a uniform for vertex shader
glDrawElements(...);


Queries:

  1. Please offer your advice on whether or not the above structure is sensible.
  2. What options do I have to reduce the number of draw calls? Merging static geometry into a single vertex list using a common texture atlas seems to be the only option?
  3. Re the MVP matrix above, why do some sources present the final matrix value as -1, and others as 1? What should I use?

Any other tips, suggestions on this structure welcome.

Primary references:

OpenGL wiki page on viewing and transformations
Joe Groff's tutorial on transformation and projection
Advertisement
The order of the projection-view matrix is wrong. It shall be Proj*view. So the MVP matrix would be P*V*M. Some of this is done in the shader, some is not. It depends on the complexity of your design. The shader is much faster to compute this, but if a result stays the same for every pixel, then you might as well do it in the main application before sending it to the shader. You don't need a "window matrix", it is included in the projection matrix.

The vertex shader now need to compute PVM*v, for each vertex 'v'.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Hi larspensjo,

Thanks for clarifying, I've updated the question by removing the reference to the "window" matrix.

Also could you confirm the order then, for matrix multiplication:
[source lang="java"]
modelViewMatrix = viewMatrix * modelMatrix; //??
modelViewProjectionMatrix = projectionMatrix * modelViewMatrix; //??[/source]

...Assuming matrix mul() ordering is thisMatrix * otherMatrix?

Hi larspensjo,

Thanks for clarifying, I've updated the question by removing the reference to the "window" matrix.

Also could you confirm the order then, for matrix multiplication:
[source lang="java"]
modelViewMatrix = viewMatrix * modelMatrix; //??
modelViewProjectionMatrix = projectionMatrix * modelViewMatrix; //??[/source]

...Assuming matrix mul() ordering is thisMatrix * otherMatrix?

Confirmed. If you are using C++, I recommend the package glm. It is a matrix manipulation package, with a syntax close to the shader language.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Ah, many thanks. Updated again with the multiplication order. This should allow me to proceed. I hope you have a good weekend too smile.png
Personally, I find this reference helpful: www.songho.ca/opengl/gl_transform.html

You could split up the modelview matrix into two parts, and so you would transform your vertices using your model matrix first, and then by your view matrix.

I like thinking in terms which order the transformations are applied, then afterwards rearrange things to match the multiplication order I am working with (i.e. "pre" or "post" multiplication.)
Stop twiddling your bits and use them already!

This topic is closed to new replies.

Advertisement