Esempio n. 1
0
        public static void ConvertBSPtoOBJ(string fileName, RenderWareModelFile bspFile, bool flipUVs)
        {
            int totalVertexIndices = 1;

            string materialLibrary          = Path.ChangeExtension(fileName, "MTL");
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

            StreamWriter OBJWriter = new StreamWriter((Path.ChangeExtension(fileName, "OBJ")), false);

            List <Triangle> triangleList = new List <Triangle>();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    OBJWriter.WriteLine("# Exported by Heroes Power Plant");
                    OBJWriter.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
                    OBJWriter.WriteLine();
                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(OBJWriter, (AtomicSector_0009)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, flipUVs);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(OBJWriter, (PlaneSector_000A)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, flipUVs);
                    }
                }
            }

            for (int i = 0; i < bspFile.MaterialList.Count; i++)
            {
                OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + bspFile.MaterialList[i]);
                OBJWriter.WriteLine("usemtl " + bspFile.MaterialList[i] + "_m");

                foreach (Triangle j in triangleList)
                {
                    if (j.materialIndex == i)
                    {
                        OBJWriter.WriteLine("f "
                                            + j.vertex1.ToString() + "/" + j.vertex1.ToString() + "/" + j.vertex1.ToString() + " "
                                            + j.vertex2.ToString() + "/" + j.vertex2.ToString() + "/" + j.vertex2.ToString() + " "
                                            + j.vertex3.ToString() + "/" + j.vertex3.ToString() + "/" + j.vertex3.ToString());
                    }
                }

                OBJWriter.WriteLine();
            }

            OBJWriter.Close();
            WriteMaterialLib(bspFile.MaterialList.ToArray(), materialLibrary);
        }
Esempio n. 2
0
        public static void ConvertDFFtoOBJ(string fileName, RenderWareModelFile renderWareModelFile, bool flipUVs)
        {
            lastMaterial = "";
            int totalVertexIndices = 1;

            string          materialLibrary     = Path.ChangeExtension(fileName, "MTL");
            int             untexturedMaterials = 0;
            List <Triangle> triangleList        = new List <Triangle>();

            StreamWriter writer = new StreamWriter((Path.ChangeExtension(fileName, "obj")), false);

            writer.WriteLine("# Exported by Industrial Park");
            writer.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
            writer.WriteLine();

            foreach (RWSection rw in renderWareModelFile.GetAsRWSectionArray())
            {
                if (rw is Clump_0010 w)
                {
                    foreach (Geometry_000F rw2 in w.geometryList.geometryList)
                    {
                        ExportGeometryToOBJ(writer, rw2, ref triangleList, ref totalVertexIndices, ref untexturedMaterials, flipUVs);
                    }
                }
            }

            writer.Close();

            StreamWriter MTLWriter = new StreamWriter(materialLibrary, false);

            MTLWriter.WriteLine("# Exported by Industrial Park");
            MTLWriter.WriteLine();

            foreach (RWSection rw in renderWareModelFile.GetAsRWSectionArray())
            {
                if (rw is Clump_0010 w)
                {
                    foreach (Geometry_000F rw2 in w.geometryList.geometryList)
                    {
                        WriteMaterialLib(rw2, MTLWriter, ref untexturedMaterials);
                    }
                }
            }

            MTLWriter.Close();
        }
