OpenGL (1.2 - 2.0) Code Samples - Page 4

Multi-Texture Blending
 

This sample demonstrates how to use the OpenGL extensions ARB_multitexture and ARB_texture_env_combine in conjunction with specially encoded vertex colors to blend three textures together.

This technique is very popular in terrain rendering engines which use it to blend dramatically different textures such as rock and grass together with out creating a noticeable edge. For example, with three textures consisting of stone, grass, and sand you can render a mountain that blends in patches of grass and sand at its base.

Relevant Keywords: GL_ARB_multitexture, glActiveTextureARB, glMultiTexCoord2fARB, glClientActiveTextureARB, glTexEnvi, glVertex3f, wglGetProcAddress, glGetString, auxDIBImageLoad, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glEnable, glDisable, glBindTexture, AUX_RGBImageRec, GL_INTERPOLATE_ARB, GL_SRC_COLOR, GL_SRC_ALPHA, GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE, GL_ADD, GL_TEXTURE0_ARB, GL_TEXTURE1_ARB, GL_TEXTURE2_ARB, GL_EXTENSIONS, GL_BLEND, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_UNSIGNED_BYTE, PFNGLACTIVETEXTUREARBPROC, PFNGLMULTITEXCOORD2FARBPROC, and PFNGLCLIENTACTIVETEXTUREARBPROC.

 

Color Key Transparency

 

This sample demonstrates how to use a color key to create a RGBA texture image that supports alpha transparency from a simpler RGB image like a bitmap (.bmp), which doesn't support alpha transparency.

Relevant Keywords: auxDIBImageLoad, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glEnable, glDisable, glBindTexture, AUX_RGBImageRec, GL_T2F_V3F, GL_RGBA, GL_RGB, GL_BLEND, GL_DEPTH_TEST, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_NEAREST, and GL_UNSIGNED_BYTE.

 

DDS Texture Loader
 

This sample demonstrates how to use ARB_texture_compression to load .dds compressed texture files. The benefits of using compressed textures is clearly shown by loading two identical textures (512 x 512) for comparison. One is a regular .bmp texture, and the other is a .dds file which uses DXT1 compression. With very little change in image quality, the compressed texture file not only reduces the file size down from 768.0 KB to 170.0 KB, but also includes compressed mip-map levels.

Relevant Keywords: glCompressedTexImage2DARB, PFNGLCOMPRESSEDTEXIMAGE2DARBPROC, auxDIBImageLoad, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glEnable, FOURCC_DXT1, FOURCC_DXT3, FOURCC_DXT5, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, AUX_RGBImageRec, GL_T2F_V3F, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_UNSIGNED_BYTE, and DDSURFACEDESC2.

 

Dot3 Per-Pixel Bump Mapping
 

This sample demonstrates how to perform Dot3 per-pixel bump mapping using a normal map and the GL_DOT3_RGB_EXT texture-blending operation. This technique is sometimes referred to as per-pixel lighting or per-pixel attenuation, but Dot3 per-pixel bump-mapping is what most programmers know it as. Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

This sample also demonstrates how to create tangent, binormal, and normal vectors, for each vertex of our test geometry (a simple cube). These vectors are used during rendering to define an inverse tangent matrix for each vertex. This has to be done because a normal-map stores its normals in tangent-space. Therefore, we need an inverse tangent matrix so we can transform our scene's light vector from object-space into tangent-space. Once transformed, we then encode this new light vector as a RGB color and pass it into the texture blending stage as the vertex's Diffuse color.

Relevant Keywords: GL_ARB_multitexture, glActiveTexture, glMultiTexCoord2f, glClientActiveTexture, glTexEnvi, glVertex3f, wglGetProcAddress, glGetString, auxDIBImageLoad, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glEnable, glDisable, glBindTexture, AUX_RGBImageRec, GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE, GL_TEXTURE0, GL_TEXTURE1, GL_EXTENSIONS, GL_BLEND, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_UNSIGNED_BYTE, PFNGLACTIVETEXTUREPROC, PFNGLMULTITEXCOORD2FPROC, PFNGLCLIENTACTIVETEXTUREPROC, and GL_DOT3_RGB_EXT.

 

Alpha Blending in the Frame buffer
 

Demonstrates how to perform alpha-blending in the frame-buffer. The sample renders a textured cube which is alpha-blended into the frame-buffer in such a way as to create a translucent effect. Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

Relevant Keywords: auxDIBImageLoad, glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glEnable, glDisable, glBindTexture, AUX_RGBImageRec, GL_T2F_V3F, GL_BLEND, GL_DEPTH_TEST, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR, and GL_UNSIGNED_BYTE.