16 simple GL3 examples

by Ingemar Ragnemalm

These are a set of very simple OpenGL 3.2 examples, moving through some basic functionality in small steps. This includes vertex/color/normal/texture coordinate buffers, basic transformations, textures, lighting and OBJ loading. If you find these too simple (and you will, sooner or later) you may want to check out my other demos.

The code is written and mostly tested under MacOS X 10.7 or 10.8 using Lightweight IDE (version 0.9.5p+3 or better recommended). It has also been tested under Linux, but testing there is limited so bugs may creep in. See "White triangle" and "Perspective bunny" for examples with compilation instructions for Linux (in the source). Some of the demos have also been tested under MS Windows.

Makefiles? Sorry, for 16 demos I don't quite have the time right now. For Lightweight IDE you don't need one (major point with the program). I may produce a makefile later if I find the time.

In approximate order of complexity (which is pretty low for all of them):

White triangle

The simplest possible example, a single white triangle. This demo is about geometry upload, passing geometry data to vertices in VAOs. It also contains the simplest shaders, a pass-through vertex shader and a set-to-white fragment shader.



White triangle bundled

This is the same demo aimed at building to a Mac application bundle - which is not mandatory, just how you should to to make a "real Mac application". The other demos also work on the Mac, don't worry about that.

Rotation

Applies a static rotation to the triangle, uploaded to the vertex shader.



Rotation animation

Get moving! This example adds code for animation, by starting a timer (using glutRepeatingTimer) to redraw the picture repeatedly. If you want to do this in the old GLUT, you should use glutTimerFunc.



Colored triangle

This demo adds a little color, by vertex. Again, this information has to be uploaded to the GPU in VAOs.



Phong triangle

The same triangle with Phong shading applied (diffuse component only)



Textured quad

A quad (two triangles) with a texture read from a file.



Textured quad inline

Same quad, with a texture created directly from code.

This demo is bit of nostagia for me, personally, since I used to use this inline texture a lot in the past, to make my examples stand-alone. It still has some value, showing how you can get closer to the texture, so you don't get the impression that textures need to be abstracted.



Color cube

A cube with a solid color on each side. This is another classic, your typical first 3D exercise in the Immediate Mode days. It was also the simplest demo for Java 3D, which gave beginners the delusion that Java3D was actually usable and suitable for education, which it was not. (Believe me. I have tried OpenGL and Java3D side by side and the difference for a beginning CG course was astonishing.) Today, this example is relatively clumsy, since all vertices have to be specified three times each. I created it in order to have a good object for bump mapping examples.



Textured cube

A cube with a texture (similar to the color cube).



Mipmapped cube

A textured cube with mip-mapping. Distance varies, and you can turn mipmapping on and off to see the difference. For an even more powerful mip-mapping example, check out "mipmapcube4".



Rotating gouraud cube

A cube, without perspective, Gouraud shaded.



Rotating Phong cube

Phong shaded cube, with specular shading. This is probably one of the most interesting demos in the pack, as a demo of how to make a Phong shader in GLSL.



Perspective bunny

A low-res bunny model (not to be confused with the Stanford bunny) rendered with Gouraud shading and perspective.



Rotating perspective bunny

Same, but animated.

Textured teapot

The famous Utah Teapot, loaded from a file, with a texture. Since this is the last demo in the set, I chose to make this in a "plus" version, where I use my vector unit VectorUtils plus take more advantage of loadobj to handle the data upload as well, which results in significantly simplified code. Why didn't I do that all along? Because I want to show what happens behind the curtain. But here I also wanted to take a step closer to the typical code packaging that you will do in a real program.



plus the folder "Common":

This folder holds a set of utility files:

- GL_utilities: The main feature here is the shader loader (which will move into MicroGLut in the future once the Linux version is stable).
- loadobj: Loads Wavefront OBJ 3D models. Note: Limited to simple models. Some models are built from multiple separate shapes, and we don't support that.
- LoadTGA: Loads TGA images to textures. TGA is a simple image format with alpha support, suited for laboratory work.
- MicroGlut: This is a GLUT subset, Mac only so far. It is a really small cross-platform glue library. (A Linux version exists but is not sufficiently tested. Until we dare releasing the Linux version, FreeGLUT can be used.)
- VectorUtils3: A math library mainly dealing with vectors and matrices relevant for computer graphics.

The demos are in C, but all utility code is prepared to be usable from C++. VectorUtils3 includes some operator overloading definitions.

Future plans: I consider adding one or two demos that demonstrate the higher-level usage of loadobj, where you can load and use OBJ files with very little code.

Thanks to Jens Ogniewski for testing early versions under Linux.

Download (21 january version)


by Ingemar Ragnemalm 2012-2013