#region usings using System; using System.Collections.Generic; using System.Linq; using OpenTK; using OpenTK.Graphics.OpenGL4; #endregion namespace SM.OGL.Framebuffer { /// /// Represents a OpenGL Framebuffer. /// public class Framebuffer : GLObject { /// protected override bool AutoCompile { get; set; } = true; /// /// The window for the screen /// public static IFramebufferWindow ScreenWindow; /// /// Represents the screen buffer. /// public static readonly Framebuffer Screen = new() { _id = 0, CanCompile = false, _window = ScreenWindow, _windowScale = 1, }; private IFramebufferWindow _window; private float _windowScale; /// public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer; /// /// Contains the size of the framebuffer. /// public Vector2 Size { get; private set; } /// /// Contains all color attachments. /// public Dictionary ColorAttachments { get; private set; } = new Dictionary(); /// /// Contains the current renderbuffer attachments of the framebuffer. /// public Dictionary RenderbufferAttachments { get; } = new Dictionary(); /// /// Gets the color attachment with the specified color name. /// /// /// public ColorAttachment this[string colorName] => ColorAttachments[colorName]; /// /// Creates a buffer without any options. /// public Framebuffer() { } /// /// Creates a buffer, while always respecting the screen. /// /// /// public Framebuffer(IFramebufferWindow window, float scale = 1) : this(new Vector2(window.Width * scale, window.Height * scale)) { _window = window; _windowScale = scale; } /// /// Creates a buffer, with a specified size. /// /// public Framebuffer(Vector2 size) { Size = size; } /// public override void Compile() { if (_id == 0) _window = ScreenWindow; if (_window != null) Size = new Vector2(_window.Width * _windowScale, _window.Height * _windowScale); base.Compile(); _id = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, _id); var enums = new DrawBuffersEnum[ColorAttachments.Count]; var c = 0; foreach (var pair in ColorAttachments) { pair.Value.Generate(this); enums[c++] = pair.Value.DrawBuffersEnum; } GL.DrawBuffers(enums.Length, enums); foreach (var pair in ColorAttachments) GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.Target, pair.Value.ID, 0); foreach (RenderbufferAttachment attachment in RenderbufferAttachments.Keys.ToArray()) { int att = attachment.Generate(this); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachment.FramebufferAttachment, RenderbufferTarget.Renderbuffer, att); RenderbufferAttachments[attachment] = att; } var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (err != FramebufferErrorCode.FramebufferComplete) throw new Exception("Failed loading framebuffer.\nProblem: " + err); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.BindTexture(TextureTarget.Texture2D, 0); } /// public override void Dispose() { foreach (var attachment in ColorAttachments.Values) attachment.Dispose(); foreach (KeyValuePair pair in RenderbufferAttachments.ToArray()) { GL.DeleteRenderbuffer(pair.Value); RenderbufferAttachments[pair.Key] = -1; } GL.DeleteFramebuffer(this); base.Dispose(); } /// /// Appends a color attachment. /// public void Append(string key, int pos) => Append(key, new ColorAttachment(pos)); /// /// Appends a color attachment. /// public void Append(string key, ColorAttachment value) { ColorAttachments.Add(key, value); } /// /// Appends a renderbuffer attachment to the framebuffer. /// /// public void AppendRenderbuffer(RenderbufferAttachment attachment) { RenderbufferAttachments.Add(attachment, -1); } /// /// Activates the framebuffer without clearing the buffer. /// public void Activate() { Activate(FramebufferTarget.Framebuffer, ClearBufferMask.None); } /// /// Activates the framebuffer for the specific target framebuffer and without clearing. /// /// public void Activate(FramebufferTarget target) { Activate(target, ClearBufferMask.None); } /// /// Activates the framebuffer while clearing the specified buffer. /// /// public void Activate(ClearBufferMask clearMask) { Activate(FramebufferTarget.Framebuffer, clearMask); } /// /// Activates the framebuffer for the specific target and with clearing. /// /// /// public void Activate(FramebufferTarget target, ClearBufferMask clear) { GL.BindFramebuffer(target, this); GL.Clear(clear); } /// /// Returns a handle of the current framebuffer. /// /// /// public static Framebuffer GetCurrentlyActive(FramebufferTarget target = FramebufferTarget.Framebuffer) { Framebuffer buffer = new() { CanCompile = false, ReportAsNotCompiled = true }; switch (target) { case FramebufferTarget.ReadFramebuffer: GL.GetInteger(GetPName.ReadFramebufferBinding, out buffer._id); break; case FramebufferTarget.DrawFramebuffer: GL.GetInteger(GetPName.DrawFramebufferBinding, out buffer._id); break; case FramebufferTarget.Framebuffer: GL.GetInteger(GetPName.FramebufferBinding, out buffer._id); break; } return buffer; } } }