Exemplo n.º 1
0
        public void Begin(bool clear = true)
        {
            if (started)
            {
                throw new InvalidOperationException("Already started");
            }

            DrawState.FlushRenderer();

            validate();

            previousViewport = DrawState.Viewport;

            GL.GetInteger(GetPName.FramebufferBinding, out previousFrameBufferId);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId);
            GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            DrawState.CheckError("binding fbo");

            DrawState.Viewport = new Rectangle(0, 0, width, height);

            if (clear)
            {
                // XXX need GL.DepthMask(true); to clear depth, but setting it here may cause issues with renderstate cache
                GL.ClearColor(0, 0, 0, 0);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            }

            started = true;
        }
Exemplo n.º 2
0
        public void Draw(Vector3 start, Vector3 end, Color4 color)
        {
            if (!rendering)
            {
                throw new InvalidOperationException("Not rendering");
            }

            if (linesInBatch == maxLinesPerBatch)
            {
                DrawState.FlushRenderer(true);
            }

            var linePrimitive = default(LinePrimitive);

            linePrimitive.x1 = start.X;
            linePrimitive.y1 = start.Y;
            linePrimitive.z1 = start.Z;
            linePrimitive.x2 = end.X;
            linePrimitive.y2 = end.Y;
            linePrimitive.z2 = end.Z;

            linePrimitive.color1 = linePrimitive.color2 = color.ToRgba();

            lineArray[linesInBatch] = linePrimitive;

            RenderedSpriteCount++;
            linesInBatch++;
        }
Exemplo n.º 3
0
        public void Begin(bool clear = true)
        {
            if (started)
            {
                throw new InvalidOperationException("Already started");
            }

            DrawState.FlushRenderer();
            if (!initialized)
            {
                initialize();
            }

            previousViewport = DrawState.Viewport;

            GL.GetInteger(GetPName.FramebufferBinding, out previousFrameBufferId);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId);
            if (renderTextures.Length == 1)
            {
                GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            }
            else if (renderTextures.Length > 1)
            {
                var buffers = new DrawBuffersEnum[renderTextures.Length];
                for (int i = 0, size = renderTextures.Length; i < size; i++)
                {
                    Debug.Assert(DrawBuffersEnum.ColorAttachment0 + i <= DrawBuffersEnum.ColorAttachment15);
                    buffers[i] = DrawBuffersEnum.ColorAttachment0 + i;
                }
                GL.DrawBuffers(buffers.Length, buffers);
            }
            DrawState.CheckError("binding fbo");

            DrawState.Viewport = new Rectangle(0, 0, width, height);

            if (clear)
            {
                GL.ClearColor(0, 0, 0, 0);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            }

            started = true;
        }
Exemplo n.º 4
0
        public void End()
        {
            if (!started)
            {
                throw new InvalidOperationException("Not started");
            }

            DrawState.FlushRenderer();

            GL.GetInteger(GetPName.FramebufferBinding, out int currentFrameBufferId);
            if (currentFrameBufferId != frameBufferId)
            {
                throw new InvalidOperationException("Invalid current frame buffer");
            }

            DrawState.Viewport = previousViewport;

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, previousFrameBufferId);

            started = false;
        }
        public void Draw(Texture2dRegion texture, float x, float y, float originX, float originY, float scaleX, float scaleY, float rotation, Color4 color, float textureX0, float textureY0, float textureX1, float textureY1)
        {
            if (!rendering)
            {
                throw new InvalidOperationException("Not rendering");
            }
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            if (currentTexture != texture.BindableTexture)
            {
                DrawState.FlushRenderer();
                currentTexture = texture.BindableTexture;
            }
            else if (spritesInBatch == maxSpritesPerBatch)
            {
                DrawState.FlushRenderer(true);
            }

            var width  = textureX1 - textureX0;
            var height = textureY1 - textureY0;

            float fx  = -originX;
            float fy  = -originY;
            float fx2 = width - originX;
            float fy2 = height - originY;

            var flipX = false;
            var flipY = false;

            if (scaleX != 1 || scaleY != 1)
            {
                flipX = scaleX < 0;
                flipY = scaleY < 0;

                var absScaleX = flipX ? -scaleX : scaleX;
                var absScaleY = flipY ? -scaleY : scaleY;

                fx  *= absScaleX;
                fy  *= absScaleY;
                fx2 *= absScaleX;
                fy2 *= absScaleY;
            }

            float p1x = fx;
            float p1y = fy;
            float p2x = fx;
            float p2y = fy2;
            float p3x = fx2;
            float p3y = fy2;
            float p4x = fx2;
            float p4y = fy;

            float x1;
            float y1;
            float x2;
            float y2;
            float x3;
            float y3;
            float x4;
            float y4;

            if (rotation != 0)
            {
                var cos = (float)Math.Cos(rotation);
                var sin = (float)Math.Sin(rotation);

                x1 = cos * p1x - sin * p1y;
                y1 = sin * p1x + cos * p1y;
                x2 = cos * p2x - sin * p2y;
                y2 = sin * p2x + cos * p2y;
                x3 = cos * p3x - sin * p3y;
                y3 = sin * p3x + cos * p3y;
                x4 = x1 + (x3 - x2);
                y4 = y3 - (y2 - y1);
            }
            else
            {
                x1 = p1x;
                y1 = p1y;
                x2 = p2x;
                y2 = p2y;
                x3 = p3x;
                y3 = p3y;
                x4 = p4x;
                y4 = p4y;
            }

            var spritePrimitive = default(SpritePrimitive);

            spritePrimitive.x1 = x1 + x;
            spritePrimitive.y1 = y1 + y;
            spritePrimitive.x2 = x2 + x;
            spritePrimitive.y2 = y2 + y;
            spritePrimitive.x3 = x3 + x;
            spritePrimitive.y3 = y3 + y;
            spritePrimitive.x4 = x4 + x;
            spritePrimitive.y4 = y4 + y;

            var textureUvBounds = texture.UvBounds;
            var textureUvRatio  = texture.UvRatio;

            var textureU0 = textureUvBounds.Left + textureX0 * textureUvRatio.X;
            var textureV0 = textureUvBounds.Top + textureY0 * textureUvRatio.Y;
            var textureU1 = textureUvBounds.Left + textureX1 * textureUvRatio.X;
            var textureV1 = textureUvBounds.Top + textureY1 * textureUvRatio.Y;

            float u0, v0, u1, v1;

            if (flipX)
            {
                u0 = textureU1;
                u1 = textureU0;
            }
            else
            {
                u0 = textureU0;
                u1 = textureU1;
            }
            if (flipY)
            {
                v0 = textureV1;
                v1 = textureV0;
            }
            else
            {
                v0 = textureV0;
                v1 = textureV1;
            }

            spritePrimitive.u1 = u0;
            spritePrimitive.v1 = v0;
            spritePrimitive.u2 = u0;
            spritePrimitive.v2 = v1;
            spritePrimitive.u3 = u1;
            spritePrimitive.v3 = v1;
            spritePrimitive.u4 = u1;
            spritePrimitive.v4 = v0;

            spritePrimitive.color1 = spritePrimitive.color2 = spritePrimitive.color3 = spritePrimitive.color4 = color.ToRgba();

            spriteArray[spritesInBatch] = spritePrimitive;

            RenderedSpriteCount++;
            spritesInBatch++;
        }