예제 #1
0
        public Mesh Load(Game game, string name)
        {
            unsafe {
                ModelRoot root;
                using (var stream = game.GetResourceStream(name))
                    root = ModelRoot.ReadGLB(stream, new SharpGLTF.Schema2.ReadSettings());
                var primitive = root.LogicalMeshes[0].Primitives[0];

                var indices  = primitive.IndexAccessor;
                var vertices = primitive.VertexAccessors["POSITION"];
                var normals  = primitive.VertexAccessors["NORMAL"];

                var vertexArray = VertexArray.Gen();
                using (vertexArray.Bind()) {
                    Buffer.CreateFromData(indices.SourceBufferView.Content,
                                          BufferTargetARB.ElementArrayBuffer);

                    Buffer.CreateFromData(vertices.SourceBufferView.Content);
                    GFX.GL.EnableVertexAttribArray(POSITION_ATTRIB_INDEX);
                    GFX.GL.VertexAttribPointer(POSITION_ATTRIB_INDEX, 3,
                                               (GLEnum)vertices.Encoding, vertices.Normalized,
                                               (uint)vertices.SourceBufferView.ByteStride, (void *)vertices.ByteOffset);

                    Buffer.CreateFromData(normals.SourceBufferView.Content);
                    GFX.GL.EnableVertexAttribArray(NORMAL_ATTRIB_INDEX);
                    GFX.GL.VertexAttribPointer(NORMAL_ATTRIB_INDEX, 3,
                                               (GLEnum)vertices.Encoding, vertices.Normalized,
                                               (uint)vertices.SourceBufferView.ByteStride, (void *)vertices.ByteOffset);
                }

                var numVertices  = vertices.Count;
                var numTriangles = primitive.IndexAccessor.Count / 3;
                return(new Mesh(vertexArray, numTriangles));
            }
        }
예제 #2
0
        public Mesh Load(Game game, string name)
        {
            unsafe {
                ModelRoot root;
                using (var stream = game.GetResourceStream(name))
                    root = ModelRoot.ReadGLB(stream, new());
                var primitive = root.LogicalMeshes[0].Primitives[0];

                var indices  = primitive.IndexAccessor;
                var vertices = primitive.VertexAccessors["POSITION"];
                var normals  = primitive.VertexAccessors["NORMAL"];

                var vertexArray = VertexArray.Gen();
                using (vertexArray.Bind()) {
                    Buffer.CreateFromData(indices.SourceBufferView.Content,
                                          BufferTargetARB.ElementArrayBuffer);

                    Buffer.CreateFromData(vertices.SourceBufferView.Content);
                    Gfx.Gl.EnableVertexAttribArray(PositionAttribIndex);
                    Gfx.Gl.VertexAttribPointer(PositionAttribIndex, 3,
                                               (VertexAttribPointerType)vertices.Encoding, vertices.Normalized,
                                               (uint)vertices.SourceBufferView.ByteStride, (void *)vertices.ByteOffset);

                    Buffer.CreateFromData(normals.SourceBufferView.Content);
                    Gfx.Gl.EnableVertexAttribArray(NormalAttribIndex);
                    Gfx.Gl.VertexAttribPointer(NormalAttribIndex, 3,
                                               (VertexAttribPointerType)vertices.Encoding, vertices.Normalized,
                                               (uint)vertices.SourceBufferView.ByteStride, (void *)vertices.ByteOffset);
                }

                var numTriangles = primitive.IndexAccessor.Count / 3;
                return(new(vertexArray, numTriangles));
            }
        }