1

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

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

这是问题的截图 在此处输入图像描述

在此处编辑代码:

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(); 
}
4

1 回答 1

0

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

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

和:

GL_DEPTH_TEST

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

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

于 2013-04-15T13:08:10.510 回答