OpenGL (1.2 - 2.0) Code Samples - Page 8

Shadow Mapping
 

This sample demonstrates how to perform shadow mapping under OpenGL. The shadow mapping technique basically uses a depth texture or "shadow map" to figure out whether or not a pixel’s Z value places it within a shadowed region or not.

 This involves two render passes. The first pass uses a p-buffer to create a depth texture from the light’s point of view. On the second pass, we render our 3D content and perform a depth comparison between each pixel generated and the depth texture. If the pixel has a greater Z value than the depth texture, the pixel is further away from the light’s position and we know it lies within a shadowed region. On the other had, if the pixel’s Z value is less than the depth texture, we know that the current pixel is closer to the light than the object or shadow caster used to create the depth texture and couldn’t possible be in a shadowed region.

Note: This sample uses some nVIDIA specific features and may not run properly on ATI cards.

Relevant Keywords: wglGetProcAddress, wglGetExtensionsStringARB, wglCreatePbufferARB, wglGetPbufferDCARB, wglReleasePbufferDCARB, wglDestroyPbufferARB, wglChoosePixelFormatARB, wglBindTexImageARB, wglReleaseTexImageARB, glPolygonOffset, WGL_SUPPORT_OPENGL_ARB, WGL_DRAW_TO_PBUFFER_ARB, WGL_BIND_TO_TEXTURE_RGBA_ARB, WGL_DOUBLE_BUFFER_ARB, WGL_DEPTH_TEXTURE_FORMAT_NV, WGL_TEXTURE_DEPTH_COMPONENT_NV, WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB, WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, GL_TEXTURE_COMPARE_OPERATOR_SGIX, GL_TEXTURE_LEQUAL_R_SGIX, GL_TEXTURE_COMPARE_SGIX, GL_CLAMP_TO_EDGE, GL_POLYGON_OFFSET_FILL, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR, GL_EYE_PLANE, GL_TEXTURE_GEN_S, GL_TEXTURE_GEN_T, GL_TEXTURE_GEN_R, GL_TEXTURE_GEN_Q, GL_S, GL_R, GL_T, and GL_Q.

 

Stencil Shadow Volume
 

This sample demonstrates how to create real-time shadows under OpenGL using shadow volumes. Please note that this sample is the simplest shadow volume sample that I could create. It demonstrates only the basics which are required to create a shadow volume and doesn't attempt to fix any special cases problems such as what to do when the viewing position is inside the shadow volume or how to produce shadow volumes for more complicated objects which require edge detection. This is an educational sample for beginners who know nothing about shadow volumes.

Relevant Keywords: glDepthMask, glStencilFunc, glStencilOp, glColorMask, glPolygonMode, glCullFace, GL_CULL_FACE, GL_STENCIL_TEST, GL_FALSE, GL_TRUE, GL_ALWAYS, GL_KEEP, GL_BACK, GL_FRONT, GL_EQUAL, GL_LESS, GL_LINE, GL_FILL, and GL_STENCIL_BUFFER_BIT.

 

Point-Rotated Billboards
 

This little sample is similar to the Axis-Aligned sample, but our billboard is now oriented perpendicular to the viewer's direction as defined by the model-view matrix. This technique is widely used in particle systems to keep all the particles facing the viewer.

 

Axis-Aligned Billboards
 

Demonstrates how to rotate and orient a textured quad along a particular axis for the purposes of billboarding. This technique is widely used to render trees in games.

 

Textured Sphere
 

Demonstrates a simple routine for rendering a sphere complete with the correct texture coordinates required for proper texturing mapping.