Esempio n. 1
0
        // An array of RGBA
        public void SetData(uint[,] colors)
        {
            VerifyThreadAffinity();
            var width  = colors.GetUpperBound(1) + 1;
            var height = colors.GetUpperBound(0) + 1;

            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            unsafe
            {
                fixed(uint *ptr = &colors[0, 0])
                {
                    var intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height,
                                  0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, intPtr);
                    ErrorHandler.CheckGlError();
                }
            }
        }
Esempio n. 2
0
        public void SetData(Bitmap bitmap)
        {
            var allocatedBitmap = false;

            if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
            {
                bitmap          = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
                allocatedBitmap = true;
            }
            try
            {
                size = new Size(bitmap.Width, bitmap.Height);
                var bits = bitmap.LockBits(bitmap.Bounds(),
                                           ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                PrepareTexture();
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, bits.Width, bits.Height,
                              0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0);           // TODO: weird strides
                ErrorHandler.CheckGlError();
                bitmap.UnlockBits(bits);
            }
            finally
            {
                if (allocatedBitmap)
                {
                    bitmap.Dispose();
                }
            }
        }
Esempio n. 3
0
        // An array of RGBA
        public void SetData(uint[,] colors)
        {
            VerifyThreadAffinity();
            var width  = colors.GetUpperBound(1) + 1;
            var height = colors.GetUpperBound(0) + 1;

            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            unsafe
            {
                fixed(uint *ptr = &colors[0, 0])
                {
                    var intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, width, height,
                                        0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
                    OpenGL.CheckGLError();
                }
            }
        }
Esempio n. 4
0
        public void SetData(byte[] colors, int width, int height)
        {
            Threading.EnsureUIThread();
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException(string.Format("Non-power-of-two array {0} X {1}", width, height));
            }

            Size = new Size(width, height);
            //ConvertToABGR(height, width, colors);
            colors = ConvertToRGBA(colors);
            unsafe
            {
                fixed(byte *ptr = &colors[0])
                {
                    var intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr);
                    //GL.TexImage2D(TextureTarget.Texture2D, 0,
                    //            PixelInternalFormat.Rgba8, width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, intPtr);
                    //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr);
                    GraphicsExtensions.CheckGLError();
                }
            }
        }
Esempio n. 5
0
        public void SetData(Bitmap bitmap)
        {
            VerifyThreadAffinity();
            var allocatedBitmap = false;

            if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
            {
                bitmap          = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
                allocatedBitmap = true;
            }

            try
            {
                Size = new Size(bitmap.Width, bitmap.Height);
                var bits = bitmap.LockBits(bitmap.Bounds(),
                                           ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                PrepareTexture();
                OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, bits.Width, bits.Height,
                                    0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, bits.Scan0);     // TODO: weird strides
                OpenGL.CheckGLError();
                bitmap.UnlockBits(bits);
            }
            finally
            {
                if (allocatedBitmap)
                {
                    bitmap.Dispose();
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Convert from ARGB to ABGR
        /// </summary>
        /// <param name="pixelHeight"></param>
        /// <param name="pixelWidth"></param>
        /// <param name="colors"></param>
        //private static void ConvertToABGR(int pixelHeight, int pixelWidth, byte[] colors)
        //{
        //    int pixelCount = pixelHeight * pixelWidth;
        //    //int pixelCount = colors.Length;
        //    for(int i = 0; i < pixelCount; i++)
        //    {
        //        uint pixel = colors[i];
        //        colors[i] = (byte)((pixel & 0xFF00FF00) | ((pixel & 0x00FF0000) >> 16) | ((pixel & 0x000000FF) << 16));
        //    }
        //}

        //public static void ConvertTARGB(byte[] colors) {

        //    for (int i = 0; i < colors.Length; i++) {
        //        uint pixel = colors[i];
        //        colors[i] = (byte)((pixel & 0xFFFFFF00) | ((pixel&0x000000FF)<<24));
        //    }
        //}


        //An array of RGBA
        public void SetData(uint[,] colors)
        {
            Threading.EnsureUIThread();

            var width  = colors.GetUpperBound(1) + 1;
            var height = colors.GetUpperBound(0) + 1;

            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException(string.Format("Non-power-of-two array {0}x{1}", width, height));
            }

            Size = new Size(width, height);

            unsafe
            {
                fixed(uint *ptr = &colors[0, 0])
                {
                    var intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8,
                                  width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, intPtr);
                    //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr);
                }
            }
        }
Esempio n. 7
0
        public void SetEmpty(int width, int height)
        {
            VerifyThreadAffinity();
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            SetData(IntPtr.Zero, width, height);
        }
Esempio n. 8
0
        public void SetEmpty(int width, int height)
        {
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            size = new Size(width, height);
            PrepareTexture();
            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height,
                            0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, null);
            ErrorHandler.CheckGlError();
        }
