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