1

我正在尝试做基于 OpenGL GLSL 的阴影映射。问题是,在我完成了阴影贴图的渲染之后,我正在将贴图渲染到屏幕上以测试渲染是否正确,即我只是将新生成的纹理用作我要映射到屏幕上的纹理. 预期的结果是我会看到新的纹理。但相反,我看到的是一个白色区域,其纹理已绘制但非常微弱。也就是说,如果我将屏幕倾斜到某个角度,我才能看到阴影贴图的微弱轮廓。

谁能告诉我我做错了什么?

这是我的代码的相关部分:

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

        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 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;
        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); 
        glClear(GL_DEPTH_BUFFER_BIT); 
        glCullFace(GL_FRONT);
        if(wframe)
            glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
        else
            glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 


        glUseProgram(programObject);
        //Draw the stuff using Light Position as camera 
        //glutSwapBuffers();
        glActiveTexture(GL_TEXTURE3);
        glBindTexture(GL_TEXTURE_2D,depthTex);
        glUseProgram(0);  

    }

void generateScene()
{
        //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;
    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); 

    //Draw the stuff using camera as camera position

    }
    glutSwapBuffers();
    glUseProgram(0);  

}



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

1 回答 1

1

您的深度纹理看起来正常,深度覆盖范围从近到远的裁剪平面,您可以使用设置一些合理的裁剪平面glFrustumf(...)

于 2012-11-20T22:09:03.477 回答