# OpenGL Help understanding NeHe lesson 9

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

## Recommended Posts

Okay, I may be having some kind of brain strain or something currently so I apologise for the stupidity now, but I would rather appreciate someone helping me out with this one. I cannot seem to understand what the hell is going on here... code snippet follows (NeHe Productions, Lesson 9 "Moving Bitmaps in 3D space") glLoadIdentity(); // Reset The View Before We Draw Each Star glTranslatef(0.0f,0.0f,zoom); // Zoom Into The Screen (Using The Value In 'zoom') glRotatef(tilt,1.0f,0.0f,0.0f); // Tilt The View (Using The Value In 'tilt') glRotatef(star[loop].angle,0.0f,1.0f,0.0f); // Rotate To The Current Stars Angle glTranslatef(star[loop].dist,0.0f,0.0f); // Move Forward On The X Plane glRotatef(-star[loop].angle,0.0f,1.0f,0.0f); // Cancel The Current Stars Angle glRotatef(-tilt,1.0f,0.0f,0.0f); // Cancel The Screen Tilt I understand that I want the flat bitmap to be drawn facing the camera, yes, but can someone explain the above rotations then cancelling the rotations in laymans terms please to achieve this. I admit, my OpenGL (especially working in 3D is rusty).

##### Share on other sites
Thats done when you want to rotate around a specific point and not around the origin.

##### Share on other sites
In a desperate attempt to visualize the concept.

The world axes are fix, nothing you ever do will change them. All transformations are relative to the OBJECTS coordinate system at the time you apply them.

##### Share on other sites
Trienco: thanks! I've also had trouble understanding that translation and rotating in OpenGL, but that image really cleared it up. I guess it's a bit like drawing in LOGO...

##### Share on other sites
Thankyou, that picture was 1000 x better than what I have sketched thus far.

One question, where you have translate(10, 0, 0) then rotate(45, 0, 0, 1) in my head it would rotate around the origin but it seems like you have it rotating around itself at that point still (as if it hadn't been translated from the origin?) am I still confused?

##### Share on other sites
Quote:
 Original post by mopheadOne question, where you have translate(10, 0, 0) then rotate(45, 0, 0, 1) in my head it would rotate around the origin but it seems like you have it rotating around itself at that point still (as if it hadn't been translated from the origin?) am I still confused?

As I said, all transformations are in regard to the current local (ie. object) coordinate system, that includes the local origin.

The not-so intuitive alternative is to pretend that all transformations are applied in reverse order, that way one can keep thinking in terms of the world coordinate system. But as soon as you have a more complex hierarchy and push/pop the matrix a lot it can get really confusing.

Maybe making the matrix less "mysterious". The four columns (in memory it's usually the rows) are the local x,y,z axes and the origin.

Meaning the same as the world without transformation (obviously), ie. the identity matrix. x=1,0,0 y=0,1,0 origin=0,0,0 (let's ignore the w coordinate, but it helps to imagine that w=1 means a position (origin, vertex position, point light) and w=0 are directions (the three axes, a normal vector, a directional light). If you take apart the matrix math w=0 more or less filters out the "origin" (ie. positional part) of the matrix.

So the matrix after rotating 45 deg (using right/up/forward/position instead of "local x,y,z,origin" to make the difference more obivous. (rough approx. of sin/cos)

   Right  Up   Foward Positionx   .7   -.7    0        0y   .7    .7    0        0z   0     0     1        0w   0     0     0        1

If you do translate(5,0,0) now, it will translate 5 units to the "right", (ie. 5*.7 along world-x and 5*.7 along world-y).

   Right  Up   Foward Positionx   .7   -.7    0        3.5y   .7    .7    0        3.5z   0     0     1        0w   0     0     0        1

If you do the inverse rotation now, the rotation matrix is something like

 .7   .7    0    0-.7   .7    0    0  0    0    1    0  0    0    0    1

The resulting position is:
(.7,-.7,0,.3.5) dot (0,0,0,1) = 3.5
(.7, .7,0,.3,5) dot (0,0,0,1) = 3.5
(0,0,1,0) dot (0,0,0,1) = 0
(0,0,0,1) dot (0,0,0,1) = 1

Unless I got my rows and columns mixed up again, the dot product is always with the w-coordinates (ie. 0,0,0,1), meaning a rotation never can change the position. Unless you multiply from the other side, which turns a local transformation into a global transformation by effectively reversing the order of the transformations (see above).
0+0+0+1*1

[Edited by - Trienco on September 30, 2007 9:38:13 AM]

##### Share on other sites
I do see now, thanks again very much for your help. I usually do think in world coordinates and I forgot that when applying each transformation in the code this technique works backwards from the drawing operation.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002054
×