Esempio n. 1
0
        /// <summary>
        /// Create a sphere.
        /// </summary>
        /// <param name="radius">
        /// A <see cref="Single"/> that specifies the radius of the sphere.
        /// </param>
        /// <param name="slices">
        /// A <see cref="Int32"/> that specifies the number of horizontal subdivisions of the sphere.
        /// </param>
        /// <param name="stacks">
        /// A <see cref="Int32"/> that specifies the number of vertical subdivisions of the sphere.
        /// </param>
        /// <returns>
        /// It returns a <see cref="VertexArrayObject"/> defining the following semantics:
        /// - Positions
        /// - Normals
        /// </returns>
        public static VertexArrayObject CreateSphere(float radius, int slices, int stacks)
        {
            VertexArrayObject vertexArray = new VertexArrayObject();

            // Vertex generation
            Vertex3f[] position, normal;
            ushort[]   indices;
            int        vertexCount;

            GenerateSphere(radius, slices, stacks, out position, out normal, out indices, out vertexCount);

            // Buffer definition
            ArrayBufferObject <Vertex3f> positionBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            positionBuffer.Create(position);
            vertexArray.SetArray(positionBuffer, VertexArraySemantic.Position);

            ArrayBufferObject <Vertex3f> normalBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            normalBuffer.Create(normal);
            vertexArray.SetArray(normalBuffer, VertexArraySemantic.Normal);

            ElementBufferObject <ushort> elementBuffer = new ElementBufferObject <ushort>(BufferObjectHint.StaticCpuDraw);

            elementBuffer.Create(indices);
            vertexArray.SetElementArray(PrimitiveType.TriangleStrip, elementBuffer);

            return(vertexArray);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a <see cref="VertexArrayObject"/> for rendering glyphs.
        /// </summary>
        private void LinkSharedResources(GraphicsContext ctx)
        {
            CheckCurrentContext(ctx);

            string resourceClassId = "OpenGL.Objects.FontPatch";
            string resourceBaseId  = String.Format("{0}.{1}-{2}-{3}", resourceClassId, Family, Size, Style);

            string vertexArrayId = resourceBaseId + ".VertexArray";
            string glyphDbId     = resourceBaseId + ".GlyphDb";

            #region Vertex Arrays

            _VertexArrays = (VertexArrayObject)ctx.GetSharedResource(vertexArrayId);
            Dictionary <char, Glyph> glyphsDb = null;

            if (_VertexArrays == null)
            {
                _VertexArrays = new VertexArrayObject();

                List <GlyphPolygons> glyphPolygons  = GenerateGlyphs(Family, Size, Style);
                List <Vertex2f>      glyphsVertices = new List <Vertex2f>();
                GlyphPolygons        gGlyph         = null;
                uint gVertexIndex = 0;

                glyphsDb = new Dictionary <char, Glyph>();

                using (Tessellator tessellator = new Tessellator()) {
                    tessellator.Begin += delegate(object sender, TessellatorBeginEventArgs e) {
                        gVertexIndex = (uint)glyphsVertices.Count;
                    };
                    tessellator.End += delegate(object sender, EventArgs e) {
                        // Create element (range)
                        int glyphIndex = _VertexArrays.SetElementArray(PrimitiveType.Triangles, gVertexIndex, (uint)glyphsVertices.Count - gVertexIndex);

                        glyphsDb.Add(gGlyph.GlyphChar, new Glyph(gGlyph.GlyphChar, gGlyph.GlyphSize, glyphIndex));
                    };
                    tessellator.Vertex += delegate(object sender, TessellatorVertexEventArgs e) {
                        glyphsVertices.Add((Vertex2f)e.Vertex);
                    };

                    // Tessellate all glyphs
                    foreach (GlyphPolygons glyph in glyphPolygons)
                    {
                        gGlyph = glyph;

                        if (glyph.Contours.Count == 0)
                        {
                            glyphsDb.Add(gGlyph.GlyphChar, new Glyph(gGlyph.GlyphChar, gGlyph.GlyphSize, -1));
                            continue;
                        }

                        tessellator.BeginPolygon();
                        foreach (List <Vertex2f> countour in glyph.Contours)
                        {
                            tessellator.AddContour(countour.ToArray(), Vertex3f.UnitZ);
                        }
                        tessellator.EndPolygon();
                    }
                }

                // Element vertices
                ArrayBufferObject <Vertex2f> gVertexPosition = new ArrayBufferObject <Vertex2f>(BufferObjectHint.StaticCpuDraw);
                gVertexPosition.Create(glyphsVertices.ToArray());
                _VertexArrays.SetArray(gVertexPosition, VertexArraySemantic.Position);

                // Share
                ctx.SetSharedResource(vertexArrayId, _VertexArrays);
            }
            LinkResource(_VertexArrays);

            #endregion

            #region Glyph Metadata

            _Glyphs = (Dictionary <char, Glyph>)ctx.GetSharedResource(glyphDbId);

            if (glyphsDb != null)
            {
                _Glyphs = glyphsDb;
            }
            if (_Glyphs == null)
            {
                throw new InvalidProgramException("no glyph metadata");
            }

            // Share
            ctx.SetSharedResource(glyphDbId, _Glyphs);

            #endregion
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static VertexArrayObject CreatePlane(float x, float y, float z, uint dx, uint dy)
        {
            VertexArrayObject vertexArray = new VertexArrayObject();

            // Vertex generation
            Vertex3f[] position = new Vertex3f[(dx + 1) * (dy + 1)];
            float      x2 = x / 2.0f, y2 = y / 2.0f;
            float      vdx = x / dx, vdy = y / dy;
            int        vidx = 0;

            for (float vy = -y2; vy <= y2; vy += vdy)
            {
                for (float vx = -x2; vx <= x2; vx += vdx)
                {
                    Debug.Assert(vidx < position.Length);
                    position[vidx++] = new Vertex3f(vx, vy, z);
                }
            }

            // Elements generation
            List <uint> indices = new List <uint>();
            uint        vstride = dx + 1;

            for (uint i = 0; i < dy; i++)
            {
                uint yoffset = i * vstride;

                // Triangle strip start
                indices.Add(yoffset + vstride);

                for (uint ix = 0; ix < dx; ix++)
                {
                    uint xoffset = yoffset + ix;

                    indices.Add(xoffset);
                    indices.Add(xoffset + vstride + 1);
                }

                indices.Add(yoffset + vstride - 1);

                if (Gl.CurrentExtensions.PrimitiveRestart == false)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    indices.Add(uint.MaxValue);
                }
            }

            // Buffer definition
            ArrayBufferObject <Vertex3f> positionBuffer = new ArrayBufferObject <Vertex3f>(BufferObjectHint.StaticCpuDraw);

            positionBuffer.Create(position);
            vertexArray.SetArray(positionBuffer, VertexArraySemantic.Position);

            ElementBufferObject <uint> elementBuffer = new ElementBufferObject <uint>(BufferObjectHint.StaticCpuDraw);

            elementBuffer.Create(indices.ToArray());
            elementBuffer.RestartIndexEnabled = Gl.CurrentExtensions.PrimitiveRestart;
            vertexArray.SetElementArray(PrimitiveType.TriangleStrip, elementBuffer);

            return(vertexArray);
        }
Esempio n. 4
0
        /// <summary>
        /// Create a <see cref="VertexArrayObject"/> for rendering glyphs.
        /// </summary>
        /// <param name="fontFamily"></param>
        /// <param name="emSize"></param>
        /// <param name="glyphs"></param>
        /// <returns></returns>
        private static VertexArrayObject CreateVertexArray(GraphicsContext ctx, FontFamily fontFamily, uint emSize, FontStyle fontStyle, out Dictionary <char, Glyph> glyphs)
        {
            CheckCurrentContext(ctx);

            string resourceBaseId = String.Format("OpenGL.Objects.FontPatch.{0}-{1}-{2}", fontFamily, emSize, fontStyle);
            string vertexArrayId  = resourceBaseId + ".VertexArray";
            string glyphDbId      = resourceBaseId + ".GlyphDb";

            VertexArrayObject        vertexArrays = (VertexArrayObject)ctx.GetSharedResource(vertexArrayId);
            Dictionary <char, Glyph> glyphsDb     = (Dictionary <char, Glyph>)ctx.GetSharedResource(glyphDbId);

            if (vertexArrays != null && glyphsDb != null)
            {
                glyphs = glyphsDb;
                return(vertexArrays);
            }

            vertexArrays = new VertexArrayObject();
            glyphsDb     = new Dictionary <char, Glyph>();

            List <GlyphPolygons> glyphPolygons  = GenerateGlyphs(fontFamily, emSize, fontStyle);
            List <Vertex2f>      glyphsVertices = new List <Vertex2f>();
            GlyphPolygons        gGlyph         = null;
            uint gVertexIndex = 0;

            glyphs = new Dictionary <char, Glyph>();

            using (Tessellator tessellator = new Tessellator()) {
                tessellator.Begin += delegate(object sender, TessellatorBeginEventArgs e) {
                    gVertexIndex = (uint)glyphsVertices.Count;
                };
                tessellator.End += delegate(object sender, EventArgs e) {
                    // Create element (range)
                    int glyphIndex = vertexArrays.SetElementArray(PrimitiveType.Triangles, gVertexIndex, (uint)glyphsVertices.Count - gVertexIndex);

                    glyphsDb.Add(gGlyph.GlyphChar, new Glyph(gGlyph.GlyphChar, gGlyph.GlyphSize, glyphIndex));
                };
                tessellator.Vertex += delegate(object sender, TessellatorVertexEventArgs e) {
                    glyphsVertices.Add((Vertex2f)e.Vertex);
                };

                // Tessellate all glyphs
                foreach (GlyphPolygons glyph in glyphPolygons)
                {
                    gGlyph = glyph;

                    if (glyph.Contours.Count == 0)
                    {
                        glyphsDb.Add(gGlyph.GlyphChar, new Glyph(gGlyph.GlyphChar, gGlyph.GlyphSize, -1));
                        continue;
                    }

                    tessellator.BeginPolygon();
                    foreach (List <Vertex2f> countour in glyph.Contours)
                    {
                        tessellator.AddContour(countour.ToArray(), Vertex3f.UnitZ);
                    }
                    tessellator.EndPolygon();
                }
            }

            // Element vertices
            ArrayBufferObject <Vertex2f> gVertexPosition = new ArrayBufferObject <Vertex2f>(BufferObjectHint.StaticCpuDraw);

            gVertexPosition.Create(glyphsVertices.ToArray());
            vertexArrays.SetArray(gVertexPosition, VertexArraySemantic.Position);

            // Returns glyphs database
            glyphs = glyphsDb;

            // Share resources
            ctx.SetSharedResource(vertexArrayId, vertexArrays);
            ctx.SetSharedResource(glyphDbId, glyphsDb);

            return(vertexArrays);
        }
Esempio n. 5
0
        /// <summary>
        /// Create a <see cref="VertexArrayObject"/> for rendering glyphs.
        /// </summary>
        /// <param name="fontFamily"></param>
        /// <param name="emSize"></param>
        /// <param name="glyphs"></param>
        /// <returns></returns>
        private void LinkSharedResources(GraphicsContext ctx)
        {
            CheckCurrentContext(ctx);

            // Font-wide resources
            string resourceClassId = "OpenGL.Objects.FontTextureArray2d";

            string instanceArrayId = resourceClassId + ".InstanceArray";
            string vertexArrayId   = resourceClassId + ".VertexArray";

            _GlyphInstances = (ArrayBufferObjectInterleaved <GlyphInstance>)ctx.GetSharedResource(instanceArrayId);
            _VertexArrays   = (VertexArrayObject)ctx.GetSharedResource(vertexArrayId);

            if (_GlyphInstances == null)
            {
                _GlyphInstances = new ArrayBufferObjectInterleaved <GlyphInstance>(BufferObjectHint.DynamicCpuDraw);
                _GlyphInstances.Create(256);
                // Share
                ctx.SetSharedResource(instanceArrayId, _GlyphInstances);
            }
            LinkResource(_GlyphInstances);

            if (_VertexArrays == null)
            {
                _VertexArrays = new VertexArrayObject();

                ArrayBufferObject <Vertex2f> arrayPosition = new ArrayBufferObject <Vertex2f>(BufferObjectHint.StaticCpuDraw);
                arrayPosition.Create(new Vertex2f[] {
                    new Vertex2f(0.0f, 1.0f),
                    new Vertex2f(0.0f, 0.0f),
                    new Vertex2f(1.0f, 1.0f),
                    new Vertex2f(1.0f, 0.0f),
                });
                _VertexArrays.SetArray(arrayPosition, VertexArraySemantic.Position);
                _VertexArrays.SetInstancedArray(_GlyphInstances, 0, 1, "glo_GlyphModelViewProjection");
                _VertexArrays.SetInstancedArray(_GlyphInstances, 1, 1, "glo_GlyphVertexParams");
                _VertexArrays.SetInstancedArray(_GlyphInstances, 2, 1, "glo_GlyphTexParams");
                _VertexArrays.SetElementArray(PrimitiveType.TriangleStrip);
                // Share
                ctx.SetSharedResource(vertexArrayId, _VertexArrays);
            }
            LinkResource(_VertexArrays);

            // Font resources
            string resourceBaseId = String.Format("{0}.{1}-{2}-{3}", resourceClassId, Family, Size, Style);

            string glyphDbId = resourceBaseId + ".GlyphDb";
            string textureId = resourceBaseId + ".Texture";

            _GlyphDb     = (Dictionary <char, Glyph>)ctx.GetSharedResource(glyphDbId);
            _FontTexture = (TextureArray2d)ctx.GetSharedResource(textureId);

            StringFormat stringFormat = StringFormat.GenericTypographic;

            if (_GlyphDb == null)
            {
                _GlyphDb = new Dictionary <char, Glyph>();

                char[] fontChars = GetFontCharacters().ToCharArray();
                uint   layer     = 0;

                using (Bitmap bitmap = new Bitmap(1, 1))
                    using (Graphics g = Graphics.FromImage(bitmap))
                        using (System.Drawing.Font font = new System.Drawing.Font(Family, Size, Style))
                        {
                            // Avoid grid fitting
                            g.TextRenderingHint = TextRenderingHint.AntiAlias;

                            float glyphHeight = font.GetHeight();

                            foreach (char c in fontChars)
                            {
                                SizeF glyphSize;

                                switch (c)
                                {
                                case ' ':
                                    glyphSize = g.MeasureString(c.ToString(), font, 0, StringFormat.GenericDefault);
                                    break;

                                default:
                                    glyphSize = g.MeasureString(c.ToString(), font, 0, stringFormat);
                                    break;
                                }

                                Glyph glyph = new Glyph(c, glyphSize, layer++, new SizeF(1.0f, 1.0f));

                                _GlyphDb.Add(c, glyph);
                            }
                        }

                // Share
                ctx.SetSharedResource(glyphDbId, _GlyphDb);
            }

            if (_FontTexture == null)
            {
                // Get the size required for all glyphs
                float w = 0.0f, h = 0.0f;
                uint  z = 0;

                foreach (Glyph glyph in _GlyphDb.Values)
                {
                    w = Math.Max(w, glyph.GlyphSize.Width);
                    h = Math.Max(h, glyph.GlyphSize.Height);
                    z = Math.Max(z, glyph.Layer);
                }

                // Create texture
                _FontTexture = new TextureArray2d((uint)Math.Ceiling(w), (uint)Math.Ceiling(h), z + 1, PixelLayout.R8);
                _FontTexture.Create(ctx);

                using (System.Drawing.Font font = new System.Drawing.Font(Family, Size, Style))
                    using (Brush brush = new SolidBrush(Color.White))
                    {
                        foreach (Glyph glyph in _GlyphDb.Values)
                        {
                            using (Bitmap bitmap = new Bitmap((int)_FontTexture.Width, (int)_FontTexture.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                                using (Graphics g = Graphics.FromImage(bitmap)) {
                                    // Recompute texture scaling
                                    glyph.TexScale = new SizeF(
                                        glyph.GlyphSize.Width / bitmap.Width,
                                        glyph.GlyphSize.Height / bitmap.Height
                                        );

                                    // Avoid grid fitting
                                    g.TextRenderingHint = TextRenderingHint.AntiAlias;
                                    g.Clear(Color.Black);
                                    g.DrawString(glyph.GlyphChar.ToString(), font, brush, 0.0f, 0.0f, stringFormat);

                                    _FontTexture.Create(ctx, PixelLayout.R8, bitmap, glyph.Layer);
                                }
                        }
                    }

                // Share
                ctx.SetSharedResource(textureId, _FontTexture);
            }
            LinkResource(_FontTexture);
        }