NeHe OpenGL Lesson06 - Texture Mapping

screen_shot4-300x238 This program shows us how to apply texture mapping with OpenGL. Well, texture mapping increase the visual effect significantly. And also until now, the texture mapping was vastly used. Those textures are not limited to diffuse texture, but for other kind of usage: diffuse texture, specular map, light map, normal map, emissive map,parallex map, cube map, reflection map, blend mask map, light mask map, shadow map and so on. I even found that some body used some HDR textures to store some polygons’ position information in one of the GPU ray tracer.  It seems any thing that time expensive could be pre-computed in the texture, and also use some texture to emulate some very complicated visual effect.


The following are the code that used to create texture object with OpenGL API on Ubuntu:

const char* filename = “./data/NeHe.bmp”;
QImage buf;
if ( !buf.load(filename) )
{
    qWarning(“Could not read image file, using single-color instead.”);
}

QImage texture = QGLWidget::convertToGLFormat( buf );

glGenTextures(1, &mDiffuse);
glBindTexture(GL_TEXTURE_2D, mDiffuse);
glTexImage2D(GL_TEXTURE_2D, 0, 3,
        texture.width(), texture.height(), 0, GL_RGBA,
        GL_UNSIGNED_BYTE, texture.bits() );

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Here an OpenGL texture created with the raw bit map data, and the min and mag filter mode were set. Here there are several things to think about: 1) The bit map texture is not kind of compressed texture, that means some certain disk waste with the file size. May be we could compress it a DXT1(1 bit or no alpha) or DXT5(with full range alpha channel). 2) Actually, the compressed format that we need is the format that the display card will need. That means every time you create a texture object, the display card will accept those texture data without further process, compress those data that display card & GPU could support well. 3) There is no mip maps created here. No mip map will cause some texture waving effect as the viewer get close to the object from a far away distance. When you create a texture object with mip map, you need to set mip map filter mode properly. 4) About the texture filtering, there is one very advance filter mode named anisotropic. This filter mode will consider the angle between the polygon and the view plane, and then will give more realistic effect without make the texture mapping blurry(The linear filter always cause this issue).  Notice the max anisotropic parameter;  5) When you start use the mip map, that means you start use the LOD textures. You could do some tuning work on the LOD basis, and other parameters to get more visual comparison effect to find the correct result.

The whole program coded for the Ubuntu 10.4. You need to install gcc, cmake, opengl to compile and run it.

 

The full source code could be found here.

posted @ 2012-08-23 22:51  opencoder  阅读(352)  评论(0编辑  收藏  举报