Exemple #1
0
 public void WriteFBXFile(CgfData cgfData)
 {
     // The root of the functions to write FBX binary files
     // At this point, we should have a CgfData object, fully populated.
     Console.WriteLine();
     Console.WriteLine("*** Starting WriteFBX() ***");
     Console.WriteLine();
 }
        public void WriteBlend(CgfData cgfData)
        {
            // The root of the functions to write Blend files
            // At this point, we should have a CgfData object, fully populated.
            Console.WriteLine();
            Console.WriteLine("*** Starting WriteBlend() ***");
            Console.WriteLine();

            // File name will be "object name.blend"
            blendOutputFile = new FileInfo(cgfData.RootNode.Name + ".blend");
            using (BinaryWriter b = new BinaryWriter(File.Open(blendOutputFile.Name, FileMode.Create)))
            {
                WriteHeader(b);
            }
        }
Exemple #3
0
        public void WriteCollada(CgfData dataFile)  // Write the dae file
        {
            // The root of the functions to write Collada files
            // At this point, we should have a CgfData object, fully populated.
            Console.WriteLine();
            Console.WriteLine("*** Starting WriteCOLLADA() ***");
            Console.WriteLine();

            cgfData = dataFile;                                              // cgfData is now a pointer to the data object
            // File name will be "object name.dae"
            daeOutputFile = new FileInfo(cgfData.RootNode.Name + ".dae");
            GetSchema();                                                    // Loads the schema.  Needs error checking in case it's offline.
            WriteRootNode();
            WriteAsset();
            WriteLibrary_Images();
            WriteLibrary_Materials();
            WriteLibrary_Effects();
            WriteLibrary_Geometries();
            TextWriter writer = new StreamWriter(daeOutputFile.FullName);   // Makes the Textwriter object for the output

            mySerializer.Serialize(writer, daeObject);                      // Serializes the daeObject and writes to the writer
            Console.WriteLine("End of Write Collada");
        }
