示例#1
0
        public Mesh(GeometryData data)
        {
            VertexCount = data.VertexCount;

            VertexArrayObject = GL.GenVertexArray();

            GL.BindVertexArray(VertexArrayObject);

            Buffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, Buffer);
            GL.BufferData(BufferTarget.ArrayBuffer, data.Data.Length, data.Data, BufferUsageHint.StaticDraw);

            ElementBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBuffer);
            GL.BufferData(BufferTarget.ElementArrayBuffer, data.Indices.Length * 4, data.Indices, BufferUsageHint.StaticDraw);

            MeshHelper.ApplyVertexAttribs(data.Attribs);

            ElementBufferSize = data.Indices.Length;

            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }
示例#2
0
        public static GeometryData FromVertices(List <PositionVertex> vertices, List <int> indices)
        {
            var result = new GeometryData()
            {
                VertexCount = vertices.Count,
                Indices     = indices.ToArray()
            };

            using (var stream = new System.IO.MemoryStream())
                using (var writer = new System.IO.BinaryWriter(stream))
                {
                    for (int i = 0; i < vertices.Count; i++)
                    {
                        writer.Write(vertices[i].PosX);
                        writer.Write(vertices[i].PosY);
                        writer.Write(vertices[i].PosZ);
                    }

                    result.Data = stream.ToArray();
                }

            // add vertex attribs
            result.Attribs = new VertexAttrib[]
            {
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 3 * sizeof(float),
                    Offset = 0
                }
            };

            return(result);
        }
示例#3
0
        public Mesh(GeometryData data)
        {
            VertexCount = data.VertexCount;

            VertexArrayObject = GL.GenVertexArray();

            if (VertexArrayObject == 0)
            {
                throw new Exception(GL.GetError().ToString());
            }

            GL.BindVertexArray(VertexArrayObject);

            Buffer = GL.GenBuffer();

            if (Buffer == 0)
            {
                throw new Exception(GL.GetError().ToString());
            }

            GL.BindBuffer(BufferTarget.ArrayBuffer, Buffer);
            GL.BufferData(BufferTarget.ArrayBuffer, data.Data.Length, data.Data, BufferUsageHint.StaticDraw);

            ElementBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBuffer);
            GL.BufferData(BufferTarget.ElementArrayBuffer, data.Indices.Length * 4, data.Indices, BufferUsageHint.StaticDraw);

            MeshHelper.ApplyVertexAttribs(data.Attribs, InstanceBuffer);

            ElementBufferSize = data.Indices.Length;

            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }
示例#4
0
 public SvgMesh(GeometryData data) : base(data)
 {
     Shader = ShaderManager.LoadShader <PositionColorShader>();
 }
示例#5
0
        public static GeometryData CreateXYPlane(float[] bindShapeMatrix = null)
        {
            float[] vertices = new float[] {
                -1, -1, 0,    // bottom left corner
                -1, 1, 0,     // top left corner
                1, 1, 0,      // top right corner
                1, -1, 0
            };
            int[] indices = new int[] {
                0, 1, 2,      // first triangle (bottom left - top left - top right)
                0, 2, 3       // second triangle (bottom left - top right - bottom right)
            };
            float[] normals = new float[] {
                0, 0, 1
            };
            int[] normalIndices = new int[] {
                0, 0, 0,      // first triangle (bottom left - top left - top right)
                0, 0, 0       // second triangle (bottom left - top right - bottom right)
            };
            float[] uv = new float[]
            {
                1, 1,
                1, 0,
                0, 0,
                0, 1
            };
            int[] uvIndices = new int[] {
                0, 1, 2,      // first triangle (bottom left - top left - top right)
                0, 2, 3       // second triangle (bottom left - top right - bottom right)
            };

            var data        = new PositionNormalUV0Vertex[indices.Length];
            var meshIndices = new int[indices.Length];

            for (var i = 0; i < indices.Length; i++)
            {
                var vertex = (bindShapeMatrix != null) ?
                             new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                }.VectorTransform(bindShapeMatrix)
                    : new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                };

                data[i].PosX    = vertex[0];
                data[i].PosY    = vertex[1];
                data[i].PosZ    = vertex[2];
                data[i].NormalX = normals[normalIndices[i] * 3];
                data[i].NormalY = normals[normalIndices[i] * 3 + 1];
                data[i].NormalZ = normals[normalIndices[i] * 3 + 2];
                data[i].UvX     = uv[uvIndices[i] * 2];
                data[i].UvY     = uv[uvIndices[i] * 2 + 1];

                meshIndices[i] = i;
            }

            var result = new GeometryData()
            {
                VertexCount = data.Length,
                Indices     = meshIndices
            };

            using (var stream = new System.IO.MemoryStream())
                using (var writer = new System.IO.BinaryWriter(stream))
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        writer.Write(data[i].PosX);
                        writer.Write(data[i].PosY);
                        writer.Write(data[i].PosZ);
                        writer.Write(data[i].NormalX);
                        writer.Write(data[i].NormalY);
                        writer.Write(data[i].NormalZ);
                        writer.Write(data[i].UvX);
                        writer.Write(data[i].UvY);
                    }

                    result.Data = stream.ToArray();
                }

            // add vertex attribs
            result.Attribs = new VertexAttrib[]
            {
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 0
                },
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 3 * sizeof(float)
                },
                new VertexAttrib()
                {
                    Size   = 2,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 6 * sizeof(float)
                }
            };

            return(result);
        }
