可可西

修改obj三维模型文件中坐标z为其相反数

具体做法是:打开一个obj文件,查找含有“v x y z"的字串,然后对z的字串进行符号处理:

若为"-",直接将该符号删除;不为"-"且z值不为0,则在z的字串前加上"-"。

示例(原始文件box.obj)

#
# object Box01
#

v -39.306316 -32.828358 -29.183971
v -39.306316 33.259296 -29.183971
v 39.726025 33.259296 -29.183971
v 39.726025 -32.828358 -29.183971
v -39.306316 -32.828358 26.914204
v 39.726025 -32.828358 26.914204
v 39.726025 33.259296 26.914204
v -39.306316 33.259296 26.914204

-----------------------------------

转换后的文件(box.obj.flip):

#
# object Box01
#

v -39.306316 -32.828358 29.183971
v -39.306316 33.259296 29.183971
v 39.726025 33.259296 29.183971
v 39.726025 -32.828358 29.183971
v -39.306316 -32.828358 -26.914204
v 39.726025 -32.828358 -26.914204
v 39.726025 33.259296 -26.914204
v -39.306316 33.259296 -26.914204

 具体代码实现:

 

 1 #include <fstream.h>
 2 #include <math.h>
 3 
 4 bool work(const char* path)
 5 {
 6     char chs[1000];
 7     double x,y,z;
 8     CString newPath(path);
 9     newPath += ".flip";
10     const char* chNewPath = newPath;
11     ofstream opf(chNewPath);
12     if (!opf.is_open()) return false;
13     ifstream pf(path);
14     if (!pf.is_open()) {opf.close();return false;}
15 
16     while (!pf.eof())
17     {
18         pf.getline(chs, 1000, '\n');
19         if (sscanf(chs, "v %lf %lf %lf", &x, &y, &z)==3)
20         {
21             CString strChs(chs);
22             int spacePos=strChs.ReverseFind(' ');
23             char c=strChs.GetAt(spacePos+1);
24 
25             opf<<(LPCTSTR)(strChs.Left(spacePos+1));
26             if (c=='-')
27             {
28                 opf<<(LPCTSTR)(strChs.Right(strChs.GetLength()-spacePos-2))<<endl;
29             }
30             else
31             {
32                 if (fabs(z)>1e-6) opf<<"-";
33                 opf<<(LPCTSTR)(strChs.Right(strChs.GetLength()-spacePos-1))<<endl;
34             }
35         }
36         else
37         {
38             opf << chs << endl;
39         }
40     }
41 
42     opf.close();
43     pf.close();
44     return true;
45 }

 

 

 

posted on 2012-04-15 23:06  可可西  阅读(1446)  评论(0编辑  收藏  举报

导航