Beispiel #1
0
        public static Mesh Deserialize(BinaryReader reader)
        {
            Action<bool> assert = (b) => { if (!b) throw new InvalidDataException(); };
            assert(reader.ReadString() == "MESH");
            assert(reader.ReadByte() == 1);
            assert(reader.ReadByte() == 0);
            int texture = reader.ReadInt32();
            int vertexCount = reader.ReadInt32();
            int indexCount = reader.ReadInt32();

            var vertices = new BlockVertex[vertexCount];
            for (int i = 0; i < vertexCount; i++)
            {
                var vertex = new BlockVertex();

                vertex.position = reader.ReadVector3();
                vertex.normal = reader.ReadVector3();
                vertex.color = reader.ReadVector3();
                vertex.uv = reader.ReadVector3();

                vertices[i] = vertex;
            }

            var indices = new int[indexCount];
            for (int i = 0; i < indexCount; i++)
            {
                indices[i] = reader.ReadInt32();
            }

            return new Mesh(indices, vertices, texture);
        }
Beispiel #2
0
        public Mesh(int[] indices, BlockVertex[] vertices, int texture)
        {
            this.texture = texture;
            this.indexCount = indices.Length;
            this.vertexCount = vertices.Length;

            if ((this.indexCount == 0) || (this.vertexCount == 0))
                throw new ArgumentException();

            this.vertices = vertices;
            this.indices = indices;

            this.vao = GL.GenVertexArray();
            this.vertexBuffer = GL.GenBuffer();
            this.indexBuffer = GL.GenBuffer();

            // Enforce passed texture
            this.SetTexture(this.texture, false);

            // Initialize vertex array
            InitializeVertexArrayObject();

            // Initialize vertex buffer
            UploadVertices();

            // Initialize index buffer
            UploadIndices();
        }
Beispiel #3
0
        public static MeshModel Load(Stream stream, string formatHint)
        {
            using (AssimpContext importer = new AssimpContext())
            {
                var scene = importer.ImportFileFromStream(
                    stream,
                    PostProcessSteps.Triangulate |
                    // PostProcessSteps.PreTransformVertices |
                    PostProcessSteps.GenerateUVCoords |
                    PostProcessSteps.GenerateNormals,
                    formatHint);

                List <Mesh> meshes = new List <Mesh>();
                for (int i = 0; i < scene.MeshCount; i++)
                {
                    var mesh    = scene.Meshes[i];
                    var indices = mesh.GetIndices();

                    int texture = 6;

                    BlockVertex[] vertices = new BlockVertex[mesh.VertexCount];
                    var           src      = mesh.Vertices;
                    var           normals  = mesh.HasNormals ? mesh.Normals : null;
                    var           uvs      = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0] : null;
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        vertices[v].position = src[v].TK();
                        vertices[v].color    = Vector3.One;
                        if (normals != null)
                        {
                            vertices[v].normal = normals[v].TK();
                        }
                        if (uvs != null)
                        {
                            vertices[v].uv = uvs[v].TK();
                        }
                        else
                        {
                            vertices[v].uv = 0.5f * Vector3.One;
                        }

                        // Set texture ID of the model
                        vertices[v].uv.Z = texture;
                    }

                    meshes.Add(new Mesh(indices, vertices, texture));
                }

                return(new MeshModel(meshes));
            }
        }
Beispiel #4
0
        private void InitializeVertexArrayObject()
        {
            GL.BindVertexArray(this.vao);
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBuffer);

                BlockVertex.InitializeVertexBinding();

                GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.indexBuffer);
            }
            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }
Beispiel #5
0
        internal void Load()
        {
            this.vao          = GL.GenVertexArray();
            this.vertexBuffer = GL.GenBuffer();

            GL.BindVertexArray(this.vao);
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBuffer);
                BlockVertex.InitializeVertexBinding();
            }
            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            this.UpdateVertexBuffer();
        }
Beispiel #6
0
        public static Mesh Deserialize(BinaryReader reader)
        {
            Action <bool> assert = (b) => { if (!b)
                                            {
                                                throw new InvalidDataException();
                                            }
            };

            assert(reader.ReadString() == "MESH");
            assert(reader.ReadByte() == 1);
            assert(reader.ReadByte() == 0);
            int texture     = reader.ReadInt32();
            int vertexCount = reader.ReadInt32();
            int indexCount  = reader.ReadInt32();

            var vertices = new BlockVertex[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                var vertex = new BlockVertex();

                vertex.position = reader.ReadVector3();
                vertex.normal   = reader.ReadVector3();
                vertex.color    = reader.ReadVector3();
                vertex.uv       = reader.ReadVector3();

                vertices[i] = vertex;
            }

            var indices = new int[indexCount];

            for (int i = 0; i < indexCount; i++)
            {
                indices[i] = reader.ReadInt32();
            }

            return(new Mesh(indices, vertices, texture));
        }
Beispiel #7
0
        public static MeshModel Load(Stream stream, string formatHint)
        {
            using (AssimpContext importer = new AssimpContext())
            {
                var scene = importer.ImportFileFromStream(
                    stream,
                    PostProcessSteps.Triangulate |
                    // PostProcessSteps.PreTransformVertices |
                    PostProcessSteps.GenerateUVCoords |
                    PostProcessSteps.GenerateNormals,
                    formatHint);

                List<Mesh> meshes = new List<Mesh>();
                for (int i = 0; i < scene.MeshCount; i++)
                {
                    var mesh = scene.Meshes[i];
                    var indices = mesh.GetIndices();

                    int texture = 6;

                    BlockVertex[] vertices = new BlockVertex[mesh.VertexCount];
                    var src = mesh.Vertices;
                    var normals = mesh.HasNormals ? mesh.Normals : null;
                    var uvs = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0] : null;
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        vertices[v].position = src[v].TK();
                        vertices[v].color = Vector3.One;
                        if (normals != null)
                            vertices[v].normal = normals[v].TK();
                        if (uvs != null)
                            vertices[v].uv = uvs[v].TK();
                        else
                            vertices[v].uv = 0.5f * Vector3.One;

                        // Set texture ID of the model
                        vertices[v].uv.Z = texture;
                    }

                    meshes.Add(new Mesh(indices, vertices, texture));
                }

                return new MeshModel(meshes);
            }
        }