Yet another OpenGL tutorial

While I was learning OpenGL, I wrote small programs to try various things, from basics to a bit more advanced ones. Since it might be useful to someone else, I have decided to put it here. Examples are written in C, and use GLUT, and also the libjpeg for some of them, but they can be easily translated to other language or toolkit.

I must admit I'm pretty surprised by the popularity of this page. Thank you very much to all of you, who are using it or even linking to it. I started to include a Visual Studio project (VS 2008) with some of the examples, to make them easier to use for Windows users. No difference for Unix users: still a good old Makefile. :-) The libjpeg doesn't seem to be so straightforward to compile with under Windows, so I'll try to find some workaround.

If you are willing to learn OpenGL, the best advice I can give you is to get the Redbook. It is a very good book, for both beginners and advanced OpenGL coders. By the way, if you do not want to buy it, and nor does the library of your university, you can still download an old version.


Right now, there are only the following items:

I have to clean the code and add a few comments (and translate those written in French) before putting it here. Then the list should also cover textures, lights, selection mode, stencil buffer use, text rendering, static terrain rendering...

Spinning cube

An example of spinning cube

Every single OpenGL programmer has, at least one time, made this small program: the spinning cube. Most of them get back to it when everything goes wrong, that is to say, the black screen syndrome... :-)

Just hit Q or Escape to leave.

Download the spinning cube example.

Vertex array

An example of spining cube using vertex array

Here is the very same example, but using vertex arrays, far more efficient than immediate mode (which disappeared in OpenGL ES, and is rather deprecated).

glEnableClientState and glDisableClientState functions allow to enable or disable arrays, while the functions glVertexPointer, glColorPointer, glTexCoordPointer and so on allow to point on data sources. At last glDrawArrays sends data to the pipeline.

Just hit Q or Escape to leave.

Download the vertex array example.

Display lists

An example of display lists

The OpenGL API allows to compile a set of instructions, a display list, in order to call it later. Since instructions can be calls to display lists, display lists can be nested. On configurations where client and server are different hosts, this avoids sending useless instructions over the network. On recent hardware, this also permits to have the result of the compilation saved directly on the 3D accelerator.

The functions glGenLists, glNewList, glCallList, and glDeleteLists allow respectively to declare, compile, call, and delete a display list.

The example renders 27 teapots thanks to display lists. Use left click to turn the whole group, and right click to turn the teapots locally.

Download the display lists example.


example of viewport

The OpenGL function glViewport allows to define the region of the window where the final image will be mapped.

This example shows an object from different points of view. Use left click to turn the perspective view, and right click to turn the teapot.

Download the view-port example.


Three different kind of billboards

Billboards are simple polygons always facing the camera. There are different kinds of billboards, and three of them are presented here. Axis aligned billboards are usually used for rendering trees or beams for example. World aligned billboards are well suited for particules. At last, screen aligned billboards can be used for displaying text. There are also eye aligned billboards, but they are not used here.

The program displays nine groups of billboards. The bottom ones are axis aligned billboards, the middle one are world aligned, and the top ones are screen aligned. Use left click to rotate the view, and right click to rotate around camera axis.

Download the billboard example.

Render to texture

Render to texture used to create infinite motif

Rendering to texture is a very useful tool, very simple to implement by the way. Having a destination texture, the scene just has to be drawn as usual, and then the buffer is copied to the texture using the OpenGL function glCopyTexImage2D or glCopyTexSubImage2D.

The example uses rendering to texture to paint a cube with its own image, resulting in a mise en abîme.

Download the rendering to texture example.

Texture coordinates effects

Various effects thanks to texture coordinates

Books and tutorials usually present the function glTexGen* as a way to render reflective objects. But there are plenty of other nice effects which can be achieved using this function.

This example shows some effects like chrome or gold looking, outline rendering, Mercedes like color, Final Fantasy like ghost rendering... Use left click to rotate, and right click to translate.

Download the texture effects example.

Outline rendering using Z buffer

example of outlined teapot

There is a very simple way to render outlines, using the Z-buffer: the object is first rendered, then it is rendered a bit closer, culling the front faces. Thanks to the Z-buffer, only the outline will appear.

In the example, the object is first rendered transparent, then black, resulting only the black outlines. The colored rectangles are behind it. Use left click to turn the teapot.

Download the outline rendering example.

Simple water rendering

though nice water rendering

Still using texture coordinates, this time here is a very simple way to render fancy water. Waves are shaped using both a sinus function and some Perlin noise. The same texture is used for both fake reflection and transparency, using texture spherical coordinates. Tweaking the texture may lead to quite a good result I think.

Use left click to rotate, and right click to translate. You can also switch to wire-frame rendering using L, and get back using P or F. N toggles normals rendering.

Download the water rendering example.

Informations about the OpenGL implementation

Some information about OpenGL implementation

OpenGL implementation depends on both hardware and software. In particular, extensions availability changes from a configuration to another. Other parameters like matrix stacks size, number of lights, or texture size depends on it too. So it is interesting to know in advance those informations.

This program opens a window to be able to call OpenGL functions, then prints some informations, and exits. As you can guess from the snapshot (quite short extensions list), my hardware is an old one actually (3Dfx Voodoo3). ;-)

Download the OpenGL informations example.

To be continued...

Soon, but maybe not... :-)
Text Volumetric cloud rendering Anisotropic lighting


Back to the main page...

Valid HTML 4.01! Valid CSS!