NeHe OpenGL Lesson23 – Sphere Mapping Quadrics In OpenGL

screen_shot5-300x225 This sample shows us how to do a Sphere Mapping when work with auto-generate texture coordinates in OpenGL.
For sphere mapping, you need a sphere map. What you need to do in PS is selecting distort and apply a spherize modifier from filter menu.

 

To set up the sphere mapping in OpenGL, you need to set up the auto-generate texture coordinates mode to sphere instead of providing the coordinates with glTexCoord function. Just as following code:

//Texture use for automatic coordinate generation
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
 
//Sphere mapping and enable s & t texture generation
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
 
//Draw the shapes to apply the texture
...
 
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);

 

Environment Mapping: GL_REFLECTION_MAP & GL_TEXTURE_CUBE_MAP. This feature was used a lots in the game, like a metal ball or a small pool of water to reflect the around scene. The following are the code to set up a cube map:

glBindTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_X, cubeMap[0]);
glBindTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, cubeMap[1]);
glBindTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, cubeMap[2]);
glBindTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, cubeMap[3]);
glBindTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, cubeMap[4]);
glBindTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, cubeMap[5]);
 
glEnable(GL_TEXTURE_CUBE_MAP);
 
//Environment mapping (see underneath)
...
 
glDisable(GL_TEXTURE_CUBE_MAP);

 

The following code used to set up the reflect map mode:

//GL_REFLECTION_MAP for s,t,r texture coordinates
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
 
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
 
//Draw shape to aply reflection on it
...
 
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);

An article about auto-generate texture coordinates mode in OpenGL could be found from here, http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial11.php.

 

The full source code could be downloaded from here.

posted @ 2012-08-23 21:26  opencoder  阅读(457)  评论(0编辑  收藏  举报