Exemplo n.º 1
0
        public override Bitmap Bitmap(TextureAttachment textureAttachment, RectangleI rectangle)
        {
            if (textureAttachment == TextureAttachment.Depth || textureAttachment == TextureAttachment.DepthStencil)
            {
                throw new CKGLException("Cannot read pixels of the Depth(Stecil) attachment.");
            }
            if (rectangle.X < 0 || rectangle.Y < 0 || rectangle.X + rectangle.W > Width || rectangle.Y + rectangle.H > Height)
            {
                throw new CKGLException("Rectangle outside of Framebuffer bounds.");
            }

            Framebuffer originalFramebuffer = Current;

            Bind();

            if (IsDefault)
            {
                if (textureAttachment == TextureAttachment.Depth || textureAttachment == TextureAttachment.DepthStencil)
                {
                    throw new IllegalValueException(typeof(TextureAttachment), textureAttachment);
                }
                GL.ReadBuffer(ReadBuffer.Back);
            }
            else
            {
                GL.ReadBuffer((ReadBuffer)textureAttachment.ToOpenGL());
            }

            Bitmap bitmap = new Bitmap(GL.ReadPixelsAsColourArray(rectangle, PixelFormat.RGBA), rectangle.W, rectangle.H);

            originalFramebuffer.Bind();

            return(bitmap);
        }
Exemplo n.º 2
0
        public override Texture GetTexture(TextureAttachment textureAttachment)
        {
            if (IsDefault)
            {
                throw new CKGLException($"Cannot get texture attachments from the Default Framebuffer.");
            }

            Texture result;

            if (textureAttachment == TextureAttachment.Depth || textureAttachment == TextureAttachment.DepthStencil)
            {
                result = DepthStencilTexture;
            }
            else
            {
                result = Textures[(int)textureAttachment];
            }

            if (result == null)
            {
                throw new ArgumentOutOfRangeException($"No suitable texture found in Framebuffer texture attachment {textureAttachment}.");
            }

            return(result);
        }
Exemplo n.º 3
0
        public void BlitTextureTo(Framebuffer target, TextureAttachment textureAttachment, BlitFilter filter, RectangleI rect)
        {
            if (textureAttachment < 0)
            {
                throw new ArgumentOutOfRangeException($"Can't blit a depth texture.");
            }

            Graphics.State.OnStateChanging.Invoke();

            OpenGLFramebuffer originalFramebuffer = Current as OpenGLFramebuffer;

            GL.BindFramebuffer(FramebufferTarget.Read, id);
            Swaps++;
            GL.BindFramebuffer(FramebufferTarget.Draw, ((OpenGLFramebuffer)target ?? (OpenGLFramebuffer)Default).id);
            Swaps++;

            Graphics.SetViewport(target);
            Graphics.SetScissorTest(target);
            GL.ReadBuffer((ReadBuffer)((uint)ReadBuffer.Colour0 + (uint)textureAttachment));
            GL.BlitFramebuffer(new RectangleI(Width, Height), rect, BufferBit.Colour, filter.ToOpenGL());
            //GL.BlitFramebuffer(new RectangleI(Width, Height), rect, textureAttachment >= 0 ? BufferBit.Colour : BufferBit.Depth, filter.ToOpenGL());

            // Reset Framebuffer
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, (originalFramebuffer ?? (OpenGLFramebuffer)Default).id);

            Blits++;

            Graphics.State.OnStateChanged.Invoke();
        }