Esempio n. 9
0
        public void SetEmpty(int width, int height)
        {
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            size = new Size(width, height);
            PrepareTexture();
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height,
                          0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
            ErrorHandler.CheckGlError();
        }
Esempio n. 10
0
        public void SetEmpty(int width, int height)
        {
            VerifyThreadAffinity();
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            PrepareTexture();
            OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, width, height,
                                0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, IntPtr.Zero);
            OpenGL.CheckGLError();
        }
Esempio n. 11
0
        public FrameBuffer(Size size, ITextureInternal texture, Color clearColor)
        {
            this.size       = size;
            this.clearColor = clearColor;
            if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
            {
                throw new InvalidDataException("Frame buffer size ({0}x{1}) must be a power of two".F(size.Width, size.Height));
            }

            OpenGL.glGenFramebuffers(1, out framebuffer);
            OpenGL.CheckGLError();
            OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebuffer);
            OpenGL.CheckGLError();

            // Color
            this.texture = texture;
            texture.SetEmpty(size.Width, size.Height);
            OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture.ID, 0);
            OpenGL.CheckGLError();

            // Depth
            OpenGL.glGenRenderbuffers(1, out depth);
            OpenGL.CheckGLError();

            OpenGL.glBindRenderbuffer(OpenGL.GL_RENDERBUFFER, depth);
            OpenGL.CheckGLError();

            var glDepth = OpenGL.Profile == GLProfile.Embedded ? OpenGL.GL_DEPTH_COMPONENT16 : OpenGL.GL_DEPTH_COMPONENT;

            OpenGL.glRenderbufferStorage(OpenGL.GL_RENDERBUFFER, glDepth, size.Width, size.Height);
            OpenGL.CheckGLError();

            OpenGL.glFramebufferRenderbuffer(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_DEPTH_ATTACHMENT, OpenGL.GL_RENDERBUFFER, depth);
            OpenGL.CheckGLError();

            // Test for completeness
            var status = OpenGL.glCheckFramebufferStatus(OpenGL.GL_FRAMEBUFFER);

            if (status != OpenGL.GL_FRAMEBUFFER_COMPLETE)
            {
                var error = "Error creating framebuffer: {0}\n{1}".F(status, new StackTrace());
                OpenGL.WriteGraphicsLog(error);
                throw new InvalidOperationException("OpenGL Error: See graphics.log for details.");
            }

            // Restore default buffer
            OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, 0);
            OpenGL.CheckGLError();
        }
Esempio n. 12
0
        public void SetEmpty(int width, int height)
        {
            Threading.EnsureUIThread();
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException(string.Format("Non-power-of-two array {0}x{1}", width, height));
            }

            Size = new Size(width, height);
            PrepareTexture();
            //GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, IntPtr.Zero);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GraphicsExtensions.CheckGLError();
        }
Esempio n. 13
0
        public void SetData(byte[] colors, int width, int height)
        {
            VerifyThreadAffinity();
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            unsafe
            {
                fixed(byte *ptr = &colors[0])
                SetData(new IntPtr(ptr), width, height);
            }
        }
Esempio n. 14
0
        public void SetData(Bitmap bitmap)
        {
            if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
            {
                bitmap = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
            }

            size = new Size(bitmap.Width, bitmap.Height);
            var bits = bitmap.LockBits(bitmap.Bounds(),
                                       ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            PrepareTexture();
            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height,
                            0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bits.Scan0);            // todo: weird strides
            ErrorHandler.CheckGlError();
            bitmap.UnlockBits(bits);
        }
Esempio n. 15
0
        public FrameBuffer(Size size)
        {
            this.size = size;
            if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
            {
                throw new InvalidDataException("Frame buffer size ({0}x{1}) must be a power of two".F(size.Width, size.Height));
            }

            GL.Ext.GenFramebuffers(1, out framebuffer);
            ErrorHandler.CheckGlError();
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, framebuffer);
            ErrorHandler.CheckGlError();

            // Color
            texture = new Texture();
            texture.SetEmpty(size.Width, size.Height);
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, texture.ID, 0);
            ErrorHandler.CheckGlError();

            // Depth
            GL.Ext.GenRenderbuffers(1, out depth);
            ErrorHandler.CheckGlError();

            GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, depth);
            ErrorHandler.CheckGlError();

            GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent, size.Width, size.Height);
            ErrorHandler.CheckGlError();

            GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, depth);
            ErrorHandler.CheckGlError();

            // Test for completeness
            var status = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);

            if (status != FramebufferErrorCode.FramebufferCompleteExt)
            {
                var error = "Error creating framebuffer: {0}\n{1}".F(status, new StackTrace());
                ErrorHandler.WriteGraphicsLog(error);
                throw new InvalidOperationException("OpenGL Error: See graphics.log for details.");
            }

            // Restore default buffer
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
            ErrorHandler.CheckGlError();
        }
