阴影贴图中的尾随阴影

时间:2012-11-21 07:45:13

标签: opengl glsl

我创建了阴影贴图。但它有两个问题: 只有当我改变模型矩阵时,阴影才会出现。即最初没有阴影,但是当我按下一个键移动图形时,即模型矩阵发生变化,阴影就会出现。 2.帧缓冲区纹理上有一条旧渲染的痕迹导致长路径。

任何人都可以对此有所了解吗?

这是问题的截图 enter image description here

在此编辑代码:

void generateShadowTex()
{
        //Calculate final ligting properties
    glm::vec4 a_f=light_ambient*mat_ambient;
    glm::vec4 d_f=light_diffuse*mat_diffuse;
    glm::vec4 s_f=light_specular*mat_specular;
    int counter=0;
    glClear(GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_DEPTH_TEST);    // need depth test to correctly draw 3D objects 
    glClearColor(0,0,0,1); 
    //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    if(wframe)
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
    else
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 

    glActiveTexture(GL_TEXTURE3);
    glBindTexture(GL_TEXTURE_2D,depthTex);

        // MAths here for mvp manupulation 
        //Draw Elements
        if(a<17 || a==18)
            glDrawElements(GL_QUADS, masterNumberIndices[a], GL_UNSIGNED_INT, (char*) NULL+0);
        else
            glDrawElements(GL_TRIANGLES, masterNumberIndices[a], GL_UNSIGNED_INT, (char*) NULL+0);

        glBindVertexArrayAPPLE(0);

    }
    glUseProgram(0);  

}

void Init_FBO() 
{ 
    GLfloat border[] = {1.0f, 0.0f, 0.0f, 0.0f};  

    //glActiveTexture(GL_TEXTURE3);
    glGenTextures(1, &depthTex);  
    glBindTexture(GL_TEXTURE_2D, depthTex);  
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,900,900, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);  
    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
    glBindTexture(GL_TEXTURE_2D,0); 

    glGenFramebuffers(1, &shadowFBO);  
    glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);  
    glDrawBuffer(GL_NONE);  
    glReadBuffer(GL_NONE);  
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);  
    glBindFramebuffer(GL_FRAMEBUFFER, 0); // go back to the default framebuffer  
    // check FBO status 
    GLenum FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); 
    if(FBOstatus != GL_FRAMEBUFFER_COMPLETE) 
    {
        printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");
    }
    else
    {
        printf("Frame Buffer Done Succesfully\n");
    } 
}


void display() 
{
    glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO); 
    generateShadowTex(); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    generateScene(); 
}

1 个答案:

答案 0 :(得分:0)

您需要在清除之前启用深度缓冲区。从OpenGL规范:

  

如果不存在缓冲区,则指向该缓冲区的glClear无效。 1

  

<强> GL_DEPTH_TEST

     

如果启用,请进行深度比较并更新深度缓冲区。请注意,即使存在深度缓冲区且深度掩码不为零,如果禁用深度测试,也不会更新深度缓冲区。 2

关于问题的第一部分,我只能猜出问题可能是什么,但你可能没有正确初始化模型矩阵。