Contours:
Contours are widely used for information visualization. Traditionally they are created by interpolation method, which needs lengthy code. This method is inefficient both in development and at run-time. Furthermore, it is difficulty to apply visual effects, such as lighting, with interpolation method. The texture mapping method, however, is much efficient in development, fast at run-time, and flexible in adding visual effects. An increasingly used new approach is texture-mapping method. This method is mapping a predefined pixel array to polygons. By using OpenGL, this approach is very efficient in development and much fast in run-time speed. In addition, it is so easy to add visual effects like lighting and alpha blending. In this article, I will describe the procedure of one-dimensional texture mapping method, and key points on using OpenGL’s commands. An example on applying lighting to contours is presented in this article, and I leave adding alpha blending as your exercise.
OpenGL:
OpenGL is an application software interface to graphics hardware. It provides capability to develop highly efficient interactive software.
Contours with OpenGL
//You define colors of contours in a texture image then map it onto polygons.
A one-dimensional texture is a line of pixels. You can image a 1D texture as a picture with width only and 1 pixel in height, or vise versa. The color of each pixel represents data in contours. Usually, to map to a geometry object to create contours, the color of the first pixel denotes the minimum value of data, and the last pixel the maximum value.
The simplest example is to map the texture to a line. You can image the texture as a rubber line, and stretch it to match the length of the line.
/*
A magnification filter is used when the object is larger than the texture. I will demonstrate following 2 modes define in OpenGL
GL_NEAREST
GL_LINEAR
Nearest-neighbor filter NEAREST takes the closet pixel in the texture image.
By defining linear interpolation filter LINEAR, you tell OpenGL to linearly interpolate color values in the texture image before drawing anything on the screen (buffer).
*/
I use OpenGL to develop the contours example code since it provides an excellent API for graphics development. To create an OpenGL framework, all you need to know is what need to be done and when to do them. Some need to be done just once; and others need to be done when required.
I use MFC’s Doc-View architecture for the framework here just because it is efficient for GUI development. An OpenGL Wrapper for Win32, Giovanni Bavestrelli, (CUJ, Dec, 98) provides a good tutorial for wrapping OpenGL within MFC. Remember that the method of creating contours with texture mapping is nothing to do with MFC, and you can implement it solely with OpenGL, or with other GUI API.
Following is an outline of basic framework of implementation OpenGL with MFC.
Get DC.
Set up pixel format.
Create RC from DC.
Make RC current Using DC.
Set material.
Set texture.
Set light.
Set alpha blending.
Set view port.
Set frustum.
Make RC non-current.
Delete RD
Release DC.
For simplicity, I do not handle pallet for 8-bit display mode. If you run the sample with 256-color setting, the imagine will not be displayed properly. However, if your graphics card support 16- or 24-bit “true color” display, take the advantage to use them. You will not only get better-looking scenes, but also faster performance.
Create color array.
You only need to call one OpenGL function to define 1C texture.
glTexImage1D()
The target argument specifies which texture should be defined; This argument must be GL_TEXTURE_1D. The level argument indicates the texture’s level of detail and is usually 0.
The component argument specifies the number of color values used for each pixel. For color index textures, components must be 1. Values of 3 and 4 are used for RGB and RGBA texture image, respectively.
Width and border specify the size of the texture image. The border value controls the number of border pixels OpenGL should expect (and use) and may have a value of 0, 1, or 2. The width parameter specifies the width of the main texture image (without the border pixel) and must be a power of 2.
· 8
· 0-1
Summery
Contours are widely used for information visualization. An efficient approach to plot 3-dimensional contours with stunning visual effect is increasingly demanded. This article presents an approach to create contours with one-dimensional texture mapping using OpenGL. A user-defined texture is used to represent the range and scale of the data. This pixel array is then mapped to polygons to create a contours plot (Fig 1). Lighting is then applied to the scene to create visual effect (Fig 2). By using alpha blending, a transparent contours plot is generated (Fig 3). I will describe the procedure, and key points in the article. The source code is provided.
Wenfei Wu is a software development professional with extensive experience in GUI and 3D graphics. He is an R&D Engineer at ADINA R&D, Inc. He holds a Master’s degree in engineering from McMaster University, Canada. He can be reached at wwu@adina.com