示例#6
0
        public static GeometryData FromCollada(float[] vertices, int[] indices, int indicesPerFace, float[] normals, int[] normalIndices, float[] uv, int[] uvIndices, List <Dictionary <string, float> > vertexWeights, List <Bone> bones, float[] bindShapeMatrix = null)
        {
            var data        = new PositionNormalUV0SkinVertex[indices.Length];
            var meshIndices = new int[indices.Length];

            for (var i = 0; i < indices.Length; i++)
            {
                var vertex = (bindShapeMatrix != null) ?
                             new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                }.VectorTransform(bindShapeMatrix)
                    : new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                };

                data[i].PosX    = vertex[0];
                data[i].PosY    = vertex[1];
                data[i].PosZ    = vertex[2];
                data[i].NormalX = normals[normalIndices[i] * 3];
                data[i].NormalY = normals[normalIndices[i] * 3 + 1];
                data[i].NormalZ = normals[normalIndices[i] * 3 + 2];
                data[i].UvX     = uv[uvIndices[i] * 2];
                data[i].UvY     = 1f - uv[uvIndices[i] * 2 + 1];

                // skinning
                var weight = 0;

                foreach (var b in vertexWeights[indices[i]].OrderByDescending(a => a.Value).Select(a => a.Key))
                {
                    if (vertexWeights[indices[i]][b] > 0)
                    {
                        if (weight == 0)
                        {
                            data[i].BoneId1 = bones.TakeWhile(a => a.Id != b).Count();
                            data[i].Weight1 = vertexWeights[indices[i]][b];
                        }
                        else if (weight == 1)
                        {
                            data[i].BoneId2 = bones.TakeWhile(a => a.Id != b).Count();
                            data[i].Weight2 = vertexWeights[indices[i]][b];
                        }
                        else if (weight == 2)
                        {
                            data[i].BoneId3 = bones.TakeWhile(a => a.Id != b).Count();
                            data[i].Weight3 = vertexWeights[indices[i]][b];
                        }
                        else if (weight == 3)
                        {
                            data[i].BoneId4 = bones.TakeWhile(a => a.Id != b).Count();
                            data[i].Weight4 = vertexWeights[indices[i]][b];
                        }

                        weight += 1;
                    }
                }

                // normalize weights
                var totalWeights = data[i].Weight1 + data[i].Weight2 + data[i].Weight3 + data[i].Weight4;
                if (totalWeights != 1f)
                {
                    var normalizedWeight = 1.0f / totalWeights;
                    data[i].Weight1 *= normalizedWeight;
                    data[i].Weight2 *= normalizedWeight;
                    data[i].Weight3 *= normalizedWeight;
                    data[i].Weight4 *= normalizedWeight;
                }

                meshIndices[i] = i;
            }

            // check if we need  to triangulate the mesh
            if (indicesPerFace == 4)
            {
                meshIndices = MeshHelper.QuadIndicesToTriangles(meshIndices);
            }

            var result = new GeometryData()
            {
                VertexCount = data.Length,
                Indices     = meshIndices
            };

            using (var stream = new System.IO.MemoryStream())
                using (var writer = new System.IO.BinaryWriter(stream))
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        writer.Write(data[i].PosX);
                        writer.Write(data[i].PosY);
                        writer.Write(data[i].PosZ);
                        writer.Write(data[i].NormalX);
                        writer.Write(data[i].NormalY);
                        writer.Write(data[i].NormalZ);
                        writer.Write(data[i].UvX);
                        writer.Write(data[i].UvY);
                        writer.Write(data[i].BoneId1);
                        writer.Write(data[i].BoneId2);
                        writer.Write(data[i].BoneId3);
                        writer.Write(data[i].BoneId4);
                        writer.Write(data[i].Weight1);
                        writer.Write(data[i].Weight2);
                        writer.Write(data[i].Weight3);
                        writer.Write(data[i].Weight4);
                    }

                    result.Data = stream.ToArray();
                }

            // add vertex attribs
            result.Attribs = new VertexAttrib[]
            {
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 16 * sizeof(float),
                    Offset = 0
                },
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 16 * sizeof(float),
                    Offset = 3 * sizeof(float)
                },
                new VertexAttrib()
                {
                    Size   = 2,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 16 * sizeof(float),
                    Offset = 6 * sizeof(float)
                },
                new VertexAttrib()
                {
                    Size   = 4,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribIntegerType.Int,
                    Stride = 16 * sizeof(float),
                    Offset = 8 * sizeof(float)
                },
                new VertexAttrib()
                {
                    Size   = 4,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 16 * sizeof(float),
                    Offset = 12 * sizeof(float)
                }
            };

            return(result);
        }
