Exemplo n.º 1
0
        void ExportToObj(lwGeomObjInfo geom, string obj_name, string pathtomodel, ExportGroup group, ExportObjInfo info)
        {
            System.IO.StreamWriter objwriter = info.objwriter;
            System.IO.StreamWriter mtlwriter = info.mtlwriter;

            int startvertex = info.totalvertexes;
            int startnormal = info.totalnormals;
            int starttexcoord = info.totaltexcoord;
            int startindex = info.totalindexes;

            bool texcoordexist = false;
            bool normalexist = false;

            lock (threadLock)
            {
                for (int i = 0; i < geom.mesh.header.vertex_num; i++)
                {
                    D3DXVECTOR3 pos = geom.mesh.vertex_seq[i];
                    pos = pos * geom.header.mat_local;

                    if (checkBox5.Checked)
                    {
                        float z = -pos.y;
                        pos.y = pos.z;
                        pos.z = z;
                    }

                    objwriter.WriteLine("v {0:0.0000} {1:0.0000} {2:0.0000}", pos.x, pos.y, pos.z);
                    info.totalvertexes++;
                }

                if ((geom.mesh.header.fvf & 0x10) != 0)
                {
                    normalexist = true;
                    for (int i = 0; i < geom.mesh.header.vertex_num; i++)
                    {
                        objwriter.WriteLine("vn {0:0.0000} {1:0.0000} {2:0.0000}", geom.mesh.normal_seq[i].x, geom.mesh.normal_seq[i].y, geom.mesh.normal_seq[i].z);
                        info.totalnormals++;
                    }
                }
                if ((geom.mesh.header.fvf & 0x100) != 0 || (geom.mesh.header.fvf & 0x200) != 0 || (geom.mesh.header.fvf & 0x300) != 0 || (geom.mesh.header.fvf & 0x400) != 0)
                {
                    texcoordexist = true;
                    for (int i = 0; i < geom.mesh.header.vertex_num; i++)
                    {
                        if (checkBox4.Checked)
                            objwriter.WriteLine("vt {0:0.0000} {1:0.0000}", geom.mesh.texcoord0_seq[i].x, 1 - geom.mesh.texcoord0_seq[i].y);
                        else
                            objwriter.WriteLine("vt {0:0.0000} {1:0.0000}", geom.mesh.texcoord0_seq[i].x, geom.mesh.texcoord0_seq[i].y);
                        info.totaltexcoord++;
                    }
                }
            }

            for (int i = 0; i < geom.mesh.header.subset_num; i++)
            {
                string mtlname = obj_name;
                lock (threadLock)
                {
                    objwriter.WriteLine("\ng {0}_{1}", mtlname, i);
                    objwriter.WriteLine("usemtl {0}-{1}\n", mtlname,i);
                    objwriter.WriteLine("s off");

                    for (int j = 0; j < geom.mesh.subset_seq[i].primitive_num * 3; j += 3)
                    {
                        objwriter.Write("f ");
                        for (int k = 0; k < 3; k++)
                        {
                            uint facevertex = (uint)(geom.mesh.index_seq[j + k + geom.mesh.subset_seq[i].start_index] + startvertex) + 1;
                            uint facenormal = (uint)(geom.mesh.index_seq[j + k + geom.mesh.subset_seq[i].start_index] + startnormal) + 1;
                            uint facetexcoord = (uint)(geom.mesh.index_seq[j + k + geom.mesh.subset_seq[i].start_index] + starttexcoord) + 1;
                            if (normalexist && texcoordexist)
                                objwriter.Write("{0}/{1}/{2} ", facevertex, facetexcoord, facenormal);
                            else if (normalexist && !texcoordexist)
                                objwriter.Write("{0}//{2} ", facevertex, facenormal);
                            else if (!normalexist && texcoordexist)
                                objwriter.Write("{0}/{1} ", facevertex, facetexcoord);
                            else
                                objwriter.Write("{0} ", facevertex);
                        }
                        objwriter.Write("\n");
                    }

                    mtlwriter.WriteLine("newmtl {0}-{1}", mtlname,i);
                    mtlwriter.WriteLine("Ka {0} {1} {2}", geom.mtl_seq[i].mtl.amb.r, geom.mtl_seq[i].mtl.amb.g, geom.mtl_seq[i].mtl.amb.b);
                    mtlwriter.WriteLine("Kd {0} {1} {2}", geom.mtl_seq[i].mtl.dif.r, geom.mtl_seq[i].mtl.dif.g, geom.mtl_seq[i].mtl.dif.b);
                    mtlwriter.WriteLine("Ks {0} {1} {2}", geom.mtl_seq[i].mtl.spe.r, geom.mtl_seq[i].mtl.spe.g, geom.mtl_seq[i].mtl.spe.b);

                    string newtexturepath = RelativePath(System.IO.Path.GetDirectoryName(textBox1.Text) + "\\", System.IO.Path.GetDirectoryName(textBox2.Text) + "\\");
                    mtlwriter.WriteLine("map_Kd {0}", newtexturepath + CutString(geom.mtl_seq[i].tex_seq[0].file_name));
                }

                if (checkBox2.Checked)
                {
                    string path = System.IO.Path.GetDirectoryName(pathtomodel).Replace("\\model\\", "\\texture\\");
                    System.Drawing.Bitmap bmp = parentForm.LoadBitmaByTextureName(path + "\\" + CutString(geom.mtl_seq[i].tex_seq[0].file_name));
                    bmp.Save(System.IO.Path.GetDirectoryName(textBox2.Text) + "\\" + CutString(geom.mtl_seq[i].tex_seq[0].file_name));
                }
                else
                {
                    string path = System.IO.Path.GetDirectoryName(pathtomodel).Replace("\\model\\", "\\texture\\");
                    string filename = parentForm.GetRightTextureName(path + "\\" + CutString(geom.mtl_seq[i].tex_seq[0].file_name));

                    if (filename != null)
                        System.IO.File.Copy(filename, System.IO.Path.GetDirectoryName(textBox2.Text) + "\\" + CutString(geom.mtl_seq[i].tex_seq[0].file_name), true);
                }
            }
        }
