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 | ++ line;
|
---|
121 | }
|
---|
122 |
|
---|
123 | mGeometry.push_back(geom);
|
---|
124 |
|
---|
125 | mNumShapes = 1;
|
---|
126 | fclose(file);
|
---|
127 |
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void VboFormatConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
133 | {
|
---|
134 | int vertexCount = geom->mVertexCount;
|
---|
135 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
136 |
|
---|
137 | str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
|
---|
138 | str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
|
---|
139 | str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
|
---|
140 |
|
---|
141 | int texCoordCount = geom->mTexcoordCount;
|
---|
142 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
143 |
|
---|
144 | if (texCoordCount)
|
---|
145 | {
|
---|
146 | str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | ///////
|
---|
151 | //-- texture
|
---|
152 |
|
---|
153 | //int texId = -1;
|
---|
154 | int texId = 0;
|
---|
155 |
|
---|
156 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
157 | //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
|
---|
158 |
|
---|
159 | bool alphaTestEnabled = false;
|
---|
160 | //bool cullFaceEnabled = false;
|
---|
161 | bool cullFaceEnabled = true;
|
---|
162 |
|
---|
163 | str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
164 | str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
165 |
|
---|
166 | // material
|
---|
167 | bool hasMaterial = true;
|
---|
168 | //bool hasMaterial = false;
|
---|
169 | str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
170 |
|
---|
171 | if (hasMaterial)
|
---|
172 | {
|
---|
173 | SimpleVec ambient, diffuse, spec, emm;
|
---|
174 |
|
---|
175 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
176 | //diffuse.x = 0.7f; diffuse.y = 0.4f; diffuse.z = 0.2f;
|
---|
177 | diffuse.x = 0.4f; diffuse.y = 0.6f; diffuse.z = 0.6f;
|
---|
178 | //diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
179 | spec.x = spec.y = spec.z = .0f;
|
---|
180 | emm = spec;
|
---|
181 |
|
---|
182 | // only write rgb part of the material
|
---|
183 | str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
|
---|
184 | str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
|
---|
185 | str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
186 | str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | bool VboFormatConverter::WriteFile(const string &filename)
|
---|
192 | {
|
---|
193 | ogzstream ofile(filename.c_str());
|
---|
194 |
|
---|
195 | if (!ofile.is_open())
|
---|
196 | return false;
|
---|
197 |
|
---|
198 |
|
---|
199 | /////////
|
---|
200 | //-- write textures
|
---|
201 |
|
---|
202 | int textureCount = 1;
|
---|
203 | //int textureCount = 0;
|
---|
204 |
|
---|
205 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
206 |
|
---|
207 | if (textureCount > 0)
|
---|
208 | {
|
---|
209 | // hack
|
---|
210 | const string texName("FischiNormalmap.png");
|
---|
211 | //const string texName("wood.jpg");
|
---|
212 |
|
---|
213 | int texnameSize = (int)texName.length() + 1;
|
---|
214 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
215 |
|
---|
216 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
217 |
|
---|
218 | // set boundary to repeat
|
---|
219 | int boundS, boundT;
|
---|
220 | boundS = boundT = 1;
|
---|
221 |
|
---|
222 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
223 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | ///////////
|
---|
228 | //-- write shapes
|
---|
229 |
|
---|
230 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
231 |
|
---|
232 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
233 |
|
---|
234 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
235 | {
|
---|
236 | WriteGeometry(ofile, *it);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | int entityCount = 1;
|
---|
241 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
242 |
|
---|
243 |
|
---|
244 | //////////
|
---|
245 | //-- write single scene entity
|
---|
246 |
|
---|
247 | // no transformation
|
---|
248 | bool hasTrafo = false;
|
---|
249 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
250 |
|
---|
251 | // a dummy lod
|
---|
252 | int numLODs = 1;
|
---|
253 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
254 |
|
---|
255 | float dist = 0;
|
---|
256 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
257 |
|
---|
258 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
259 |
|
---|
260 | // all shapes belong to this scene entity
|
---|
261 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
262 | {
|
---|
263 | int shapeId = i;
|
---|
264 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
265 | }
|
---|
266 |
|
---|
267 | return true;
|
---|
268 | } |
---|