## rotating objects

Member
Posts: 185
Joined: 2005.02
Post: #1
I understand how to use glRotate(); to rotate things about the origin. Is there a way to set a different point to rotate around? for example rotating a globe about its axis. Is this a more complex operation that I will learn latter on, or do I just have to do this by using glRotate and glTranslate (by rotating the object around the origin then translating it back into the desired position) ?
Moderator
Posts: 529
Joined: 2003.03
Post: #2

Figure 3-4 : Rotating First or Translating First

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Member
Posts: 185
Joined: 2005.02
Post: #3
ok so ever time i have to rotate something i have to reset it to the origin then translate it?
hmm.... ok
say i have a soccer ball that i want to spin as it moves towards the goal
i would have to use something like this?
Code:
```for(progress = 0; progress < journey; spin++, move++. progress++) [ resetBallToOrigin; rotateBall(spin); moveBall(move); ];```

i can't do something like
Code:
```for(progress = 0; progress < journey; progress++) [ moveBall(forward 2); spinBall(10 degrees); ]```

?

in other words i would have to move the ball, then reset it to the origin, then move it alittle farther then i did the previous time; as opposed to repeated moving it a short distance utill it reached the goal?
Moderator
Posts: 529
Joined: 2003.03
Post: #4
More like:

glpushmatrix()

rotate the ball
translate the ball

glpopmatrix()

Something like that

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Member
Posts: 185
Joined: 2005.02
Post: #5
oh ok so i'm getting ahead of myself
pushMatrix and popMaatrix are a little later in the chapter. thanks!
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
You dont need to follow the book page by page, just mess around and try what you want. I assure you its quicker that way and more fun.

Try them ordered one way, if it doesnt work try the other.
Member
Posts: 185
Joined: 2005.02
Post: #7
ok so i've got this:
Code:
```glColor3f (0.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.2, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); glTranslatef (0.0, 0.5, 0.0); /* modeling transformation */ glutWireCube (1.0); glColor3f (0.0, 1.0, 0.0); glPushMatrix();     glTranslatef (0.5, -0.3, 0.0);     glutWireCube (1.0); glPopMatrix(); glColor3f (1.0, 0.0, 0.0); glPushMatrix();     glRotatef(45, 0.0, 0.0, 1.0);     glutWireCube (1.0); glPopMatrix(); glLoadIdentity (); glColor3f (1.0, 0.4, 0.1); glTranslatef (-0.1, 0.5, 0.0); glutWireCube (1.0);     glFlush ();```

i get a black window with 3 different cubes, but the fourth cube (the one after the glLoadIdentity(); ) doesn't show up.
Why not?
Moderator
Posts: 3,591
Joined: 2003.06
Post: #8
Calling pop before the last cube means that you're back down to the base modelview matrix you started with when you applied the gluLookAt transformation. So if you call glLoadIdentity it erases it and means that the last cube is literally being drawn at -0.1, 0.5, 0.0 - without the effects of gluLookAt in addition to the first glTranslatef and glScalef at the beginning which are unbracketed by push and pop calls as well.
Member
Posts: 185
Joined: 2005.02
Post: #9
so i have to call gluLookAt() again?
Moderator
Posts: 3,591
Joined: 2003.06
Post: #10
No. Well, you can, but that's probably not what you're trying to do. Just don't call glLoadIdentity before the last cube and be sure to bracket the other translate and scales with push and pop so you don't confuse yourself. The pattern should be as follows:

gluLookAt

push
transform
draw
pop

push
transform
draw
pop

etc...

You're doing it like:

gluLookAt

transform
draw

push
transform
draw
pop

transform
draw

... which is akward and confusing. Not that it won't work, but it just isn't working in a predictable manner. Pushing allows you to "protect" the base matrix (I call it the base matrix because it's the one you're starting with and it contains your camera transform) from the effects of any transformations you do on top of it for individual objects while still gaining its effects. Popping then "unprotects" it and erases any transforms you had "pushed on top" of it. LoadIdentity erases the current matrix on the stack, which again, after that last pop is the same matrix that is holding the gluLookAt transform. Unfortunately since you didn't protect it with a push and pop for a couple other transformations it also holds those too, which is probably not what you wanted.
Moderator
Posts: 3,591
Joined: 2003.06
Post: #11
Note: I should point out that what I'm calling "protecting" isn't an entirely accurate portrayal of what's going on when you call glPushMatrix(); What's really happening is that the current matrix on the stack is a composite of all the transforms before it and it gets *copied* onto a new level in the modelview matrix stack (presuming of course you're working with the modelview matrix mode). Calling glPopMatrix(); merely removes the top most layer on the stack and restores the one underneath it which remains unaffected. This is often referred to as saving and restoring the stack in OpenGL literature, not "protecting" and "unprotecting" as I refer to it. Like I said, it's not an entirely correct way to look at it but I find it a convenient analogy at times.
Member
Posts: 185
Joined: 2005.02
Post: #12
oh, ok i get it now