opengl-es - OpenGL ES3 帧缓冲区以红色比例绘制深度

标签 opengl-es glsl framebuffer opengl-es-3.0 shadow-mapping

因此,在努力使定向光阴影贴图起作用之后,我终于可以看到在四边形上渲染的阴影贴图,但它仅使用深度 GL_DEPTH_COMPONENT16 绘制并键入 GL_UNSIGNED_SHORT , 或 GL_DEPTH_COMPONENT32F 并输入 GL_FLOAT 但它是红色比例而不是灰色

Depth map

问题是我用了很多方法来计算深度来绘制阴影,但没有出现阴影。

glCullFace(GL_FRONT);
            glGenFramebuffers(1, &depthMapFBO);
            glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
            glGenTextures(1, &depthMap);
            glBindTexture(GL_TEXTURE_2D, depthMap);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);

            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
            glDrawBuffers(1, GL_NONE);
            glReadBuffer(GL_NONE);
            glBindFramebuffer(GL_FRAMEBUFFER, 0);
            glBindTexture(GL_TEXTURE_2D, 0);
            glCullFace(GL_BACK);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    LOGI("framebuffer incomplete");
}

片段是:

uniform mediump sampler2DShadow  shadowMap;
......
float bias = 0.005;
float visibility = 1.0;
for (int i=0;i<4;i++){
    int index = i;
    visibility -= 0.2*(1.0-texture( shadowMap, vec3(FragPosLightSpace.xy + poissonDisk[index]/700.0,  (FragPosLightSpace.z-bias)/FragPosLightSpace.w) ));           
}
result =light.intensity* (visibility * (diffuse + specular));

最佳答案

...but it is in red scale not gray scale

具有深度分量格式的纹理,例如 GL_DEPTH_COMPONENT16GL_DEPTH_COMPONENT32F 只有 1 个颜色 channel ,红色 颜色 channel 。
如果您从绑定(bind)了深度分量纹理的纹理采样器读取数据,则会自动设置绿色、蓝色和 alpha channel 。

Image Format Khronos 集团的规范说:

Image formats do not have to store each component. When the shader samples such a texture, it will still resolve to a 4-value RGBA vector. The components not stored by the image format are filled in automatically. Zeros are used if R, G, or B is missing, while a missing Alpha always resolves to 1.

Note: Texture swizzling can change what the missing values are.

因此,设置了红色,绿色和蓝色设置为 0,alpha channel 为 1。这导致不透明的红色表面。

如果您想从深度分量纹理中读取灰度颜色,则必须读取红色 颜色 channel ,并且还必须将红色 channel 应用于绿色和蓝色.

你必须像这样修改代码:

float depth          = texture( shadowMap, ..... ).r;
vec3  depthGrayscale = vec3( depth );

或者这个:

vec3  depthGrayscale = texture( shadowMap, ..... ).rrr;

关于opengl-es - OpenGL ES3 帧缓冲区以红色比例绘制深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46141786/

相关文章:

iPhone OpenGL ES 单 View 还是多 View ?

opengl-es - glVertexAttrib(相对于 glVertexAttribPointer)是用来做什么的?

ios - 如何将制服传递给着色器

linux - 将帧缓冲区映射到 X 窗口

image - Vulkan 中的帧缓冲区和图像有什么区别?

c++ - Opengl-es 2.0中渲染三角形的问题

ios - 如何使用GL_UNSIGNED_BYTE作为纹理坐标?

opengl - GLSL 距离场透明度

c++ - 为什么在与GLSL中的矩阵相乘后z坐标会翻转 - OpenGL

c - fix_screeninfo的line_length变量是怎么统计的?