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.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/23/2653283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值