TheNerd's picture

can't write to Depth Buffer FBO??

Hello OpenGL geniuses!

I'm using some borrowed code here, and I am not sure why, but I do not appear to be writing anything to the depth buffer with this code. Can someone tell me what the heck I am doing wrong?

My generate FBO Code:

int shadowMapWidth = 640 * SHADOW_MAP_RATIO; //SHADOW_MAP_RATIO = 2
        int shadowMapHeight = 480 * SHADOW_MAP_RATIO; //SHADOW_MAP_RATIO = 2
 
        // create a framebuffer object
        GL.Ext.GenFramebuffers(1, out fboId);
        GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboId);
 
 
        // Try to use a texture depth component
        GL.GenTextures(1, out depthTextureId);
        GL.BindTexture(TextureTarget.Texture2D, depthTextureId);
 
        // GL_LINEAR does not make sense for depth texture. However, next tutorial shows usage of GL_LINEAR and PCF. Using GL_NEAREST
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
 
        // Remove artefact on the edges of the shadowmap
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
 
        // This is to allow usage of shadow2DProj function in the shader
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.DepthTextureMode, (int)All.Intensity);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareFunc, (int)All.Lequal);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareMode, (int)TextureCompareMode.CompareRToTexture);
 
 
 
        GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.DepthComponent32, shadowMapWidth, shadowMapHeight, 0, PixelFormat.DepthComponent, PixelType.UnsignedByte, IntPtr.Zero);
 
 
        GL.DrawBuffer(DrawBufferMode.None);
 
 
 
        // attach the texture to FBO depth attachment point
        GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt,  TextureTarget.Texture2D, depthTextureId, 0);
 
        // check FBO status
        FramebufferErrorCode FBOstatus = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);
        if (FBOstatus != FramebufferErrorCode.FramebufferComplete)
            Console.WriteLine("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use FBO. Status is " + FBOstatus.ToString());
 
 
        // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
        int[] queryinfo = new int[6];
        GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
        GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
        GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
        GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
        GL.GetInteger(GetPName.Samples, out queryinfo[4]);
        GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
        Console.WriteLine("FBO : max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
                           "\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
 
        Console.WriteLine("Last GL Error: " + GL.GetError());
 
 
        // switch back to window-system-provided framebuffer
        GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

Then my Render code in the render routine:

//First step: Render from the light POV to a FBO, story depth values only
    GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboId);	//Rendering offscreen
 
    // Instruct openGL that we won't bind a color texture with the currently binded FBO
    GL.DrawBuffer(DrawBufferMode.None);
 
    //Using the fixed pipeline to render to the depthbuffer
    GL.UseProgram(0);
 
     GL.PushAttrib( AttribMask.ViewportBit );
     {
 
    // In the case we render the shadowmap to a higher resolution, the viewport must be modified accordingly.
            GL.Viewport(0, 0, RENDER_WIDTH * SHADOW_MAP_RATIO, RENDER_HEIGHT * SHADOW_MAP_RATIO);
 
            // Clear previous frame values
            GL.Clear(ClearBufferMask.DepthBufferBit);
 
            //Disable color rendering, we only want to write to the Z-Buffer
            GL.ColorMask(false, false, false, false);
 
            setupMatrices(p_light[0], p_light[1], p_light[2], l_light[0], l_light[1], l_light[2]);
 
            // Culling switching, rendering only backface, this is done to avoid self-shadowing
            GL.CullFace(CullFaceMode.Front);
 
            //DRAW OBJECTS
            DrawObjects();
            setTextureMatrix();
     }
 
     GL.PopAttrib();
 
    // Now rendering from the camera POV, using the FBO to generate shadows
    GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
    GL.DrawBuffer(DrawBufferMode.Back);
 
 
    GL.Viewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
 
    //Enabling color write (previously disabled for light POV z-buffer rendering)
    GL.ColorMask(true, true, true, true);
 
    // Clear previous frame values
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
 
    setupMatrices(p_camera[0], p_camera[1], p_camera[2], l_camera[0], l_camera[1], l_camera[2]);
 
    GL.CullFace(CullFaceMode.Back);
 
    //DRAW OBJECTS
    DrawObjects();
 
 
// DEBUG only. this piece of code draw the depth buffer onscreen
 
    GL.UseProgram(0);
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(-RENDER_WIDTH / 2, RENDER_WIDTH / 2, -RENDER_HEIGHT / 2, RENDER_HEIGHT / 2, 1, 20);
    GL.MatrixMode(MatrixMode.Modelview);
    GL.LoadIdentity();
    GL.Color4(1, 1, 1, 1);
    GL.ActiveTexture(TextureUnit.Texture0);
    GL.BindTexture(TextureTarget.Texture2D, depthTextureId);
    GL.Enable(EnableCap.Texture2D);
    GL.Translate(0, 0, -1);
    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1, 0); GL.Vertex3(RENDER_WIDTH / 2, 0, 0);
    GL.TexCoord2(1, 1); GL.Vertex3(RENDER_WIDTH / 2, RENDER_HEIGHT / 2, 0);
    GL.TexCoord2(0, 1); GL.Vertex3(0, RENDER_HEIGHT / 2, 0);
 
 
    GL.End();
    GL.Disable(EnableCap.Texture2D);
 
 
 
    SwapBuffers();

I thought MAYBE it was because I had this call to GL.DrawBuffer(DrawBufferMode.None); in my generate FBO code, but when I take that out I get an error about the FBO Being incomplete because it cannot attach to a drawing buffer. If I leave it in, what I end up with is a nice, perfectly BLACK square representing my Depth Buffer Texture. This leads me to believe that there is nothing IN my depth buffer... I don't know of any other way to tell. Can anyone shed some light on what I might be doing wrong, or maybe a better way to check the contents of the depth buffer?

Thanks!
TheNerd


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
iliak's picture

Maybe this can help you...

TheNerd's picture

Well, I do understand what the FBO is and does, but what I need to do is be able to peek into what is written into the DEPTH buffer.

Is there anyway to see what is IN the depth buffer (if anything)?