Monday, June 20, 2011

GLUT intro.





OpenGL Utility Toolkit (GLUT)
-  http://www.xmission.com/~nate/glut.html
- Ver: 3.7.6 (Nov 8, 2001)! 


  • Init GLUT and create window
void main(int argc, char **argv) {
    // init GLUT and create window

    glutInit(&argc, argc);
    glutInitWindowPosition(1,1);
    glutInitWindowSize(640, 480);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("CreateWindow");

    // register callbacks
   glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();
}



void renderScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glVertex3f(-.5, -.5,0);
    glVertex3f(.5, 0,0);
    glVertex3f(.0, .5,0);  
    glEnd();
    glutSwapBuffers();
}




  • The render function and callback registration
- Tell GLUT what is the function responsible for the rendering. This function will be called by GLUT everytime the window needs to be painted.


  • GLUT event processing loop
- Tell GLUT that we’re ready to get in the event processing loop.



Preparing the window for a reshape
- By default the perspective assumes that the ratio width/height is 1 and draws accordingly. So when the ratio is changed the perspective gets distorted. Therefore, every time the ratio changes the perspective needs to be recomputed.
-  GLUT provides a way to define which function should be called when the window is resized, i.e. to register a callback for recomputing the perspective. Furthermore, this function will also be called when the window is initially created so that even if you’re initial window is not square things will look OK.

void changeSize(int w, int h)
{
    if (h == 0)
        h = 1;
    float ratio = 1.0 * w / h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0, 0, w, h);
    gluPerspective(45, ratio, 0.0, 100);

    glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();
}




Animation
- The first 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.











No comments:

Post a Comment