Jump to content
  • Advertisement
Sign in to follow this  
jskywalker

OpenGL glLoadIdentity and glTranslatef

This topic is 2882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I have been playing OpenGL for a while, but still have issues with some fundamental concept, for example glLoadIdentity and glTranslatef.

Can someone please explain to me whats the difference between the two:

1)
glLoadIdentity();
glTranslatef(30.0f, 30.0f, 0);

GLshort vertexArray[8] = {100, 100, 200, 100, 100, 300, 200, 300};
glVertexPointer(2, GL_SHORT, 0, vertexArray);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

This will move the rectangle.

2)
glLoadIdentity();

GLshort vertexArray[8] = {100, 100, 200, 100, 100, 300, 200, 300};
glVertexPointer(2, GL_SHORT, 0, vertexArray);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glTranslatef(30.0f, 30.0f, 0);

This won't move the rectangle, why???????????

Share this post


Link to post
Share on other sites
Advertisement
The triangle isn't moved because your glTranslate is happening after you've already sent the vertices to OpenGL.

Share this post


Link to post
Share on other sites
Ok now, I get it.

glTranslatef only affect what will be sent to Matrix stack, NOT what has already in the Matrix stack.

glLoadIdentity copy what you have in the Matrix stack, say a set of data, these data can be modified by glTranslatef, because these data will be sent to the Matrix stack.

Share this post


Link to post
Share on other sites
Quote:
Original post by jskywalker
Ok now, I get it.

glTranslatef only affect what will be sent to Matrix stack, NOT what has already in the Matrix stack.

glLoadIdentity copy what you have in the Matrix stack, say a set of data, these data can be modified by glTranslatef, because these data will be sent to the Matrix stack.


That's not exactly right --

glTranslatef DOES affect what is currently on the matrix stack. If you want to return to the state before you called glTranslatef, you should save that state by using glPushMatrix before calling glTranslatef. When you are ready to return to the previous state, you use glPopMatrix.

glLoadIdentity does not copy anything. It completely replaces what is on the top of the matrix stack with the identity matrix. Again, if you want to return to the matrix you had before calling glLoadIdentity, you need to follow the procedure above using glPushMatrix and glPopMatrix.

Changes to the matrix do not affect any rendering calls made before the change, only after.

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
glLoadIdentity does not copy anything. It completely replaces what is on the top of the matrix stack with the identity matrix.


Only this part I am still confusing.

Here is my code, very simple:


// first rectangle : yellow one
GLshort vertexArray1[8] = {0, 0, 5, 0, 0, 20, 5, 20};
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
glVertexPointer(2, GL_SHORT, 0, vertexArray1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



glLoadIdentity();
glRotatef(30.0f, 1.0f, 1.0f, 0);



// second rectangle: red one
GLshort vertexArray[8] = {-40, -40, -30, -40, -40, -20, -30, -20};
glColor4f(1.0f, 0, 0, 1.0f);
glVertexPointer(2, GL_SHORT, 0, vertexArray);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


The result is it draws two yellow rectangles, the second yellow one rotates 30 degree. From the result, I can only think glLoadIdentity() just copied the original matrix. Sorry I feel really hard to think in mathematical matrix level, I am not good at pure math :-(.

[Edited by - jskywalker on August 27, 2010 1:03:34 PM]

Share this post


Link to post
Share on other sites
OK, let me use the following diagram to ask 2 more questions regarding the matrix thing.

A triangle matrix:
_ _
| 7 8 9 |
| -6 -4 -7 | original matrix (m1)
|_-9 -4 5 _|

*
_ _
| 1 0 1 |
| 0 1 0 | identity matrix ( glLoadIdentity(); )
|_ 0 0 1 _|

=
_ _
| 7 8 9 |
| -6 -4 -7 | current matrix (m2)
|_-9 -4 5 _|


Q1.

The matrix stack contains a set of matrix, only the top matrix (which is the current matrix) can be modified by glTranslatef , rotate or scale , is that correct? What about other old matrices ? They have been processed or waiting for being processed ?

Q2.

Similar question, see the diagram, after glLoadIdentity(), m2 becomes the current matrix, the top one in the matrix stack, am I correct ? if so, then using glTranslatef can affect the current matrix (m2), but not m1, correct ? Since m1 and m2 are identical, you will see (say the triangle) will be drawn twice.

Share this post


Link to post
Share on other sites
Quote:
Original post by jskywalker
OK, let me use the following diagram to ask 2 more questions regarding the matrix thing.

A triangle matrix:
_ _
| 7 8 9 |
| -6 -4 -7 | original matrix (m1)
|_-9 -4 5 _|

*
_ _
| 1 0 1 |
| 0 1 0 | identity matrix ( glLoadIdentity(); )
|_ 0 0 1 _|

=
_ _
| 7 8 9 |
| -6 -4 -7 | current matrix (m2)
|_-9 -4 5 _|


Q1.

The matrix stack contains a set of matrix, only the top matrix (which is the current matrix) can be modified by glTranslatef , rotate or scale , is that correct? What about other old matrices ? They have been processed or waiting for being processed ?

Q2.

Similar question, see the diagram, after glLoadIdentity(), m2 becomes the current matrix, the top one in the matrix stack, am I correct ? if so, then using glTranslatef can affect the current matrix (m2), but not m1, correct ? Since m1 and m2 are identical, you will see (say the triangle) will be drawn twice.


A1.

Yes the matrix stack contains a set of matrices. Every time you call glPushMatrix(), a copy of the top matrix is pushed to the top of the stack. When you call glPopMatrix, the top matrix is removed from the stack and the matrix under it becomes the new top matrix. And yes, only the top matrix is modified by translate, rotate, scale AND glLoadIdentity. The other matrices are not at the top of the stack, so these calls have no effect on them.

A2.

The diagram is wrong. glLoadIdentity REPLACES the matrix at the top of the stack with the identity matrix (implied by the word "Load" in the function name). There is also a function called glLoadMatrix that allows you to replace the current matrix with an arbitrary one of your choosing. glRotate, Translate and Scale however will do multiplication. So m2 in your diagram would be the identity matrix. The rest of your question is just a misunderstanding of how the matrix stack works. You only ever have 1 active matrix. That is the matrix on top of the stack. So if m1 is on top of the stack and you call glTranslate, you still have m1 at the top of the stack but now it is multiplied by the matrix created by the glTranslate call. Those calls do not perform an implicit "glPushMatrix" as your question seemed to indicate. So to answer your question, m1 and m2 are not identical, m2 replaces m1, and yes it remains the top matrix on the stack because we did not call pushmatrix or popmatrix.
Also, the number of matrices in the stack has nothing to do with how many times your geometry will be drawn. Geometry is drawn when you call glVertex inside of a glBegin block, or when you call glDrawArrays or glDrawElements or related functions. The matrix at the top of the stack will then influence the position of the geometry in the "world" and thus on your screen, but it has no influence on the number of times the geometry is rendered.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!