/// <summary> /// Constructor to create a new FrameBuffer instance. /// </summary> /// <param name="tex">Texture the frame buffer will write to</param> public FrameBuffer(Texture tex, int depthBits = 0) { Texture = tex; // Prepare the target texture for use Texture.Bind(); if (depthBits > 0) { _depthBufferID = GL.GenRenderbuffer(); RenderbufferStorage rbStorage; switch (depthBits) { case 16: rbStorage = RenderbufferStorage.DepthComponent16; break; case 24: rbStorage = RenderbufferStorage.DepthComponent24; break; case 32: rbStorage = RenderbufferStorage.DepthComponent32; break; default: throw new ArgumentException("Invalid depth buffer bit count."); } GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _depthBufferID); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, rbStorage, tex.Width, tex.Height); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); } // Assign the texture to the frame buffer GL.BindFramebuffer(FramebufferTarget.Framebuffer, FboID); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, tex.TextureTarget, tex.TextureID, 0); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, _depthBufferID); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); Tools.ErrorCheck("fbo_init"); }
/// <summary> /// Prepares the texture for usage. /// </summary> public void Bind() { // If this texture isn't already bound, bind it if (_sCurrentLoadedTexture != this) { GL.BindTexture(TextureTarget, TextureID); _sCurrentLoadedTexture = this; } // Tools.ErrorCheck("bindtexture"); // If the texture has changed since the last time it was bound, // upload the new data to video memory if (!_loaded) { Load(); _loaded = true; } }