OpenGL (1.2 - 2.0) Code Samples - Page 7

How to Directly Allocate and Use AGP/Video Memory
 

This sample demonstrates how to use OpenGL extensions to speed up the rendering of geometry by loading its vertex data and/or indices data directly into AGP or Video memory. Of course, the downside to this is that you become responsible for managing AGP and Video Memory which can seriously complicate your code, but sometimes... ya just gotta do whatcha ya gotta do!

Since there are several ways to do this, including vendor-specific extensions which will only run correctly on certain cards, I decided to offer this sample in three versions. The first version uses the ATI_vertex_array_object and ATI_element_array extensions and will only run on ATI cards. The second version uses NV_vertex_array_range and will only run on nVIDIA cards. And finally, the third version use the newer ARB_vertex_buffer_object extension and should technically run on any of the newer cards if the driver supports it.

Relevant Keywords: glNewObjectBufferATI, glIsObjectBufferATI, glUpdateObjectBufferATI, glGetObjectBufferfvATI, glGetObjectBufferivATI, glFreeObjectBufferATI, glArrayObjectATI, glGetArrayObjectfvATI, glGetArrayObjectivATI, glVariantArrayObjectATI, glGetVariantArrayObjectfvATI, glGetVariantArrayObjectivATI, glElementPointerATI, glDrawElementArrayATI, glDrawRangeElementArrayATI, wglAllocateMemoryNV, wglFreeMemoryNV, glFlushVertexArrayRangeNV, glVertexArrayRangeNV, glBindBufferARB, glDeleteBuffersARB, glGenBuffersARB, glIsBufferARB, glBufferDataARB, glBufferSubDataARB, glGetBufferSubDataARB, glMapBufferARB, glUnmapBufferARB, glGetBufferParameterivARB, glGetBufferPointervARB, GL_VERTEX_ARRAY_RANGE_NV, BUFFER_OFFSET, GL_ARRAY_BUFFER_ARB, GL_TEXTURE_COORD_ARRAY, GL_NORMAL_ARRAY, GL_VERTEX_ARRAY, GL_ELEMENT_ARRAY_BUFFER_ARB, and GL_ELEMENT_ARRAY_ATI.

  • Download (Version #1 - ATI_vertex_array_object - Last Updated: 02/01/05)
  • Download (Version #2 - NV_vertex_array_range - Last Updated: 02/01/05)
  • Download (Version #3 - ARB_vertex_buffer_object - Last Updated: 03/30/05)
  • Learn More (ATI_vertex_array_object)
  • Learn More (NV_vertex_array_range)
  • Learn More (NV_vertex_array_range)
  • Learn More (ARB_vertex_buffer_object)

 

Off-screen Rendering Using Pixel Buffers
 

The samples below demonstrate how to create dynamic textures through off-screen rendering. The off-screen rendering step is accomplished using a pbuffer, which is created through a combination of OpenGL's WGL_ARB_pbuffer, and WGL_ARB_pixel_format extensions. The second step of dynamic texture creation varies between the versions. Please see the implementation notes below for further information.

Version #1 - The dynamic texture is created by reading the pixel data out of the pbuffer with glReadPixels and then loading it into our texture with a regular call to glTexImage2D. This is by far the slowest way to create dynamic textures from a pbuffer.

Version #2 - In this version, we use wglShareLists to help out dynamic texture creation by sharing the window's rendering context with the pbuffer's context. With the two contexts sharing the same display list and texture space, we can simply load the texture with pixel data by calling glCopyTexSubImage2D. This is obviously faster than version #1, if you're willing to share contexts.

Version #3 - For our last version, we actually add a third extension, WGL_ARB_render_texture, and as the name implies, the extension basically lets us render directly to our dynamic texture. If the hardware supports it, this is the fastest method of creating dynamic textures from a pbuffer.

Relevant Keywords: WGL_ARB_pbuffer, WGL_ARB_pixel_format, WGL_ARB_render_texture, wglCreatePbufferARB, wglGetPbufferDCARB, wglReleasePbufferDCARB, wglDestroyPbufferARB, wglQueryPbufferARB, wglGetPixelFormatAttribivARB, wglGetPixelFormatAttribfvARB, wglChoosePixelFormatARB, wglBindTexImageARB, wglReleaseTexImageARB, wglSetPbufferAttribARB, wglGetExtensionsStringARB, wglGetProcAddress, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glReadPixels, glCopyTexSubImage2D, WGL_SUPPORT_OPENGL_ARB, WGL_DRAW_TO_PBUFFER_ARB, WGL_BIND_TO_TEXTURE_RGBA_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB, WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB, WGL_DEPTH_BITS_ARB, WGL_DOUBLE_BUFFER_ARB, WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB, WGL_TEXTURE_2D_ARB, WGL_PBUFFER_WIDTH_ARB, PFNWGLCREATEPBUFFERARBPROC, PFNWGLGETPBUFFERDCARBPROC, PFNWGLRELEASEPBUFFERDCARBPROC, PFNWGLDESTROYPBUFFERARBPROC, PFNWGLQUERYPBUFFERARBPROC, PFNWGLGETPIXELFORMATATTRIBIVARBPROC, PFNWGLGETPIXELFORMATATTRIBFVARBPROC, PFNWGLCHOOSEPIXELFORMATARBPROC, PFNWGLBINDTEXIMAGEARBPROC, PFNWGLRELEASETEXIMAGEARBPROC, and PFNWGLSETPBUFFERATTRIBARBPROC.

Note: The EXT_framebuffer_object extension has become an excellent replacement for the WGL_ARB_pbuffer and WGL_ARB_render_texture combo which is normally used to create dynamic textures. An example of this newer technique can be found here.

 

Occlusion Query
 

These samples demonstrate how to use OpenGL's new extension, ARB_occlusion_query, and nVIDIA's older extension, NV_occlusion_query to perform occlusion queries. Both samples are basically a simplified rewrite of the nVIDIA sample. Check out the DirectX9 source code page for the Direct3D equivalent of these samples.

Relevant Keywords: glGenOcclusionQueriesNV, glDeleteOcclusionQueriesNV, glGetOcclusionQueryuivNV, glBeginOcclusionQueryNV, glEndOcclusionQueryNV, glutBitmapCharacter, glRasterPos3f, GLUT_BITMAP_HELVETICA_18, PFNGLGENOCCLUSIONQUERIESNVPROC, PFNGLDELETEOCCLUSIONQUERIESNVPROC, PFNGLGETOCCLUSIONQUERYUIVNVPROC, PFNGLBEGINOCCLUSIONQUERYNVPROC, PFNGLENDOCCLUSIONQUERYNVPROC, GL_PIXEL_COUNT_NV, glGenQueriesARB, glDeleteQueriesARB, glIsQueryARB,glBeginQueryARB, glEndQueryARB, glGetQueryivARB,
glGetQueryObjectivARB, glGetQueryObjectuivARB, PFNGLGENQUERIESARBPROC, PFNGLDELETEQUERIESARBPROC ,PFNGLISQUERYARBPROC, PFNGLBEGINQUERYARBPROC, PFNGLENDQUERYARBPROC, PFNGLGETQUERYIVARBPROC, PFNGLGETQUERYOBJECTIVARBPROC, PFNGLGETQUERYOBJECTUIVARBPROC, and GL_SAMPLES_PASSED_ARB.

 

Emboss Bump Mapping
 

These samples demonstrate how to perform one-pass and two-pass emboss bump mapping via OpenGL's multi-texturing extension,  ARB_multitexture. I've also included code, which demonstrates how to calculate tangent and binormal vectors on a per-vertex basis. These special vectors define the texture space for each vertex, which allows for accurate texture coordinate shifting during the bump mapping process. Check out the DirectX9 source code page for the Direct3D equivalent of these samples.

 

Planar Shadows
 

This sample demonstrates how to create a planar shadow under OpenGL. Planar shadows are created by building a special matrix transform which flattens an object's geometry into a plane when rendered and is definitely one of the simplest ways of creating real-time shadows. Unfortunately, planar shadows are only useful when projected onto planar surface like a wall or floor so their use in modern applications or games is quickly falling out of favor.

Relevant Keywords: glTexEnvf, glBlendFunc, glutSolidSphere, glutSolidTeapot, glStencilFunc, glStencilOp, glColorMask, glDepthMask, GL_STENCIL_BUFFER_BIT, GL_STENCIL_TEST, GL_ALWAYS, GL_EQUAL, GL_REPLACE, GL_KEEP, GL_FALSE, GL_TRUE, GL_INCR, GL_DEPTH_TEST, GL_LIGHTING, GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE, GL_SRC_ALPHA, and GL_ONE_MINUS_SRC_ALPHA.