Exemple #4
0
        public FileInfo MtlFile;                                    // The obj .mtl file that we write.  Should be RootNode.Name + "_mtl.mtl".

        public void GetMtlFileName(CgfData cgfData)                 // Get FileInfo Mtlfile name from the MtlName chunks and read it. Assume pwd if no objectdir.
        {
            Datafile = cgfData;
            DirectoryInfo currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());

            String[] stringSeparators = new string[] { @"\", @"/" };    // to split up the paths
            String[] result;                                            // carries the results of the split

            //Console.WriteLine("*****  In MaterialFile.cs *****");
            MtlFile = new FileInfo(Datafile.RootNode.Name + "_mtl.mtl");
            // Console.WriteLine("Current dir is {0}", currentDir.FullName);
            // Find the number of material chunks.  if 1, then name is the mtl file name.  If many, find type 0x01.
            foreach (CgfData.ChunkMtlName mtlChunk in Datafile.CgfChunks.Where(a => a.chunkType == ChunkType.MtlName))
            {
                // mtlChunk.WriteChunk();
                if (mtlChunk.version == 0x800)
                {
                    // this is a parent material. Should be only one.  Don't care about the rest.
                    if (mtlChunk.MatType == 0x01 || mtlChunk.MatType == 0x10)
                    {
                        // parent material.  This is the one we want.
                        //Console.WriteLine("Mat type 0x01 found.");
                        if (mtlChunk.Name.Contains(@"\") || mtlChunk.Name.Contains(@"/"))
                        {
                            // I'm a qualified path, but don't know if objectdir exists.  Need to get name+.mtl without the path info.
                            result      = mtlChunk.Name.Split(stringSeparators, StringSplitOptions.None);
                            MtlFileName = result[result.Length - 1];        // Last element in mtlChunk.Name
                            //Console.WriteLine("MtlFileName (has slash) is {0}", MtlFileName);
                            if (Datafile.Args.ObjectDir != null)            // Check to see if objectdir was set.  if not, just check local dir
                            {
                                if (MtlFileName.EndsWith(".mtl"))
                                {
                                    XmlMtlFile = new FileInfo(Datafile.Args.ObjectDir + @"\" + mtlChunk.Name);
                                }
                                else
                                {
                                    XmlMtlFile = new FileInfo(Datafile.Args.ObjectDir + @"\" + mtlChunk.Name + ".mtl");
                                }
                            }
                            else
                            {
                                // No objectdir provided.  Only check current directory.
                                if (MtlFileName.EndsWith(".mtl"))
                                {
                                    XmlMtlFile = new FileInfo(currentDir + @"\" + MtlFileName);
                                }
                                else
                                {
                                    XmlMtlFile = new FileInfo(currentDir + @"\" + MtlFileName + ".mtl");
                                }
                            }
                        }
                        else
                        {
                            MtlFileName = mtlChunk.Name;                    // will add .mtl later.
                            //Console.WriteLine("MtlFileName (no slash) is {0}", MtlFileName);
                            XmlMtlFile = new FileInfo(currentDir + @"\" + MtlFileName + ".mtl");
                        }

                        //Console.WriteLine("MtlFile.Fullname is {0}", XmlMtlFile.FullName);

                        if (XmlMtlFile.Exists)
                        {
                            //Console.WriteLine("*** Found material file {0}.", XmlMtlFile.FullName);
                            ReadMtlFile(XmlMtlFile);
                        }
                        else
                        {
                            Console.WriteLine("*** 0x800 Unable to find material file {0}.  I'm probably going to not work well.", XmlMtlFile.FullName);
                            // Set up a dummy material and return.
                            MtlFormat material = new MtlFormat();
                            material.MaterialName  = Datafile.RootNode.Name;                // since there is no Name, use the node name.
                            material.Diffuse.Red   = 1;
                            material.Diffuse.Green = 0;
                            material.Diffuse.Blue  = 1;
                            Materials.Add(material);
                            MaterialNameArray = new MtlFormat[Materials.Count];
                            MaterialNameArray = Materials.ToArray();
                        }
                    }       // Not a material type 0x01 or 0x10.  Will be a child material.  Continue to next mtlname chunk
                }
                else  // version 0x802 file.  There will be just one, so return after it is found and read
                {
                    // Process version 0x802 files
                    //Console.WriteLine("In 0x802 section");
                    if (mtlChunk.Name.Contains(@"\") || mtlChunk.Name.Contains(@"/"))
                    {
                        // I'm a qualified path, but don't know if objectdir exists.  Need to get name+.mtl without the path info.
                        // Several cases here. Sometimes it has the .mtl ending, sometimes it doesn't.  GRRR
                        result      = mtlChunk.Name.Split(stringSeparators, StringSplitOptions.None);
                        MtlFileName = result[result.Length - 1];        // Last element in mtlChunk.Name
                        //Console.WriteLine("MtlFileName is {0}", MtlFileName);
                        if (Datafile.Args.ObjectDir != null)
                        {
                            if (MtlFileName.EndsWith(".mtl"))
                            {
                                XmlMtlFile = new FileInfo(Datafile.Args.ObjectDir + @"\" + mtlChunk.Name);
                            }
                            else
                            {
                                XmlMtlFile = new FileInfo(Datafile.Args.ObjectDir + @"\" + mtlChunk.Name + ".mtl");
                            }
                        }
                        else
                        {
                            // No objectdir provided.  Only check current directory.
                            if (MtlFileName.EndsWith(".mtl"))
                            {
                                XmlMtlFile = new FileInfo(currentDir + @"\" + MtlFileName);
                            }
                            else
                            {
                                XmlMtlFile = new FileInfo(currentDir + @"\" + MtlFileName + ".mtl");
                            }
                        }
                    }
                    else
                    {
                        // It's just a file name.  Search only in current directory.
                        MtlFileName = mtlChunk.Name;
                        XmlMtlFile  = new FileInfo(currentDir + @"\" + MtlFileName + ".mtl");
                        //Console.WriteLine("MtlFileName (short version) is {0}", MtlFileName);
                    }

                    //Console.WriteLine("MtlFile.Fullname is {0}", XmlMtlFile.FullName);

                    if (XmlMtlFile.Exists)
                    {
                        Console.WriteLine("*** 0x802 Found material file {0}.", XmlMtlFile.FullName);
                        ReadMtlFile(XmlMtlFile);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("*** 0x802 Unable to find material file {0}.  I'm probably going to not work well.", XmlMtlFile.FullName);
                        Console.WriteLine();
                        // Set up a dummy material and return.
                        // Console.WriteLine("Unable to find material file {0}.  Using group names.", Materialfile.FullName);
                        MtlFormat material = new MtlFormat();
                        material.MaterialName  = Datafile.RootNode.Name;                // since there is no Name, use the node name.
                        material.Diffuse.Red   = 1;
                        material.Diffuse.Green = 0;
                        material.Diffuse.Blue  = 1;
                        Materials.Add(material);
                        MaterialNameArray = new MtlFormat[Materials.Count];
                        MaterialNameArray = Materials.ToArray();
                    }
                    return;
                }
            }
            return;
        }
        public void WriteObjFile(CgfData cryData)
        {
            // We need to create the obj header, then for each submech write the vertex, UV and normal data.
            // First, let's figure out the name of the output file.  Should be <object name>.obj

            // Each Mesh will have a mesh subset and a series of datastream objects.  Need temporary pointers to these
            // so we can manipulate
            Console.WriteLine();
            // Console.WriteLine("*** Starting WriteObjFile() ***");
            Console.WriteLine();
            cgfData = cryData;

            // Get object name.  This is the Root Node chunk Name
            // Get the objOutputFile name
            objOutputFile = new FileInfo(cgfData.RootNode.Name + ".obj");
            Console.WriteLine("Output file is {0}", objOutputFile.Name);

            using (StreamWriter file = new StreamWriter(objOutputFile.Name))
            {
                string s1 = String.Format("# cgf-converter .obj export Version 0.84");
                file.WriteLine(s1);
                file.WriteLine("#");

                // Start populating the MatFile object
                cgfData.MatFile   = new MaterialFile();         // This will have all the material info
                cgfData.MatFile.b = file;                       // The stream writer, so the functions know where to write.
                //cgfData.MatFile.Datafile = this;                // Reference back to this CgfData so MatFile can read need info (like chunks)

                cgfData.MatFile.GetMtlFileName(cgfData);               // Gets the MtlFile name
                if (cgfData.MatFile.XmlMtlFile.Exists)
                {
                    Console.WriteLine("Matfile Full Name is {0}", cgfData.MatFile.XmlMtlFile.FullName);
                    cgfData.MatFile.WriteMtlLibInfo(file);  // writes the mtllib file.
                }
                else
                {
                    Console.WriteLine("Unable to get Mtl File name (not in expected locations).");
                }

                if (cgfData.RootNode.NumChildren == 0)
                {
                    Console.WriteLine("WriteOBJ:  Rootnode.numchildren == 0.");
                    // We have a root node with no children, so simple object.
                    string s3 = String.Format("o {0}", cgfData.RootNode.Name);
                    file.WriteLine(s3);

                    WriteObjNode(file, cgfData.RootNode);
                }
                else
                {
                    // Not a simple object.  Will need to call WriteObjNode for each Node Chunk
                    //Console.WriteLine("WriteOBJ:  Rootnode.numchildren != 0.");
                    foreach (CgfData.ChunkNode tmpNode in cgfData.CgfChunks.Where(a => a.chunkType == ChunkType.Node))
                    {
                        //tmpNode.WriteChunk();
                        //Console.WriteLine("Writing {0}", tmpNode.Name);
                        string s3 = String.Format("o {0}", tmpNode.Name);
                        file.WriteLine(s3);
                        // Grab the mesh and process that.
                        WriteObjNode(file, tmpNode);
                    }
                }

                // If this is a .chr file, just write out the hitbox info.  OBJ files can't do armatures.
                foreach (CgfData.ChunkCompiledPhysicalProxies tmpProxy in cgfData.CgfChunks.Where(a => a.chunkType == ChunkType.CompiledPhysicalProxies))
                {
                    //string s_hitbox = String.Format("o Hitbox");
                    //file.WriteLine(s_hitbox);
                    WriteObjHitBox(file, tmpProxy);
                }
            }  // End of writing the output file
        }