PDA

View Full Version : Animation problem



The_V|k|ng
02-27-2002, 07:12 AM
OK, my GLUT display func looks like this:


void display (void)
{
i = i + 0.05;
if (i > 360) i = 0;

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (red, green, blue);

glPushMatrix ();

glLoadIdentity();
glRotatef (spin, 1.0, 1.0, 0.1);

glBegin(GL_QUADS);
glColor3f (red, green, blue);
glVertex3f (g, g, g);
glVertex3f (g, -g, g);
glVertex3f (-g, -g, g);
glVertex3f (-g, g, g);

glColor3f (0.0, 0.0, 1.0);

glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, g, -g);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (-g, g, -g);
glVertex3f (-g, g, g);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (g, -g, g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glColor3f (1.0, 0.0, 1.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (g, -g, g);

glColor3f (1.0, 1.0, 0.0);
glVertex3f (-g, g, g);
glVertex3f (-g, g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glEnd;

//***** glutWireCube (0.1*g);


glPopMatrix();
glutSwapBuffers();
glFlush();

}

As you might have noticed, this is supposed to rotate a multi-colored cube by the spin-angle around the axis 1.0|1.0|.1.
So, when I leave the glutWireCube call at the end of the func excluded, the cube refuses to move, somehow; when I activate the wirecube, the call causes an Invalid Operation error, but the cube rotates properly...
So, where's the problem supposed to be?
I cant see it, so any help is appreciated!!
Thanx in advance!

The_V|k|ng

chxfryer
02-27-2002, 07:30 AM
put
change
glRotatef(spin,1.0f,1.0f,0.1f);
to
glRotatef(i,1.0f,1.0f,0.1f);


oh yeah, not positive, but isnt 0.1f in the glRotatef function the same as having 1.0f since it just looks for a NON zero number for each axis??

The_V|k|ng
02-27-2002, 08:21 AM
Ummm, no, actually...
First of all, thanx for your reply... http://www.opengl.org/discussion_boards/ubb/wink.gif
I realized I had forgotten the glutIdleFunc():
void SpinDisplay (void)
{
spin = spin + dspin;
if (spin > 360) spin = spin - 360;

glutPostRedisplay();
}

So that's actually where spin is changed. The i-variable is actually useless. Sorry... http://www.opengl.org/discussion_boards/ubb/wink.gif
I also noticed that the rotation is performed exactly once (You can see that when you alter your dspin value). The problem still persists, however...

Any further suggestions, maybe...?

nexusone
02-27-2002, 08:38 AM
Try glutTimerFunc instead, maybe due to mouse or keyboard inputs the glutIdleFunc is never called?

The problem could be in another part of your code...



Originally posted by The_V|k|ng:

Ummm, no, actually...
First of all, thanx for your reply... http://www.opengl.org/discussion_boards/ubb/wink.gif
I realized I had forgotten the glutIdleFunc():
void SpinDisplay (void)
{
spin = spin + dspin;
if (spin > 360) spin = spin - 360;

glutPostRedisplay();
}

So that's actually where spin is changed. The i-variable is actually useless. Sorry... http://www.opengl.org/discussion_boards/ubb/wink.gif
I also noticed that the rotation is performed exactly once (You can see that when you alter your dspin value). The problem still persists, however...

Any further suggestions, maybe...?



[This message has been edited by nexusone (edited 02-27-2002).]

Rob The Bloke
03-01-2002, 10:33 AM
Originally posted by nexusone:
Try glutTimerFunc instead, maybe due to mouse or keyboard inputs the glutIdleFunc is never called?

why? how often to you press the keyboard? I doubt anyone would be able to generate enough user interupts to prevent the idle function from being called.


the Code :

glPushMatrix ();
glLoadIdentity();
glRotatef (spin, 1.0, 1.0, 0.1);
..
...



this is very very bad, you are using a pushMatrix() call and then clearing the matrix stack straight afterwards. I'm very suprised that the code runs at all. This is liable to cause some serious problems when the related glPopMatrix() call is executed. Just move the glLoadIdentity() above the call to glPushMatrix().


the Code :

...
...
glPopMatrix();
glutSwapBuffers();
glFlush();



dont really need the glFlush there either.


[This message has been edited by Rob The Bloke (edited 03-01-2002).]

Furrage
03-01-2002, 10:43 AM
Originally posted by Rob The Bloke:
this is very very bad, you are using a pushMatrix() call and then clearing the matrix stack straight afterwards. I'm very suprised that the code runs at all. This is liable to cause some serious problems when the related glPopMatrix() call is executed. Just move the glLoadIdentity() above the call to glPushMatrix().

Why? As far as I know glPushMatrix() saves the matrix so that you have a copy that won't be affected by future transforms, including glLoadIdentity(). I've done it a number of times and not had any problems.

nexusone
03-01-2002, 11:30 AM
But we are controlling animation here, you want to know how offten the frame is being updated. We know with glutTimerFunc want the rate will be, with glutIdleFunc we are not sure unless we add a lot of extra code.
I am not sure what effect the processor speed has on the Idle function???



Originally posted by Rob The Bloke:
dont really need the glFlush there either.
why? how often to you press the keyboard? I doubt anyone would be able to generate enough user interupts to prevent the idle function from being called.


[This message has been edited by Rob The Bloke (edited 03-01-2002).]



[This message has been edited by nexusone (edited 03-01-2002).]

zeckensack
03-02-2002, 10:13 AM
Why do you use Push/PopMatrix at all?

Rob The Bloke
03-02-2002, 05:47 PM
Originally posted by zeckensack:
Why do you use Push/PopMatrix at all?

Prevents the call to glLoadIdentity each frame.....

Omaha
03-02-2002, 07:09 PM
glLoadIdentity does NOT "clear the matrix stack." It simply "clears" the active matrix. It's neither uncommon nor ungraceful to glLoadIdentity after glPushMatrix; in fact, it's one of the ways to work with multiple projection matrices (unless you store them and load them directly.) glPopMatrix after that combination will not only work properly, the code will only continue to work if you have it in there somewhere.

The glPushMatrix/glLoadIdentity/glPopMatrix combo is a really useful tool.

[This message has been edited by Omaha (edited 03-02-2002).]

The_V|k|ng
03-03-2002, 08:12 AM
Thanx for your help! Its just that, umm, actually, I didnt help at all... Swapping the places of glLoadIdentity with glPush- or glPopMatrix does not affect a thing, and I still fear that I made some sort ridiculous newbie mistake. Can't anyone of you write a short GLUT-program with a spinning cube without using glut????cube for me, please? You can just copy the display() and the SpinDisplay() funcs off the forum, so it shouldnt be that difficult. I just wanna know why the problem is solved by putting a tiny black glutwirecube in the middle of the screen and getting an Invalid Operation for that in return. There HAS to be a reason, I'm sure....

The_V|k|ng

reubenhawkins
03-03-2002, 09:20 AM
You have a PushMatrix call, a LoadIdentity call and a PopMatrix call. The push and the pop cansel eashother out so there's doing nothing but taking up time. Remove then and just leave LoadIdentity.

reubenhawkins
03-03-2002, 09:22 AM
i = i + 0.05;
if (i > 360) i = 0;

glRotatef (spin, 1.0, 1.0, 0.1);

It seems like that spin should be I since I is your angle of rotation...

Omaha
03-03-2002, 10:35 AM
Reuben, where is the push/identity/pop combination?

The_V|k|ng
03-03-2002, 11:16 AM
Sorry, I know the I is irritating, but it is actually a remnant of some code modification. Anyway, my glutIdleFunc is SpinDisplay() and my glutDisplayFunc is display() as follows:

void display (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (red, green, blue);

glPushMatrix ();

glRotatef (spin, 1.0f, 1.0f, 0.1f);

glBegin(GL_QUADS);
glColor3f (red, green, blue);
glVertex3f (g, g, g);
glVertex3f (g, -g, g);
glVertex3f (-g, -g, g);
glVertex3f (-g, g, g);

glColor3f (0.0, 0.0, 1.0);

glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, g, -g);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (-g, g, -g);
glVertex3f (-g, g, g);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (g, -g, g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glColor3f (1.0, 0.0, 1.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (g, -g, g);

glColor3f (1.0, 1.0, 0.0);
glVertex3f (-g, g, g);
glVertex3f (-g, g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glEnd;

//******* glutWireCube (0.1*g); *********

glPopMatrix();
glutSwapBuffers();
glFlush();
}


void SpinDisplay (void)
{
spin = spin + dspin;
if (spin > 360) spin = spin - 360;
glutPostRedisplay();
}

So as I understood from the RedBook it does not matter wether I call glLoadIdentity or bracket my model in glPushMatrix [...] glPopMatrix; I tried both, the results are the same (And time really does not matter under these circumstances... http://www.opengl.org/discussion_boards/ubb/wink.gif )
What I want to know is what the glutWireCube call changes that the cube suddenly moves. Or, put differently, why the cube does not move without the glutWireCube. The Wirecube is inside the actual cube (perhaps that is why it causes an Invalid Operation OGL error??), so pretty useless, aside from the fact that it makes the real cube move.
I for my part am pretty confused meanwhile.
Perhaps the problem lies in another part of my prog? I that might be, where...?
C'mon, please, just say why... http://www.opengl.org/discussion_boards/ubb/smile.gif

The_V|k|ng

zeckensack
03-03-2002, 11:47 AM
It is glEnd(); ! Put in the brackets and you'll be fine.

Explanation: glEnd; is not a function call. It's just a pointer that's not evaluated. Calling glPopMatrix() inside a glBegin(stuff)/glEnd() pair is an invalid operation. It won't get executed. You're probably overflowing your matrix stack. The glu drawing functions are layered on top of OpenGL and implicitly call glEnd(). That's why they help you here.

[This message has been edited by zeckensack (edited 03-03-2002).]

The_V|k|ng
03-04-2002, 08:01 AM
Hoooooooooooly Jeeeeeeeeez!!!

Yes, OF COURSE it the bloody glEnd() call! I got mixed up with Delphi and C! Thank you very, very, very much, zeckensack (despite your name... http://www.opengl.org/discussion_boards/ubb/wink.gif )! 8)
I just KNEW this was something very stupid... http://www.opengl.org/discussion_boards/ubb/smile.gif
Anyway, I am glad that problem is solved by now.
G'Day, folks!

The_V|k|ng