A simple OpenGL program

The following program will open a window and render a red triangle in 3-space from a particular viewpoint:
#include "GL/glx.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "iostream.h"

void
OpenGLWindow(unsigned int width, 
             unsigned int height, 
             Display *&dpy, 
             Window &win,
             GLXContext &cx)
{
    XVisualInfo *vi;
    Colormap cmap;
    XSetWindowAttributes swa;
    XEvent event;
    int attributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
			    GLX_DEPTH_SIZE, 24, None };

    /* get a connection */
    dpy = XOpenDisplay(0);

    /* get an appropriate visual */
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
    if (vi == 0)
	{
	    cout << "Couldn't get a spiffy visual -> no window" << endl;
	}

    /* create a GLX context */
    cx = glXCreateContext(dpy, vi, 0, GL_TRUE);

    /* create a color map */
    cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
			   vi->visual, AllocNone);

    /* create a window */
    swa.colormap = cmap;
    swa.border_pixel = 0;
    swa.event_mask = StructureNotifyMask;
    win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height,
			0, vi->depth, InputOutput, vi->visual,
			CWBorderPixel|CWColormap|CWEventMask, &swa);
    XMapWindow(dpy, win);

    /* Wait for it to be placed on the screen */
    while(1)
	{
	    XNextEvent(dpy,&event);
	    if ((event.type == MapNotify) && (event.xmap.window == win))
		break;
	}

    /* connect the context to the window */
    glXMakeCurrent(dpy, win, cx);
}

int 
main()
{
    Display *dpy;
    Window win;
    GLXContext cx;

    // Need a pointer to the display and window being
    // used in order to do double buffering.
    // See glXSwapBuffers.
    OpenGLWindow(800,800,dpy,win,cx);

    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40,1,1,50);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();	    
    gluLookAt(0,0,10,0,0,0,0,1,0);

    glBegin(GL_TRIANGLES);
    glColor3f(1,0,0);
    glVertex3f(-1,0,0);
    glVertex3f(1,0,0);
    glVertex3f(0,1,0);
    glEnd();
    glXSwapBuffers(dpy,win);

    sleep(10);
}
The function OpenGLWindow() opens up a window and returns three identifiers (dpy, win, and cx) which allow us to switch between different X Windows for OpenGL to draw on and different contexts (OpenGL state) in which to draw in.

We now clear the framebuffer with the glClear(GL_COLOR_BUFFER_BIT) statement.

Then we set up the perspective projection matrix. First we specify that we want to be working on the projection matrix with the glMatrixMode(GL_PROJECTION) statement. We then "clear" the matrix by loading the identity matrix onto it. Then we multiply the identity matrix with a matrix what will give us a 40 degree y field of view, a 1.0 x-y aspect ratio, a near clipping plane at 1, and a far clipping plane at 50.

We then switch to the ModelView matrix onto which we load the identity matrix and then specify that we want to look from (0,0,10) to (0,0,0) with an up vector of (0,1,0).

The glBegin(GL_TRIANGLES) statement tells OpenGL that we will now be describing triangles for it to be drawn on the screen.

The glColor3f(1,0,0) statement says that the current color to use is red.

The glVertex3f(...) statements describe the 3D vertices for our triangle.

The glEnd() statement matches the glBegin(GL_TRIANGLES) statement and says that we are finished describing the triangles to be drawn.

Finally, the glXSwapBuffers() call brings the back buffer to the front so we can see what we drew.

At this point, the triangle will be drawn. We then wait a while and finally exit the program.