Exemple #1
0
 public ModelHierarchy(GFF gffFile)
 {
     loadedMesh = false;
     binaryFile = gffFile;
     setStructDefinitions();
     readData();
 }
 public ModelHierarchy(GFF gffFile)
 {
     loadedMesh = false;
     binaryFile = gffFile;
     setStructDefinitions();
     readData();
 }
Exemple #3
0
        private List <BiowareModel> readTerrainModels()
        {
            BinaryReader file = headerFile.openReader();

            int  numSectors, reference;
            long startOfList;

            //Seek to the reference to the terrain chunk list and get it
            file.BaseStream.Seek(headerFile.dataOffset + headerFile.structs[TOP_LEVEL_STRUCT_INDEX].fields[TERRAIN_CHUNK_LIST_INDEX].index, SeekOrigin.Begin);
            reference = file.ReadInt32();
            //Seek to the list
            file.BaseStream.Seek(headerFile.dataOffset + reference, SeekOrigin.Begin);
            //Get the lenght of the list
            numSectors = file.ReadInt32();

            //Make a list for the terrain meshes
            List <BiowareModel> terrainModels = new List <BiowareModel>(numSectors);

            int         currentSectorFileIndex, currentSectorID = 0;
            uint        currentSectorModelID = 1;
            GFF         currentSectorFile;
            TerrainMesh currentTerrainMesh;

            startOfList = file.BaseStream.Position;
            for (int i = 0; i < numSectors; i++)
            {
                //Seek to the next struct in the list and get the model id and sector id
                file.BaseStream.Seek(startOfList + (i * terrainChunkStruct.structSize), SeekOrigin.Begin);
                //THIS IS UNSAFE ACCESS TO STRUCT FIELDS!!!!!
                currentSectorModelID = file.ReadUInt32();
                currentSectorID      = file.ReadInt32();

                //Get the index of the next sector file
                currentSectorFileIndex = diskFile.indexOf(String.Format("sector{0:0000}.tmsh", currentSectorID));
                //If its not there something is wrong
                if (currentSectorFileIndex < 0)
                {
                    Console.WriteLine("Could not find file \"sector00" + i + ".tmsh\" :(");
                }
                //Otherwise read it in
                else
                {
                    currentSectorFile  = new GFF(diskFile.path, diskFile.resourceOffsets[currentSectorFileIndex]);
                    currentTerrainMesh = new TerrainMesh(currentSectorFile);
                    terrainModels.Add(currentTerrainMesh.toModel(currentSectorModelID));
                }
            }


            return(terrainModels);
        }
Exemple #4
0
        public Level(String filePath)
        {
            // Initialize the erf (lvl) file in memory
            diskFile = new ERF(filePath);

            //find the index of the header file in the erf
            int headerIndex = diskFile.indexOf("header.gff");

            if (headerIndex < 0)
            {
                throw new Exception("Could not find the header file in " + filePath + "! Please make sure it is a level file.");
            }

            //Initialize the header file
            headerFile     = new GFF(filePath, diskFile.resourceOffsets[headerIndex]);
            baseModels     = new Dictionary <String, Model>();
            lightmapModels = new List <ModelInstance>();
            layoutName     = "WHAT";
            //Set up the struct definitions (for sanity!)
            setStructDefinitions();
            readObjects();
        }
 public BiowareMesh(GFF gffFile)
 {
     binaryFile = gffFile;
     readData();
 }
 public TerrainMesh(GFF binaryFile):base(binaryFile)
 { }
Exemple #7
0
 public ModelMesh(GFF gffFile)
     : base(gffFile)
 {
 }
Exemple #8
0
 public TerrainMesh(GFF binaryFile) : base(binaryFile)
 {
 }
 public BiowareStruct (GFF f, GFFStructDefinition def)
 {
     _binaryFile = f;
     _definition = def;
 }
 public ModelMesh(GFF gffFile)
     : base(gffFile)
 { }
