Skip to main content
gamedev.net
🔒 Locked

Rotation Problems

Started by Lord FireFox Sep 21, 2000 at 4:28 PM 8 replies 900+ views
Original Post
Lord FireFox
Lord FireFox
Below is simple source for a OpenGL test program, I've had to replace much of it cause some functions were Obsolete, the problem I'm haveing with it is I cannot seem to enable rotation in the program, I have commented it out untile I can get it working, If anyone can point me in the right direction it would be much appreciate...Thanks in advance /*------------------------------------------*/ /* most of this code has been reconstructed */ /* to be compatible with the latest version */ /* of GLUT libraries and source */ /* - */ /* Lord FireFox */ /*------------------------------------------*/ #include "glut.h" static float g_Angle = 0.0f; // for later #define ANGLE_INCREMENT 5 #define SCREEN_WIDTH 256 #define SCREEN_HEIGHT 256 void display(); void idle(); int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(SCREEN_WIDTH,SCREEN_HEIGHT); glutCreateWindow("Simple GLUT program"); // Window Title (duh) LF /* Set the background color */ // LF: I fixed this part here because it was // LF: obsolete glClearColor (0.0, 0.0, 0.0, 0.0); // Callback functions ( oh fun!! ) LF glutDisplayFunc(display); glutIdleFunc(idle); // Setup the projection matrix glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.f,SCREEN_WIDTH,0.f,SCREEN_HEIGHT,0.f,-800.f); glTranslatef(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,0.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Main Loop glutMainLoop(); return 0; } void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glRotatef(g_Angle, 0, 0, 1); glBegin(GL_QUADS); glColor3f(0,1,0); glVertex2f(-100.0,-100.0); // Today kids, we're drawing a box LF glVertex2f(-100.0, 100.0); glVertex2f( 100.0, 100.0); glVertex2f( 100.0,-100.0); glEnd(); glPopMatrix(); glFlush(); glutSwapBuffers(); } void idle() { // LF: This part here may be obsolete too, // LF: I'm still trying to fix it. Thats // LF: why its commented out for now. /* g_Angle += ANGLE_INCREMENT; while(g_Angle>360) g_Angle -= 360; while(g_Angle>0) g_Angle += 360; glutPostRedisplay(); // Still working on this part LF */ } "I hope you brought your wits along with you for sanity is in short supply here." Hratli of Kurast (Diablo 2) Edited by - Lord FireFox on 9/21/00 4:31:27 PM
"I hope you brought your wits along with you for sanity is in short supply here." - Hratli of Kurast (Diablo 2)
Strylinys
Strylinys
If this is the code stripped right from your program then it''s in your very last function there... idle()...

You should be using an if/else if structure there instead of a while otherwise you will get issues because you have nothing to break the while loop and therefore it will never get to the last line of the function and your program will stop responding...

It''s this line right here, from what I can tell, that is inhibitting you from continuing.

while(g_Angle>0) g_Angle += 360;

Since g_Angle is 5 to start with, you will add 360 until it''s a negative number...

positive + number of positives = negative only after the limits of a float are hit. This is why it appears you are not rotating. If you wait for a while, I think you will find it will rotate very slightly.

S.
Lord FireFox
Lord FireFox
Yeah it should rotate slowly, but the real problem is when its executed it locks up instead of continueing on after execution. the code is stripped right out of what I was doing and yes it ends there, I figured that the idle() might have been the culprit, I''m still working at it trying to figure out how to get it working, so far no luck, I''ll try the if/else instead of while and hopefully it will work. Thanks for helping
"I hope you brought your wits along with you for sanity is in short supply here." - Hratli of Kurast (Diablo 2)
Bad Monkey
Bad Monkey
Shouldn''t you just change the sign for that test condition, ie:

while(g_angle < 0) g_angle +=360;

Otherwise, it looks OK to me. Maybe I just totally missed something?


-------------
I was sick of my crappy sig, so I changed it
Lord FireFox
Lord FireFox
My bust...yeah your right it should be...

while(g_Angle>0) g_Angle += 360;

Thanks
"I hope you brought your wits along with you for sanity is in short supply here." - Hratli of Kurast (Diablo 2)
alistair b
alistair b
i think the problem is:

while(g_Angle>360) g_Angle -= 360;
while(g_Angle>0) g_Angle += 360;

both conditions are met when g_Angle is greater than 360 so you add 360 then subtract 360

what i think you need is:
while(g_Angle>360) g_Angle -= 360;
while(g_Angle<360) g_Angle ++;

this will increment g_Angle by 1 until you reach 360 then reset to 0 (or 1?) thus providing smooth rotation.

alistair
Strylinys
Strylinys
quote "both conditions are met when g_Angle is greater than 360 so you add 360 then subtract 360"

... actually, alistair b, the first will never be met since it starts passing to the while loops as 5.0f... This means it''s not greater than 360, but is greater than 0. The problem with while loops is this -- when it hits the second loop it will execute as many times until the case is no longer true, ie, until g_Angle is less than 0.

If you start above 0, and add 360, you will have to wait until you surpass the limit of the data type. When this occurs, it will then be set to an extremely low negative number... This negative number will do nothing when it passes through these loops since it is never greater than 360 or 0, and therefor it will be neither incremented nor decremented. Hence the need for an if/else if structure...

if (g_Angle >= 721)
g_Angle = 721;
else if (g_Angle > 360)
g_Angle -= 360;
else if (g_Angle > 0)
g_Angle += 360;
else if (g_Angle <= 0)
g_Angle = 0;

or, as suggested above, a modification to the while loop...

while(g_Angle < 0) g_Angle +=360;

The above if/else if will execute only once during every pass but will insure that the proper range of degrees is met, even if it somehow gets below 0 or above 721.

The modified while loop will also account for the angle being below 0 or above 721, but, depending on how much higher the number is, it may execute many times before the desired range is reached.

S.
Strylinys
Strylinys
Oops, forgot to note on one thing... He''s already incrementing by 5 degrees every time, so the g_Angle++ is not needed, alistair b...

S.
DIL
DIL
Hey, guys !

As noted above by Strylinys, line

while(g_Angle>0) g_Angle += 360;

takes very long time to complete (much more than a minute) !

I think it should be:

while(g_Angle<=-360) g_Angle += 360;

Dont worry, be smart

SKSlayer
SKSlayer
No kiddin, if you do that, that''s gonna take years
(you can find me on IRC : #opengl on undernet)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.