miujin's picture

Render VBO in FBO

Hi,
I want to render my scene (with VBO) in several FBOs (one for the normal scene, one for the depth map, one for the normal map etc).

But I render nothing than the background in the FBOs.

The code:

       private void PanelLoad(object sender, EventArgs e)
        {
            // ... camera & VBO setup
 
            // FBO
            GL.GenTextures(1, out _fbo.TextureID);
            GL.BindTexture(TextureTarget.Texture2D, _fbo.TextureID);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            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,
                          _panel.Width,
                          _panel.Height,
                          0,
                          PixelFormat.Rgba,
                          PixelType.Byte,
                          IntPtr.Zero);
            GL.BindTexture(TextureTarget.Texture2D, 0);
 
            GL.Ext.GenRenderbuffers(1, out _fbo.DepthBufferID);
            GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, _fbo.DepthBufferID);
 
            GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt,
                                       RenderbufferStorage.DepthComponent32,
                                       _panel.Width,
                                       _panel.Height);
 
 
            GL.Ext.GenFramebuffers(1, out _fbo.FboID);
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, _fbo.FboID);
 
            GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt,
                                           FramebufferAttachment.DepthAttachmentExt,
                                           RenderbufferTarget.RenderbufferExt,
                                           _fbo.DepthBufferID);
 
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt,
                                        FramebufferAttachment.ColorAttachment0Ext,
                                        TextureTarget.Texture2D,
                                        _fbo.TextureID,
                                        0); 
 
            switch (GL.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
            {
                case FramebufferErrorCode.FramebufferCompleteExt:
                    break;
                case FramebufferErrorCode.FramebufferIncompleteAttachmentExt:
                    MessageBox.Show("Incomplete attachment");
                    return;
                case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt:
                    MessageBox.Show("Missing attachment");
                    return;
                case FramebufferErrorCode.FramebufferIncompleteDimensionsExt:
                    MessageBox.Show("Incomplete dimension");
                    return;
                case FramebufferErrorCode.FramebufferIncompleteFormatsExt:
                    MessageBox.Show("Incomplete formats");
                    return;
                case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt:
                    MessageBox.Show("Incomplete draw buffer");
                    return;
                case FramebufferErrorCode.FramebufferIncompleteReadBufferExt:
                    MessageBox.Show("Incomplete read buffer");
                    return;
                case FramebufferErrorCode.FramebufferUnsupportedExt:
                    MessageBox.Show("Incomplete read buffer");
                    return;
            }
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
        }
 
       private void RenderTexture()
        {
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, _fbo.FboID);
 
            GL.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0Ext);
 
            GL.PushAttrib(AttribMask.ViewportBit);
            {
                GL.Viewport(0, 0, _panel.Width, _panel.Height);
                var look = cam.ModelViewProjectionMatrix;
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref look);
 
                // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
                //GL.ClearColor(1f, 0f, 0f, 0f);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.ClearColor(Color.PowderBlue);
 
                GL.EnableClientState(ArrayCap.VertexArray);
                GL.EnableClientState(ArrayCap.ColorArray);
                GL.EnableClientState(ArrayCap.NormalArray);
 
                GL.Arb.BindBuffer(BufferTargetArb.ArrayBuffer, _vbo.VboID);
                GL.Arb.BindBuffer(BufferTargetArb.ElementArrayBuffer, _vbo.EboID);
 
                GL.ColorPointer(4, ColorPointerType.Float, 10 * sizeof(float), IntPtr.Zero);
                GL.NormalPointer(NormalPointerType.Float, 10 * sizeof(float), (IntPtr)(4 * sizeof(float)));
                GL.VertexPointer(3, VertexPointerType.Float, 10 * sizeof(float), (IntPtr)(7 * sizeof(float)));
 
                GL.DrawElements(BeginMode.Triangles, _vbo.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero);
 
                GL.DisableClientState(ArrayCap.NormalArray);
                GL.DisableClientState(ArrayCap.ColorArray);
                GL.DisableClientState(ArrayCap.VertexArray);
            }
            GL.PopAttrib();
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
            GL.DrawBuffer(DrawBufferMode.Back);
        }
 
        private void PanelPaint(object sender, EventArgs e)
        {
            _panel.MakeCurrent();
 
            RenderTexture();
 
            GL.Viewport(0, 0, _panel.Width, _panel.Height);
            var look = cam.ModelViewProjectionMatrix;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref look);
 
            GL.ClearColor(Color4.DarkGray);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Blend);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
 
            GL.PushMatrix();
                // Draw the Color Texture
                GL.Translate(2.1f, 0f, 0f);
                GL.BindTexture(TextureTarget.Texture2D, _fbo.TextureID);
                GL.Enable(EnableCap.Texture2D);
                GL.Begin(BeginMode.Quads);
                {
                    GL.TexCoord2(0f, 1f);
                    GL.Vertex2(-100.0f, 100.0f);
                    GL.TexCoord2(0.0f, 0.0f);
                    GL.Vertex2(-100.0f, -100.0f);
                    GL.TexCoord2(1.0f, 0.0f);
                    GL.Vertex2(100.0f, -100.0f);
                    GL.TexCoord2(1.0f, 1.0f);
                    GL.Vertex2(100.0f, 100.0f);
                }
                GL.End();
                GL.Disable(EnableCap.Texture2D);
                GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.PopMatrix();
 
            _panel.SwapBuffers();
        }

That would be very nice if anyone could help.