PDA

View Full Version : Rotating Camera in GLUT



Stack Overflow
11-14-2002, 01:25 PM
Does anyone have an idea of how to rotate a Camera without glutPostRedisplay() or at least correctly using GLUT?

So far I am rotating the camera using glutPostRedisplay() in my Display function, but it's making my computer go really slow, because it's trying to update every second.

[This message has been edited by VC6-OGL (edited 11-14-2002).]

nexusone
11-14-2002, 02:03 PM
It is correct that you must redraw the scene whenever something has changed in the scene, camera, objects, etc.

There is no problem calling redisplay if the scene has changed.

How about posting some of your code, can advise as to what you can do to maybe inprove your drawing speed.



Originally posted by VC6-OGL:
Does anyone have an idea of how to rotate a Camera without glutPostRedisplay() or at least correctly using GLUT?

So far I am rotating the camera using glutPostRedisplay() in my Display function, but it's making my computer go really slow, because it's trying to update every second.

[This message has been edited by VC6-OGL (edited 11-14-2002).]

Stack Overflow
11-14-2002, 02:07 PM
Define floats:
float g_RotateX = 0.0f;
float g_RotationSpeed = 0.1f;

Look At function:
gluLookAt ( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); {
glRotatef( g_RotateX, 0, 1.0f, 0 );
g_RotateX += g_RotationSpeed;
glutPostRedisplay();
}

nexusone
11-14-2002, 02:30 PM
First a little more code would be nice...
But glrotation is 0 to 360 in degrees, you use 0.1 degree movement, that is for a full rotation 3600 movements..... change to 1.0 degree movement that will increase speed and not really make the movement jurky.


Also if you are going to use glRotate to change your view, just do away with the gluLookAt. and use glTranslate and glRotate together to move the camera view.

Are you using doubble buffers?

I put normally put the lookat or view changes in the display...


void display(void)
{
bla
bla
bla

glLookAt or glRotate/translate

draw world.


Originally posted by VC6-OGL:
Define floats:
float g_RotateX = 0.0f;
float g_RotationSpeed = 0.1f;

Look At function:
gluLookAt ( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); {
glRotatef( g_RotateX, 0, 1.0f, 0 );
g_RotateX += g_RotationSpeed;
glutPostRedisplay();
}

Stack Overflow
11-14-2002, 02:34 PM
I will E-mail you my code so you can look at it.

nexusone
11-14-2002, 02:59 PM
Note with out a floor or other object really hard to tell if the object or camera is moveing.

Here is some edits:

void display( void ) {
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // glClear - COLOR/DEPTH
glColor3f ( 1.0, 1.0, 1.0 ); // Cube Color - White
glLoadIdentity(); // Load gl Identity
glTranslatef ( 0.0, 0.0, -5.0 ); // Camera Position - Straight
glRotatef(g_RotateX, 0, 1.0f, 0); // Camera Rotation
g_RotateX += g_RotationSpeed; // Setup g_RotateX
glutPostRedisplay(); // Remove should no be called here to draw cube. // Update Screen
glScalef ( 1.0, 1.0, 1.0 ); // Cube sizeScale
glutWireCube ( 2.0 ); // Cube Size on screen
glFlush(); glutPostRedisplay(); // place here. not the best way to update screen but fix's problem, best to use glutIdleFunc(); // Flush gl Memory
}

void specialkeys ( int key, int x, int y ) {
switch ( key ) {
case GLUT_KEY_LEFT: // On Left-Key
ycam = ( ycam - 2 ) % 360; // Move camera one notch to the left
break;
case GLUT_KEY_RIGHT: // On Right-Key
ycam = ( ycam + 2 ) % 360; // Move camera one notch to the right
break;
case GLUT_KEY_UP: // On Key-Up
xcam = ( xcam - 2 ) % 360; // Move camera one notch to the up
break;
case GLUT_KEY_DOWN: // On Key-Down
xcam = ( xcam + 2 ) % 360; // Move camera one notch to the down
break;
case GLUT_KEY_PAGE_UP: // On Page-Up
zcam = ( zcam - 2 ) % 360; // Move camera right on z-axis
break;
case GLUT_KEY_PAGE_DOWN: // On Page-Down
zcam = ( zcam + 2 ) % 360; // Move camera left on z-axis
break;
default:
break;
}
glutPostRedisplay(); // just update here....
}


Originally posted by VC6-OGL:
I will E-mail you my code so you can look at it.



[This message has been edited by nexusone (edited 11-14-2002).]

nexusone
11-14-2002, 03:31 PM
another change would be to add glutIdleFunc() place before glutMainLoop();

glutIdleFunc(My_idle_func);


void My_idle_func(void)
{
glutPostRedisplay(); // Place here instead of the end of the display routine.
}

Stack Overflow
11-14-2002, 03:31 PM
glutIdleFunc() does not take 0 paramaters. What am I supposed to do?

Stack Overflow
11-14-2002, 03:41 PM
Ok, I added the glutIdleFunc(), but whenever i minimize, maximize or close the program it sort of hangs on my screen. Do you have any idea why?

[This message has been edited by VC6-OGL (edited 11-14-2002).]

Stack Overflow
11-14-2002, 03:49 PM
Are there any other ways to refresh the screen besides glutPostRedisplay(). Or is there any other way to move a camera without using Rotatef().

[This message has been edited by VC6-OGL (edited 11-14-2002).]