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 @   opencoder  阅读(457)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示