Exemplo n.º 1
0
        public void Draw(PrimitiveType mode)
        {
            lock (SyncRoot)
            {
                using (new GLEnableCap(EnableCap.Texture2D))
                    using (new GLEnableCap(EnableCap.Blend))
                    {
                        GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo);
                        if (Indexed)
                        {
                            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _ibo);
                        }
                        GL.EnableClientState(ArrayCap.VertexArray);
                        GL.EnableClientState(ArrayCap.ColorArray);
                        GL.EnableClientState(ArrayCap.TextureCoordArray);
                        GLEnableCap blend = null;
                        GL.BindTexture(TextureTarget.Texture2D, Texture);
                        if (Texture != 0)
                        {
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
                        }
                        if (ChangedVerticies.Count != 0 && !_reloadVertices)
                        {
                            unsafe
                            {
                                foreach (var change in ChangedVerticies)
                                {
                                    var vert = vertices[change];
                                    GL.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(Vertex.Size * change),
                                                     new IntPtr(Vertex.Size), ref vert);
                                }
                                ChangedVerticies.Clear();
                            }
                        }
                        if (ChangedIndices.Count != 0 && !_reloadIndices)
                        {
                            unsafe
                            {
                                foreach (var change in ChangedIndices)
                                {
                                    var ind = indices[change];
                                    GL.BufferSubData(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(int) * change), new IntPtr(sizeof(int)), ref ind);
                                }
                                ChangedIndices.Clear();
                            }
                        }
                        if (_reloadVertices)
                        {
                            ChangedVerticies.Clear();
                            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * Vertex.Size), vertices, BufferUsageHint.DynamicDraw);
                            _reloadVertices = false;
                        }

                        if (_reloadIndices && Indexed)
                        {
                            ChangedIndices.Clear();
                            GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(indices.Length * sizeof(int)), indices, BufferUsageHint.StreamDraw);
                            _reloadIndices = false;
                        }
                        if (vCount != 0 && (!Indexed || iCount != 0))
                        {
                            GL.VertexPointer(2, VertexPointerType.Float, Vertex.Size, 0);
                            GL.ColorPointer(4, ColorPointerType.UnsignedByte, Vertex.Size, 8);
                            GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Size, 12);
                            if (Indexed)
                            {
                                GL.DrawElements(mode, iCount, DrawElementsType.UnsignedInt, 0);
                            }
                            else
                            {
                                GL.DrawArrays(mode, 0, vCount);
                            }
                        }
                        GL.BindTexture(TextureTarget.Texture2D, 0);
                        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                        GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                        GL.DisableClientState(ArrayCap.TextureCoordArray);
                        GL.DisableClientState(ArrayCap.ColorArray);
                        GL.DisableClientState(ArrayCap.VertexArray);
                        if (blend != null)
                        {
                            blend.Dispose();
                        }
                    }
            }
        }
Exemplo n.º 2
0
        public static void DrawScarf(DynamicLine[] lines, float opacity)
        {
            GLEnableCap blend = null;

            if (opacity < 1)
            {
                blend = new GLEnableCap(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            }

            GameDrawingMatrix.Enter();
            VAO  scarf = new VAO(false, true);//VAO does not need disposing, it does not allocate a buffer
            bool t     = false;

            for (int i = 0; i < lines.Length; i++)
            {
                Color    c      = Color.FromArgb((byte)(255 * opacity), 209, 1, 1);
                var      alt    = Color.FromArgb((byte)(255 * opacity), 255, 100, 100);
                var      diff   = lines[i].Position2 - lines[i].Position;
                var      dangle = MathHelper.RadiansToDegrees(Math.Atan2(diff.Y, diff.X)) + 90;
                Vector2d q1;
                Vector2d q2;
                Vector2d q3;
                Vector2d q4;
                float    size = 2;
                if (i == 0)
                {
                    var    diff2   = lines[i + 1].Position2 - lines[i + 1].Position;
                    var    dangle2 = MathHelper.RadiansToDegrees(Math.Atan2(diff2.Y, diff2.X)) + 90;
                    Turtle turtle  = new Turtle(lines[i].Position);
                    turtle.Move(dangle2, -(size / 2));
                    q1 = turtle.Point;
                    turtle.Move(0, size);
                    q2           = turtle.Point;
                    turtle.Point = lines[i].Position2;
                    turtle.Move(dangle, size / 2);
                    q3 = turtle.Point;
                    turtle.Move(0, -size);
                    q4 = turtle.Point;
                }
                else
                {
                    var    diff2   = lines[i - 1].Position2 - lines[i - 1].Position;
                    var    dangle2 = MathHelper.RadiansToDegrees(Math.Atan2(diff2.Y, diff2.X)) + 90;
                    Turtle turtle  = new Turtle(lines[i].Position);
                    turtle.Move(dangle2, -(size / 2));
                    q1 = turtle.Point;
                    turtle.Move(0, size);
                    q2           = turtle.Point;
                    turtle.Point = lines[i].Position2;
                    turtle.Move(dangle, size / 2);
                    q3 = turtle.Point;
                    turtle.Move(0, -size);
                    q4 = turtle.Point;
                }
                scarf.AddVertex(new Vertex((Vector2)q1, t ? c : alt));
                scarf.AddVertex(new Vertex((Vector2)q2, t ? c : alt));
                scarf.AddVertex(new Vertex((Vector2)q3, t ? c : alt));
                scarf.AddVertex(new Vertex((Vector2)q4, t ? c : alt));
                t = !t;
            }
            scarf.Draw(PrimitiveType.Quads);
            GameDrawingMatrix.Exit();

            if (blend != null)
            {
                blend.Dispose();
            }
        }