Example 01

Example 01

This is a simple OpenGL program that draws a square and quits when the user presses q.

ex_01/hello.png

Source Code

hello.cc:

// Peter Bui
// CSE 40166 Computer Graphics (Fall 2010)
// Example 1: simple hello, world program
 
#include <cstdlib>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

// Display callback ------------------------------------------------------------

void 
display()
{
    glClear(GL_COLOR_BUFFER_BIT);   // Erase everything

    glColor3f(0.0, 0.0, 1.0);       // Set color blue
    glBegin(GL_POLYGON); {          // Define a polygon 
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5,  0.5);
        glVertex2f( 0.5,  0.5);
        glVertex2f( 0.5, -0.5);
    } glEnd();

    glFlush(); // Flush all drawings to the screen
}

// Keyboard callback ------------------------------------------------------------

void
keyboard(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q')
        exit(EXIT_SUCCESS);
}

// Main execution ---------------------------------------------------------------

int 
main(int argc, char *argv[])
{
    glutInit(&argc, argv);      // Initialize GLUT
    glutCreateWindow("hello");  // Create a window
    glutDisplayFunc(display);   // Register display callback
    glutKeyboardFunc(keyboard); // Register keyboard callback
    glutMainLoop();             // Enter main event loop

    return (EXIT_SUCCESS);
}

// vim: sts=4 sw=4 ts=8 ft=cpp

Files