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 loadPolygonsAsMeshese)
|
---|
47 | {
|
---|
48 | map<string, Vector3, ltstr> vht; // hash table for vectors
|
---|
49 | map<string, Vector3, ltstr> cht; // hash table for colors
|
---|
50 |
|
---|
51 | FILE *file;
|
---|
52 | char str[80];
|
---|
53 | Face *face;
|
---|
54 | //Vector3 *vptr;
|
---|
55 |
|
---|
56 | float x,y,z;
|
---|
57 | int i;
|
---|
58 |
|
---|
59 | if ((file = fopen(filename.c_str(),"rt")) == NULL)
|
---|
60 | return false;
|
---|
61 |
|
---|
62 | SceneGraphNode *root = new SceneGraphNode;
|
---|
63 | Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
64 |
|
---|
65 | int meshGrouping;
|
---|
66 | Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
|
---|
67 | meshGrouping);
|
---|
68 |
|
---|
69 | while(fscanf(file,"%s",str)!=EOF) {
|
---|
70 | switch (str[0]) {
|
---|
71 |
|
---|
72 | case 'c':
|
---|
73 | if (strcmp(str,"c_rgb")==0)
|
---|
74 | {
|
---|
75 | cout<<"c";
|
---|
76 | cout.flush();
|
---|
77 | /* vertex */
|
---|
78 | fscanf(file,"%s",str); // label
|
---|
79 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
80 |
|
---|
81 | cht[str] = Vector3(x,y,z); // swap the file coordinates
|
---|
82 | }
|
---|
83 | break;
|
---|
84 |
|
---|
85 |
|
---|
86 | case 'v': // is it v ?
|
---|
87 | cout<<".";
|
---|
88 | cout.flush();
|
---|
89 | /* vertex */
|
---|
90 | fscanf(file,"%s",str); // label
|
---|
91 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
92 |
|
---|
93 | vht[str] = Vector3(x,z,-y); // swap the file coordinates
|
---|
94 | // vptr = new Vector3C(x,y,z); // swap the file coordinates
|
---|
95 | break;
|
---|
96 |
|
---|
97 | case 'f': {
|
---|
98 | cout<<"+";
|
---|
99 | vector<Vector3> vertices;
|
---|
100 | VertexIndexContainer vertexIndices;
|
---|
101 |
|
---|
102 | /* face */
|
---|
103 | fscanf(file,"%s",str); // label may be ?
|
---|
104 | if (str[0]!='(')
|
---|
105 | fscanf(file,"%s",str);
|
---|
106 |
|
---|
107 | i = 0; // index of the vertex
|
---|
108 |
|
---|
109 |
|
---|
110 | map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
|
---|
111 |
|
---|
112 | if (c == vht.end()) {
|
---|
113 | cout<<"error reading file : "<<
|
---|
114 | filename<<" ---missing vertex:"<<str<<endl; exit(1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | vertices.push_back((*c).second);
|
---|
118 |
|
---|
119 | fscanf(file,"%s",str); // get the vertex label
|
---|
120 |
|
---|
121 | while(str[0]!=')') {
|
---|
122 |
|
---|
123 | c = vht.find(str);
|
---|
124 |
|
---|
125 | if (c == vht.end())
|
---|
126 | {
|
---|
127 | cout<<"error reading file : "<<filename<<" -- missing vertex\n";
|
---|
128 | exit(1); }
|
---|
129 |
|
---|
130 | vertices.push_back((*c).second);
|
---|
131 | fscanf(file,"%s",str); // get the next vertex label
|
---|
132 | }
|
---|
133 |
|
---|
134 | // add vertices to the mesh vertex list
|
---|
135 | int index = (int)currentMesh->mVertices.size();
|
---|
136 | for (i=0; i < vertices.size(); i++, index++) {
|
---|
137 | currentMesh->mVertices.push_back(vertices[i]);
|
---|
138 | vertexIndices.push_back(index);
|
---|
139 | }
|
---|
140 |
|
---|
141 | face = new Face(vertexIndices);
|
---|
142 |
|
---|
143 | fscanf(file,"%s",str); // get the next vertex label
|
---|
144 |
|
---|
145 | if (str[0]!=';') {
|
---|
146 | c = cht.find(str);
|
---|
147 | if (currentMesh->mMaterial == NULL) {
|
---|
148 | Material *mat = MaterialManager::GetSingleton()->CreateResource();
|
---|
149 | if (c != cht.end()) {
|
---|
150 | mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
|
---|
151 | currentMesh->mMaterial = mat;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | currentMesh->AssignRandomMaterial();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | currentMesh->AddFace(face);
|
---|
159 | if (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping) {
|
---|
160 | if (ROTATE_SCENE)
|
---|
161 | RotateMesh(currentMesh);
|
---|
162 |
|
---|
163 | currentMesh->Preprocess();
|
---|
164 | // make an instance of this mesh
|
---|
165 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
166 | root->mGeometry.push_back(mi);
|
---|
167 | currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
168 | }
|
---|
169 | }
|
---|
170 | break;
|
---|
171 | } /* end face */
|
---|
172 |
|
---|
173 | // get the rest of the line
|
---|
174 | fgets(str,80,file);
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (currentMesh->mVertices.size()) {
|
---|
178 | if (1)
|
---|
179 | RotateMesh(currentMesh);
|
---|
180 |
|
---|
181 | currentMesh->Preprocess();
|
---|
182 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
183 | root->mGeometry.push_back(mi);
|
---|
184 | }
|
---|
185 |
|
---|
186 | fclose(file);
|
---|
187 |
|
---|
188 | *proot = root;
|
---|
189 |
|
---|
190 | return true;
|
---|
191 | }
|
---|
192 |
|
---|
193 | } |
---|