This short example demonstrates how you can use a combination of OpenGL blend modes and multiple passes to achieve some quite sophisticated texturing effects. It also shows how you can use multi-pass rendering to get more from a very small texture. The example uses a single channel 16 x 16 texture, so the total texture requirement is just 256 bytes, or (256 + 64 + 16 + 4 + 1) = 341 bytes with all mip-maps. Its speed depends on the machine you're using: if you've got the fill-rate, it will run fast.
The code also demonstrates:
Look at source.
Download sources (if you want to compile it, you'll also need GLUT).
Download Windows 95/Windows NT executable, linked with opengl32.dll.
Here's the process visually, using the default settings of 3 noise octaves and full lighting.
|Pass 1: just draw the random texture at full scale, with brightness 0.5.|
|Pass 2: add the same texture at 2.0 scale, randomly rotated, with brightness 0.25.|
|Pass 3: add the same texture at 4.0 scale, randomly rotated, with brightness 0.125. The texturing stage is over.|
|Pass 4: Disable texturing, enable lighting, modulate the texture by the diffuse lighting|
|Pass 5: Enable dither, add specular highlights, and we're done!|
Drag with the left mouse button to spin the cube; pressing shift and dragging up or down with the mouse lets you zoom in or out. The right button gives you a menu for switching the two lights on and off, and for going full-screen.
Various keys do interesting things:
|set the number of noise 'octaves' - i.e. how many layers of texture to apply|
|drift - slides the texture slowly across the polygons|
|toggles trilinear filtering|
|toggles lighting (both diffuse and specular)|
|toggles the object between a cube and a torus|
|toggles specular highlights|
|toggles use of depth buffer|
If you're interested in procedural texturing, you should check out Texturing and Modeling - A Procedural Approach Ed. David S. Ebert, AP Professional, 1994. ISBN 0-12-228760-6.
In roughly ascending order of ambitiousness:
glBlendFunc(GL_DST_ALPHA,GL_ONE);By doing this you can use the texture's alpha channel to control how much light the object reflects specularly, giving the effect of a tarnished surface.