OpenGL阴影贴图对象

时间:2017-10-24 15:53:00

标签: android c++ opengl-es-3.0 shadow-mapping

我在Android Native OpenGL C ++应用程序中使用定向光实现阴影贴图,阴影渲染正确但是我的代码中有一些错误会产生结果,如附图所示:

enter image description here

如图所示,模型上有奇怪的三角形阴影,实现如下:

首先创建深度缓冲区和深度图,如下所示:

if (sun->castShadow) {
    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_COMPONENT, 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);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_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");
    }
}

glTexImage2D的内部格式不适用于GL_Float所以我使用了GL_UNSIGNED_SHORT。

然后我从灯光视图渲染场景,首先光线矢量是(50,100,50)并且我有关于直接光线方向的冲突,它是朝向光线或光线的方向,但是该值会产生阴影和使用不同的光成分渲染场景。 代码是:

    if (sun->castShadow) {
        double delta = GetCurrentTime() - firstFrame;
        glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
        glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
        glClear(GL_DEPTH_BUFFER_BIT);
        glm::vec3 pos = player->getPosition();
        glm::vec3 tar = pos + (-sun->direction);
        glm::mat4 lightSpaceProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -20.0f, 500.0f);
        glm::mat4 lightSpaceView = glm::lookAt(pos, tar, glm::vec3(0, 1, 0));
        lightSpaceMatrix = lightSpaceProjection * lightSpaceView;
        shaders.setDepthLightSpaceUniform(lightSpaceMatrix);
        terrain->renderDepth(delta);
        for (item_it it = engineItems.begin(); it != engineItems.end(); ++it) {
            if (it->get()->castShadow)
                it->get()->renderDepth(delta);
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

最终正常渲染场景:

shaders.setLightsUniforms(directlights, pointlights, spotlights);
shaders.setViewUniforms(camera);
double first = GetCurrentTime() - firstFrame;
terrain->render(first);
for (item_it it = engineItems.begin(); it != engineItems.end(); ++it) {
    it->get()->render(first);
}

直接光计算的片段着色器部分是:

    vec3 calcDirectLight(DirectLight light,vec3 norm,vec3 tLightPos,vec3 diffColor,vec3 specColor){ 
    vec3 lightDir ;
    if(Has_normal_map==1)
        lightDir= normalize(tLightPos-tFragPos);
    else
        lightDir = normalize(light.direction - tFragPos);

    float diff = max(dot(lightDir,norm), 0.0);
    vec3 diffuse = light.color * diff * diffColor;

    vec3 viewDir = normalize(tViewPos - tFragPos);      
    vec3 halfwayDir = normalize(lightDir + viewDir);
    float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);
    vec3 specular = shininess * spec *specColor* light.color;
    vec3 result;
    if(light.castShadow==1){
        vec3 projCoords = FragPosLightSpace.xyz / FragPosLightSpace.w;
        projCoords = projCoords * 0.5 + 0.5;
        float shadow = 1.0; 
        for (int i=0;i<4;i++){
            shadow -= 0.2*(1.0-texture( shadowMap, projCoords.xy + poissonDisk[i]/700.0).r);
        } 
        if(projCoords.z > 1.0)
            shadow = 0.0; 
        result =light.intensity* shadow * (diffuse + specular);
    }
    else
        result =light.intensity* (diffuse + specular);
    return result;
}

还有其他一些错误,但这是主要错误

1 个答案:

答案 0 :(得分:3)

我相信问题在这里:

if(Has_normal_map==1)
    lightDir= normalize(tLightPos-tFragPos);
else
    lightDir = normalize(light.direction - tFragPos);

我看到你正在使用定向灯。 要计算定向光的方向,只需:

lightDir = light.direction

另外,我不明白你为什么使用不同的公式来计算光照方向,具体取决于Has_normal_map是否bash: bundle: command not found -----> Loading rbenv 。我相信光的方向并不取决于法线贴图。我希望能解决你的错误。