Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: cannot understand the rotation

  1. #1
    Newbie Newbie
    Join Date
    Nov 2013
    Posts
    1

    Unhappy cannot understand the rotation

    Hi

    I try to write something to show rotation, but I cannot understand why the whole rotating process is repeated again and again when I run the project.

    I run it on Windows using codeblocks. Can anyone please help me?

    Here is my code:
    Code :
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <Windows.h>
    void init (void)
    {
        glClearColor (1.0, 1.0, 1.0, 0.0);
        glMatrixMode (GL_PROJECTION);
        gluOrtho2D (-5.0, 5.0, -5.0, 5.0);
        glMatrixMode (GL_MODELVIEW);
    }
     
    void drawSquare(void)
    {
        glBegin (GL_POLYGON);
        glVertex2f (-1.0f,-1.0f);
        glVertex2f (1.0f,-1.0f);
        glVertex2f (1.0f, 1.0f);
    //    glVertex2f (0.0f, 1.2f);
        glVertex2f (-1.0f,1.0f);
        glEnd ( );
    }
     
    void rotate()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glColor3f(1.0, 0.0, 0.0);
        drawSquare();
        glFlush();
        Sleep(1000);
        glClear(GL_COLOR_BUFFER_BIT);
        for(int i = 0; i < 30; i++)
        {
            glTranslatef(2.0 / 30, 3.0 / 30, 0.0);
            glRotatef(1, 0.0, 0.0, 1.0);
            glColor3f(0.0, 1.0, 0.0);
            drawSquare();
            glFlush();
            Sleep(100);
            glClear(GL_COLOR_BUFFER_BIT);
        }
    }
     
    int main (int argc, char** argv)
    {
        glutInit (&argc, argv);
        glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
        glutInitWindowPosition (100, 100);
        glutInitWindowSize (600, 600);
        glutCreateWindow ("Geometrical Transfromation");
        init();
        glutDisplayFunc (rotate);
        glutMainLoop ( );
    }

  2. #2
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Posts
    537
    The method you have used is not suitable to show rotation. Use glut to set a timer and use double buffers. Then draw the quad including its translation and rotation once per frame.

    Beside this point, you are not using glLoadIdentity() before glTranslate()/glRotate() command. Since OpenGL glare in object space, your 3D geometry is translated and rotated in each loop 'reiterative to its current translation/rotation status'.
    Instead, use something like this:

    void Render()
    {
    //Activate identity matrix
    //load identity matrix
    //translate
    //use an incremental variable for glRotate
    //Rotate the square using incremental variable
    //draw the square
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •