Ejemplo n.º 1
0
        private static void CreateIndexBuffer(Mesh mesh)
        {
            const uint vertexCount = Slices * Stacks;
            const uint indexCount  = vertexCount * 6;
            var        indices     = new UInt32Buffer(indexCount);
            int        count       = 0;

            ushort n = 0;

            for (ushort i = 0; i < Slices; i++)
            {
                for (ushort j = 0; j < Stacks; j++)
                {
                    indices[count++] = (UInt32)((n + j + Stacks) % vertexCount);
                    indices[count++] = (UInt32)(n + (j + 1) % Stacks);
                    indices[count++] = (UInt32)(n + j);

                    indices[count++] = (UInt32)((n + (j + 1) % Stacks + Stacks) % vertexCount);
                    indices[count++] = (UInt32)((n + (j + 1) % Stacks) % vertexCount);
                    indices[count++] = (UInt32)((n + j + Stacks) % vertexCount);
                }

                n += (ushort)Stacks;
            }

            mesh.Indices = indices;
        }
Ejemplo n.º 2
0
        public static Mesh Create1(int gridCount, float width)
        {
            var result = new Mesh();

            var vertices  = new Vector3Buffer(gridCount * gridCount);
            var texCoords = new Vector2Buffer(gridCount * gridCount);

            result.Type      = PrimitiveType.Lines;
            result.TexCoords = texCoords;
            result.Vertices  = vertices;
            result.Normals   = Enumerable
                               .Repeat(new Vector3(0, 1, 0), gridCount * gridCount)
                               .ToArray();

            for (int i = 0; i < gridCount; i++)
            {
                for (int j = 0; j < gridCount; j++)
                {
                    vertices[i * gridCount + j] = 0.5f * width *
                                                  new Vector3(i / (gridCount - 1f) - 0.5f, 0, j / (gridCount - 1f) - 0.5f);
                }
            }
            for (int i = 0; i < gridCount; i++)
            {
                for (int j = 0; j < gridCount; j++)
                {
                    texCoords[i * gridCount + j] = new Vector2(i / (gridCount - 1f), j / (gridCount - 1f));
                }
            }

            int index   = 0;
            var indices = new UInt32Buffer(4 * gridCount * (gridCount - 1));

            for (int j = 0; j < gridCount; j++)
            {
                for (int i = 0; i < gridCount - 1; i++)
                {
                    indices[index++] = (UInt32)(i + j * gridCount);
                    indices[index++] = (UInt32)(i + 1 + j * gridCount);
                }
            }

            for (int j = 0; j < gridCount; j++)
            {
                for (int i = 0; i < gridCount - 1; i++)
                {
                    indices[index++] = (UInt32)(j + i * gridCount);
                    indices[index++] = (UInt32)(j + (i + 1) * gridCount);
                }
            }
            result.Indices = indices;

            result.CalculateBounds();
            return(result);
        }
Ejemplo n.º 3
0
        public Mesh CreateMesh(string text)
        {
            var vertices  = new List <Vector3>(text.Length * 4);
            var texCoords = new List <Vector2>(text.Length * 4);

            var charSize = _fontSize / (float)ImageSize;
            var offset   = new Vector2();

            foreach (var c in text)
            {
                var rect   = GetCharUV(c);
                var width  = rect.Width / charSize;
                var height = rect.Height / charSize;
                var scale  = 0.1f;

                vertices.Add((Vector3)(scale * (new Vector2(0, 0) + offset)));
                vertices.Add((Vector3)(scale * (new Vector2(width, 0) + offset)));
                vertices.Add((Vector3)(scale * (new Vector2(width, height) + offset)));
                vertices.Add((Vector3)(scale * (new Vector2(0, height) + offset)));

                texCoords.Add(new Vector2(rect.X, rect.Y + rect.Height));
                texCoords.Add(new Vector2(rect.X + rect.Width, rect.Y + rect.Height));
                texCoords.Add(new Vector2(rect.X + rect.Width, rect.Y));
                texCoords.Add(new Vector2(rect.X, rect.Y));

                offset.X += width;
            }

            var indices = new UInt32Buffer(text.Length * 6);

            for (int i = 0; i < text.Length; i++)
            {
                indices[i * 6 + 0] = (ushort)(i * 4 + 0);
                indices[i * 6 + 1] = (ushort)(i * 4 + 1);
                indices[i * 6 + 2] = (ushort)(i * 4 + 2);
                indices[i * 6 + 3] = (ushort)(i * 4 + 0);
                indices[i * 6 + 4] = (ushort)(i * 4 + 2);
                indices[i * 6 + 5] = (ushort)(i * 4 + 3);
            }

            var result = new Mesh();

            result.Vertices  = vertices;
            result.TexCoords = texCoords;
            result.Indices   = indices;
            result.CalculateBounds();
            return(result);
        }
Ejemplo n.º 4
0
        public static Mesh Create(Quaternion rotation, Vector2 radiuses, int segments = 20) {
            var vertices = new Vector3Buffer(segments);
            var normals = new Vector3Buffer(segments);
            var indices = new UInt32Buffer(2 * segments);

            for (int i = 0; i < segments; i++) {
                var angle = MathF.PI * 2 * (i / (float)segments);
                var normal = Vector3.Transform(new Vector3(MathF.Sin(angle), MathF.Cos(angle), 0), rotation);
                var point = Vector3.Transform((Vector3)(new Vector2(MathF.Sin(angle), MathF.Cos(angle)) * radiuses), rotation);

                vertices[i] = point;
                indices[2 * i] = (UInt32)i;
                indices[2 * i + 1] = (UInt32)((i + 1) % segments);
                normals[i] = normal;
            }

            return new Mesh() { Normals = normals, Vertices = vertices, Indices = indices, Type = PrimitiveType.Lines };
        }
Ejemplo n.º 5
0
Archivo: Torus.cs Proyecto: xorza/NetGL
        public static Mesh Create(float majorRadius, float minorRadius, int torusPrecision = 48)
        {
            var numVertices = (torusPrecision + 1) * (torusPrecision + 1);
            var numIndices  = 2 * torusPrecision * torusPrecision * 3;

            var vertices = new Vector3Buffer(numVertices);
            var normals  = new Vector3Buffer(numVertices);

            for (int i = 0; i < torusPrecision + 1; i++)
            {
                var sTangent = new Vector3(0.0f, 0.0f, -1.0f);
                var tTangent = (new Vector3(0.0f, -1.0f, 0.0f)).GetRotatedZ(i * 360.0f / torusPrecision);

                var position = (new Vector3(minorRadius, 0.0f, 0.0f)).GetRotatedZ(i * 360.0f / torusPrecision) +
                               new Vector3(majorRadius, 0.0f, 0.0f);
                var normal = Vector3.Cross(tTangent, sTangent);

                vertices[i] = position;
                normals[i]  = normal;
                //vertices[i].s = 0.0f;
                //vertices[i].t = (float)i / torusPrecision;
            }

            for (int ring = 1; ring < torusPrecision + 1; ring++)
            {
                for (int i = 0; i < torusPrecision + 1; i++)
                {
                    vertices[ring * (torusPrecision + 1) + i] = vertices[i].GetRotatedY(ring * 360.0f / torusPrecision);
                    normals[ring * (torusPrecision + 1) + i]  = normals[i].GetRotatedY(ring * 360.0f / torusPrecision);

                    //vertices[ring * (torusPrecision + 1) + i].s = 2.0f * ring / torusPrecision;
                    //vertices[ring * (torusPrecision + 1) + i].t = vertices[i].t;

                    //vertices[ring * (torusPrecision + 1) + i].sTangent =
                    //    vertices[i].sTangent.GetRotatedY(ring * 360.0f / torusPrecision);
                    //vertices[ring * (torusPrecision + 1) + i].tTangent =
                    //    vertices[i].tTangent.GetRotatedY(ring * 360.0f / torusPrecision);
                }
            }

            var indices = new UInt32Buffer(numIndices);

            //  Calculate the indices
            for (int ring = 0; ring < torusPrecision; ring++)
            {
                for (int i = 0; i < torusPrecision; i++)
                {
                    indices[((ring * torusPrecision + i) * 2) * 3 + 0]     = (UInt32)(ring * (torusPrecision + 1) + i);
                    indices[((ring * torusPrecision + i) * 2) * 3 + 1]     = (UInt32)((ring + 1) * (torusPrecision + 1) + i);
                    indices[((ring * torusPrecision + i) * 2) * 3 + 2]     = (UInt32)(ring * (torusPrecision + 1) + i + 1);
                    indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 0] = (UInt32)(ring * (torusPrecision + 1) + i + 1);
                    indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 1] = (UInt32)((ring + 1) * (torusPrecision + 1) + i);
                    indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 2] =
                        (UInt16)((ring + 1) * (torusPrecision + 1) + i + 1);
                }
            }


            var result = new Mesh();

            result.Indices  = indices;
            result.Vertices = vertices;
            result.Normals  = normals;
            result.CalculateBounds();

            return(result);
        }