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 *root,
|
---|
46 | const bool loadMeshes,
|
---|
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 |
|
---|
56 | float x,y,z;
|
---|
57 | //int i;
|
---|
58 |
|
---|
59 | if ((file = fopen(filename.c_str(),"rt")) == NULL)
|
---|
60 | return false;
|
---|
61 |
|
---|
62 | Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
63 |
|
---|
64 | int meshGrouping;
|
---|
65 | Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
|
---|
66 | meshGrouping);
|
---|
67 |
|
---|
68 | while(fscanf(file,"%s",str)!=EOF)
|
---|
69 | {
|
---|
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 | {
|
---|
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 | map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
|
---|
109 |
|
---|
110 | if (c == vht.end())
|
---|
111 | {
|
---|
112 | cout<<"error reading file : "<< filename
|
---|
113 | <<" ---missing vertex:"<<str<<endl; exit(1);
|
---|
114 | }
|
---|
115 |
|
---|
116 | vertices.push_back((*c).second);
|
---|
117 |
|
---|
118 | fscanf(file,"%s",str); // get the vertex label
|
---|
119 |
|
---|
120 | while(str[0]!=')')
|
---|
121 | {
|
---|
122 | c = vht.find(str);
|
---|
123 | if (c == vht.end())
|
---|
124 | {
|
---|
125 | cout << "error reading file : " << filename << " -- missing vertex\n";
|
---|
126 | exit(1);
|
---|
127 | }
|
---|
128 |
|
---|
129 | vertices.push_back((*c).second);
|
---|
130 | fscanf(file,"%s",str); // get the next vertex label
|
---|
131 | }
|
---|
132 |
|
---|
133 | // add vertices to the mesh vertex list
|
---|
134 | int index = (int)currentMesh->mVertices.size();
|
---|
135 |
|
---|
136 | for (int i=0; i < (int)vertices.size(); ++ i, ++ index)
|
---|
137 | {
|
---|
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 | {
|
---|
148 | c = cht.find(str);
|
---|
149 | if (currentMesh->mMaterial == NULL)
|
---|
150 | {
|
---|
151 | Material *mat = MaterialManager::GetSingleton()->CreateResource();
|
---|
152 | if (c != cht.end())
|
---|
153 | {
|
---|
154 | mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
|
---|
155 | currentMesh->mMaterial = mat;
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | currentMesh->AssignRandomMaterial();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | currentMesh->AddFace(face);
|
---|
165 | if (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping)
|
---|
166 | {
|
---|
167 | if (ROTATE_SCENE)
|
---|
168 | {
|
---|
169 | RotateMesh(currentMesh);
|
---|
170 | }
|
---|
171 |
|
---|
172 | currentMesh->Preprocess();
|
---|
173 | // make an instance of this mesh
|
---|
174 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
175 | root->mGeometry.push_back(mi);
|
---|
176 | currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
177 | }
|
---|
178 | }
|
---|
179 | break;
|
---|
180 | } /* end face */
|
---|
181 |
|
---|
182 | // get the rest of the line
|
---|
183 | fgets(str,80,file);
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (!currentMesh->mVertices.empty())
|
---|
187 | {
|
---|
188 | if (ROTATE_SCENE)
|
---|
189 | {
|
---|
190 | RotateMesh(currentMesh);
|
---|
191 | }
|
---|
192 |
|
---|
193 | currentMesh->Preprocess();
|
---|
194 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
195 | root->mGeometry.push_back(mi);
|
---|
196 | }
|
---|
197 |
|
---|
198 | fclose(file);
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|