我在我的应用程序中使用延迟渲染,我试图创建包含深度和模板的纹理。
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0,
???, GL_FLOAT, 0);
对于这种特殊的纹理,opengl需要什么样的格式枚举。我试了几次,结果都犯了错误
此外,什么是正确的glsl语法访问深度和模板部分的纹理。我知道深度纹理通常是均匀的sampler2Dshadow。但我需要吗
float depth = texture(depthstenciltex,uv).r;// <- first bit ? all 32 bit ? 24 bit ?
float stencil = texture(depthstenciltex,uv).a;
发布于 2014-12-17 22:38:59
对于这种特殊的纹理,opengl需要什么样的格式枚举。
您遇到的问题是,Depth+Stencil是一个完全古怪的数据组合。前24位(深度)为定点,其余8位(模板)为无符号整数.这需要一种特殊的打包数据类型:GL_UNSIGNED_INT_24_8
此外,什么是正确的glsl语法访问深度和模板部分的纹理。我知道深度纹理通常是均匀的sampler2Dshadow。
实际上,永远不能用相同的取样器制服对这两件东西进行采样,原因如下:
OpenGL阴影语言4.50规范机 对于深度/模板纹理,取样器类型应该与通过OpenGL API设置的访问组件相匹配。当深度/模板纹理模式设置为
GL_DEPTH_COMPONENT
时,应使用浮点取样器类型。当深度/模板纹理模式设置为GL_STENCIL_INDEX
时,应使用无符号整数取样器类型。使用不支持的组合进行纹理查找将返回未定义的值。
这意味着,如果要在着色器中同时使用深度和模板,则必须使用纹理视图(OpenGL 4.2+)并将这些纹理绑定到两个不同的纹理图像单元(每个视图对于GL_DEPTH_STENCIL_TEXTURE_MODE
具有不同的状态)。这两个因素结合在一起意味着您至少需要一个OpenGL 4.4实现。
采样深度和模板的碎片着色器:
#version 440
// Sampling the stencil index of a depth+stencil texture became core in OpenGL 4.4
layout (binding=0) uniform sampler2D depth_tex;
layout (binding=1) uniform usampler2D stencil_tex;
in vec2 uv;
void main (void) {
float depth = texture (depth_tex, uv);
uint stencil = texture (stencil_tex, uv);
}
创建模具视图纹理:
// Alternate view of the image data in `depth_stencil_texture`
GLuint stencil_view;
glGenTextures (&stencil_view, 1);
glTextureView (stencil_view, GL_TEXTURE_2D, depth_stencil_tex,
GL_DEPTH24_STENCIL8, 0, 1, 0, 1);
// ^^^ This requires `depth_stencil_tex` be allocated using `glTexStorage2D (...)`
// to satisfy `GL_TEXTURE_IMMUTABLE_FORMAT` == `GL_TRUE`
此着色器的OpenGL状态设置:
// Texture Image Unit 0 will treat it as a depth texture
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, depth_stencil_tex);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
// Texture Image Unit 1 will treat the stencil view of depth_stencil_tex accordingly
glActiveTexture (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, stencil_view);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
发布于 2014-12-17 22:36:32
nvm发现了它
glTexImage2D(gl.TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w,h,0,GL_DEPTH_STENCIL,
GL_UNSIGNED_INT_24_8, 0);
uint24_8是我的问题。
glsl (330)中的用法:
sampler2D depthstenciltex;
...
float depth = texture(depthstenciltex,uv).r;//access the 24 first bit,
//transformed between [0-1]
https://stackoverflow.com/questions/27535727
复制相似问题