Beispiel #1
0
        public void Draw(Texture2D texture, RectangleF area, Matrix transformMatrix, BlendMode blendMode = BlendMode.BlendmodeAlpha, ColorTextureEffect customEffect = null)
        {
            currentEffect = customEffect;
            if (currentEffect == null)
            {
                currentEffect = defaultEffect;
            }

            var textureSize = texture.Size;
            var scale       = area.Size / textureSize;

            var color = Color.White;

            transformMatrix.Translation /= new Vector3(area.Width, area.Height, 1);
            transformMatrix             *= Matrix.CreateScale(scale.X, scale.Y, 1);

            vertices[0] = new Vertex2Tex2Color(area.TopLeft, color, Vector2.Transform(new Vector2(0, 0), transformMatrix));
            vertices[1] = new Vertex2Tex2Color(area.BottomLeft, color, Vector2.Transform(new Vector2(0, 1), transformMatrix));
            vertices[2] = new Vertex2Tex2Color(area.TopRight, color, Vector2.Transform(new Vector2(1, 0), transformMatrix));
            vertices[3] = new Vertex2Tex2Color(area.BottomRight, color, Vector2.Transform(new Vector2(1, 1), transformMatrix));

            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.DepthTest);
            //GL.Viewport(graphicsDevice.Viewport.X, graphicsDevice.BackBufferSize.Y - graphicsDevice.Viewport.Y - graphicsDevice.Viewport.Height, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);

            currentEffect.Projection = Projection ?? CurrentContext.ActiveScreen.DefaultProjection;
            currentEffect.View       = Matrix.Identity;
            currentEffect.Enable();

            GL.BindTexture(TextureTarget.Texture2D, texture.textureId);
            RenderHelpers.SetSamplers(TextureSampling.LinearWrap);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObjects[0]);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjects[1]);

            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vertex2Tex2Color.GetSize()), vertices, BufferUsageHint.DynamicDraw);

            if (currentEffect.ColorAttribute.HasValue)
            {
                GL.VertexAttribPointer(currentEffect.ColorAttribute.Value, 4, VertexAttribPointerType.UnsignedByte, true, Vertex2Tex2Color.GetSize(), sizeof(float) * 2);
            }

            GL.VertexAttribPointer(currentEffect.VertexAttribute, 2, VertexAttribPointerType.Float, false, Vertex2Tex2Color.GetSize(), 0);
            GL.VertexAttribPointer(currentEffect.TextureCoordinatesAttribute, 2, VertexAttribPointerType.Float, false, Vertex2Tex2Color.GetSize(), sizeof(float) * 2 + sizeof(byte) * 4);
            GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, 0);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            currentEffect.Disable();
        }
Beispiel #2
0
        private void Render(RenderQueue.Batch renderBatch)
        {
            var batch   = (Batch)renderBatch;
            var entries = batch.Entries;

            if (entries.Count > 0)
            {
                RenderHelpers.SetBlendMode(blendMode);

                GL.Disable(EnableCap.CullFace);
                GL.Disable(EnableCap.DepthTest);
                //GL.Viewport(graphicsDevice.Viewport.X, graphicsDevice.BackBufferSize.Y - graphicsDevice.Viewport.Y - graphicsDevice.Viewport.Height, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);

                currentEffect.Projection = Projection ?? CurrentContext.ActiveScreen.DefaultProjection;
                currentEffect.View       = transform;

                currentEffect.Enable();

                for (int i = 0; i < entries.Count;)
                {
                    int        quadIndex  = 0;
                    BatchEntry firstEntry = entries[i];
                    Texture2D  texture    = firstEntry.Texture;

                    if (AddEntry(ref firstEntry, quadIndex++) == 0)
                    {
                        Debug.WriteLine("Warning: out of QuadBatch capacity!");
                        break;
                    }

                    // Push in new quads until we reach a new texture
                    int entryCount = 1;
                    for (int j = i + 1; j < entries.Count; ++j)
                    {
                        BatchEntry futureEntry = entries[j];
                        if (futureEntry.Texture != texture)
                        {
                            break;
                        }

                        int added = AddEntry(ref futureEntry, quadIndex++);
                        if (added == 0)
                        {
                            Debug.WriteLine("Warning: out of QuadBatch capacity!");
                        }
                        entryCount += added;                         // If we're out of entry slots, do not increase entryCount
                    }

                    // Set texture
                    GL.BindTexture(TextureTarget.Texture2D, texture.textureId);
                    RenderHelpers.SetSamplers(textureSampling);

                    GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObjects[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(entryCount * 6 * Vertex2Tex2Color.GetSize()), quads, BufferUsageHint.DynamicDraw);
                    //GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (IntPtr)(entryCount * 6 * Vertex2Tex2Color.GetSize()), quads);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjects[1]);

                    if (currentEffect.ColorAttribute.HasValue)
                    {
                        GL.VertexAttribPointer(currentEffect.ColorAttribute.Value, 4, VertexAttribPointerType.UnsignedByte, true, Vertex2Tex2Color.GetSize(), sizeof(float) * 2);
                    }

                    GL.VertexAttribPointer(currentEffect.VertexAttribute, 2, VertexAttribPointerType.Float, false, Vertex2Tex2Color.GetSize(), 0);
                    GL.VertexAttribPointer(currentEffect.TextureCoordinatesAttribute, 2, VertexAttribPointerType.Float, false, Vertex2Tex2Color.GetSize(), sizeof(float) * 2 + sizeof(byte) * 4);
                    GL.DrawElements(BeginMode.Triangles, entryCount * 6, DrawElementsType.UnsignedShort, 0);

                    i += entryCount;
                }

                entries.Clear();

                currentEffect.Disable();
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            }

            // This group is done, recycle it to the pool
            batchPool.Delete(batch);
        }