Esempio n. 3
0
        private void buttonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog a = new OpenFileDialog()
            {
                Filter      = "All supported types|*.obj;*.bsp|OBJ Files|*.obj|BSP Files|*.bsp|All files|*.*",
                Multiselect = true
            };

            if (a.ShowDialog() == DialogResult.OK)
            {
                foreach (string i in a.FileNames)
                {
                    ReadFileMethods.isCollision = true;

                    RenderWareModelFile file = new RenderWareModelFile(Path.GetFileNameWithoutExtension(i) + ".BSP");
                    file.isShadowCollision = true;

                    try
                    {
                        file.ChunkNumber = Convert.ToByte(Path.GetFileNameWithoutExtension(i).Split('_').Last());
                    }
                    catch { file.ChunkNumber = -1; };

                    if (Path.GetExtension(i).ToLower() == ".obj")
                    {
                        file.SetForRendering(Program.MainForm.renderer.Device, CreateShadowCollisionBSPFile(ReadOBJFile(i, true)), null);
                    }
                    else
                    {
                        file.SetForRendering(Program.MainForm.renderer.Device, ReadFileMethods.ReadRenderWareFile(i), File.ReadAllBytes(i));
                    }

                    bspRenderer.ShadowColBSPList.Add(file);
                    listBoxLevelModels.Items.Add(file.fileName);

                    ReadFileMethods.isCollision = false;
                }

                buttonExport.Enabled = true;
            }
        }
Esempio n. 4
0
        private void buttonImport_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog a = new OpenFileDialog()
            {
                Filter = "All supported types|*.dae;*.obj;*.bsp|DAE Files|*.dae|OBJ Files|*.obj|BSP Files|*.bsp|All files|*.*",
                Multiselect = true
            })
                if (a.ShowDialog() == DialogResult.OK)
                {
                    progressBar1.Minimum = 0;
                    progressBar1.Value   = 0;
                    progressBar1.Step    = 1;
                    progressBar1.Maximum = a.FileNames.Count();

                    foreach (string i in a.FileNames)
                    {
                        RenderWareModelFile file = new RenderWareModelFile(Path.GetFileNameWithoutExtension(i) + ".BSP");
                        file.SetChunkNumberAndName();

                        if (Path.GetExtension(i).ToLower() == ".obj")
                        {
                            file.SetForRendering(CreateBSPFile(i, ReadOBJFile(i, false)), null);
                        }
                        else if (Path.GetExtension(i).ToLower() == ".dae")
                        {
                            file.SetForRendering(CreateBSPFile(i, ConvertDataFromDAEObject(ReadDAEFile(i), false)), null);
                        }
                        else if (new string[] { ".bsp", ".rg1", ".rp2", ".rx1" }.Contains(Path.GetExtension(i).ToLower()))
                        {
                            file.SetForRendering(ReadFileMethods.ReadRenderWareFile(i), File.ReadAllBytes(i));
                        }

                        BSPStream.Add(file);
                        listBoxLevelModels.Items.Add(file.fileName);
                        progressBar1.PerformStep();
                    }

                    progressBar1.Value = 0;
                }
        }
