[Python] OpenGL FrameBuffer Depth Z-Buffer problem , black or white screen only

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    with python and OpenGL I'm trying to implement a Depth of Field through depth in post processing via a FrameBuffer. The blur somehow seems to work, but the main problem is that when I try to render only the depth (even linearized) it does not draw the shades on the objects, that is, it only draws black screen. FrameBuffer:

    class FrameBuffer:
    def __init__(self):
    self.texture=0
    self.dtexture=0
    self.FBO=0
    #genframebuffer
    self.FBO=glGenFramebuffers(1)
    glBindFramebuffer(GL_FRAMEBUFFER,self.FBO)
    # if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE):
    # print("COMPLETED")
    #textures
    self.texture=glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D,self.texture)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, None)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0)
    #depth texture
    self.dtexture=glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, self.dtexture)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None)
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, self.dtexture, 0)

    if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
    raise RuntimeError("Framebuffer incompleto!")
    else:
    print("Buffer Completo")
    #render buffer
    # self.depthStencilBuffer=glGenRenderbuffers(1)
    # glBindRenderbuffer(GL_RENDERBUFFER,self.depthStencilBuffer)
    # glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH24_STENCIL8,SCREEN_WIDTH,SCREEN_HEIGHT)
    # glBindRenderbuffer(GL_RENDERBUFFER,0)
    # glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,self.depthStencilBuffer)

    glBindFramebuffer(GL_FRAMEBUFFER,0)

    def UseTexture(self,id):
    glBindTexture(GL_TEXTURE_2D,id)

    def destroy(self):
    glDeleteTextures(self.texture)
    glDeleteFramebuffers(self.FBO)
    glDeleteRenderbuffers(self.depthStencilBuffer)


    Rendering loop:

    def render(self,scene,engine):
    glDepthMask(GL_TRUE)
    glBindFramebuffer(GL_FRAMEBUFFER,engine.fbo.FBO)
    glViewport(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)
    glEnable(GL_DEPTH_TEST)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    self.switch_render(engine.shader)
    self.draw(scene, engine) # Draw scene to depth

    #Post framebuffer and shader
    self.switch_render(engine.postshader)
    glBindFramebuffer(GL_FRAMEBUFFER, 0)
    #glBlitFramebuffer(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_DEPTH_BUFFER_BIT, GL_NEAREST)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glActiveTexture(GL_TEXTURE0)
    glBindTexture(GL_TEXTURE_2D, engine.fbo.texture)
    glUniform1i(glGetUniformLocation(engine.postshader, "screenTexture"), 0)

    glActiveTexture(GL_TEXTURE1)
    glBindTexture(GL_TEXTURE_2D, engine.fbo.dtexture)
    glUniform1i(glGetUniformLocation(engine.postshader, "depthTexture"), 1)

    # glDisable(GL_DEPTH_TEST)
    # glDepthMask(GL_FALSE)
    #glEnable(GL_DEPTH_TEST)
    glBindVertexArray(scene.screen.vao)
    glDrawArrays(GL_TRIANGLES,0,scene.screen.vertex_count)
    #self.draw(scene,engine) NON DA FARE ATTENZIONE!!!

    def destroy(self):
    glDeleteProgram(self.shader)


    Vertex Shader:

    #version 330 core
    layout (location = 0) in vec2 vertexPos;
    layout (location = 1) in vec2 vertexTexCoord;

    out vec2 fragmentTexCoord;

    void main()
    {
    fragmentTexCoord = vertexTexCoord;
    gl_Position = vec4(vertexPos.xy, 0.0, 1.0);
    }


    Fragment Shader (Something like this):

    #version 330 core
    out vec4 FragColor;
    in vec2 fragmentTexCoord; //TexCoords

    uniform sampler2D screenTexture;
    uniform sampler2D depthTexture;

    //const float offset = 1.0 / 300.0;
    // DEPTH
    float near = 0.1;
    float far = 100.0;
    float focusDistance=200;
    int sb=0; //1 : screen 0: blurred
    float LinearizeDepth(float depth)
    {
    float z = depth * 2.0 - 1.0; // back to NDC
    return (2.0 * near * far) / (far + near - z * (far - near));
    }
    //

    void main()
    {
    float depth=texture(depthTexture,fragmentTexCoord).r;
    float linearDepth = LinearizeDepth(texture(depthTexture,fragmentTexCoord).r);
    FragColor = vec4(vec3(depth), 1.0);
    //Same thing with linearDepth
    FragColor = vec4(vec3(linearDepth ), 1.0);
    }

    Continue reading...

Compartilhe esta Página