yunharla's picture

FBO DepthTexture problem

Hi all,

Im trying to add a hardware accelerated codepath into my renderer (currently it uses raytracing only). So far i was able to convert a huge part of code for opengl but then i tried to render the polyhedrons, intersecting my lights, into FBOs i noticed that my depthtexture is not updated correctly. To be more specific it just stays to the value of GL.ClearDepth although the colortexture shows the correct result.

to setup my FBO im using:

            Texture ColorTexture = new Texture();
            ColorTexture.Bind();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, w, h, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            Texture DepthTexture = new Texture();
            DepthTexture.Bind();
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
            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, w, h, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
 
 
            GL.Ext.GenFramebuffers(1, out FboHandle);
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FboHandle);
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture.source, 0);
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, TextureTarget.Texture2D, DepthTexture.source, 0);
            FramebufferErrorCode error = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);
            if(error != FramebufferErrorCode.FramebufferCompleteExt)
            {
                throw new Exception(error.ToString());
            }
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

while the actual drawing takes place in those functions:

        public void Draw(Action act)
        {
 
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FboHandle);
 
            GL.PushAttrib(AttribMask.ViewportBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            act();
            GL.PopAttrib();
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PopMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
 
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
 
        }

and

        FrameBuffer.Draw(delegate()
            {
 
                GL.Viewport(0, 0, App.RenderingWindow.ClientRectangle.Width, App.RenderingWindow.ClientRectangle.Height);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadMatrix(ref cam_modelView);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref cam_projection);
                GL.ClearColor(1.0f, 0.0f, 0.0f, 0.0f);
                GL.ClearDepth(1.0f);
                GL.Disable(EnableCap.CullFace);
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(DepthFunction.Lequal);
                GL.DepthMask(true);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                foreach (KeyValuePair<String, Material> kv in SceneMaterials)
                {
                    if (kv.Value != null)
                    {
                        kv.Value.Bind();
                    }
 
                    foreach (KeyValuePair<Guid, List<Surface>> brush in SceneBrushes)
                    {
                        foreach (Surface surf in (from val in brush.Value where val.SurfaceMaterial.CompareTo(kv.Key) == 0 select val))
                        {
                            surf.Draw();
                        }
                    }
 
                    if (kv.Value != null)
                    {
                        kv.Value.Unbind();
                    }
                }
 
            });

any idea what is going wrong here?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mOfl's picture
yunharla wrote:

To be more specific it just stays to the value of GL.ClearDepth although the colortexture shows the correct result.

I don't quite see the problem - is the "correct result" also correct regarding a correct z-test, i.e. the objects overlap each other in the right way? If so, it seems that you have no problem. It is common that if you display a depth map, it might be plain white, not because nothing is drawn into it but because 1) the depth data is stored non-linearly to provide more precision near the camera and 2) your monitor is likely to display only 256 shades of gray. To visualize the depth map in a correct way, you have to linearize it (-> http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffe...).

yunharla's picture

ahh thanks alot that did the trick ^^