understanding glLoadIdentity()

Started by
5 comments, last by WilyCoder 18 years, 10 months ago
I've been working through NeHe, making some simple projects of my own, and reading the red book. The glLoadIdentity() function seems to be quite common, but I don't understand exactly what it is for. Clearing the model view matrix doesn't mean much to me: I know that this is sort of how the camera is positioned, but I don't understand the effects of glLoadIdentity() on it other than it "clears it". What does that mean? Does it reset the camera's coordinates to (0.0f, 0.0f, 0.0f)? Does it reset the rotation? Help is appreciated - I tried! Thanks.
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
glLoadIdentity() makes the current matrix in the current matrix mode an identity matrix -that is a matrix whose diagonal components, from top left to bottom right, are all 1's. Example of a 3x3 identity matrix:
| 1 0 0 |
| 0 1 0 |
| 0 0 1 |

Multiplying anything by an identity matrix will yield the original value.

So if you are in model-view mode (glMatrixMode(GL_MODELVIEW)), and you call glLoadIdentity(), the matrix that is to transform geometry into world space will be an identity matrix -that is, when you multiply this matrix by any vector (or matrix), you will obtain the original vector (or matrix). In such a case, the final calculated position of the point (1,2,3) would simply be (1,2,3). In this sense, calling glLoadIdentity(), resets the scene's transformations.

Consequently, if you were to call glTranslatef(1.0f, 0.0f, 0.0f) once every frame, without calling glLoadIdentity(), your geometry would be translated 1 unit along the x axis every frame (thus moving your geometry beyond where it should be).
Read the Red Book chapter where it explains the mathematical basis of geometry transformations. glLoadIdentity, unlike most other functions that affect the active matrix, sets the matrix directly rather than postmultiplying it.
Quote:Original post by Sneftel
Read the Red Book chapter where it explains the mathematical basis of geometry transformations. glLoadIdentity, unlike most other functions that affect the active matrix, sets the matrix directly rather than postmultiplying it.


I did, and right after came to ask this question :)

odiousangel: Thanks. Does that mean that if I started the camera at (0, 0, 0) and called glTranslatef(1, 0, 0) once per frame without calling glLoadIdentity(), I would start the second frame at (1, 0, 0) and then get translated to (2, 0, 0)?
Also, just to make sure, how often (frequently/infrequently) would you say glLoadIdentity() is called? Thanks.
my siteGenius is 1% inspiration and 99% perspiration
Yes, as long as you don't call glLoadIdentity() OpenGL will continue multiplying your transformations.
You should call it before you render a new frame, or are switching to a new cameraposition.
An identity transformation is one that maintains the identity of the coordinate system. In other words, it doesn't change anything.

So, OpenGL says that when your model view matrix is identity, it's basically got the camera looking down the -z axis, with its up as +y and its right as +x. LoadIdentity resets everything to this point.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:
Consequently, if you were to call glTranslatef(1.0f, 0.0f, 0.0f) once every frame, without calling glLoadIdentity(), your geometry would be translated 1 unit along the x axis every frame (thus moving your geometry beyond where it should be).


Yep, had that myself. I couldn't figure out why my mesh was "falling"...a proper glLoadIdentity() cleared it up for me.

This topic is closed to new replies.

Advertisement