1 | #include <stdlib.h>
|
---|
2 | #include <iostream>
|
---|
3 | #include <list>
|
---|
4 | #include <map>
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | #include "Vector3.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "SceneGraph.h"
|
---|
10 |
|
---|
11 | #include "UnigraphicsParser.h"
|
---|
12 | #include "Material.h"
|
---|
13 | #include "Environment.h"
|
---|
14 | #include "ResourceManager.h"
|
---|
15 |
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
18 |
|
---|
19 | #define ROTATE_SCENE 0
|
---|
20 | // HACK
|
---|
21 | static void RotateMesh(Mesh *mesh)
|
---|
22 | {
|
---|
23 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
24 |
|
---|
25 | const float angle = 30.0f * PI / 180.0f;
|
---|
26 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
27 |
|
---|
28 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
29 | {
|
---|
30 | (*it) = rot * (*it);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | struct ltstr
|
---|
36 | {
|
---|
37 | bool operator()(const string s1, const string s2) const
|
---|
38 | {
|
---|
39 | return s1 < s2;
|
---|
40 | }
|
---|
41 | };
|
---|
42 |
|
---|
43 | bool
|
---|
44 | UnigraphicsParser::ParseFile(const string filename,
|
---|
45 | SceneGraphNode **proot,
|
---|
46 | const bool loadPolygonsAsMeshes,
|
---|
47 | vector<FaceParentInfo> *parents)
|
---|
48 | {
|
---|
49 | map<string, Vector3, ltstr> vht; // hash table for vectors
|
---|
50 | map<string, Vector3, ltstr> cht; // hash table for colors
|
---|
51 |
|
---|
52 | FILE *file;
|
---|
53 | char str[80];
|
---|
54 | Face *face;
|
---|
55 | //Vector3 *vptr;
|
---|
56 |
|
---|
57 | float x,y,z;
|
---|
58 | int i;
|
---|
59 |
|
---|
60 | if ((file = fopen(filename.c_str(),"rt")) == NULL)
|
---|
61 | return false;
|
---|
62 |
|
---|
63 | SceneGraphNode *root = new SceneGraphNode;
|
---|
64 | Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
65 |
|
---|
66 | int meshGrouping;
|
---|
67 | Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
|
---|
68 | meshGrouping);
|
---|
69 |
|
---|
70 | while(fscanf(file,"%s",str)!=EOF) {
|
---|
71 | switch (str[0]) {
|
---|
72 |
|
---|
73 | case 'c':
|
---|
74 | if (strcmp(str,"c_rgb")==0)
|
---|
75 | {
|
---|
76 | cout<<"c";
|
---|
77 | cout.flush();
|
---|
78 | /* vertex */
|
---|
79 | fscanf(file,"%s",str); // label
|
---|
80 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
81 |
|
---|
82 | cht[str] = Vector3(x,y,z); // swap the file coordinates
|
---|
83 | }
|
---|
84 | break;
|
---|
85 |
|
---|
86 |
|
---|
87 | case 'v': // is it v ?
|
---|
88 | cout<<".";
|
---|
89 | cout.flush();
|
---|
90 | /* vertex */
|
---|
91 | fscanf(file,"%s",str); // label
|
---|
92 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
93 |
|
---|
94 | vht[str] = Vector3(x,z,-y); // swap the file coordinates
|
---|
95 | // vptr = new Vector3C(x,y,z); // swap the file coordinates
|
---|
96 | break;
|
---|
97 |
|
---|
98 | case 'f': {
|
---|
99 | cout<<"+";
|
---|
100 | vector<Vector3> vertices;
|
---|
101 | VertexIndexContainer vertexIndices;
|
---|
102 |
|
---|
103 | /* face */
|
---|
104 | fscanf(file,"%s",str); // label may be ?
|
---|
105 | if (str[0]!='(')
|
---|
106 | fscanf(file,"%s",str);
|
---|
107 |
|
---|
108 | i = 0; // index of the vertex
|
---|
109 |
|
---|
110 |
|
---|
111 | map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
|
---|
112 |
|
---|
113 | if (c == vht.end()) {
|
---|
114 | cout<<"error reading file : "<<
|
---|
115 | filename<<" ---missing vertex:"<<str<<endl; exit(1);
|
---|
116 | }
|
---|
117 |
|
---|
118 | vertices.push_back((*c).second);
|
---|
119 |
|
---|
120 | fscanf(file,"%s",str); // get the vertex label
|
---|
121 |
|
---|
122 | while(str[0]!=')') {
|
---|
123 |
|
---|
124 | c = vht.find(str);
|
---|
125 |
|
---|
126 | if (c == vht.end())
|
---|
127 | {
|
---|
128 | cout<<"error reading file : "<<filename<<" -- missing vertex\n";
|
---|
129 | exit(1); }
|
---|
130 |
|
---|
131 | vertices.push_back((*c).second);
|
---|
132 | fscanf(file,"%s",str); // get the next vertex label
|
---|
133 | }
|
---|
134 |
|
---|
135 | // add vertices to the mesh vertex list
|
---|
136 | int index = (int)currentMesh->mVertices.size();
|
---|
137 | for (i=0; i < vertices.size(); i++, index++) {
|
---|
138 | currentMesh->mVertices.push_back(vertices[i]);
|
---|
139 | vertexIndices.push_back(index);
|
---|
140 | }
|
---|
141 |
|
---|
142 | face = new Face(vertexIndices);
|
---|
143 |
|
---|
144 | fscanf(file,"%s",str); // get the next vertex label
|
---|
145 |
|
---|
146 | if (str[0]!=';') {
|
---|
147 | c = cht.find(str);
|
---|
148 | if (currentMesh->mMaterial == NULL) {
|
---|
149 | Material *mat = MaterialManager::GetSingleton()->CreateResource();
|
---|
150 | if (c != cht.end()) {
|
---|
151 | mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
|
---|
152 | currentMesh->mMaterial = mat;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | currentMesh->AssignRandomMaterial();
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | currentMesh->AddFace(face);
|
---|
160 | if (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping) {
|
---|
161 | if (ROTATE_SCENE)
|
---|
162 | RotateMesh(currentMesh);
|
---|
163 |
|
---|
164 | currentMesh->Preprocess();
|
---|
165 | // make an instance of this mesh
|
---|
166 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
167 | root->mGeometry.push_back(mi);
|
---|
168 | currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | break;
|
---|
172 | } /* end face */
|
---|
173 |
|
---|
174 | // get the rest of the line
|
---|
175 | fgets(str,80,file);
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (currentMesh->mVertices.size()) {
|
---|
179 | if (1)
|
---|
180 | RotateMesh(currentMesh);
|
---|
181 |
|
---|
182 | currentMesh->Preprocess();
|
---|
183 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
184 | root->mGeometry.push_back(mi);
|
---|
185 | }
|
---|
186 |
|
---|
187 | fclose(file);
|
---|
188 |
|
---|
189 | *proot = root;
|
---|
190 |
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|