Exemplo n.º 1
0
        public void RenderModel(int LOD)
        {
            //renderer.Render_Device.SetTexture(0, null);
            for (int x = 0; x < Regions.Count; x++)
            {
                ushort permIndex = 0;
                //if (Regions[x].PermutationsIndexs.Count <= LOD)
                //    permIndex = Regions[x].PermutationsIndexs[Regions[x].PermutationsIndexs.Count - 1];
                //else
                //    permIndex = Regions[x].PermutationsIndexs[LOD];

                ModelPart mp = ModelParts[permIndex];

                renderer.Device.VertexFormat = HaloVertex.FVF;
                renderer.Device.SetStreamSource(0, mp.vertexBuffer, 0);
                renderer.Device.Indices = mp.IndexBuffer;

                for (int y = 0; y < mp.Submeshes.Count; y++)
                {
                    renderer.Device.SetTexture(0, shader[mp.Submeshes[y].ShaderIndex].texture);
                    for (int z = mp.Submeshes[y].SubsetIndex;
                         z < mp.Submeshes[y].SubsetIndex +
                         mp.Submeshes[y].SubsetCount; z++)
                    {
                        Subset subset = mp.Subsets[z];
                        // renderer.Render_Device.DrawPrimitives(PrimitiveType.TriangleStrip,
                        //      0, subset.FaceLength);//mp.Submeshes[y].VertexCount,subset.FaceLength,m, 0, subset.FaceStart,
                        renderer.Device.DrawIndexedPrimitives(PrimitiveType.TriangleStrip,
                                                              0, 0, mp.Subsets[y].VertexCount, subset.FaceStart, subset.FaceLength);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static ModelInfo GetModelInfo(HaloMap Map, int TagIndex)
        {
            // Create our structure to hold the info
            ModelInfo mi = new ModelInfo();

            // Get our Name
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset);
            mi.Name = Map.StringTable.StringItems[(Map.StringTable.GetStringItemIndexByID(Map, Map.IO.In.ReadInt32()))].Name;

            // Get our Region info first
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 12);
            int regionCount   = Map.IO.In.ReadInt32();
            int regionPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            mi.Regions = new List <Region>();
            for (int x = 0; x < regionCount; x++)
            {
                Region region = new Region();

                Map.IO.SeekTo(regionPointer + (16 * x));
                region.Name = Map.StringTable.StringItems[Map.StringTable.GetStringItemIndexByID(Map, Map.IO.In.ReadInt32())].Name;
                int permutationCount   = Map.IO.In.ReadInt32();
                int permutationPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;
                region.Permutations = new List <Permutation>();
                Map.IO.SeekTo(permutationPointer);
                for (int y = 0; y < permutationCount; y++)
                {
                    Permutation perm = new Permutation();
                    perm.Name = Map.StringTable.StringItems[Map.StringTable.GetStringItemIndexByID(Map, Map.IO.In.ReadInt32())].Name;
                    perm.LODs = new List <short>();
                    for (int l = 0; l < 6; l++)
                    {
                        perm.LODs.Add(Map.IO.In.ReadInt16());
                    }
                    region.Permutations.Add(perm);
                }

                mi.Regions.Add(region);
            }

            // Get our nodes
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 48);
            int nodeCount   = Map.IO.In.ReadInt32();
            int nodePointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            mi.Nodes = new List <Node>();
            Map.IO.SeekTo(nodePointer);
            for (int x = 0; x < nodeCount; x++)
            {
                mi.Nodes.Add(new Node(Map, Map.IO.In));
            }

            // Get our marker groups
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 60);
            int markerGroupCount   = Map.IO.In.ReadInt32();
            int markerGroupPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            mi.MarkerGroups = new List <MarkerGroup>();
            for (int x = 0; x < markerGroupCount; x++)
            {
                Map.IO.SeekTo(markerGroupPointer + (16 * x));
                mi.MarkerGroups.Add(new MarkerGroup(Map, Map.IO.In));
            }

            // Read our shaders
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 72);
            mi.shaderid = new int[Map.IO.In.ReadInt32()];
            int pointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            for (int x = 0; x < mi.shaderid.Length; x++)
            {
                Map.IO.SeekTo(pointer + (x * 0x24) + 0xC);
                mi.shaderid[x] = Map.IO.In.ReadInt32();
            }

            // Get our model info first
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 104);
            int modelPartCount   = Map.IO.In.ReadInt32();
            int modelPartPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            mi.ModelParts = new List <ModelPart>();
            for (int x = 0; x < modelPartCount; x++)
            {
                ModelPart part = new ModelPart();

                Map.IO.SeekTo(modelPartPointer + (76 * x));
                int submeshCount   = Map.IO.In.ReadInt32();
                int submeshPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;
                part.Submeshes = new List <Submesh>();
                Map.IO.SeekTo(submeshPointer);
                for (int y = 0; y < submeshCount; y++)
                {
                    Submesh submesh = new Submesh();

                    submesh.ShaderIndex = Map.IO.In.ReadInt16();
                    Map.IO.In.ReadInt16();
                    submesh.FaceStart   = Map.IO.In.ReadInt16();
                    submesh.FaceLength  = Map.IO.In.ReadInt16();
                    submesh.SubsetIndex = Map.IO.In.ReadInt16();
                    submesh.SubsetCount = Map.IO.In.ReadInt16();
                    submesh.Flags       = Map.IO.In.ReadInt16();
                    submesh.VertexCount = Map.IO.In.ReadInt16();

                    part.totalVertexCount += submesh.VertexCount;
                    part.totalFaceCount   += submesh.FaceLength;

                    part.Submeshes.Add(submesh);
                }

                Map.IO.SeekTo(modelPartPointer + (76 * x) + 12);
                int subsetCount   = Map.IO.In.ReadInt32();
                int subsetPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;
                part.Subsets = new List <Subset>();
                Map.IO.SeekTo(subsetPointer);
                for (int y = 0; y < subsetCount; y++)
                {
                    Subset subset = new Subset();

                    subset.FaceStart     = Map.IO.In.ReadInt16();
                    subset.FaceLength    = Map.IO.In.ReadInt16();
                    subset.SubmeshParent = Map.IO.In.ReadInt16();
                    subset.VertexCount   = Map.IO.In.ReadInt16();

                    part.Subsets.Add(subset);
                }

                Map.IO.SeekTo(modelPartPointer + (76 * x) + 32);
                part.RawIdentifier = Map.IO.In.ReadInt32();

                Map.IO.SeekTo(modelPartPointer + (76 * x) + 46);
                part.VertexFormat  = Map.IO.In.ReadByte();
                part.UnknownFormat = Map.IO.In.ReadByte();

                part.triangleStripData = new ushort[part.totalFaceCount];
                part.VertexData        = new Vertex[part.totalVertexCount];

                mi.ModelParts.Add(part);
            }

            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 116);
            int boundingBoxCount   = Map.IO.In.ReadInt32();
            int boundingBoxPointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

            mi.BoundingBox = new BoundingBox();
            Map.IO.SeekTo(boundingBoxPointer + 4);
            mi.BoundingBox.xMin = Map.IO.In.ReadSingle();
            mi.BoundingBox.xMax = Map.IO.In.ReadSingle();
            mi.BoundingBox.yMin = Map.IO.In.ReadSingle();
            mi.BoundingBox.yMax = Map.IO.In.ReadSingle();
            mi.BoundingBox.zMin = Map.IO.In.ReadSingle();
            mi.BoundingBox.zMax = Map.IO.In.ReadSingle();
            mi.BoundingBox.uMin = Map.IO.In.ReadSingle();
            mi.BoundingBox.uMax = Map.IO.In.ReadSingle();
            mi.BoundingBox.vMin = Map.IO.In.ReadSingle();
            mi.BoundingBox.vMax = Map.IO.In.ReadSingle();

            // Get our raw ID now
            Map.IO.SeekTo(Map.IndexItems[TagIndex].Offset + 224);
            int rawid = Map.IO.In.ReadInt32();

            // Lets load our raw data now
            mi.Data = Map.RawInformation.GetRawDataFromID(rawid);

            // Now lets start to breakdown our raw data
            EndianIO rawIo = new EndianIO(mi.Data, EndianType.BigEndian);

            rawIo.Open();

            for (int x = 0; x < mi.ModelParts.Count; x++)
            {
                // Get our model part
                ModelPart mp = mi.ModelParts[x];

                int unknownSize = 1;
                if (mp.UnknownFormat == 0 ||
                    mp.UnknownFormat == 2)
                {
                    unknownSize = 4;
                }
                else if (mp.UnknownFormat == 3)
                {
                    unknownSize = 12;
                }

                for (int z = 0; z < mi.ModelParts[x].totalVertexCount; z++)
                {
                    mp.VertexData[z].X =
                        decompressShort(rawIo.In.ReadUInt16(), mi.BoundingBox.xMin, mi.BoundingBox.xMax);
                    mp.VertexData[z].Y =
                        decompressShort(rawIo.In.ReadUInt16(), mi.BoundingBox.yMin, mi.BoundingBox.yMax);
                    mp.VertexData[z].Z =
                        decompressShort(rawIo.In.ReadUInt16(), mi.BoundingBox.zMin, mi.BoundingBox.zMax);
                    mp.VertexData[z].padding  = rawIo.In.ReadBytes(2);
                    mp.VertexData[z].textureU =
                        decompressShort(rawIo.In.ReadUInt16(), mi.BoundingBox.uMin, mi.BoundingBox.uMax);
                    mp.VertexData[z].textureV = 1 -
                                                decompressShort(rawIo.In.ReadUInt16(), mi.BoundingBox.vMin, mi.BoundingBox.vMax);
                    mp.VertexData[z].extra = rawIo.In.ReadBytes(12);
                    if (mp.VertexFormat == 2)
                    {
                        mp.VertexData[z].boneID1     = rawIo.In.ReadByte();
                        mp.VertexData[z].boneID2     = rawIo.In.ReadByte();
                        mp.VertexData[z].boneID3     = rawIo.In.ReadByte();
                        mp.VertexData[z].boneID4     = rawIo.In.ReadByte();
                        mp.VertexData[z].VertWeight1 = (float)rawIo.In.ReadByte() / 0xFF;
                        mp.VertexData[z].VertWeight2 = (float)rawIo.In.ReadByte() / 0xFF;
                        mp.VertexData[z].VertWeight3 = (float)rawIo.In.ReadByte() / 0xFF;
                        mp.VertexData[z].VertWeight4 = (float)rawIo.In.ReadByte() / 0xFF;
                    }
                }

                //Read the unkown
                byte[] unknwon = rawIo.In.ReadBytes(mp.totalVertexCount * unknownSize);

                //Make sure we are padded
                rawIo.Stream.Position = getPaddedOffset((int)rawIo.Stream.Position);

                //Save back our model part
                mi.ModelParts[x] = mp;
            }

            //Now that we have read the vertex data lets read the face data
            for (int x = 0; x < mi.ModelParts.Count; x++)
            {
                // Get our model part
                ModelPart mp = mi.ModelParts[x];

                for (int y = 0; y < mp.totalFaceCount; y++)
                {
                    mp.triangleStripData[y] = rawIo.In.ReadUInt16();
                }

                rawIo.Stream.Position = getPaddedOffset((int)rawIo.Stream.Position);
            }

            // Lets return our bitmap now
            return(mi);
        }