分类: LINUX
2012-01-29 12:48:23
Opengl里各个变量的属性太多,一个不留神,属性设置错误,得到的结果就会差别很大。
关于depth texture,这条属性的设置很重要:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
手册中的解释引用如下:
GL_TEXTURE_COMPARE_MODESpecifies the texture comparison mode for currently bound depth textures. That is, a texture whose internal format is GL_DEPTH_COMPONENT_*; see glTexImage2D) Permissible values are:
Specifies that the interpolated and clamped r texture coordinate should be compared to the value in the currently bound depth texture. See the discussion of GL_TEXTURE_COMPARE_FUNC for details of how the comparison is evaluated. The result of the comparison is assigned to luminance, intensity, or alpha (as specified by GL_DEPTH_TEXTURE_MODE).
GL_NONESpecifies that the luminance, intensity, or alpha (as specified by GL_DEPTH_TEXTURE_MODE) should be assigned the appropriate value from the currently bound depth texture.
GL_TEXTURE_COMPARE_FUNC
Specifies the comparison operator used when GL_TEXTURE_COMPARE_MODE is set to GL_COMPARE_R_TO_TEXTURE. Permissible values are:
Texture Comparison Function | Computed result |
---|---|
GL_LEQUAL |
result=1.00.0 r<=Dtr>Dt |
GL_GEQUAL |
result=1.00.0 r>=Dtr |
GL_LESS |
result=1.00.0 r |
GL_GREATER |
result=1.00.0 r>Dtr<=Dt |
GL_EQUAL |
result=1.00.0 r=Dtr≠Dt |
GL_NOTEQUAL |
result=1.00.0 r≠Dtr=Dt |
GL_ALWAYS |
result=1.0 |
GL_NEVER |
result=0.0 |
where r is the current interpolated texture coordinate, and Dt is the depth texture value sampled from the currently bound depth texture. result is assigned to the either the luminance, intensity, or alpha (as specified by GL_DEPTH_TEXTURE_MODE.)
解释:
1。如果用GL_COMPARE_R_TO_TEXTURE模式,则我们存到纹理中的就是result值,要么0,要么1。
2。如果用GL_NONE,才会保存下来按照设定的逻辑比较关系得到的结果深度值。
我一开始用的是GL_COMPARE_R_TO_TEXTURE,在shader里发现读取出来的值都是1.0,改为GL_NONE后就正常了。
Open issue:
我在程序里用的是FBO,并且render both a color image texture and depth texture to FBO,然后用
glReadBuffer(GL_DEPTH_ATTACHMENT_EXT);
glReadPixels(0, 0, s_width, s_height, GL_DEPTH_COMPONENT, GL_FLOAT, p);
读取深度值的时候,即使上边设置为GL_COMPARE_R_TO_TEXTURE也能正常读取出深度信息来。这又是为何呢?