Example #1
0
        public OpenGLFramebuffer(int width, int height, GLint colourTextures, TextureFormat textureColourFormat, TextureFormat?textureDepthFormat = null)
        {
            if (colourTextures < 1)
            {
                throw new CKGLException("Must have at least 1 colour texture.");
            }
            if (colourTextures > GL.MaxColourAttachments || colourTextures > GL.MaxDrawBuffers)
            {
                throw new CKGLException("Too many colour textures.");
            }
            if (textureColourFormat.ToOpenGL().PixelFormat() == PixelFormat.Depth || textureColourFormat.ToOpenGL().PixelFormat() == PixelFormat.DepthStencil)
            {
                throw new CKGLException("textureColourFormat cannot be a depth(stencil) texture.");
            }
            if (textureDepthFormat.HasValue && !(textureDepthFormat.Value.ToOpenGL().PixelFormat() == PixelFormat.Depth || textureDepthFormat.Value.ToOpenGL().PixelFormat() == PixelFormat.DepthStencil))
            {
                throw new CKGLException("textureDepthFormat is not a depth(stencil) texture.");
            }

            this.width  = width;
            this.height = height;

            camera2D.Width  = width;
            camera2D.Height = height;

            id = GL.GenFramebuffer();

            Bind();

            Textures = new Texture[colourTextures];
            DrawBuffer[] drawBuffers = new DrawBuffer[colourTextures];
            for (int i = 0; i < colourTextures; i++)
            {
                Textures[i]    = Texture.Create2D(Width, Height, textureColourFormat);
                drawBuffers[i] = (DrawBuffer)((GLuint)DrawBuffer.Colour0 + i);
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, ((TextureAttachment)((uint)TextureAttachment.Colour0 + i)).ToOpenGL(), (Textures[i] as OpenGLTexture).TextureTarget, (Textures[i] as OpenGLTexture).ID, 0);
                Textures[i].Unbind();
                CheckStatus();
            }
            GL.DrawBuffers(colourTextures, drawBuffers);
            CheckStatus();

            if (textureDepthFormat.HasValue)
            {
                DepthStencilTexture = Texture.Create2D(Width, Height, textureDepthFormat.Value);
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, textureDepthFormat.Value.ToOpenGL().TextureAttachment(), (DepthStencilTexture as OpenGLTexture).TextureTarget, (DepthStencilTexture as OpenGLTexture).ID, 0);
                DepthStencilTexture.Unbind();
                CheckStatus();
            }
        }