Exemplo n.º 4
0
        public void BlitTextureTo(Framebuffer target, TextureAttachment textureAttachment, BlitFilter filter, RectangleI rect)
        {
            if (textureAttachment < 0)
            {
                throw new ArgumentOutOfRangeException($"Can't blit a depth texture.");
            }

            Graphics.State.OnStateChanging.Invoke();

            WebGL2Framebuffer originalFramebuffer = Current as WebGL2Framebuffer;

            GL.bindFramebuffer(READ_FRAMEBUFFER_Static, id);
            Swaps++;
            GL.bindFramebuffer(DRAW_FRAMEBUFFER_Static, ((WebGL2Framebuffer)target ?? (WebGL2Framebuffer)Default).id);
            Swaps++;

            Graphics.SetViewport(target);
            Graphics.SetScissorTest(target);
            GL.readBuffer(textureAttachment.ToWebGL2());
            GL.blitFramebuffer(0, 0, Width, Height, rect.Left, rect.Bottom, rect.Right, rect.Top, COLOR_BUFFER_BIT, filter.ToWebGL2());
            //GL.blitFramebuffer(new RectangleI(Width, Height), rect, textureAttachment >= 0 ? BufferBit.Colour : DEPTH_BUFFER_BIT, filter.ToWebGL2());

            // Reset Framebuffer
            GL.bindFramebuffer(FRAMEBUFFER, (originalFramebuffer ?? (WebGL2Framebuffer)Default).id);

            Blits++;

            Graphics.State.OnStateChanged.Invoke();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Attach a texture.
        /// <para>Bind() this framebuffer before invoking this method.</para>
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Texture Attach(TextureAttachment type)
        {
            const int level  = 0;
            Texture   result = null;

            switch (type)
            {
            case TextureAttachment.ColorAttachment:
                result = new Texture(TextureTarget.Texture2D,
                                     new TexImage2D(TexImage2D.Target.Texture2D, 0, (int)GL.GL_RGBA, this.Width, this.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));
                result.Initialize();
                glFramebufferTexture(GL.GL_FRAMEBUFFER, attachment_id[nextColorAttachmentIndex++], result.Id, level);
                break;

            case TextureAttachment.DepthAttachment:
                result = new Texture(TextureTarget.Texture2D,
                                     new TexImage2D(TexImage2D.Target.Texture2D, 0, (int)GL.GL_DEPTH_COMPONENT32, this.Width, this.Height, 0, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT));
                // 设置默认滤波模式
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));
                // 设置深度比较模式
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureCompareMode, (int)GL.GL_COMPARE_REF_TO_TEXTURE));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureCompareFunc, (int)GL.GL_LEQUAL));
                // 设置边界截取模式
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE));
                result.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE));
                result.Initialize();
                glFramebufferTexture(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, result.Id, level);
                //glFramebufferTexture(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_STENCIL_ATTACHMENT, result.Id, level);
                //glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_TEXTURE_2D, result.Id, level);
                //glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_STENCIL_ATTACHMENT, GL.GL_TEXTURE_2D, result.Id, level);// error
                break;

            case TextureAttachment.StencilAttachment:
                throw new NotImplementedException();
                break;

            case TextureAttachment.DepthStencilAttachment:
                throw new NotImplementedException();
                break;

            default:
                throw new NotImplementedException();
            }

            return(result);
        }
Exemplo n.º 6
0
        private void AddTextureAttachment(TextureAttachment texa)
        {
            int index = _textureAttachments.FindIndex(a => a.Attachment == texa.Attachment);

            if (index == -1)
            {
                if (_textureAttachments.Count == _maxAttacments)
                {
                    throw new IndexOutOfRangeException("Maximum number of textures have already been attached to the FrameBuffer.");
                }
                _textureAttachments.Add(texa);
                SetDrawBuffers();
            }
            else
            {
                _textureAttachments[index] = texa;
            }
            SetSize();
            SetStatus();
        }
Exemplo n.º 7
0
        public void Dispose()
        {
            if (Id == 0)
            {
                throw new InvalidOperationException("Already disposed.");
            }
            ;

            var gl = XEngineContext.Graphics;

            TextureAttachment?.Dispose();
            TextureAttachment = null;

            DepthTextureAttachment?.Dispose();
            DepthTextureAttachment = null;

            DepthBufferAttachment?.Dispose();
            DepthBufferAttachment = null;

            gl.DeleteFramebuffersEXT(1u, glFrameBufferArray);
            glFrameBufferArray[0] = 0u;
        }
Exemplo n.º 8
0
 public void BlitTextureTo(Framebuffer target, TextureAttachment textureAttachment, BlitFilter filter, RectangleI rect)
 {
     throw new CKGLException($"WebGL 1.0 does not support blitting framebuffers.");
 }
Exemplo n.º 9
0
 public void BlitTextureTo(Framebuffer target, TextureAttachment textureAttachment, BlitFilter filter, int x, int y) => BlitTextureTo(target, textureAttachment, filter, new RectangleI(x, y, Width, Height));
Exemplo n.º 10
0
 public override Bitmap Bitmap(TextureAttachment textureAttachment, RectangleI rectangle) => throw new NotImplementedException();
Exemplo n.º 11
0
 public abstract Bitmap Bitmap(TextureAttachment textureAttachment, RectangleI rectangle);
