/// <summary> /// Allocate this RenderBuffer. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocation. /// </param> /// <param name="w"> /// A <see cref="Int32"/> that specify the width of the renderbuffer. /// </param> /// <param name="h"> /// A <see cref="Int32"/> that specify the height of the renderbuffer. /// </param> public void Allocate(GraphicsContext ctx, uint w, uint h) { // Allocate buffer Gl.BindRenderbuffer(Gl.RENDERBUFFER, ObjectName); // Define buffer storage Gl.RenderbufferStorage(Gl.RENDERBUFFER, Pixel.GetGlInternalFormat(_InternalFormat, ctx), (int)w, (int)h); }
/// <summary> /// Actually create this GraphicsResource resources. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocating resources. /// </param> protected override void CreateObject(GraphicsContext ctx) { int currentBinding; Gl.Get(Gl.RENDERBUFFER_BINDING, out currentBinding); Gl.BindRenderbuffer(Gl.RENDERBUFFER, ObjectName); // Define buffer storage Gl.RenderbufferStorage(Gl.RENDERBUFFER, Pixel.GetGlInternalFormat(_InternalFormat, ctx), (int)_Width, (int)_Height); // Restore previous RenderBuffer binding Gl.BindRenderbuffer(Gl.RENDERBUFFER, (uint)currentBinding); }
/// <summary> /// Creates a framebuffer object and its associated resources (depth and pbuffers). /// </summary> /// <param name="Size">Specifies the size (in pixels) of the framebuffer and it's associated buffers.</param> /// <param name="Attachments">Specifies the attachments to use for the frame buffer.</param> /// <param name="Format">Specifies the internal pixel format for the frame buffer.</param> /// <param name="Mipmaps">Specified whether to build mipmaps after the frame buffer is unbound.</param> public FBO(Size Size, FramebufferAttachment[] Attachments, PixelInternalFormat Format, bool Mipmaps) { this.Size = Size; this.Attachments = Attachments; this.Format = Format; this.mipmaps = Mipmaps; // First create the framebuffer BufferID = Gl.GenFramebuffer(); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, BufferID); if (Attachments.Length == 1 && Attachments[0] == FramebufferAttachment.DepthAttachment) { TextureID = new uint[] { Gl.GenTexture() }; Gl.BindTexture(TextureTarget.Texture2D, TextureID[0]); Gl.TexImage2D(TextureTarget.Texture2D, 0, Format, Size.Width, Size.Height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Nearest); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.Nearest); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureParameter.ClampToEdge); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureParameter.ClampToEdge); Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureID[0], 0); Gl.DrawBuffer(DrawBufferMode.None); Gl.ReadBuffer(ReadBufferMode.None); } else { // Create and attach a 24-bit depth buffer to the framebuffer DepthID = Gl.GenRenderbuffer(); Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, DepthID); Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, Size.Width, Size.Height); // Create n texture buffers (known by the number of attachments) TextureID = new uint[Attachments.Length]; Gl.GenTextures(Attachments.Length, TextureID); // Bind the n texture buffers to the framebuffer for (int i = 0; i < Attachments.Length; i++) { Gl.BindTexture(TextureTarget.Texture2D, TextureID[i]); Gl.TexImage2D(TextureTarget.Texture2D, 0, Format, Size.Width, Size.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); if (Mipmaps) { Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Linear); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.LinearMipMapLinear); Gl.GenerateMipmap(GenerateMipmapTarget.Texture2D); } Gl.FramebufferTexture(FramebufferTarget.Framebuffer, Attachments[i], TextureID[i], 0); } // Build the framebuffer and check for errors Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, DepthID); } FramebufferErrorCode status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != FramebufferErrorCode.FramebufferComplete) { Console.WriteLine("Frame buffer did not compile correctly. Returned {0}, glError: {1}", status.ToString(), Gl.GetError().ToString()); } Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0); }