1 | #include "VboFormatConverter.h"
|
---|
2 | #include "SimpleTri.h"
|
---|
3 | #include "SimpleVec.h"
|
---|
4 | #include "gzstream.h"
|
---|
5 | #include <iostream>
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 |
|
---|
11 | VboFormatConverter::VboFormatConverter()
|
---|
12 | {}
|
---|
13 |
|
---|
14 |
|
---|
15 | VboFormatConverter::~VboFormatConverter()
|
---|
16 | {
|
---|
17 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
18 | {
|
---|
19 | delete [] mGeometry[i]->mVertices;
|
---|
20 | delete [] mGeometry[i]->mNormals;
|
---|
21 | delete [] mGeometry[i]->mTangents;
|
---|
22 | delete [] mGeometry[i]->mTexcoords;
|
---|
23 |
|
---|
24 | delete mGeometry[i];
|
---|
25 | }
|
---|
26 |
|
---|
27 | mGeometry.clear();
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | bool VboFormatConverter::Convert(const string &filename,
|
---|
32 | const std::string &outputFilename)
|
---|
33 | {
|
---|
34 | mNumShapes = 0;
|
---|
35 |
|
---|
36 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
37 | {
|
---|
38 | delete [] mGeometry[i]->mVertices;
|
---|
39 | delete [] mGeometry[i]->mNormals;
|
---|
40 | delete [] mGeometry[i]->mTexcoords;
|
---|
41 |
|
---|
42 | delete mGeometry[i];
|
---|
43 | }
|
---|
44 |
|
---|
45 | mGeometry.clear();
|
---|
46 |
|
---|
47 | if (!ReadFile(filename))
|
---|
48 | {
|
---|
49 | cerr << "could not read file" << endl;
|
---|
50 | return false;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (!WriteFile(outputFilename))
|
---|
54 | {
|
---|
55 | cerr << "could not write file" << endl;
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | bool VboFormatConverter::ReadFile(const string &filename)
|
---|
65 | {
|
---|
66 | FILE *file;
|
---|
67 | if ((file = fopen(filename.c_str(), "r")) == NULL) return false;
|
---|
68 |
|
---|
69 | int line = 0;
|
---|
70 |
|
---|
71 | const int len = 10000;
|
---|
72 | char str[len];
|
---|
73 |
|
---|
74 | int numElements;
|
---|
75 |
|
---|
76 | // first line only holds number of vertices
|
---|
77 | fgets(str, len, file);
|
---|
78 | sscanf(str, "%d", &numElements);
|
---|
79 |
|
---|
80 | Geometry *geom = new Geometry();
|
---|
81 |
|
---|
82 | // convert the triangles to geometry
|
---|
83 | geom->mVertexCount = numElements;
|
---|
84 | geom->mTexcoordCount = numElements;
|
---|
85 |
|
---|
86 | geom->mVertices = new SimpleVec[numElements];
|
---|
87 | geom->mNormals = new SimpleVec[numElements];
|
---|
88 | geom->mTangents = new SimpleVec[numElements];
|
---|
89 | geom->mTexcoords = new Texcoord[numElements];
|
---|
90 |
|
---|
91 | SimpleVec vtx, normal, tangent;
|
---|
92 | Texcoord tex;
|
---|
93 |
|
---|
94 | cout << "elements: " << numElements << endl;
|
---|
95 | while (fgets(str, len, file) != NULL)
|
---|
96 | {
|
---|
97 | sscanf(str, "%f %f %f %f %f %f %f %f %f %f %f",
|
---|
98 | &vtx.x, &vtx.y, &vtx.z,
|
---|
99 | &normal.x, &normal.y, &normal.z,
|
---|
100 | &tex.first, &tex.second,
|
---|
101 | &tangent.x, &tangent.y, &tangent.z);
|
---|
102 |
|
---|
103 | const float scale = 0.05f;
|
---|
104 |
|
---|
105 | geom->mVertices[line].x = vtx.x * scale;
|
---|
106 | geom->mVertices[line].y = vtx.z * scale;
|
---|
107 | geom->mVertices[line].z = vtx.y * scale;
|
---|
108 |
|
---|
109 | geom->mNormals[line].x = normal.x;
|
---|
110 | geom->mNormals[line].y = normal.z;
|
---|
111 | geom->mNormals[line].z = normal.y;
|
---|
112 |
|
---|
113 | geom->mTangents[line].x = tangent.x;
|
---|
114 | geom->mTangents[line].y = tangent.z;
|
---|
115 | geom->mTangents[line].z = tangent.y;
|
---|
116 |
|
---|
117 | geom->mTexcoords[line].first = tex.first;
|
---|
118 | geom->mTexcoords[line].second = 1.0f - tex.second;
|
---|
119 |
|
---|
120 | static int dummy = 0;
|
---|
121 | if (dummy ++ < 5) cout << "tangent: " << geom->mTangents[line] << endl;
|
---|
122 | ++ line;
|
---|
123 | }
|
---|
124 |
|
---|
125 | mGeometry.push_back(geom);
|
---|
126 |
|
---|
127 | mNumShapes = 1;
|
---|
128 | fclose(file);
|
---|
129 |
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | void VboFormatConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
135 | {
|
---|
136 | int vertexCount = geom->mVertexCount;
|
---|
137 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
138 |
|
---|
139 | str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
|
---|
140 | str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
|
---|
141 | str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
|
---|
142 |
|
---|
143 | int texCoordCount = geom->mTexcoordCount;
|
---|
144 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
145 |
|
---|
146 | if (texCoordCount)
|
---|
147 | {
|
---|
148 | str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | ///////
|
---|
153 | //-- texture
|
---|
154 |
|
---|
155 | //int texId = -1;
|
---|
156 | int texId = 0;
|
---|
157 |
|
---|
158 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
159 | //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
|
---|
160 |
|
---|
161 | bool alphaTestEnabled = false;
|
---|
162 | //bool cullFaceEnabled = false;
|
---|
163 | bool cullFaceEnabled = true;
|
---|
164 |
|
---|
165 | str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
166 | str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
167 |
|
---|
168 | // material
|
---|
169 | bool hasMaterial = true;
|
---|
170 | //bool hasMaterial = false;
|
---|
171 | str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
172 |
|
---|
173 | if (hasMaterial)
|
---|
174 | {
|
---|
175 | SimpleVec ambient, diffuse, spec, emm;
|
---|
176 |
|
---|
177 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
178 | //diffuse.x = 0.7f; diffuse.y = 0.4f; diffuse.z = 0.2f;
|
---|
179 | diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
180 | spec.x = spec.y = spec.z = .0f;
|
---|
181 | emm = spec;
|
---|
182 |
|
---|
183 | // only write rgb part of the material
|
---|
184 | str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
|
---|
185 | str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
|
---|
186 | str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
187 | str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | bool VboFormatConverter::WriteFile(const string &filename)
|
---|
193 | {
|
---|
194 | ogzstream ofile(filename.c_str());
|
---|
195 |
|
---|
196 | if (!ofile.is_open())
|
---|
197 | return false;
|
---|
198 |
|
---|
199 |
|
---|
200 | /////////
|
---|
201 | //-- write textures
|
---|
202 |
|
---|
203 | int textureCount = 1;
|
---|
204 | //int textureCount = 0;
|
---|
205 |
|
---|
206 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
207 |
|
---|
208 | if (textureCount > 0)
|
---|
209 | {
|
---|
210 | // hack
|
---|
211 | const string texName("FischiNormalmap.png");
|
---|
212 | //const string texName("wood.jpg");
|
---|
213 |
|
---|
214 | int texnameSize = (int)texName.length() + 1;
|
---|
215 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
216 |
|
---|
217 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
218 |
|
---|
219 | // set boundary to repeat
|
---|
220 | int boundS, boundT;
|
---|
221 | boundS = boundT = 1;
|
---|
222 |
|
---|
223 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
224 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | ///////////
|
---|
229 | //-- write shapes
|
---|
230 |
|
---|
231 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
232 |
|
---|
233 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
234 |
|
---|
235 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
236 | {
|
---|
237 | WriteGeometry(ofile, *it);
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | int entityCount = 1;
|
---|
242 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
243 |
|
---|
244 |
|
---|
245 | //////////
|
---|
246 | //-- write single scene entity
|
---|
247 |
|
---|
248 | // no transformation
|
---|
249 | bool hasTrafo = false;
|
---|
250 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
251 |
|
---|
252 | // a dummy lod
|
---|
253 | int numLODs = 1;
|
---|
254 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
255 |
|
---|
256 | float dist = 0;
|
---|
257 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
258 |
|
---|
259 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
260 |
|
---|
261 | // all shapes belong to this scene entity
|
---|
262 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
263 | {
|
---|
264 | int shapeId = i;
|
---|
265 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
266 | }
|
---|
267 |
|
---|
268 | return true;
|
---|
269 | } |
---|