PDA

View Full Version : Rotating an object



sarahippo
06-30-2003, 03:47 PM
Hi,
one very basic question.
I need to rotate my tilted NURBS object on y-axis constantly.In my code, i use glRotatef twice to tilt the object and then to rotate it.
I also use the keyboard functions to start the rotation, simply by calling glutPostRedisplay.
The problem is that everytime i call the function, the program is rotating the object twice(tilting + rotating). I only want it to tilt it in the beginning and then keep rotating the object. it seems very simple, but somehow i can't get this to work!!!!!
To do this, what am I supposed to keep in mind? help, please~ Thanks!

mikael_aronsson
06-30-2003, 10:09 PM
Hi !

It's difficult to say without seeing the code, if you could post the piece of code that does the rotation it would be easier.

Mikael

Relic
07-01-2003, 02:15 AM
Does it look like this?



...
if (bAnimated)
{
fRotate += fIncrement;
if (fRotate < 0.0)
fRotate += 360.0f;
else if (fRotate >= 360.0)
fRotate -= 360.0f;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -fOrigin); // Center of your frustum.
glRotatef(fTilt, 1.0f, 0.0f, 0.0f);
glRotatef(fRotate, 0.0f, 1.0f, 0.0f);
DrawMyModel();
SwapBuffers(hdc);
...

sarahippo
07-02-2003, 11:00 AM
yes, my code looks similar:

void display(){
init() /*for initializing NURBS*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glRotate(10.0, 1.0, 0.0, 0.0); /*titiling*/
glRotate(spin, 0.0, 1.0, 0.0);

drawNURBS();
glutSwapBuffers();

}

void spinDisplay(){

spin = 1.0;
if(spin>360)
spin = 0;
glutPostRedisplay();


}

any suggestion?

Relic
07-03-2003, 01:25 AM
Your code incrementally rotates the modelviewmatrix without resetting it.
Spin never changes.

If you do this for some hours, the matrix will run away because of floating point errors accumulating over time.

Read my post again and change your spin angle handling to be actually incremented like in mine. In your example it stays at 1.0 all the time.

Add the modelview initialization with glLoadIdentity(), glTranslatef() or whatever you did to setup you initial view.
Or better put glPushMatrix, glPopMatrix around your code like this:
push
rotate
rotate
draw
pop

[This message has been edited by Relic (edited 07-03-2003).]

sarahippo
07-08-2003, 02:16 PM
thanks =)