Esempio n. 5
0
        private void buttonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog()
            {
                Filter      = GetImportFilter(), // "All supported types|*.dae;*.obj;*.bsp|DAE Files|*.dae|OBJ Files|*.obj|BSP Files|*.bsp|All files|*.*",
                Multiselect = true
            };

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                progressBar1.Minimum = 0;
                progressBar1.Value   = 0;
                progressBar1.Step    = 1;
                progressBar1.Maximum = openFile.FileNames.Count();

                foreach (string i in openFile.FileNames)
                {
                    RenderWareModelFile file = new RenderWareModelFile(Path.GetFileNameWithoutExtension(i) + ".BSP");
                    file.SetChunkNumberAndName();

                    try
                    {
                        if (new string[] { ".bsp", ".rg1", ".rp2", ".rx1" }.Contains(Path.GetExtension(i).ToLower()))
                        {
                            file.SetForRendering(Program.MainForm.renderer.Device, ReadFileMethods.ReadRenderWareFile(i), File.ReadAllBytes(i));
                        }
                        else if (Path.GetExtension(i).ToLower() == ".obj" || Path.GetExtension(i).ToLower() == ".dae")
                        {
                            try
                            {
                                if (Path.GetExtension(i).ToLower() == ".obj")
                                {
                                    file.SetForRendering(Program.MainForm.renderer.Device, CreateBSPFile(i, ReadOBJFile(i, false), checkBoxTristrip.Checked, checkBoxFlipUVs.Checked), null);
                                }
                                else
                                {
                                    file.SetForRendering(Program.MainForm.renderer.Device, CreateBSPFile(i, ConvertDataFromDAEObject(ReadDAEFile(i), false), checkBoxTristrip.Checked, checkBoxFlipUVs.Checked), null);
                                }
                            }
                            catch
                            {
                                file.SetForRendering(Program.MainForm.renderer.Device, CreateBSPFromAssimp(i, checkBoxFlipUVs.Checked), null);
                            }
                        }
                        else
                        {
                            file.SetForRendering(Program.MainForm.renderer.Device, CreateBSPFromAssimp(i, checkBoxFlipUVs.Checked), null);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error importing {Path.GetFileName(i)} : {ex.Message}");
                        progressBar1.PerformStep();
                        continue;
                    }

                    bspRenderer.BSPList.Add(file);
                    listBoxLevelModels.Items.Add(file.fileName);
                    progressBar1.PerformStep();
                }

                progressBar1.Value = 0;
            }
        }
        public static void ConvertBSPtoOBJ(string fileName, RenderWareModelFile bspFile)
        {
            int totalVertexIndices = 0;

            string materialLibrary          = Path.ChangeExtension(fileName, "MTL");
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

            StreamWriter OBJWriter = new StreamWriter((Path.ChangeExtension(fileName, "OBJ")), false);

            List <Triangle> triangleList = new List <Triangle>();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    OBJWriter.WriteLine("# Exported by Heroes Power Plant");
                    OBJWriter.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
                    OBJWriter.WriteLine();
                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(OBJWriter, (AtomicSector_0009)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, bspFile.isShadowCollision);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(OBJWriter, (PlaneSector_000A)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, bspFile.isShadowCollision);
                    }
                }
            }

            if (bspFile.isShadowCollision)
            {
                RenderWareFile.Color currentColFlag = new RenderWareFile.Color(3, 3, 3, 3);

                foreach (TriangleExt j in triangleList)
                {
                    if (j.collisionFlag != currentColFlag)
                    {
                        currentColFlag = j.collisionFlag;
                        OBJWriter.WriteLine();
                        OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + currentColFlag.ToString());
                        OBJWriter.WriteLine("usemtl colmat_" + currentColFlag.ToString());
                    }

                    OBJWriter.WriteLine("f "
                                        + (j.vertex1 + 1).ToString() + " "
                                        + (j.vertex2 + 1).ToString() + " "
                                        + (j.vertex3 + 1).ToString());
                }

                OBJWriter.Close();
                WriteCollisionMaterialLib(triangleList, bspFile.MaterialList.ToArray(), materialLibrary);
            }
            else
            {
                for (int i = 0; i < bspFile.MaterialList.Count; i++)
                {
                    OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + bspFile.MaterialList[i]);
                    OBJWriter.WriteLine("usemtl " + bspFile.MaterialList[i] + "_m");

                    foreach (Triangle j in triangleList)
                    {
                        if (j.MaterialIndex == i)
                        {
                            OBJWriter.WriteLine("f "
                                                + (j.vertex1 + 1).ToString() + "/" + (j.vertex1 + 1).ToString() + " "
                                                + (j.vertex2 + 1).ToString() + "/" + (j.vertex2 + 1).ToString() + " "
                                                + (j.vertex3 + 1).ToString() + "/" + (j.vertex3 + 1).ToString());
                        }
                    }

                    OBJWriter.WriteLine();
                }

                OBJWriter.Close();
                WriteMaterialLib(bspFile.MaterialList.ToArray(), materialLibrary);
            }
        }
