OpenGL ES Depthbuffer无法正常工作

时间:2015-07-29 07:34:10

标签: ios opengl-es shadow depth

我希望有人可以帮助我,现在长期坚持这个问题。我正在使用OpenGl-ES开发3D游戏。我有一个地图/世界和一些对象,我想用阴影贴图制作阴影。 我的问题是,在渲染时,阴影贴图信息不适合我的光照空间信息。

我不确定我的FBO是否有问题,我在这里给出的是:

-   (void)createShadowMap:(GLboolean)c Depth:(GLboolean)d Stencil:(GLboolean)s{
//Get ID
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);

//Texture
glGenTextures(1, &_colorBuffer);
glBindTexture(GL_TEXTURE_2D, _colorBuffer);
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA,_screenWidth, _screenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//SetupFBO
glGenFramebuffers(1, &_frameBuffer);
glGenRenderbuffers(1, &_depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, _screenWidth, _screenHeight);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _colorBuffer, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
    exit(1);
}

glBindFramebuffer ( GL_FRAMEBUFFER, defaultFBO );
glBindTexture ( GL_TEXTURE_2D, 0 );
}

但我认为问题在于计算,我只是找不到它。所以我在这里有深度贴图的着色器:

void main()
{
    vTexCoord = projectionMatrix *  viewMatrix *  modelMatrix * position;

    gl_Position = projectionMatrix *  viewMatrix * modelMatrix * position;
}

片段:

void main()
{
    highp float depth = gl_FragCoord.z;
    highp float linearDepth = (2.0 * 0.1) / (60.0 + 1.0 - depth * (60.0 - 1.0));

    lowp vec4 color = vec4(vec3(linearDepth), 1.0);

    gl_FragColor = color;
}

现在渲染时的着色器: VERT:

vec4 lightSight = projectionMatrix * lightSpaceMatrix *  modelMatrix * position;
projCoords = lightSight;

分片:

highp vec3 depth = projCoords.xyz/projCoords.w;
depth = (depth +1.0)*0.5;

highp float currentDepth = (projCoords.xyz/projCoords.w).z;    
highp float closestDepth = texture2D(shadowMap, depth).r;

lowp float shadow;

if (closestDepth > currentDepth) {
    shadow = 0.0;
} else {
    shadow = 0.5;
}

lowp vec4 color = vec4(vec3(shadow), 1.0);

0 个答案:

没有答案