public void ModelLoads()
 {
     Settings.initializeSettings();
     String modelERFPath = "E:\\Program Files (x86)\\Steam\\steamapps\\common\\dragon age origins\\packages\\core\\data\\modelhierarchies.erf";
     
     ERF models = new ERF(modelERFPath);
     models.readKeyData();
     Assert.Greater(models.resourceCount, 0);
     int failures = 0;
     for (int i = 0; i < models.resourceCount; i++)
     {
         GFF temp = IO.findFile<GFF>(models.resourceNames[i]);
         Assert.NotNull(temp, "Not found: |" + models.resourceNames[i] +"|" + i);
         if (Path.GetExtension(models.resourceNames[i]) == ".mmh")
         {
             try
             {
                 ModelHierarchy tempH = new ModelHierarchy(temp);
             }
             catch (Exception)
             {
                 Console.WriteLine(models.resourceNames[i]);
                 failures++;
             }
         }
     }
     Assert.AreEqual(0, failures);
 }
        private void modelListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (modelListBox.SelectedItem != null)
            {
                String itemName = (String)modelListBox.SelectedItem;

                if (Path.GetExtension(itemName) == ".msh")
                {
                    GFF tempGFF = ResourceManager.findFile<GFF>(itemName);
                    if (tempGFF != null)
                    {
                        renderer.clearOverlays();
                        renderer.overlayText(tempGFF.path);
                        renderer.showOverlays();
                        renderer.displayModel(new ModelMesh(tempGFF));

                    }
                    else
                    {
                        Console.WriteLine("Couldn't find {0}", itemName);
                    }
                }
                else if (Path.GetExtension(itemName) == ".mmh")
                {
                    GFF tempGFF = ResourceManager.findFile<GFF>(itemName);
                    if (tempGFF != null)
                    {
                        ModelHierarchy mh = new ModelHierarchy(tempGFF);
                        if (mh.loadedMesh)
                        {
                            renderer.clearOverlays();
                            renderer.overlayText(mh.mmhName);
                            renderer.showOverlays();
                            renderer.displayModel(mh.mesh);
                        }
                        else
                        {
                            Console.WriteLine("Couldn't load mesh {0} for {1}", mh.mshName, mh.mmhName);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find {0}", itemName);
                    }
                }
                else if (Path.GetExtension(itemName) == ".dds")
                {
                    try
                    {
                        DDS texture = ResourceManager.findFile<DDS>(itemName);
                        if (texture != null)
                        {
                            renderer.displayDDS(texture);
                        }
                        else
                        {
                            Console.WriteLine("Couldn't find {0}", itemName);
                        }
                    }
                    catch (NotImplementedException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (Path.GetExtension(itemName) == ".mao")
                {
                    MaterialObject mao = ResourceManager.findFile<MaterialObject>(itemName);
                }
            }
        }
Beispiel #3
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);
        }
        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;
        }
        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();
        }
        private void readScene(String path)
        {

            BinaryReader sceneReader = new BinaryReader(new FileStream(path + "\\job_scene.xml", FileMode.Open, FileAccess.Read, FileShare.Read));
            String sceneXML = Encoding.UTF8.GetString(sceneReader.ReadBytes((int)sceneReader.BaseStream.Length));
            sceneReader.Close();

            XmlDocument sceneSpecification = new XmlDocument();
            sceneSpecification.LoadXml(sceneXML);

            XmlNode scene = sceneSpecification.SelectSingleNode("RenderFarmScene");
            foreach (XmlNode model in scene.ChildNodes)
            {
                String modelName = model.Attributes.GetNamedItem("Source").Value;
                String pathName = Path.GetDirectoryName(modelName);
                String filename = Path.GetFileName(modelName).ToLower();
                modelName = pathName + "\\" + filename;

                ResourceManager.addFilePath(pathName);

                if (Path.GetFileName(modelName) == "prp_fertblsm_01.mmh")
                    continue;
                GFF hierarchy = ResourceManager.findFile<GFF>(modelName);
                ModelHierarchy modelHierarchy = new ModelHierarchy(hierarchy);

                Model modelMesh = modelHierarchy.mesh.toModel();

                foreach (XmlNode instance in model.ChildNodes)
                {
                    String instanceName = instance.Attributes.GetNamedItem("Name").Value;
                    Matrix4 transform = xmlToMatrix(instance.SelectSingleNode("Transform").Attributes);
                    modelDictionary.Add(instanceName, new ModelInstance(instanceName, modelMesh, transform));
                }
                ResourceManager.removeFilePath(pathName);
            }
        }