PDA

View Full Version : glRotatef : some confusion



centurion
05-15-2011, 12:23 PM
float x_rot_angle;
float y_rot_angle;

void keypress ( int key, int x ,int y)
{
if (y_rot_angle >= 360)
{
y_rot_angle = 0;
}
if (x_rot_angle >= 360)
{
x_rot_angle = 0;
}

if ( key == GLUT_KEY_LEFT)
{
y_rot_angle += 1.0f;
}
if ( key == GLUT_KEY_RIGHT)
{
y_rot_angle -= 1.0f;
}
if ( key == GLUT_KEY_UP)
{
x_rot_angle += 1.0f;
}
if ( key == GLUT_KEY_DOWN)
{
x_rot_angle -= 1.0f;
}

if (y_rot_angle < 0)
{
y_rot_angle = 360 + y_rot_angle;
}
if (x_rot_angle < 0)
{
x_rot_angle = 360 + x_rot_angle;
}
glutPostRedisplay();
}

void keyPressed (unsigned char key, int x, int y)
{
if ( key == 27)
{
exit(0);
}
}

void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f );
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
glVertex3d(-1.0f,-1.0f,0.0f);
glVertex3d(-1.0f,1.0f,0.0f);
glVertex3d(1.0f,1.0f,0.0f);
glVertex3d(1.0f,-1.0f,0.0f);
glEnd();
glRotatef(x_rot_angle, 1.0f, 0.0f, 0.0f);
glRotatef(y_rot_angle, 0.0f, 0.0f, 1.0f);
glPopMatrix();
glutSwapBuffers();
}
void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

int main (int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("3dGame");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
glutMainLoop();
}


In the above piece of code the rotation of objects didnt take place whereas when i changed the position of the glRotate3f commands the rotation started .. Cud anybody tell the reason why ???

Changed piece of code only the display function
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f );
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glRotatef(x_rot_angle, 1.0f, 0.0f, 0.0f);
glRotatef(y_rot_angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3d(-1.0f,-1.0f,0.0f);
glVertex3d(-1.0f,1.0f,0.0f);
glVertex3d(1.0f,1.0f,0.0f);
glVertex3d(1.0f,-1.0f,0.0f);
glEnd();

glPopMatrix();
glutSwapBuffers();
}

BionicBytes
05-15-2011, 12:43 PM
The glRotatef command builds an internal matrix to multiply against the ModelView matrix. You will not observe the effect of this multiplication until you next issue a draw call. In your first block of code you issue glRotatef but then follow with glPopmatrix. glPopMatrix restores the ModelView matrix from the stack ( saved when you issued glPushMatrix) and thus the glRotatef matrix multiplication is undone.

In your second code block you follow up the glRotatef with draw calls and thus the primitives are drawn with the modified transformation matrices.