Exemple #1
0
            private void ReadVertices(BinaryReader reader)
            {
                var curPos = reader.BaseStream.Position;

                reader.BaseStream.Seek((long)DataOffset, SeekOrigin.Begin);
                //var data = reader.ReadBytes((int) vbHeader.SizeBytes);
                for (ulong i = 0; i < NumVertices; i++)
                {
                    var vertex = new PosNormalTexTan();
                    foreach (var element in Decl)
                    {
                        switch (element.Type)
                        {
                        case DeclarationType.Float3:
                            var v3 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                            switch (element.Usage)
                            {
                            case DeclarationUsage.Position:
                                vertex.Pos = v3;
                                break;

                            case DeclarationUsage.Normal:
                                vertex.Normal = v3;
                                break;

                            case DeclarationUsage.Tangent:
                                vertex.Tan = v3;
                                break;
                            }
                            //Console.WriteLine("{0} - {1}", element.Usage, v3);
                            break;

                        case DeclarationType.Float2:
                            var v2 = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                            switch (element.Usage)
                            {
                            case DeclarationUsage.TextureCoordinate:
                                vertex.Tex = v2;
                                break;
                            }
                            //Console.WriteLine("{0} - {1}", element.Usage, v2);
                            break;
                        }
                    }
                    Vertices.Add(vertex);
                }
                reader.BaseStream.Position = curPos;
            }
Exemple #2
0
            public SdkMeshVertexBufferHeader(BinaryReader reader)
            {
                NumVertices = reader.ReadUInt64();
                SizeBytes   = reader.ReadUInt64();
                StrideBytes = reader.ReadUInt64();
                Decl        = new List <VertexElement>();
                var processElem = true;

                for (int j = 0; j < MaxVertexElements; j++)
                {
                    var stream     = reader.ReadUInt16();
                    var offset     = reader.ReadUInt16();
                    var type       = reader.ReadByte();
                    var method     = reader.ReadByte();
                    var usage      = reader.ReadByte();
                    var usageIndex = reader.ReadByte();
                    if (stream < 8 && processElem)
                    {
                        var element = new VertexElement((short)stream, (short)offset, (DeclarationType)type, (DeclarationMethod)method, (DeclarationUsage)usage, usageIndex);
                        Decl.Add(element);
                    }
                    else
                    {
                        processElem = false;
                    }
                }
                DataOffset = reader.ReadUInt64();
                Vertices   = new List <PosNormalTexTan>();
                if (SizeBytes > 0)
                {
                    var curPos = reader.BaseStream.Position;
                    reader.BaseStream.Seek((long)DataOffset, SeekOrigin.Begin);
                    //var data = reader.ReadBytes((int) vbHeader.SizeBytes);
                    for (ulong i = 0; i < NumVertices; i++)
                    {
                        var vertex = new PosNormalTexTan();
                        foreach (var element in Decl)
                        {
                            switch (element.Type)
                            {
                            case DeclarationType.Float3:
                                var v3 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                                switch (element.Usage)
                                {
                                case DeclarationUsage.Position:
                                    vertex.Pos = v3;
                                    break;

                                case DeclarationUsage.Normal:
                                    vertex.Normal = v3;
                                    break;

                                case DeclarationUsage.Tangent:
                                    vertex.Tan = v3;
                                    break;
                                }
                                //Console.WriteLine("{0} - {1}", element.Usage, v3);
                                break;

                            case DeclarationType.Float2:
                                var v2 = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                                switch (element.Usage)
                                {
                                case DeclarationUsage.TextureCoordinate:
                                    vertex.Tex = v2;
                                    break;
                                }
                                //Console.WriteLine("{0} - {1}", element.Usage, v2);
                                break;
                            }
                        }
                        Vertices.Add(vertex);
                    }
                    reader.BaseStream.Position = curPos;
                }
            }
Exemple #3
0
 private void ReadVertices(BinaryReader reader) {
     var curPos = reader.BaseStream.Position;
     reader.BaseStream.Seek((long)DataOffset, SeekOrigin.Begin);
     //var data = reader.ReadBytes((int) vbHeader.SizeBytes);
     for (ulong i = 0; i < NumVertices; i++) {
         var vertex = new PosNormalTexTan();
         foreach (var element in Decl) {
             switch (element.Type) {
                 case DeclarationType.Float3:
                     var v3 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                     switch (element.Usage) {
                         case DeclarationUsage.Position:
                             vertex.Pos = v3;
                             break;
                         case DeclarationUsage.Normal:
                             vertex.Normal = v3;
                             break;
                         case DeclarationUsage.Tangent:
                             vertex.Tan = v3;
                             break;
                     }
                     //Console.WriteLine("{0} - {1}", element.Usage, v3);
                     break;
                 case DeclarationType.Float2:
                     var v2 = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                     switch (element.Usage) {
                         case DeclarationUsage.TextureCoordinate:
                             vertex.Tex = v2;
                             break;
                     }
                     //Console.WriteLine("{0} - {1}", element.Usage, v2);
                     break;
             }
         }
         Vertices.Add(vertex);
     }
     reader.BaseStream.Position = curPos;
 }
Exemple #4
0
        public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath)
        {
            _subsets = new List<MeshGeometry.Subset>();
            _vertices = new List<PosNormalTexTan>();
            _indices = new List<short>();
            DiffuseMapSRV = new List<ShaderResourceView>();
            NormalMapSRV = new List<ShaderResourceView>();
            Materials = new List<Material>();
            _modelMesh = new MeshGeometry();

            var importer = new AssimpImporter();
            if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) {
                throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported!  Cannot load {1}", "filename");
            }
            #if DEBUG

            importer.AttachLogStream(new ConsoleLogStream());
            importer.VerboseLoggingEnabled = true;
            #endif
            var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace);

            var min = new Vector3(float.MaxValue);
            var max = new Vector3(float.MinValue);
            var verts = new List<PosNormalTexTan>();
            foreach (var mesh in model.Meshes) {
                var subset = new MeshGeometry.Subset {

                    VertexCount = mesh.VertexCount,
                    VertexStart = _vertices.Count,
                    FaceStart = _indices.Count / 3,
                    FaceCount = mesh.FaceCount
                };
                _subsets.Add(subset);
                // bounding box corners

                for (var i = 0; i < mesh.VertexCount; i++) {
                    var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3();
                    min = Vector3.Minimize(min, pos);
                    max = Vector3.Maximize(max, pos);

                    var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D();
                    var texC = mesh.HasTextureCoords(0) ? mesh.GetTextureCoords(0)[i] : new Vector3D();
                    var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D();
                    var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3());
                    verts.Add(v);
                }

                _vertices.AddRange(verts);

                var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList();
                _indices.AddRange(indices);

                var mat = model.Materials[mesh.MaterialIndex];
                var material = mat.ToMaterial();

                Materials.Add(material);

                var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath;
                if (Path.GetExtension(diffusePath) == ".tga") {
                    // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor
                    diffusePath = diffusePath.Replace(".tga", ".png");
                }
                if (!string.IsNullOrEmpty(diffusePath)) {
                    DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath)));
                }
                var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath;
                if (!string.IsNullOrEmpty(normalPath)) {
                    NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath)));
                } else {
                    var normalExt = Path.GetExtension(diffusePath);
                    normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt;

                    NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath)));

                }
            }
            BoundingBox = new BoundingBox(min, max);
            _modelMesh.SetSubsetTable(_subsets);
            _modelMesh.SetVertices(device, _vertices);
            _modelMesh.SetIndices(device, _indices);
        }