Beispiel #1
0
        private void initialize(int width, int height)
        {
            Width  = width;
            Height = height;

            PixelInternalFormat pixelInternalFormat = (PixelInternalFormat)storage;
            PixelFormat         pixelFormat;
            PixelType           pixelType;

            switch (storage)
            {
            case RenderbufferStorage.DepthComponent:
            case RenderbufferStorage.DepthComponent16:
            case RenderbufferStorage.DepthComponent24:
            case RenderbufferStorage.DepthComponent32:
            case RenderbufferStorage.DepthComponent32f:
                pixelFormat = PixelFormat.DepthComponent;
                pixelType   = PixelType.Float;
                break;

            case RenderbufferStorage.DepthStencil:
            case RenderbufferStorage.Depth32fStencil8:
            case RenderbufferStorage.Depth24Stencil8:
                pixelFormat = PixelFormat.DepthStencil;
                pixelType   = PixelType.UnsignedInt248;
                break;

            case RenderbufferStorage.StencilIndex1:
            case RenderbufferStorage.StencilIndex4:
            case RenderbufferStorage.StencilIndex8:
            case RenderbufferStorage.StencilIndex16:
                pixelFormat = PixelFormat.StencilIndex;
                pixelType   = PixelType.Float;
                break;

            default:
                pixelFormat = PixelFormat.Rgba;
                pixelType   = PixelType.UnsignedByte;
                break;
            }

            var textureId = GL.GenTexture();

            Texture = new Texture2d(textureId, Width, Height, "rendertexture");

            DrawState.BindPrimaryTexture(textureId);
            GL.TexImage2D(TextureTarget.Texture2D, 0, pixelInternalFormat, Width, Height, 0, pixelFormat, pixelType, IntPtr.Zero);
            DrawState.CheckError("creating a render texture's texture");

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);

            DrawState.UnbindTexture(textureId);

            Debug.Print(width + "x" + height + " " + storage + " render texture created");
        }
Beispiel #2
0
        public void Update(Bitmap bitmap, int x, int y)
        {
            DrawState.BindPrimaryTexture(textureId, TexturingModes.Texturing2d);

            var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexSubImage2D(TextureTarget.Texture2D, 0, x, y, bitmapData.Width, bitmapData.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0);
            GL.Finish();
            bitmap.UnlockBits(bitmapData);

            DrawState.CheckError("updating texture");
        }
Beispiel #3
0
        public void Update(Bitmap bitmap, int x, int y)
        {
            if (bitmap.Width < 1 || bitmap.Height < 1)
            {
                throw new InvalidOperationException($"Invalid bitmap size: {bitmap.Width}x{bitmap.Height}");
            }

            if (x + bitmap.Width > Width || y + bitmap.Height > Height)
            {
                throw new InvalidOperationException($"Invalid update bounds: {bitmap.Width}x{bitmap.Height} at {x},{y} overflows {Width}x{Height}");
            }

            DrawState.BindPrimaryTexture(textureId, TexturingModes.Texturing2d);

            var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexSubImage2D(TextureTarget.Texture2D, 0, x, y, bitmapData.Width, bitmapData.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0);
            GL.Finish();
            bitmap.UnlockBits(bitmapData);

            DrawState.CheckError("updating texture");
        }
Beispiel #4
0
        private void initialize()
        {
            textureId = GL.GenTexture();
            texture   = new Texture2d(textureId, width, height, "rendertarget");

            DrawState.BindPrimaryTexture(textureId);
            GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, pixelFormat, pixelType, IntPtr.Zero);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)textureMagFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)textureMinFilter);

            GL.GetInteger(GetPName.FramebufferBinding, out previousFrameBufferId);

            frameBufferId = GL.GenFramebuffer();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, textureId, 0);
            DrawState.CheckError("creating fbo");

            if (renderBufferType != null)
            {
                renderBufferId = GL.GenRenderbuffer();
                GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderBufferId);
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, renderBufferType.Value, width, height);

                switch (renderBufferType.Value)
                {
                case RenderbufferStorage.DepthComponent:
                case RenderbufferStorage.DepthComponent16:
                case RenderbufferStorage.DepthComponent24:
                case RenderbufferStorage.DepthComponent32:
                case RenderbufferStorage.DepthComponent32f:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, renderBufferId);
                    break;

                case RenderbufferStorage.DepthStencil:
                case RenderbufferStorage.Depth24Stencil8:
                case RenderbufferStorage.Depth32fStencil8:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, renderBufferId);
                    break;

                case RenderbufferStorage.StencilIndex1:
                case RenderbufferStorage.StencilIndex4:
                case RenderbufferStorage.StencilIndex8:
                case RenderbufferStorage.StencilIndex16:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachment, RenderbufferTarget.Renderbuffer, renderBufferId);
                    break;

                default:
                    throw new NotSupportedException("renderBufferType " + renderBufferType.Value + " isn't supported.");
                }
            }

            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception("frame buffer couldn't be constructed: " + status);
            }

            DrawState.UnbindTexture(textureId);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, previousFrameBufferId);

            Debug.Print(width + "x" + height + " render target created");
        }