Пример #1
0
        public static SceneMesh CreateMesh(Assimp.Mesh resource)
        {
            var mesh = new SceneMesh();

            mesh.MaterialIndex = resource.MaterialIndex;
            var vertices = GetVertices(resource, out int stride, out VertexBounds bounds);

            mesh.Bounds      = new Bounds(bounds.Center, bounds.Extents);
            mesh.VertexCount = resource.VertexCount;
            mesh.DrawMode    = PrimitiveType.Triangles;
            GL.BindVertexArray(mesh.VertexArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, mesh.VertexBuffer);
            GL.BufferData(BufferTarget.ArrayBuffer,
                          new IntPtr(vertices.Length * BlittableValueType <float> .Stride),
                          vertices, BufferUsageHint.StaticDraw);
            if (resource.HasFaces)
            {
                mesh.EnsureElementArray();
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.ElementArray);
                if (resource.VertexCount <= short.MaxValue)
                {
                    var indices = resource.GetShortIndices();
                    mesh.VertexCount      = indices.Length;
                    mesh.ElementArrayType = DrawElementsType.UnsignedShort;
                    GL.BufferData(BufferTarget.ElementArrayBuffer,
                                  new IntPtr(indices.Length * BlittableValueType <short> .Stride),
                                  indices, BufferUsageHint.StaticDraw);
                }
                else
                {
                    var indices = resource.GetIndices();
                    mesh.VertexCount      = indices.Length;
                    mesh.ElementArrayType = DrawElementsType.UnsignedInt;
                    GL.BufferData(BufferTarget.ElementArrayBuffer,
                                  new IntPtr(indices.Length * BlittableValueType <int> .Stride),
                                  indices, BufferUsageHint.StaticDraw);
                }
            }

            var attrib = 0;
            var offset = 0;

            if (resource.HasVertices)
            {
                offset = PushAttribArray(attrib++, ElementSizeVector3D, stride, offset);
            }
            for (int k = 0; k < resource.TextureCoordinateChannelCount; k++)
            {
                offset = PushAttribArray(attrib++, resource.UVComponentCount[k], stride, offset);
            }
            for (int c = 0; c < resource.VertexColorChannelCount; c++)
            {
                offset = PushAttribArray(attrib++, ElementSizeColor4D, stride, offset);
            }
            if (resource.HasNormals)
            {
                PushAttribArray(attrib++, ElementSizeVector3D, stride, offset);
            }

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindVertexArray(0);
            return(mesh);
        }