Exemple #11
0
        private List <ModelInstance> readPropModels(GenericList objectList, Vector3 roomOffset, Quaternion roomOrientation, int roomID)
        {
            List <ModelInstance> propModels = new List <ModelInstance>();
            BinaryReader         file       = headerFile.openReader();
            long currentPosition;     //position of beginning of model struct (for offsets within struct

            GenericList propertyList; //to hold the list of properties of the model
            int         reference;    //for referencing structs

            Vector3    position;
            Quaternion rotation;
            String     modelFileName;
            int        lightmapValue;
            uint       modelID;

            //for all the objects in the list
            for (int i = 0; i < objectList.length; i++)
            {
                //if its a model object process it
                if (headerFile.structs[(int)objectList.type[i].id].type == GFFSTRUCTTYPE.MODEL)
                {
                    //seek to the model struct data
                    file.BaseStream.Seek(headerFile.dataOffset + objectList[i], SeekOrigin.Begin);
                    currentPosition = file.BaseStream.Position;

                    //get position
                    file.BaseStream.Seek(currentPosition + modelStruct.fields[MODEL_POSITION_INDEX].index, SeekOrigin.Begin);
                    position  = new Vector3(file.ReadSingle(), file.ReadSingle(), file.ReadSingle());
                    position += roomOffset;

                    //get the rotation
                    file.BaseStream.Seek(currentPosition + modelStruct.fields[MODEL_ROTATION_INDEX].index, SeekOrigin.Begin);
                    rotation = new Quaternion(file.ReadSingle(), file.ReadSingle(), file.ReadSingle(), file.ReadSingle());
                    rotation = rotation * roomOrientation;

                    //get the property List reference
                    file.BaseStream.Seek(currentPosition + modelStruct.fields[MODEL_PROPERTY_INDEX].index + propertyStruct.fields[PROPERTY_CHILDREN_INDEX].index, SeekOrigin.Begin);
                    reference = file.ReadInt32();
                    //seek to the children field  and make the children list
                    file.BaseStream.Seek(headerFile.dataOffset + reference, SeekOrigin.Begin);
                    propertyList = new GenericList(file);

                    // get the reference to the model file name string
                    file.BaseStream.Seek(headerFile.dataOffset + propertyList[MODEL_FILENAME_INDEX] + propertyStruct.fields[PROPERTY_VALUE_INDEX].index, SeekOrigin.Begin);
                    modelFileName = IOUtilities.readECString(file, headerFile.dataOffset + file.ReadInt32()) + ".mmh";

                    //get the lightmap value
                    file.BaseStream.Seek(headerFile.dataOffset + propertyList[MODEL_LIGHTMAPVALUE_INDEX] + propertyStruct.fields[PROPERTY_VALUE_INDEX].index, SeekOrigin.Begin);

                    lightmapValue = Int32.Parse(IOUtilities.readECString(file, headerFile.dataOffset + file.ReadInt32()));

                    //get the ID value
                    file.BaseStream.Seek(currentPosition + modelStruct.fields[MODEL_ID_INDEX].index, SeekOrigin.Begin);
                    modelID = file.ReadUInt32();

                    //If the model isnt in the dictionary already
                    if (!baseModels.ContainsKey(modelFileName))
                    {
                        //Find the mmh file
                        GFF tempGFF = ResourceManager.findFile <GFF>(modelFileName);
                        //If the file was not found
                        if (tempGFF != null)
                        {
                            ModelHierarchy h = new ModelHierarchy(tempGFF);
                            //Only add it if its not a effects model
                            if (!h.isFXModel)
                            {
                                baseModels.Add(modelFileName, h.mesh.toModel());
                            }
                        }
                        else
                        {
                            //Print an error
                            //Settings.stream.AppendFormatLine("Could not find model file \"{0}\".",modelFileName);
                        }
                    }

                    //If its not in the dictionary this time then we just ignore it
                    if (baseModels.ContainsKey(modelFileName))
                    {
                        if (baseModels[modelFileName].isLightmapped || baseModels[modelFileName].castsShadows)
                        {
                            propModels.Add(new ModelInstance(modelFileName, baseModels[modelFileName], position, rotation, modelID, roomID, layoutName));
                        }
                    }
                }
                else if (headerFile.structs[(int)objectList.type[i].id].type == GFFSTRUCTTYPE.LVL_GROUP)
                {
                    //seek to the group struct data
                    file.BaseStream.Seek(headerFile.dataOffset + objectList[i], SeekOrigin.Begin);
                    currentPosition = file.BaseStream.Position;

                    file.BaseStream.Seek(currentPosition + levelGroupStruct.fields[GROUP_POSITION_INDEX].index, SeekOrigin.Begin);
                    Vector3 groupPosition = new Vector3(file.ReadSingle(), file.ReadSingle(), file.ReadSingle());

                    file.BaseStream.Seek(currentPosition + levelGroupStruct.fields[GROUP_ROTATION_INDEX].index, SeekOrigin.Begin);
                    Quaternion groupRotation = new Quaternion(file.ReadSingle(), file.ReadSingle(), file.ReadSingle(), file.ReadSingle());

                    //seek to the list
                    file.BaseStream.Seek(currentPosition + levelGroupStruct.fields[GROUP_LIST_INDEX].index, SeekOrigin.Begin);
                    reference = file.ReadInt32();
                    file.BaseStream.Seek(headerFile.dataOffset + reference, SeekOrigin.Begin);

                    propModels.AddRange(readPropModels(new GenericList(file), roomOffset + groupPosition, groupRotation * roomOrientation, roomID));
                }
            }
            file.Close();
            return(propModels);
        }
