Results 1 to 2 of 2

Thread: glRotatef : some confusion

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    3

    glRotatef : some confusion

    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();
    }

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: glRotatef : some confusion

    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.

Similar Threads

  1. glRotatef
    By kiwon0905 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-03-2012, 12:28 AM
  2. glRotatef???
    By badwhorsey in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 07-06-2012, 02:47 PM
  3. Why do i need glRotatef()?
    By openujs in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 11-14-2005, 06:33 AM
  4. Replace two glRotatef() with one glRotatef()?
    By tgtt in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 02-24-2004, 02:33 PM
  5. glRotatef
    By Krime384 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-01-2000, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean