|
In this tutorial I will show you the basic structure of a program that uses GLUT. First thing you need to understand is that you don't call glut functions...they call you :D What I mean is that in main() you will surrender control to GLUT, after you've set everything up of course. Basically you tell glut to call your functions as events. // The headers that we need #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> /* This is an initialization function. In this function you usually set things that need to be executed one like the shadeing model. Not that it would be the purpose of this tutorial but the // shadeing model is what gives you the impression of volume: some part of your object are more lit then others. This color difference is what makes you think it's 3D.*/ void init(void) {   glClearColor (0.0, 0.0, 0.0, 0.0);   glShadeModel (GL_FLAT); } // The display function is usually where you draw things. Glut calls this function whenever things need to be redrawn. It's like the WM_PAINT in windows. void display(void) {   // Every time you render a new frame you should clear the color buffer. This is where everything you draw ends up.   glClear (GL_COLOR_BUFFER_BIT);   // Set the color you draw with. The parameters are float values between 0 and 1 representing RGB components. 0 is like 0% or none and 1 is 100%. So (1,1,1) means white, (1,0,0) is red, .....   glColor3f (1.0, 1.0, 1.0);    // I'm not going to get into matrixes right now. I want to explain these in detail in the next tutorial.    glLoadIdentity (); /* clear the matrix */    /* viewing transformation */    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    glScalef (1.0, 2.0, 1.0); /* modeling transformation */    // This draws a primitive    glutWireCube (1.0);    /* This line is very important: it tells opengl we are ready to show the user what we have drawn. It's like a "Save changes". This call will actualy display things. Depending of what type of screen you used you would call glFush() or glSwapBuffers(). If your screen is double buffered you would call the second. If it's single then the first. What's a double buffered screen you ask? Well you'd have two screens: one that you see and one that's invisible. OpenGL would draw in the invisible one and when you call SwapBuffers it would switch them: make the one you saw invisible and the invisible one visible. That way you never see a flicker when OpenGL draws stuff as it never draws directly on the screen. I'll show you later where you choose this option.*/    glFlush (); } /* This function is called when the window is resized. w and h will be the new width and height of the screen. Some adjusting needs to be made if the screen is resized but I woun't get into any details for now.*/ void reshape (int w, int h) {   glViewport (0, 0, (GLsizei) w, (GLsizei) h);   glMatrixMode (GL_PROJECTION);   glLoadIdentity ();   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);   glMatrixMode (GL_MODELVIEW); } /* This function is called when a key is pressed. This is where you would treat your hotkeys. 27 is the code of the ESC key so in this example you would quit the program when the esc key is pressed*/ void keyboard(unsigned char key, int x, int y) {  switch (key)  {      case 27:      exit(0);      break;   } }
int main(int argc, char** argv) { Â Â // initialize GLUT Â Â Â glutInit(&argc, argv); Â Â Â /*This is where you tell glut if you want a single buffer or a double buffered window. GLUT_RGB tells what kind of color system you want. We could have chosen GLUT_RGBA too if we needed the alpha component of the color.*/ Â Â glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); Â Â // These are the coordinates and size of the window that will be opened. Â Â glutInitWindowSize (500, 500); Â Â glutInitWindowPosition (100, 100); Â Â // Create the window now! Â Â glutCreateWindow (argv[0]); Â Â init (); Â Â /*Glut is an event based framework. You don't actually control anything. You tell GLUT where and when to call your functions to do your stuff. It's like "Don't call us...we'll call you :D". All you have to do is have the right signature for a function (the right parameters) and give GLUT the pointer to it. It will call it at the right time with the right arguments This is how you register a display function. Actualy this is how you register all functions...by passing a pointer to opengl saying: this is the function you need to call to render my scene. This goes for all other events like mouse, keyboard and reshape.*/ Â Â glutDisplayFunc(display); Â Â glutReshapeFunc(reshape); Â Â glutKeyboardFunc(keyboard); Â Â // This call will give the control to glut. It will loop infinitly and call your functions when the events you registered with happen. Â Â glutMainLoop(); Â Â return 0; }
|