Exemple #1
0
        public byte[] GetData()
        {
            VerifyThreadAffinity();
            var data = new byte[4 * Size.Width * Size.Height];

            // GLES doesn't support glGetTexImage so data must be read back via a frame buffer
            if (OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES))
            {
                // Query the active framebuffer so we can restore it afterwards
                int lastFramebuffer;
                OpenGL.glGetIntegerv(OpenGL.GL_FRAMEBUFFER_BINDING, out lastFramebuffer);

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

                OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture, 0);
                OpenGL.CheckGLError();

                unsafe
                {
                    fixed(byte *ptr = &data[0])
                    {
                        var intPtr = new IntPtr((void *)ptr);

                        OpenGL.glReadPixels(0, 0, Size.Width, Size.Height, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
                        OpenGL.CheckGLError();
                    }
                }

                OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, (uint)lastFramebuffer);
                OpenGL.glDeleteFramebuffers(1, ref framebuffer);
                OpenGL.CheckGLError();
            }
            else
            {
                OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
                unsafe
                {
                    fixed(byte *ptr = &data[0])
                    {
                        var intPtr = new IntPtr((void *)ptr);

                        OpenGL.glGetTexImage(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_BGRA,
                                             OpenGL.GL_UNSIGNED_BYTE, intPtr);
                    }
                }

                OpenGL.CheckGLError();
            }

            return(data);
        }
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }
            disposed = true;
            texture.Dispose();

            OpenGL.glDeleteFramebuffers(1, ref framebuffer);
            OpenGL.CheckGLError();
            OpenGL.glDeleteRenderbuffers(1, ref depth);
            OpenGL.CheckGLError();
        }
        void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }
            disposed = true;
            if (disposing)
            {
                foreach (Texture t in texture)
                {
                    t.Dispose();
                }
            }

            OpenGL.glDeleteFramebuffers(1, ref framebuffer);
            OpenGL.CheckGLError();
            OpenGL.glDeleteRenderbuffers(1, ref depth);
            OpenGL.CheckGLError();
        }
Exemple #4
0
        public byte[] GetData()
        {
            VerifyThreadAffinity();
            var data = new byte[4 * Size.Width * Size.Height];

            // GLES doesn't support glGetTexImage so data must be read back via a frame buffer
            if (OpenGL.Profile == GLProfile.Embedded)
            {
                // Query the active framebuffer so we can restore it afterwards
                int lastFramebuffer;
                OpenGL.glGetIntegerv(OpenGL.GL_FRAMEBUFFER_BINDING, out lastFramebuffer);

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

                OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture, 0);
                OpenGL.CheckGLError();

                var canReadBGRA = OpenGL.Features.HasFlag(OpenGL.GLFeatures.ESReadFormatBGRA);

                unsafe
                {
                    fixed(byte *ptr = &data[0])
                    {
                        var intPtr = new IntPtr(ptr);

                        var format = canReadBGRA ? OpenGL.GL_BGRA : OpenGL.GL_RGBA;

                        OpenGL.glReadPixels(0, 0, Size.Width, Size.Height, format, OpenGL.GL_UNSIGNED_BYTE, intPtr);
                        OpenGL.CheckGLError();
                    }
                }

                // Convert RGBA to BGRA
                if (!canReadBGRA)
                {
                    for (var i = 0; i < 4 * Size.Width * Size.Height; i += 4)
                    {
                        var temp = data[i];
                        data[i]     = data[i + 2];
                        data[i + 2] = temp;
                    }
                }

                OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, (uint)lastFramebuffer);
                OpenGL.glDeleteFramebuffers(1, ref framebuffer);
                OpenGL.CheckGLError();
            }
            else
            {
                OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
                unsafe
                {
                    fixed(byte *ptr = &data[0])
                    {
                        var intPtr = new IntPtr((void *)ptr);

                        OpenGL.glGetTexImage(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_BGRA,
                                             OpenGL.GL_UNSIGNED_BYTE, intPtr);
                    }
                }

                OpenGL.CheckGLError();
            }

            return(data);
        }