Swiftless Game Programming Site
Home Tutorials Contact
Home
News
OpenGL
GLSL
OpenCL
Maths
Misc
Work in Progress
Resume
 
 

Smooth Rotation in OpenGL using Double Buffering

If you would like to see this site updated, please help out and

Last time we looked at rotation the cube looked 'broken' now it
looked like this because we had a single buffer running. This
means that the program is automatically drawing straight to the
window. To fix this we add a second buffer, by changing the:
glutInitDisplayMode (GLUT_SINGLE);
to:
glutInitDisplayMode (GLUT_DOUBLE);
this is giving the programa buffer to draw what it has to, then transfer
what is acctually needed to the screen.
You may notice that alot of games these days even have a triple
buffer, this is pretty redundant in OpenGL, in fact it doesn't even exist in OpenGL.

When changing the buffer from Single to Double we also have to
tell the program to swap the buffers, so we acctually see what
is on the second, and not just what we saw before.

To do this, change the line in the 'display' function that says:
glFlush();
to:
glutSwapBuffers();

And your done, the cube should be rotating perfectly.

If you have any questions, please email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
  #include <GL/gl.h>
#include <GL/glut.h>

GLfloat angle = 0.0; //set the angle of rotation

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
    glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
    glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(2);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();  
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glutSwapBuffers();
    angle ++;
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
;
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE); //set up the double 
buffering

    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}


Download C++ Source Code for this Tutorial

Download Visual Basic Source Code for this Tutorial


Comments:

Name: CC
Date: 2010-02-07 16:38:52
Comment:
Great set of tutorials. I looked at the ones at Nehe and they were too complex for a beginner. Thanks!

Name: icl_3rdY_2010
Date: 2010-02-24 08:41:50
Comment:
what do you mean by broken? the cube rotates quite fast. even if i use double buffers. isn't better to make it smooth by increasing the angle small amount? (instead of 1 , perhaps 0.01)?

Name: Donald Urquhart
Date: 2010-02-24 12:58:50
Comment:
Hi,

Yes it would be better to increment by a smaller amount if you want it to look smoother still. The point of this tutorial is to show the difference between the single and double buffering (single buffering being the first tutorial on rotation), to show how double buffering makes your rendering appear smoother.

When I say broken, on a single buffered window, you may have half the scene rendered, and then the system is ready to render another frame, and does so instantly, producing artifacts. Whereas using the double buffer, only a fully rendered scene is sent to the window once it is completed, removing these artifacts.

Cheers,
Donald Urquhart
Name   Email


Reload Image

 
     

 

Copyright 2009, Donald Urquhart AKA Swiftless
Check out: http://www.cdadc.com