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...
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. |
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. |
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. |
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. |
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. |