Exemplo n.º 12
0
 public Bitmap Bitmap(TextureAttachment textureAttachment) => Bitmap(textureAttachment, new RectangleI(0, 0, Width, Height));
Exemplo n.º 13
0
 public abstract Texture GetTexture(TextureAttachment textureAttachment);
Exemplo n.º 14
0
        internal static OpenGLBindings.TextureAttachment ToOpenGL(this TextureAttachment textureAttachment)
        {
            switch (textureAttachment)
            {
            case TextureAttachment.Depth:
                return(OpenGLBindings.TextureAttachment.Depth);

            case TextureAttachment.DepthStencil:
                return(OpenGLBindings.TextureAttachment.DepthStencil);

            case TextureAttachment.Colour0:
                return(OpenGLBindings.TextureAttachment.Colour0);

            case TextureAttachment.Colour1:
                return(OpenGLBindings.TextureAttachment.Colour1);

            case TextureAttachment.Colour2:
                return(OpenGLBindings.TextureAttachment.Colour2);

            case TextureAttachment.Colour3:
                return(OpenGLBindings.TextureAttachment.Colour3);

            case TextureAttachment.Colour4:
                return(OpenGLBindings.TextureAttachment.Colour4);

            case TextureAttachment.Colour5:
                return(OpenGLBindings.TextureAttachment.Colour5);

            case TextureAttachment.Colour6:
                return(OpenGLBindings.TextureAttachment.Colour6);

            case TextureAttachment.Colour7:
                return(OpenGLBindings.TextureAttachment.Colour7);

            case TextureAttachment.Colour8:
                return(OpenGLBindings.TextureAttachment.Colour8);

            case TextureAttachment.Colour9:
                return(OpenGLBindings.TextureAttachment.Colour9);

            case TextureAttachment.Colour10:
                return(OpenGLBindings.TextureAttachment.Colour10);

            case TextureAttachment.Colour11:
                return(OpenGLBindings.TextureAttachment.Colour11);

            case TextureAttachment.Colour12:
                return(OpenGLBindings.TextureAttachment.Colour12);

            case TextureAttachment.Colour13:
                return(OpenGLBindings.TextureAttachment.Colour13);

            case TextureAttachment.Colour14:
                return(OpenGLBindings.TextureAttachment.Colour14);

            case TextureAttachment.Colour15:
                return(OpenGLBindings.TextureAttachment.Colour15);

            default:
                throw new IllegalValueException(typeof(TextureAttachment), textureAttachment);
            }
        }
Exemplo n.º 15
0
        internal static double ToWebGL2(this TextureAttachment textureAttachment)
        {
            switch (textureAttachment)
            {
            case TextureAttachment.Depth:
                return(DEPTH_ATTACHMENT);

            case TextureAttachment.DepthStencil:
                return(DEPTH_STENCIL_ATTACHMENT);

            case TextureAttachment.Colour0:
                return(COLOR_ATTACHMENT0);

            case TextureAttachment.Colour1:
                return(COLOR_ATTACHMENT1_Static);

            case TextureAttachment.Colour2:
                return(COLOR_ATTACHMENT2_Static);

            case TextureAttachment.Colour3:
                return(COLOR_ATTACHMENT3_Static);

            case TextureAttachment.Colour4:
                return(COLOR_ATTACHMENT4_Static);

            case TextureAttachment.Colour5:
                return(COLOR_ATTACHMENT5_Static);

            case TextureAttachment.Colour6:
                return(COLOR_ATTACHMENT6_Static);

            case TextureAttachment.Colour7:
                return(COLOR_ATTACHMENT7_Static);

            case TextureAttachment.Colour8:
                return(COLOR_ATTACHMENT8_Static);

            case TextureAttachment.Colour9:
                return(COLOR_ATTACHMENT9_Static);

            case TextureAttachment.Colour10:
                return(COLOR_ATTACHMENT10_Static);

            case TextureAttachment.Colour11:
                return(COLOR_ATTACHMENT11_Static);

            case TextureAttachment.Colour12:
                return(COLOR_ATTACHMENT12_Static);

            case TextureAttachment.Colour13:
                return(COLOR_ATTACHMENT13_Static);

            case TextureAttachment.Colour14:
                return(COLOR_ATTACHMENT14_Static);

            case TextureAttachment.Colour15:
                return(COLOR_ATTACHMENT15_Static);

            default:
                throw new IllegalValueException(typeof(TextureAttachment), textureAttachment);
            }
        }