简体   繁体   中英

OpenGL Triangle rotation

I drew a triangle and i want to rotate it on X-Axis continuously. Below code is rotating it for couple of times.

//Paints the GL scene
void GLobj::paintGL(void)
{
   static int angle;
for (angle = 1; angle<=360; angle += 1)
{  
   glClear (GL_COLOR_BUFFER_BIT);
   glClear(GL_DEPTH_BUFFER_BIT);
   glRotatef(angle, 1, 0 , 0);
   glBegin(GL_TRIANGLES);
   glColor3f (1.0, 0.0,1.0);
   glVertex2f(-0.45,0.0);
   glVertex2f(-0.65,0.5);
   glVertex2f(-0.25,0.5);
   glEnd();
   usleep(1000);
glFlush ();

}

If i change the above code as below, output is just busy in executing and not displaying anything.

//Paints the GL scene
void GLobj::paintGL(void)
{
   static int angle;
for (angle = 1; angle<=360; angle += 1)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glClear(GL_DEPTH_BUFFER_BIT);

   glRotatef(angle, 1, 0 , 0);

   glBegin(GL_TRIANGLES);
   glColor3f (1.0, 0.0,1.0);
   glVertex2f(-0.45,0.0);
   glVertex2f(-0.65,0.5);
   glVertex2f(-0.25,0.5);
   glEnd();
   usleep(1000);

if (angle == 360)
{angle = 1;}
}

glFlush ();

}

Is there any other better way of implementing continuous rotation?

Most graphic APIs like OpenGL and DirectX write all changes into something called back buffer: The operations are only displayed once you have rendered your complete scene. If each rotation step or glEnd() rendered immediately, you would always see your scene building up each time. The result would be flickering which is not nice to see.

You can tell OpenGL when you have finished drawing your scene, so that everything can be displayed at once. You do this with glFlush .

In your second example, you conduct a rotation and another one and yet another one... at the very end you display your final scene once. The nice transition of your triangle through the rotation phases is not rendered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM