Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Please tell your teacher that he is doing it wrong!</strong> To do a animation with GLUT don't put a animation loop into the display function, but register a idle function, that reissues a display. Also this code misses a buffer swap (which is mandatory on MacOS X if not running in full screen mode, to tell the compositor you're done with rendering).</p> <p>Also you shouldn't sleep for an animation (the buffer swap will do this anyway) but measure the time between frames.</p> <hr> <h3>Update: Fixed code</h3> <pre><code>#include &lt;OpenGL/gl.h&gt; #include &lt;OpenGL/glu.h&gt; #include &lt;GLUT/glut.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/time.h&gt; #include &lt;math.h&gt; #include &lt;stdio.h&gt; double getftime( void ) { struct timeval tv; gettimeofday(&amp;tv, NULL); return tv.tv_sec + tv.tv_usec*1e-6; } static double lasttime; void display( void ) { int width, height; double finishtime, delta_t; static float angle = 0; width = glutGet(GLUT_WINDOW_WIDTH); height = glutGet(GLUT_WINDOW_HEIGHT); glClearColor( 0.5, 0.5, 0.5, 1.0 ); /* combine the clearing flags for more efficient operation */ glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); glViewport(0, 0, width, height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 90, (float)width/(float)height, 1, 12 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0, 4, 4, 0, 0, 0, 0, 1, 0 ); glEnable( GL_DEPTH_TEST ); glRotated( angle, 1, 0, 0 ); glColor3f( 1, 0, 0 ); glutSolidSphere( 1, 20, 20 ); glColor3f( 0.8, 0.8, 0 ); glutWireSphere( 1, 20, 20 ); glColor3f( 0, 0, 1 ); glutSolidTorus( 1, 2, 40, 40 ); glDisable( GL_DEPTH_TEST ); glutSwapBuffers(); finishtime = getftime(); delta_t = finishtime - lasttime; angle = fmodf(angle + 10*delta_t, 360); lasttime = finishtime; } int main( int argc, char **argv ) { glutInit( &amp;argc, argv ); glutInitWindowSize( 600, 600 ); /* glutInitDisplayMode must be called before glutCreateWindow, flags are prefixed with GLUT_ not GL */ glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); glutCreateWindow( "Depth Buffer" ); glutDisplayFunc( display ); /* register glutPostRedisplay for continuous animation */ glutIdleFunc(glutPostRedisplay); lasttime = getftime(); glutMainLoop( ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload