Пример #1
0
        public static Onyx3D.Mesh ToOnyx3D(this Assimp.Mesh mesh)
        {
            Onyx3D.Mesh newMesh = new Onyx3D.Mesh();

            newMesh.Indices = mesh.GetIndices();
            for (int vi = 0; vi < mesh.VertexCount; ++vi)
            {
                Vertex newVertex = new Vertex();
                newVertex.Position = mesh.Vertices[vi].ToOnyx3D();

                if (mesh.HasTextureCoords(0))
                {
                    Assimp.Vector3D texCoord = mesh.TextureCoordinateChannels[0][vi];
                    newVertex.TexCoord = texCoord.ToOnyx3D().Xy;
                }
                if (mesh.HasNormals)
                {
                    newVertex.Normal = mesh.Normals[vi].ToOnyx3D().Normalized();
                    if (mesh.HasTangentBasis)
                    {
                        newVertex.Bitangent = mesh.BiTangents[vi].ToOnyx3D().Normalized();
                        newVertex.Tangent   = mesh.Tangents[vi].ToOnyx3D().Normalized();
                    }
                }


                newMesh.Vertices.Add(newVertex);
            }

            newMesh.GenerateVAO();
            return(newMesh);
        }
Пример #2
0
        public Mesh(Assimp.Mesh mesh)
        {
            Assign();

            bbox = new BoundingBox(Vector3.PositiveInfinity, Vector3.NegativeInfinity);

            GL.BindVertexArray(Vao);
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);

            int[] indices = mesh.GetIndices();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, Ebo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw);
            iCount = indices.Length;

            // positions + normals
            float[] vertexData = new float[mesh.VertexCount * 6];
            for (int i = 0; i < mesh.VertexCount; ++i)
            {
                vertexData[i * 6 + 0] = mesh.Vertices[i].X;
                vertexData[i * 6 + 1] = mesh.Vertices[i].Y;
                vertexData[i * 6 + 2] = mesh.Vertices[i].Z;

                bbox.MinPoint.X = Math.Min(bbox.MinPoint.X, mesh.Vertices[i].X);
                bbox.MinPoint.Y = Math.Min(bbox.MinPoint.Y, mesh.Vertices[i].Y);
                bbox.MinPoint.Z = Math.Min(bbox.MinPoint.Z, mesh.Vertices[i].Z);
                bbox.MaxPoint.X = Math.Max(bbox.MaxPoint.X, mesh.Vertices[i].X);
                bbox.MaxPoint.Y = Math.Max(bbox.MaxPoint.Y, mesh.Vertices[i].Y);
                bbox.MaxPoint.Z = Math.Max(bbox.MaxPoint.Z, mesh.Vertices[i].Z);

                try
                {
                    vertexData[i * 6 + 3] = mesh.Normals[i].X;
                    vertexData[i * 6 + 4] = mesh.Normals[i].Y;
                    vertexData[i * 6 + 5] = mesh.Normals[i].Z;
                }
                catch (System.ArgumentOutOfRangeException)
                {
                    System.Console.Error.WriteLine("Failed indexing mesh normals");
                    throw;
                }
            }

            GL.BindBuffer(BufferTarget.ArrayBuffer, Vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * vertexData.Length, vertexData, BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 24, new IntPtr(0));
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 24, new IntPtr(12));

            vCount = mesh.VertexCount;
        }
Пример #3
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);
        }