示例#7
0
        public static GeometryData FromCollada(float[] vertices, int[] indices, int indicesPerFace, float[] normals, int[] normalIndices, float[] uv, int[] uvIndices, float[] bindShapeMatrix = null)
        {
            var data        = new PositionNormalUV0Vertex[indices.Length];
            var meshIndices = new int[indices.Length];

            for (var i = 0; i < indices.Length; i++)
            {
                var vertex = (bindShapeMatrix != null) ?
                             new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                }.VectorTransform(bindShapeMatrix)
                    : new float[] {
                    vertices[indices[i] * 3],
                    vertices[indices[i] * 3 + 1],
                    vertices[indices[i] * 3 + 2]
                };

                data[i].PosX    = vertex[0];
                data[i].PosY    = vertex[1];
                data[i].PosZ    = vertex[2];
                data[i].NormalX = normals[normalIndices[i] * 3];
                data[i].NormalY = normals[normalIndices[i] * 3 + 1];
                data[i].NormalZ = normals[normalIndices[i] * 3 + 2];
                data[i].UvX     = uv[uvIndices[i] * 2];
                data[i].UvY     = 1f - uv[uvIndices[i] * 2 + 1];

                meshIndices[i] = i;
            }

            // check if we need  to triangulate the mesh
            if (indicesPerFace == 4)
            {
                meshIndices = QuadIndicesToTriangles(meshIndices);
            }

            var result = new GeometryData()
            {
                VertexCount = data.Length,
                Indices     = meshIndices
            };

            using (var stream = new System.IO.MemoryStream())
                using (var writer = new System.IO.BinaryWriter(stream))
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        writer.Write(data[i].PosX);
                        writer.Write(data[i].PosY);
                        writer.Write(data[i].PosZ);
                        writer.Write(data[i].NormalX);
                        writer.Write(data[i].NormalY);
                        writer.Write(data[i].NormalZ);
                        writer.Write(data[i].UvX);
                        writer.Write(data[i].UvY);
                    }

                    result.Data = stream.ToArray();
                }

            // add vertex attribs
            result.Attribs = new VertexAttrib[]
            {
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 0
                },
                new VertexAttrib()
                {
                    Size   = 3,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 3 * sizeof(float)
                },
                new VertexAttrib()
                {
                    Size   = 2,
                    Type   = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float,
                    Stride = 8 * sizeof(float),
                    Offset = 6 * sizeof(float)
                }
            };

            return(result);
        }
示例#8
0
 public InstancedMesh(GeometryData data) : base(data)
 {
 }
示例#9
0
 public SkinnedMesh(GeometryData data, List <Bone> bones)
     : base(data)
 {
     Bones = ((Bone[])bones.ToArray().Clone()).ToList();
 }