Esempio n. 16
0
        public FrameBuffer(Size size)
        {
            this.size = size;
            if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
            {
                throw new InvalidDataException("Frame buffer size ({0}x{1}) must be a power of two".F(size.Width, size.Height));
            }

            Gl.glGenFramebuffersEXT(1, out framebuffer);
            ErrorHandler.CheckGlError();
            Gl.glBindFramebufferEXT(Gl.GL_FRAMEBUFFER_EXT, framebuffer);
            ErrorHandler.CheckGlError();

            // Color
            texture = new Texture();
            texture.SetEmpty(size.Width, size.Height);
            Gl.glFramebufferTexture2DEXT(Gl.GL_FRAMEBUFFER_EXT, Gl.GL_COLOR_ATTACHMENT0_EXT, Gl.GL_TEXTURE_2D, texture.ID, 0);
            ErrorHandler.CheckGlError();

            // Depth
            Gl.glGenRenderbuffersEXT(1, out depth);
            ErrorHandler.CheckGlError();

            Gl.glBindRenderbufferEXT(Gl.GL_RENDERBUFFER_EXT, depth);
            ErrorHandler.CheckGlError();

            Gl.glRenderbufferStorageEXT(Gl.GL_RENDERBUFFER_EXT, Gl.GL_DEPTH_COMPONENT, size.Width, size.Height);
            ErrorHandler.CheckGlError();

            Gl.glFramebufferRenderbufferEXT(Gl.GL_FRAMEBUFFER_EXT, Gl.GL_DEPTH_ATTACHMENT_EXT, Gl.GL_RENDERBUFFER_EXT, depth);
            ErrorHandler.CheckGlError();

            // Test for completeness
            var status = Gl.glCheckFramebufferStatusEXT(Gl.GL_FRAMEBUFFER_EXT);

            if (status != Gl.GL_FRAMEBUFFER_COMPLETE_EXT)
            {
                var error = "Error creating framebuffer: {0}\n{1}".F((ErrorHandler.GlError)status, new StackTrace());
                ErrorHandler.WriteGraphicsLog(error);
                throw new InvalidOperationException("OpenGL Error: See graphics.log for details.");
            }

            // Restore default buffer
            Gl.glBindFramebufferEXT(Gl.GL_FRAMEBUFFER_EXT, 0);
            ErrorHandler.CheckGlError();
        }
Esempio n. 17
0
        // An array of RGBA
        public void SetData(uint[,] colors)
        {
            VerifyThreadAffinity();
            var width  = colors.GetUpperBound(1) + 1;
            var height = colors.GetUpperBound(0) + 1;

            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            Size = new Size(width, height);
            unsafe
            {
                fixed(uint *ptr = &colors[0, 0])
                SetData(new IntPtr(ptr), width, height);
            }
        }
Esempio n. 18
0
        public FrameBuffer(Size size)
        {
            this.size = size;

            if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
            {
                throw new InvalidDataException(string.Format("Frame buffer size ({0}x{1}) must be a power of two", size.Width, size.Height));
            }

            GL.GenFramebuffers(1, out framebuffer);
            GraphicsExtensions.CheckGLError();
            GL.BindFramebuffer(FramebufferTarget.FramebufferExt, framebuffer);
            GraphicsExtensions.CheckGLError();

            //Color
            texture = new Graphics.Texture();
            texture.SetEmpty(size.Width, size.Height);
            GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, texture.ID, 0);
            GraphicsExtensions.CheckGLError();

            //Depth
            GL.GenRenderbuffers(1, out depth);
            GraphicsExtensions.CheckGLError();

            GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, depth);
            GraphicsExtensions.CheckGLError();

            GL.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent24Oes, size.Width, size.Height);
            GraphicsExtensions.CheckGLError();

            GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachment, RenderbufferTarget.RenderbufferExt, depth);
            GraphicsExtensions.CheckGLError();
            //Test for completeness
            var status = GL.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);

            if (status != FramebufferErrorCode.FramebufferCompleteExt)
            {
                throw new InvalidOperationException("OpenGL Error:" + status);
            }

            //Restore default buffer
            GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
            GraphicsExtensions.CheckGLError();
        }
Esempio n. 19
0
        public void SetData(byte[] colors, int width, int height)
        {
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            size = new Size(width, height);
            unsafe
            {
                fixed(byte *ptr = &colors[0])
                {
                    IntPtr intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height,
                                    0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr);
                    ErrorHandler.CheckGlError();
                }
            }
        }
Esempio n. 20
0
        public void SetData(byte[] colors, int width, int height)
        {
            if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
            {
                throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
            }

            size = new Size(width, height);
            unsafe
            {
                fixed(byte *ptr = &colors[0])
                {
                    var intPtr = new IntPtr((void *)ptr);

                    PrepareTexture();
                    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height,
                                  0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, intPtr);
                    ErrorHandler.CheckGlError();
                }
            }
        }