OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  

 

              Bugs

GLUT Tutorial   

  GLUT Tutorial

Index
Setup

Basics
Initialization
Resizing the Window
Animation

Input
Keyboard
Moving the Camera
Advanced Keyboard
Moving the Camera II
Mouse

Pop-up Menus
Basics
Sub Menus
Modifying a Menu
Swapping Menus

Fonts
Bitmap Fonts
Bitmaps and the Ortho View
Stroke Fonts

Extras
Frames per Second
GLUT Game Mode

Subwindows
Creating and Destroying Subwindows
Resizing SubWindows
Rendering to SubWindows


Google

OpenGLTutorials @ Lighthouse3d.com

Led Shader
View Frustum Culling
GLSL Tutorial
Maths Tutorial
Billboarding Tutorial
Picking Tutorial
Terrain Tutorial
Display Lists Tutorial
GLUT Tutorial

   
[Previous: Resizing the Window] [Next: Keyboard]

GLUT Tutorial


Animation


OK, so far so good. We have an OpenGL window with a white triangle. Nothing very exciting, but hey, its a start. Now to complete this part of the GLUT tutorial lets have that triangle spinning.

Lets go back to the main function and add some extra stuff. First lets tell GLUT that we want a double buffer. Double buffering allows for smooth animation by keeping the drawing in a back buffer and swapping the back with the front buffer (the visible one) when the rendering is complete. Using double buffering prevents flickering.


    
	...
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	...



The second thing we must do is to tell GLUT that when the application is idle the render function should be called. This causes GLUT to keep calling our rendering function therefore enabling animation. GLUT provides a function, glutIdleFunc, that lets you register a callback function to be called when the application is idle.


void glutIdleFunc(void (*func)(void));

Parameters:
func - The name of the function that will be called whenever the application is idle.


In our case, when the application is idle we want to call the previously defined function that does the actual rendering: renderScene. We also need to enable depth testing. By default OpenGL doesn't do this, so it doesn't know what objects are in the front and which are on the back. Unless we did draw everything in back to front order, the objects would be incorrectly drawn. Fortunately all that is required is to enable depth testing as shown in the main function below. OK, so the main function now looks like this:


    
void main(int argc, char **argv) {
	glutInit(&argc, argv);
	
	// This is where we say that we want a double buffer
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("3D Tech- GLUT Tutorial");
	glutDisplayFunc(renderScene);
	
	// here is the setting of the idle function
	glutIdleFunc(renderScene);
	
	glutReshapeFunc(changeSize);
	
	// enable depth testing
	glEnable(GL_DEPTH_TEST);
	glutMainLoop();
}



Afterwards, we go and change the rendering itself. First lets declare a floating point variable angle, and initialize it to 0.0 . Then lets add the necessary stuff to the renderScene function.


    
float angle=0.0;

void renderScene(void) {

	// notice that we're now clearing the depth buffer 
	// as well this is required, otherwise the depth buffer 
	// gets filled and nothing gets rendered. 
	// Try it out, remove the depth buffer part.
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	// save the previous settings, in this case save 
	// we're refering to the camera settings.
	glPushMatrix();
	
	// Perform a rotation around the y axis (0,1,0) 
	// by the amount of degrees defined in the variable angle
	glRotatef(angle,0.0,1.0,0.0);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5,-0.5,0.0);
		glVertex3f(0.5,0.0,0.0);
		glVertex3f(0.0,0.5,0.0);
	glEnd();
	
	// discard the modelling transformations
	// after this the matrix will have only the camera settings.
	glPopMatrix();
	
	// swapping the buffers causes the rendering above to be 
	// shown
	glutSwapBuffers();
	
	// finally increase the angle for the next frame
	angle++;
}



The glutSwapBuffers function cause the front and back buffers to switch thereby showing what was previously drawn in the back buffer. The syntax is as follows:


void glutSwapBuffers();

There you have it, a spinning triangle You may download the VC project here (glut2.zip). Isn't it great? OK, ok...but I warned you, no fancy stuff in here, keeping the code to the minimum and focusing on GLUT is the way to learn this!

[Previous: Resizing the Window] [Next: Keyboard]

       


Site designed and maintained by António Ramires Fernandes
Your comments, suggestions and references to further material are welcome!

Lighthouse 3D privacy statement