opengl纹理相关知识

来源:互联网 发布:邪恶吧下载软件 编辑:程序博客网 时间:2024/04/30 13:25
Texture is stored by OpenGL using an integer for identification. We can send an integer variable to our Shader program by using Uniform.

1 纹理矩阵
 纹理矩阵堆栈基本和ModelView,Projection矩阵堆栈一样,OpenGL的矩阵堆栈操作都能使用。只不过要操作这个堆栈的时候要提供的参数是GL_TEXTURE。在纹理坐标生成的时候,会用到纹理矩阵操作。
2 纹理坐标的生成
Often, when you are loading models, texture coordinates are provided for you. If necessary, you can easily map texture coordinates manually to some surfaces such asspheres or flat planes. Sometimes, however, you may have a complex surface for which it is not so easy to manually derive the coordinates. OpenGL can automatically generate texture coordinates for you within certain limitations.
glTexGen是OpenGL提供的一个自动生成纹理坐标的函数,以某个平面为参照面,以物体顶点和平面方程系数的线性组合计算出纹理坐标。参照面可以是相对于眼睛的,也可以是相对于模型本身的(区别就在于模型动的时候纹理坐标跟不跟着变)。 glTexGen也可以生成以球为参照的纹理坐标。

Tips: Opengl默认纹理坐标(gl_TexCoord.st/gl_TexCoord.q)的范围是[-1,-1],若想变换到[0,-1],可通过设置纹理矩阵进行缩放和平移。
http://msdn.microsoft.com/en-us/library/windows/desktop/dd368633%28v=vs.85%29.aspx


(1)Texture coordinate generation is enabled on the S, T, R, and Q texture coordinates usingglEnable:

glEnable(GL_TEXTURE_GEN_S); 
When texture coordinate generation is enabled, any calls to glTexCoord are ignored, and OpenGL calculates the texture coordinates for each vertex for you.
(2)set the function or method used to generate texture coordinates with the following functions:
void glTexGenf(GLenum coord, GLenum pname, GLfloat param);
void glTexGenfv(GLenum coord, GLenum pname, GLfloat *param);
(3)turn off texture coordinate generation(We will specify texture coordinates)     
glDisable(GL_TEXTURE_GEN_S);
Note that the texture coordinate generation function can be based on a different plane equation for each coordinate.
3 纹理坐标生成的三种方式

(1)Object Linear
// Projection plane     GLfloat zPlane[] = { 0.0f, 0.0f, 1.0f, 0.0f }; glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);             glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);             glTexGenfv(GL_S, GL_OBJECT_PLANE, zPlane);             glTexGenfv(GL_T, GL_OBJECT_PLANE, zPlane);
Texture coordinates are generated using the following function:
coord = P1*X + P2*Y + P3*Z + P4*W
The X, Y, Z, and W values are the vertex coordinates from the object being textured, and the P1–P4 values are the coefficients for a plane equation.


(2)Eye Linear
// Projection plane     GLfloat zPlane[] = { 0.0f, 0.0f, 1.0f, 0.0f };glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);             glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);             glTexGenfv(GL_S, GL_EYE_PLANE, zPlane);             glTexGenfv(GL_T, GL_EYE_PLANE, zPlane); 
When the texture generation mode is set to GL_EYE_LINEAR, texture coordinates are generated in a similar manner toGL_OBJECT_LINEAR. The coordinate generation looks the same, except that now the X, Y, Z, and W coordinates indicate the location of the point of view (where the camera or eye is located)XYZW是在eye space坐标系下的位置. The plane equation coefficients are also inverted before being applied to the equation to account for the fact that now everything is in eye coordinates.
参考:http://www.360doc.com/content/11/0108/19/5367933_85050219.shtml
(3)Sphere Mapping
// Projection plane     GLfloat zPlane[] = { 0.0f, 0.0f, 1.0f, 0.0f };glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);             glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); 

参考网址:http://www.informit.com/articles/article.aspx?p=770639&seqNum=4

4 矩阵堆栈
使用矩阵堆栈比使用单个矩阵更高效,拿模型视图变换来说,在绘制复杂的场景时,会涉及到复杂的模型矩阵变换,常常我们需要保存这些变换的中间状态,以便在进行一些变换后快速恢复,而无需对当前模型矩阵进行反变换以进行恢复(费时)。举例解释:
比如,当前模型视图矩阵状态为矩阵A,场景绘制需要模型视图矩阵从A->B,然后从A->C......。利用矩阵堆栈,程序只需要保存矩阵 A(glPushMatrix),执行完A->B后,恢复模式视图矩阵为A(glPopMatrix),执行变换A->C。

5 functions
(1) glTexImage2D
为纹理设置图像数据

(2) glCopyTexSubImage2D从帧缓存中拷贝图像数据到纹理
Copy date from framebuffer to texture. If the texture is depth texture, the corresponding framebuffer is depth buffer. For example:

//Read the depth buffer into the shadow map texture

glBindTexture(GL_TEXTURE_2D, shadowMapTexture);glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);


原创粉丝点击