Exemple #12
0
        public void readData()
        {
            BinaryReader file = binaryFile.openReader();
            int          reference;

            //Get the name of the mmh file
            file.BaseStream.Seek(binaryFile.dataOffset + binaryFile.structs[0].fields[MMH_NAME_INDEX].index, SeekOrigin.Begin);
            mmhName = IOUtilities.readECString(file, binaryFile.dataOffset + file.ReadInt32()).ToLower();

            //Get the name of the msh file
            file.BaseStream.Seek(binaryFile.dataOffset + binaryFile.structs[0].fields[MSH_NAME_INDEX].index, SeekOrigin.Begin);
            mshName = IOUtilities.readECString(file, binaryFile.dataOffset + file.ReadInt32()).ToLower();

            //Get the total number of bones in the mmh
            file.BaseStream.Seek(binaryFile.dataOffset + binaryFile.structs[0].fields[TOTAL_BONES_INDEX].index, SeekOrigin.Begin);
            numBones = file.ReadInt32();

            //Apparently fx models have an extra field...
            isFXModel = binaryFile.structs[0].fields.Length == 8;
            if (isFXModel)
            {
                file.Close();
                return;
            }

            //Get the children list (should only contain GOB)
            file.BaseStream.Seek(binaryFile.dataOffset + binaryFile.structs[0].fields[TOP_LEVEL_CHILDREN_INDEX].index, SeekOrigin.Begin);
            reference = file.ReadInt32();
            file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin);
            GenericList childrenList = new GenericList(file);

            //Get the children of the GOB object
            //Sometimes its a node struct, sometimes its a mshh struct . . .

            if ((int)(childrenList.type[0].id) == nodeStructIndex)
            {
                file.BaseStream.Seek(binaryFile.dataOffset + childrenList[0] + nodeStruct.fields[GOB_CHILDREN_INDEX].index, SeekOrigin.Begin);
            }
            else if ((int)(childrenList.type[0].id) == meshChunkInfoIndex)
            {
                file.BaseStream.Seek(binaryFile.dataOffset + childrenList[0] + meshChunkInfoStruct.fields[MSH_CHUNK_CHILDREN_INDEX].index, SeekOrigin.Begin);
            }

            reference = file.ReadInt32();
            file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin);
            childrenList = new GenericList(file);


            //Find the mesh
            GFF temp = ResourceManager.findFile <GFF>(mshName);

            //If its not there throw an exception cause we need it
            if (temp == null)
            {
                Console.WriteLine("Could not find mesh file \"{0}\".", mshName);
                file.Close();
                return;
                //throw new Exception("COULD NOT FIND MESH FILE, LOOK AT CONSOLE!!!!!!");
            }

            loadedMesh = true;

            //Make the mesh
            mesh = new ModelMesh(temp);

            //For each thing in the child list
            for (int i = 0; i < childrenList.length; i++)
            {
                //If the child is a mesh chunk info struct
                if ((int)childrenList.type[i].id == meshChunkInfoIndex)
                {
                    //Fill in the missing mesh chunk info
                    updateChunk(file, binaryFile.dataOffset + childrenList[i], new Vector3(), new Quaternion());
                }
            }
            file.Close();
        }
        private List<BiowareModel> readTerrainModels()
        {

            BinaryReader file = headerFile.openReader();

            int numSectors, reference;
            long startOfList;

            //Seek to the reference to the terrain chunk list and get it
            file.BaseStream.Seek(headerFile.dataOffset + headerFile.structs[TOP_LEVEL_STRUCT_INDEX].fields[TERRAIN_CHUNK_LIST_INDEX].index, SeekOrigin.Begin);
            reference = file.ReadInt32();
            //Seek to the list
            file.BaseStream.Seek(headerFile.dataOffset + reference, SeekOrigin.Begin);
            //Get the lenght of the list
            numSectors = file.ReadInt32();

            //Make a list for the terrain meshes
            List<BiowareModel> terrainModels = new List<BiowareModel>(numSectors);

            int currentSectorFileIndex,currentSectorID = 0;
            uint currentSectorModelID = 1;
            GFF currentSectorFile;
            TerrainMesh currentTerrainMesh;

            startOfList = file.BaseStream.Position;
            for (int i = 0; i < numSectors; i++)
            {
                //Seek to the next struct in the list and get the model id and sector id
                file.BaseStream.Seek(startOfList + (i * terrainChunkStruct.structSize), SeekOrigin.Begin);
                //THIS IS UNSAFE ACCESS TO STRUCT FIELDS!!!!!
                currentSectorModelID = file.ReadUInt32();
                currentSectorID = file.ReadInt32();

                //Get the index of the next sector file
                currentSectorFileIndex = diskFile.indexOf(String.Format("sector{0:0000}.tmsh",currentSectorID));
                //If its not there something is wrong
                if(currentSectorFileIndex < 0)
                {
                    Console.WriteLine("Could not find file \"sector00"+i+".tmsh\" :(");
                }
                //Otherwise read it in
                else
                {
                    currentSectorFile = new GFF(diskFile.path, diskFile.resourceOffsets[currentSectorFileIndex]);
                    currentTerrainMesh = new TerrainMesh(currentSectorFile);
                    terrainModels.Add(currentTerrainMesh.toModel(currentSectorModelID));
                }
            }


            return terrainModels;
        }
        public Level(String filePath)
        {
            // Initialize the erf (lvl) file in memory
            diskFile = new ERF(filePath);

            //find the index of the header file in the erf
            int headerIndex = diskFile.indexOf("header.gff");

            if (headerIndex < 0)
            {
                throw new Exception("Could not find the header file in "+filePath+"! Please make sure it is a level file.");
            }

            //Initialize the header file
            headerFile = new GFF(filePath, diskFile.resourceOffsets[headerIndex]);
            baseModels = new Dictionary<String, Model>();
            lightmapModels = new List<ModelInstance>();
            layoutName = "WHAT";
            //Set up the struct definitions (for sanity!)
            setStructDefinitions();
            readObjects();
        }
        private void btn_load_Click(object sender, EventArgs e)
        {
            setButtons(false);
            String filePath = tb_path.Text;
            file = filePath;
            String extention = Path.GetExtension(filePath);
            List<Mesh> renderableMeshes = new List<Mesh>();
            drawString = "File: " + file;

            updateBitmap(drawString);

            //Try and find the model file
            if (extention == ".mmh")
            {
                GFF tempGFF = new GFF(filePath);
                ModelHierarchy mh = new ModelHierarchy(tempGFF);
                currentlyShowing = Showing.Model;
                meshes = mh.mesh.toModel().meshes;
                setButtons(true);
                if (meshes.Length > 0)
                {
                    setMeshNum(0);
                }
            }
            else if (extention == ".msh")
            {
                GFF tempGFF = new GFF(filePath);
                ModelMesh mm = new ModelMesh(tempGFF);
                currentlyShowing = Showing.Model;
                meshes = mm.toModel().meshes;
                setButtons(true);
                if (meshes.Length > 0)
                {
                    setMeshNum(0);
                }
            }
            else if (extention == ".tga")
            {
                texture = new Targa(filePath);
                currentlyShowing = Showing.Texture;
            }
            else if (extention == ".lvl")
            {
                level = new LevelScene(filePath);
                currentlyShowing = Showing.Level;
                List<Patch> patchList = new List<Patch>();
                List<Triangle> tris = new List<Triangle>();
                foreach (ModelInstance m in level.lightmapModels)
                {
                    for (int i = 0; i < m.meshes.Length; i++)
                    {
                        if (m.meshes[i].isLightmapped)
                        {
                            //Make the lightmap
                            LightMap temp = new LightMap(m, m.meshes[i]);
                            //For each patch instance in the lightmap
                            foreach (Patch p in temp.patches)
                            {
                                patchList.Add(p);
                            }
                        }
                    }
                    if (m.baseModel.castsShadows)
                        tris.AddRange(m.tris);
                }
                octree = new Octree(tris);
                patches = patchList.ToArray();
            }
            else if (extention == ".xml")
            {
                level = new XMLScene(filePath);
                currentlyShowing = Showing.Level;
                List<Patch> patchList = new List<Patch>();
                List<Triangle> tris = new List<Triangle>();
                foreach (ModelInstance m in level.lightmapModels)
                {
                    for (int i = 0; i < m.meshes.Length; i++)
                    {
                        if (m.meshes[i].isLightmapped)
                        {
                            //Make the lightmap
                            LightMap temp = new LightMap(m, m.meshes[i]);
                            //For each patch instance in the lightmap
                            foreach (Patch p in temp.patches)
                            {
                                patchList.Add(p);
                            }
                        }
                    }
                    if (m.baseModel.castsShadows)
                        tris.AddRange(m.tris);
                }
                octree = new Octree(tris);
                patches = patchList.ToArray();
            }
            //If its not the right type of file then print an error
            else
            {
                drawString = "This is not a valid model (.mmh or .msh), texture (.tga), level (.lvl), or scene (.xml) file!";
            }
            refreshView();
        }
 public BiowareMesh(GFF gffFile)
 {
     binaryFile = gffFile;
     readData();
 }