纹理映射相关函数解析

 
 glGenTextures(1, &texObj); 

   glBindTexture(GL_TEXTURE_2D, texObj); 

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 
                  0, GL_RGBA, GL_UNSIGNED_BYTE, texImage); 
texObj是用于存储从将作为纹理的图片的值以及后面对这个texture的paramener的设置。在设置texture的参数时,我们必须先将这个纹理作为current texture,而
glBindTexture就是用于实现这一功能的。

glEnable(GL_TEXTURE_2D); 

   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
   glBindTexture(GL_TEXTURE_2D, texObj); 

   glBegin(GL_QUADS); 
     glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0); 
     glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0); 
     glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); 
     glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0); 
     glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0); 
     glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0); 
     glTexCoord2f(1.0, 1.0); glVertex3f(2.4, 1.0, -1.4); 
     glTexCoord2f(1.0, 0.0); glVertex3f(2.4, -1.0, -1.4); 
   glEnd(); 

这里的glTexEnvf用于设置fragment原始的值与从texture mapping中获得的值的结合方式。因为fragment在rasterization的阶段用了顶点进行差值(顶点的值在vertex processing的阶段用了lighting computation进行了计算),然后texture mapping之后,这些fragment又获得了值,然后要考虑从lighting computation获得的值与从texture mapping获得的值如何结合。

Secondary Colors
 A fragment may have a primary color and a secondary
color
 If lighting is enabled
 Primary color contains ambient and diffuse contributions
Secondary color contains specular contribution
 Texture color are applied to primary color (according to texture function)
 Secondary color is added to the post-texturing fragment color

Useful for producing more realistic specular highlights, which could avoid effect(效果) of specular highlight are reduced as combine with texture value.

 

the mode to combine the result of lighting computation and texture mapping value are setting by the following function:

 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, f)

f is the the specific mode which could be GL_DECAL, GL_REPLACE, GL_MODULATE, GL_BLEND, GL_ADD or GL_COMBINE.

常用的是 GL_REPLACE 和 GL_MODULATE

GL_REPLACE 是用纹理值替代lighting computation的值,

GL_MODULATE 是将二者相乘。

 

posted @ 2013-08-30 10:56  qingsun_ny  阅读(533)  评论(0编辑  收藏  举报