Пример #1
0
    public static void Main()
    {
        SearchOption SOAD = SearchOption.AllDirectories;

        string[] filesName = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.b3d", SOAD);
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

        foreach (var fileIn in filesName)
        {
            using (BinaryReader br = new BinaryReader(File.Open(fileIn, FileMode.Open)))
            {
                int submesh_count = br.ReadInt32();

                for (int o = 0; o < submesh_count; o++)
                {
                    using (StreamWriter sw = new StreamWriter(fileIn + o + ".obj"))
                    {
                        O3dModel o3d = new O3dModel();

                        uint type = br.ReadUInt32();

                        br.ReadBytes(8);
                        br.ReadBytes(48);
                        for (int i = 0; i < 6; i++)
                        {
                            br.ReadSingle();
                        }

                        vertexCount = br.ReadUInt32();
                        faceCount   = br.ReadUInt32();
                        uint faceCount2   = br.ReadUInt32();
                        uint vertexCount1 = br.ReadUInt32();

                        for (int i = 0; i < vertexCount; i++)
                        {
                            o3d_vertex newv = new o3d_vertex();
                            newv.x = br.ReadSingle();
                            newv.y = br.ReadSingle();
                            newv.z = br.ReadSingle();
                            o3d.vertexes.Add(newv);
                        }

                        for (int i = 0; i < faceCount; i++)
                        {
                            o3d_face newf = new o3d_face();

                            br.ReadByte();
                            br.ReadByte();
                            br.ReadByte();
                            br.ReadByte();

                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();
                            br.ReadSingle();

                            for (int ii = 0; ii < 4; ii++)
                            {
                                newf.index[ii] = br.ReadUInt16();
                            }
                            o3d.faces.Add(newf);

                            br.ReadUInt16();
                            br.ReadUInt16();
                            br.ReadUInt16();
                        }

// ========================================================

                        foreach (var v in o3d.vertexes)
                        {
                            sw.WriteLine("v " + v.x + " " + v.y + " " + v.z);
                        }

                        var enumerator = o3d.faces.GetEnumerator();

                        foreach (var v in o3d.faces)
                        {
                            if (v.index[0] == 65535 || v.index[1] == 65535 || v.index[2] == 65535)
                            {
                                enumerator.MoveNext();
                            }
                            sw.WriteLine("f " + (v.index[0] + 1) + " " + (v.index[1] + 1) + " " + (v.index[2] + 1));
                            if (v.index[3] != 65535)
                            {
                                sw.WriteLine("f " + (v.index[0] + 1) + " " + (v.index[2] + 1) + " " + (v.index[3] + 1));
                            }
                        }
                    }
                }
            }
        } // foreach (var file in filesName)
    }     // public static void Main()
Пример #2
0
    public static void Main()
    {
        SearchOption SOAD = SearchOption.AllDirectories;

        string[] filesName = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.o3d", SOAD);
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

        foreach (var fileIn in filesName)
        {
            using (BinaryReader br = new BinaryReader(File.Open(fileIn, FileMode.Open)))
            {
                using (StreamWriter sw = new StreamWriter(fileIn + ".ply"))
                {
                    O3dModel o3d = new O3dModel();

                    vertexCount = br.ReadUInt32();
                    faceCount   = br.ReadUInt32();
                    //  Two 32bit words of unknown contents follow.
                    br.ReadInt32();
                    br.ReadInt32();

                    o3d.vertexes = new o3d_vertex[vertexCount]; // malloc(vertexCount * sizeof(o3d.vertexes[0]))
                    //  fread(o3d.vertexes, sizeof(o3d.vertexes[0]), vertexCount, fileIn); // == vertexCount
                    for (int i = 0; i < vertexCount; i++)
                    {
                        o3d.vertexes[i] = new o3d_vertex();

                        o3d.vertexes[i].x = br.ReadSingle();
                        o3d.vertexes[i].y = br.ReadSingle();
                        o3d.vertexes[i].z = br.ReadSingle();
                    }

                    o3d.faces = new o3d_face[faceCount]; // malloc(faceCount * sizeof(o3d.faces[0]));
                    //  fread(o3d.faces, sizeof(o3d.faces[0]), faceCount, fileIn); // == faceCount

                    for (int i = 0; i < faceCount; i++)
                    {
                        o3d.faces[i] = new o3d_face();

                        o3d.faces[i].color   = new o3d_color();
                        o3d.faces[i].color.b = br.ReadByte();
                        o3d.faces[i].color.g = br.ReadByte();
                        o3d.faces[i].color.r = br.ReadByte();
                        o3d.faces[i].color.a = br.ReadByte();

                        for (int ii = 0; ii < 4; ii++)
                        {
                            o3d.faces[i].texCoords[ii]   = new o3d_texcoord();
                            o3d.faces[i].texCoords[ii].u = br.ReadSingle();
                            o3d.faces[i].texCoords[ii].v = br.ReadSingle();
                        }

                        for (int ii = 0; ii < 4; ii++)
                        {
                            o3d.faces[i].index[ii] = br.ReadUInt16();
                        }

                        o3d.faces[i].unknown = br.ReadUInt32();

                        o3d.faces[i].texNumber = br.ReadUInt16();
                    }

// ========================================================

                    sw.WriteLine("ply");
                    sw.WriteLine("format ascii 1.0");
                    //sw.WriteLine("comment TextureFile " + model_texture.jpg);
                    sw.WriteLine("element vertex " + vertexCount);  // количество вершин

                    sw.WriteLine("property float x");
                    sw.WriteLine("property float y");
                    sw.WriteLine("property float z");

                    //sw.WriteLine("property uchar red") ;
                    //sw.WriteLine("property uchar green") ;
                    //sw.WriteLine("property uchar blue") ;
                    //sw.WriteLine("property uchar alpha") ;

                    foreach (var v in o3d.faces)
                    {
                        if (v.index[3] != 65535)
                        {
                            faceCount++;
                        }
                    }

                    sw.WriteLine("element face " + faceCount); // общее количество граней

                    sw.WriteLine("property list uchar int vertex_indices");
                    sw.WriteLine("property list uchar double texcoord");
                    //sw.WriteLine("property int texnumber");

                    sw.WriteLine("end_header");

                    foreach (var v in o3d.vertexes)
                    {
                        sw.WriteLine(v.x + " " + v.y + " " + v.z);
                    }

                    // буду разбивать квады на треугольники, потому что...

                    foreach (var v in o3d.faces)
                    {
                        sw.WriteLine("3 " + v.index[0] + " " + v.index[1] + " " + v.index[2] + " 6 " +
                                     +v.texCoords[0].u / 255f + " " + v.texCoords[0].v / 255f + " "
                                     + v.texCoords[1].u / 255f + " " + v.texCoords[1].v / 255f + " "
                                     + v.texCoords[2].u / 255f + " " + v.texCoords[2].v / 255f + " " /*+ v.texNumber*/);

                        //  0 1 2 3 => 0 1 2 + 0 2 3

                        if (v.index[3] != 65535)
                        {
                            sw.WriteLine("3 " + v.index[0] + " " + v.index[2] + " " + v.index[3] + " 6 " +
                                         +v.texCoords[0].u / 255f + " " + v.texCoords[0].v / 255f + " "
                                         + v.texCoords[2].u / 255f + " " + v.texCoords[2].v / 255f + " "
                                         + v.texCoords[3].u / 255f + " " + v.texCoords[3].v / 255f + " " /*+ v.texNumber*/);
                        }
                    }
                }
            }
        } // foreach (var file in filesName)
    }     // public static void Main()