知道什么是凹凸贴图是怎么回事,也知道浮雕和点乘凹凸贴图的理论计算,也做过个简单的相关shader。可一直来不知道怎么通过MultiPass及MultiTex来实现。(其实一直不知道,稍微高级点的,NEHE中22课也有相关的,可惜看着很累,讲解不透彻,一些语句没法读通),今天有幸找到一个例子,而且那位作者很认真,不仅给出完整的实例,也给了相应的理论分析。于是也摘录点,与大家一起分享。
那位作者的链接:http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
学习之余,我也贴上一点。
//bump texture
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texID[1]);//texID[1] for bump texture
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
//restore for tangent light vector (see paul's project)
const float rate=1.0;
GLfloat lightPos[4]={0.36*rate, 0.48*rate, 0.8*rate, 0.0};
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID[1]);//为什么一定要绑定纹理?
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, lightPos);
//base texture
glActiveTextureARB(GL_TEXTURE2_ARB);
glBindTexture(GL_TEXTURE_2D, texID[0]);//texID[0] for base texture
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
//draw
glBegin(GL_POLYGON);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB,0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB,1.0, 0.0);
glVertex3f(1.0,-1,0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB,1.0, 1.0);
glVertex3f(1.0,1.0,0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB,0.0, 1.0);
glVertex3f(-1.0,1.0,0.0);
glEnd();
glActiveTextureARB(GL_TEXTURE2_ARB);
glDisable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);
本来也学他作两个pass的,搞了半天也没弄明白为什么在vc6.0下,他第一个pass始终不和第二个pass混合。
混合方式:glBlendFunc(GL_DST_COLOR, GL_ZERO); glEnable(GL_BLEND);
同时显示两个pass时,他只绘制了第一个。而在vc2005中,感觉和他的代码基本一样。
编写了之后,还是算有点小小的明白了吧。希望对你也有帮助^_^