Exemplo n.º 2
0
        void ExportToObj(ExportGroup group,ExportObjInfo info)
        {
            System.IO.StreamWriter objwriter = info.objwriter;
            System.IO.StreamWriter mtlwriter = info.mtlwriter;

            lock (threadLock)
            {
                objwriter.WriteLine("mtllib {0}.mtl\n", group.targetfilename);
                objwriter.WriteLine("o {0}\n", System.IO.Path.GetFileNameWithoutExtension(group.targetfilename));
            }

            int objId=0;
            foreach (lwGeomObjInfo geom in group.geoms)
            {
                ExportToObj(geom,
                    System.IO.Path.GetFileNameWithoutExtension(group.geomsoriginpath[objId]) + "_" + objId,
                    group.geomsoriginpath[objId],
                    group,
                    info);
                objId++;
            }

            int mdlId=0;
            foreach (lwModelObjInfo model in group.models)
            {
                for (int i = 0; i < model.geom_obj_num; i++)
                {
                    ExportToObj(model.geom_obj_seq[i],
                        System.IO.Path.GetFileNameWithoutExtension(group.modelsoriginpath[mdlId]) + "-" + i + "_" + objId,
                        group.modelsoriginpath[mdlId],
                        group,
                        info);
                    objId++;
                }
                mdlId++;
            }

            mtlwriter.Close();
            objwriter.Close();
        }
Exemplo n.º 3
0
        void Export(ExportGroup group)
        {
            if (listBoxFormats.SelectedIndex == 0)
            {
                string filename=textBox3.Text;
                if (!checkBox3.Checked)
                {
                    if (group.geomsoriginpath.Count > 0) filename = group.geomsoriginpath[0];
                    else if (group.modelsoriginpath.Count > 0) filename = group.modelsoriginpath[0];
                    else if (group.bonesoriginpath.Count > 0) filename = group.bonesoriginpath[0];
                    else return;
                }

                ExportObjInfo info = new ExportObjInfo(System.IO.Path.GetDirectoryName(textBox1.Text) + "\\" + System.IO.Path.GetFileName(filename));
                group.targetfilename = System.IO.Path.GetFileName(filename);

                ExportToObj(group, info);

                info.stream.Close();
                info.mtlstream.Close();
            }
        }