/// <summary>
        ///  Ucitavanje podataka o sceni iz odgovarajuceg fajla.
        /// </summary>
        private void LoadScene()
        {
            // Instanciranje klase za ucitavanje podataka o sceni.
            AssimpImporter importer = new AssimpImporter();

            // Definisanje callback delegata za belezenje poruka u toku ucitavanja podataka o sceni.
            LogStream logstream = new LogStream(delegate(String msg, String userData)
            {
                Console.WriteLine(msg);
            });
            importer.AttachLogStream(logstream);

            // Ucitavanje podataka o sceni iz odgovarajuceg fajla.
            m_scene = importer.ImportFile(Path.Combine(m_scenePath, m_sceneFileName), PostProcessPreset.TargetRealTimeMaximumQuality);

            // Oslobadjanje resursa koriscenih za ucitavanje podataka o sceni.
            importer.Dispose();
        }
Beispiel #2
0
        public static MeshResource Load(string filePath)
        {
            AssimpImporter importer = new AssimpImporter();
            importer.SetConfig(new NormalizeVertexComponentsConfig(true));
            importer.SetConfig(new MultithreadingConfig(-1));

            LogStream logStream = new LogStream(delegate(String msg, String userdata)
            {
                Log.Message(msg);
                Log.Message(userdata);
            });
            importer.AttachLogStream(logStream);

            Assimp.Scene model = importer.ImportFile(filePath, PostProcessPreset.TargetRealTimeMaximumQuality | PostProcessSteps.FlipUVs);
            MeshResource meshResource = new MeshResource();
            meshResource.Materials = model.Materials.Select(m => (BaseMaterial)new TexturedMaterial(m, Directory.GetParent(filePath).FullName)).ToList();

            foreach (var modelMesh in model.Meshes)
            {
                EntityBuffer buffer = new EntityBuffer();
                List<ushort> indices = new List<ushort>();
                List<Vertex> vertices = new List<Vertex>();
                Vector3D[] texCoords = modelMesh.HasTextureCoords(0) ? modelMesh.GetTextureCoords(0) : null;

                foreach (var face in modelMesh.Faces)
                {
                    for (int i = 0; i < face.IndexCount; i++)
                    {
                        indices.Add((ushort)face.Indices[i]);
                    }
                }

                var material = model.Materials[modelMesh.MaterialIndex];
                TexturedMaterial texMat = new TexturedMaterial(material, Directory.GetParent(filePath).FullName);

                for (int i = 0; i < modelMesh.VertexCount; i++)
                {
                    var vertex = modelMesh.Vertices[i];
                    var texCoord = texCoords[i];
                    var normal = modelMesh.Normals[i];

                    vertices.Add(new Vertex(new Position3(vertex.X, vertex.Y, vertex.Z), new Position2(texCoord.X, texCoord.Y), new Position3(normal.X, normal.Y, normal.Z)));
                }

                buffer.SetVertices(vertices, indices);

                Mesh mesh = new Mesh(buffer);
                meshResource.Meshes.Add(modelMesh.MaterialIndex, mesh);
            }

            importer.Dispose();

            return meshResource;
        }