
However, even though most OpenGL functions can accept any type, it's usually best to pass floats. OpenGL uses floating point values for all its internal calculations, so passing any other type is a waste of time since OGL will just convert it back to floating point anyway.
The next function, glutSwapBuffers(), swaps the back buffer to the screen buffer. In double-buffered mode, the drawing commands do not draw to the screen. Rather, they draw to an offscreen buffer. Once you are done drawing, you copy the entire back buffer to the screen at once, thus producing smooth animation. Of course, in this simple example there is no animation, but without a double buffer even moving the window around the screen will cause the rectangle to flicker. Besides, it's good to get into the habit of producing smooth graphics!
OK, now feel free to copy this program into an editor, compile it, and run it. Note you will have to link with whatever libs your version of OpenGL has provided, as well as the GLUT library. Under linux you will also probably have to link the X Window libraries such as libX11.a.
Congratulations! You are now an OpenGL Programmer! But I know what you're
saying…"Where are the 3D graphics? You think I'm reading this junk to learn
how to draw a stupid square?" Well, since you're so impatient, lets move on to creating 3D worlds with OpenGL…
II. Points, Lines, and Polygons
Before we start drawing 3D objects, lets quickly go over some fundamentals. You will find drawing to a 3D canvas is in fact very similar to plain old 2D applications. This is because OpenGL uses a coodinate system called the Cartesian plane. The difference here is that instead of having two axis which stretch horizontally and vertically, we add a third axis, the z (or depth) axis. You can think of this as a line which runs through the origin (0,0 on a Cartesian coordinate system), going in the direction from away from you to straight towards you (in OpenGL, the positive z-axis always points towards you, while the negative points away). Using this system, we can represent a point in 3D space, called a "vertex", with three coordinites, representing x, y, and z. For example:
