예제 #1
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);
        }
예제 #2
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);
        }