Esempio n. 7
0
        public static void ExportAssimp(string fileName, RenderWareModelFile bspFile, bool flipUVs, ExportFormatDescription format, string textureExtension)
        {
            Scene scene = new Scene();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    for (int i = 0; i < w.materialList.materialList.Length; i++)
                    {
                        var mat = w.materialList.materialList[i];

                        scene.Materials.Add(new Material()
                        {
                            ColorDiffuse = new Color4D(
                                mat.materialStruct.color.R / 255f,
                                mat.materialStruct.color.G / 255f,
                                mat.materialStruct.color.B / 255f,
                                mat.materialStruct.color.A / 255f),
                            TextureDiffuse = new TextureSlot()
                            {
                                FilePath    = mat.texture.diffuseTextureName.stringString + textureExtension,
                                TextureType = TextureType.Diffuse
                            },
                            Name = "mat_" + mat.texture.diffuseTextureName.stringString
                        });

                        scene.Meshes.Add(new Mesh(PrimitiveType.Triangle)
                        {
                            MaterialIndex = i, Name = "mesh_" + mat.texture.diffuseTextureName.stringString
                        });
                    }

                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(scene, (AtomicSector_0009)w.firstWorldChunk, flipUVs);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(scene, (PlaneSector_000A)w.firstWorldChunk, flipUVs);
                    }
                }
            }

            scene.RootNode = new Node()
            {
                Name = "root"
            };

            Node latest = scene.RootNode;

            for (int i = 0; i < scene.MeshCount; i++)
            {
                latest.Children.Add(Newtonsoft.Json.JsonConvert.DeserializeObject <Node>(
                                        "{\"Name\":\"" + scene.Meshes[i].Name + "\", \"MeshIndices\": [" + i.ToString() + "]}"));

                //latest = latest.Children[0];
            }

            new AssimpContext().ExportFile(scene, fileName, format.FormatId);
        }
        public static void ExportAssimp(string fileName, RenderWareModelFile bspFile, bool flipUVs, ExportFormatDescription format, string textureExtension)
        {
            Scene scene = new Scene();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    for (int i = 0; i < w.materialList.materialList.Length; i++)
                    {
                        var    mat     = w.materialList.materialList[i];
                        string objName = Path.GetFileNameWithoutExtension(fileName) + "_" + (mat.materialStruct.isTextured != 0 ? mat.texture.diffuseTextureName.stringString : "default");

                        scene.Materials.Add(new Material()
                        {
                            ColorDiffuse = new Color4D(
                                mat.materialStruct.color.R / 255f,
                                mat.materialStruct.color.G / 255f,
                                mat.materialStruct.color.B / 255f,
                                mat.materialStruct.color.A / 255f),
                            TextureDiffuse = mat.materialStruct.isTextured != 0 ? new TextureSlot()
                            {
                                FilePath    = mat.texture.diffuseTextureName.stringString + textureExtension,
                                TextureType = TextureType.Diffuse
                            } : new TextureSlot(),
                            Name = "mat_" + objName
                        });

                        scene.Meshes.Add(new Mesh(PrimitiveType.Triangle)
                        {
                            MaterialIndex = i, Name = "mesh_" + objName
                        });
                    }

                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(scene, (AtomicSector_0009)w.firstWorldChunk);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(scene, (PlaneSector_000A)w.firstWorldChunk);
                    }
                }
            }

            scene.RootNode = new Node()
            {
                Name = "root"
            };

            Node latest = scene.RootNode;

            for (int i = 0; i < scene.MeshCount; i++)
            {
                latest.Children.Add(Newtonsoft.Json.JsonConvert.DeserializeObject <Node>(
                                        "{\"Name\":\"" + scene.Meshes[i].Name + "\", \"MeshIndices\": [" + i.ToString() + "]}"));

                //latest = latest.Children[0];
            }

            new AssimpContext().ExportFile(scene, fileName, format.FormatId,

                                           //PostProcessSteps.GenerateNormals |
                                           PostProcessSteps.JoinIdenticalVertices |
                                           PostProcessSteps.RemoveRedundantMaterials |
                                           PostProcessSteps.ValidateDataStructure |

                                           PostProcessSteps.Debone |
                                           PostProcessSteps.FindInstances |
                                           PostProcessSteps.FindInvalidData |
                                           PostProcessSteps.OptimizeGraph |
                                           PostProcessSteps.OptimizeMeshes |
                                           PostProcessSteps.Triangulate |
                                           PostProcessSteps.PreTransformVertices |

                                           (flipUVs ? PostProcessSteps.FlipUVs : 0));
        }