]> Dogcows Code - chaz/yoink/blob - src/moof/mesh.cc
use only triangles; no quads
[chaz/yoink] / src / moof / mesh.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstring>
13 #include <fstream>
14 #include <sstream>
15 #include <stack>
16 #include <stdexcept>
17
18 #include <boost/algorithm/string/trim.hpp>
19 #include <zlib.h>
20
21 #include <stlplus/strings/string_utilities.hpp>
22
23 #include "debug.hh"
24 #include "log.hh"
25 #include "mesh.hh"
26 #include "opengl.hh"
27
28
29 #define AC3D_FORMAT_VERSION (0x0b)
30 #define ZLIB_BUF_SIZE (262114)
31
32
33 namespace moof {
34
35
36 static std::string read_string(std::istream& stream)
37 {
38 std::string str;
39 char atom;
40
41 do // skip to the next non-space character
42 {
43 stream.get(atom);
44 }
45 while (stream && std::isspace(atom));
46
47 if (atom == '"')
48 {
49 do
50 {
51 stream.get(atom);
52 if (atom == '"') break;
53 str += atom;
54 }
55 while (stream);
56 }
57 else
58 {
59 do
60 {
61 stream.get(atom);
62 if (std::isspace(atom)) break;
63 str += atom;
64 }
65 while (stream);
66 }
67
68 return str;
69 }
70
71 inline int read_hex(std::istream& stream)
72 {
73 int hex;
74 std::ios::fmtflags flags = stream.flags();
75 stream.setf(std::ios::hex, std::ios::basefield);
76 stream >> hex;
77 stream.flags(flags);
78 return hex;
79 }
80
81 inline vector2 read_pair(std::istream& stream)
82 {
83 vector2 triplet;
84 stream >> triplet[0] >> triplet[1];
85 return triplet;
86 }
87
88
89 template <int D>
90 inline vector< scalar, fixed<D> > read_triplet(std::istream& stream)
91 {
92 vector< scalar, fixed<D> > triplet;
93 stream >> triplet[0] >> triplet[1] >> triplet[2];
94 return triplet;
95 }
96
97
98 void mesh::import(std::istream& stream)
99 {
100 std::string atom;
101
102 object_ptr obj;
103 std::stack<int> kids;
104
105 // read and verify the AC3D header
106 {
107 char magic[5];
108 unsigned version = 0;
109
110 stream.get(magic, sizeof(magic));
111 if (!stream || strncmp(magic, "AC3D", 4) != 0)
112 {
113 throw std::runtime_error("invalid mesh header");
114 }
115
116 version = read_hex(stream);
117 if (version > AC3D_FORMAT_VERSION)
118 {
119 throw std::runtime_error("wrong mesh file format version");
120 }
121 }
122
123 while (stream)
124 {
125 stream >> atom;
126 if (!stream) break;
127
128 if (atom == "MATERIAL")
129 {
130 materials_.push_back(material(read_string(stream)));
131
132 stream >> atom;
133 materials_.back().diffuse = read_triplet<4>(stream);
134 stream >> atom;
135 materials_.back().ambient = read_triplet<3>(stream);
136 stream >> atom;
137 materials_.back().emissive = read_triplet<3>(stream);
138 stream >> atom;
139 materials_.back().specular = read_triplet<3>(stream);
140
141 stream >> atom >> materials_.back().shininess;
142 stream >> atom >> materials_.back().diffuse[3]; // transparency
143 materials_.back().diffuse[3] = 1.0; // FIXME: temporary
144
145 log_info("read material", materials_.back().name,
146 materials_.back().diffuse);
147 }
148 else if (atom == "OBJECT")
149 {
150 stream >> atom;
151 if (atom != "world" && atom != "group" && atom != "poly")
152 {
153 throw std::runtime_error("unexpected object type " + atom);
154 }
155
156 object_ptr newObj = object::alloc();
157
158 if (obj)
159 {
160 obj->kids.push_back(newObj);
161 newObj->parent = obj;
162 }
163 else
164 {
165 objects_.push_back(newObj);
166 }
167
168 obj = newObj;
169 }
170 else if (atom == "name")
171 {
172 if (obj) obj->name = read_string(stream);
173 else throw std::runtime_error("unexpected atom: " + atom);
174 }
175 else if (atom == "data")
176 {
177 std::getline(stream, atom);
178 std::getline(stream, obj ? obj->data : atom);
179 }
180 else if (atom == "texture")
181 {
182 if (obj) obj->texture = resource::load(read_string(stream));
183 }
184 else if (atom == "texrep")
185 {
186 if (obj) obj->texrep = read_pair(stream);
187 else throw std::runtime_error("unexpected atom: " + atom);
188 }
189 else if (atom == "rot")
190 {
191 // TODO
192 std::getline(stream, atom);
193 }
194 else if (atom == "loc")
195 {
196 // TODO
197 std::getline(stream, atom);
198 }
199 else if (atom == "url")
200 {
201 if (obj) std::getline(stream, obj->url);
202 else throw std::runtime_error("unexpected atom: " + atom);
203 }
204 else if (atom == "numvert")
205 {
206 if (!obj) throw std::runtime_error("unexpected atom: " + atom);
207
208 int numvert = 0;
209 stream >> numvert;
210
211 log_warning("adding verts", numvert);
212
213 for (int i = 0; i < numvert; ++i)
214 {
215 obj->verts.push_back(read_triplet<3>(stream));
216 log_error("vert", obj->verts.back());
217 }
218 }
219 else if (atom == "numsurf")
220 {
221 if (!obj) throw std::runtime_error("unexpected atom: " + atom);
222
223 int numsurf = 0;
224 stream >> numsurf;
225
226 for (int i = 0; i < numsurf; ++i)
227 {
228 stream >> atom;
229 if (atom != "SURF") throw std::runtime_error("uh oh");
230
231 int flags = read_hex(stream);
232 log_info(flags);
233
234 int material = 0;
235 stream >> atom;
236 if (atom == "mat") stream >> material >> atom;
237
238 if (atom != "refs")
239 {
240 throw std::runtime_error("blaaaaaaaahhh!!");
241 }
242
243 int numrefs = 0;
244 stream >> numrefs;
245 ASSERT(numrefs >= 3);
246
247 if ((int)obj->faces.size() <= material)
248 {
249 log_info("inserting face...");
250 //obj->faces.insert(obj->faces.begin() + material,
251 //material_group());
252 obj->faces.resize(material + 1);
253 log_info("inserted face", material, obj->faces.size());
254 }
255
256 material_group& face = obj->faces[material];
257
258 int vert;
259 stream >> vert;
260 vector2 uv = read_pair(stream);
261 face.triangles.push_back(vert);
262 face.triangles_uv.push_back(uv);
263
264 unsigned first = face.triangles.back();
265 vector2 first_uv = face.triangles_uv.back();
266
267 stream >> vert;
268 uv = read_pair(stream);
269 face.triangles.push_back(vert);
270 face.triangles_uv.push_back(uv);
271
272 stream >> vert;
273 uv = read_pair(stream);
274 face.triangles.push_back(vert);
275 face.triangles_uv.push_back(uv);
276
277 unsigned last = face.triangles.back();
278 vector2 last_uv = face.triangles_uv.back();
279
280 for (int j = 3; j < numrefs; ++j)
281 {
282 // first
283 face.triangles.push_back(first);
284 face.triangles_uv.push_back(first_uv);
285
286 // last
287 face.triangles.push_back(last);
288 face.triangles_uv.push_back(last_uv);
289
290 stream >> vert;
291 uv = read_pair(stream);
292 face.triangles.push_back(vert);
293 face.triangles_uv.push_back(uv);
294
295 last = face.triangles.back();
296 last_uv = face.triangles_uv.back();
297 }
298 }
299 }
300 else if (atom == "kids")
301 {
302 for (int i = kids.size(); i > 0; --i)
303 {
304 if (--kids.top() <= 0)
305 {
306 ASSERT(obj && "should be an object");
307 obj = obj->parent;
308 kids.pop();
309 }
310 else break;
311 }
312
313 int numkids = 0;
314 stream >> numkids;
315 if (numkids > 0) kids.push(numkids);
316
317 if (kids.size() > 0)
318 {
319 log_info("KIDS", kids.top(), "|", kids.size());
320 }
321 }
322 else
323 {
324 log_warning("UNKNOWN ATOM:", atom);
325 }
326 }
327 while (stream);
328 }
329
330
331 mesh::mesh(const std::string& path)
332 {
333 std::ifstream file(path.c_str(), std::ifstream::in |
334 std::ifstream::binary);
335 if (!file) throw std::runtime_error("cannot find mesh file");
336
337 // if we can read the header, the file isn't compressed
338 char magic[5];
339 file.get(magic, sizeof(magic));
340 if (strncmp(magic, "AC3D", 4) == 0)
341 {
342 log_info("text mesh detected");
343 file.seekg(std::ios::beg);
344
345 import(file);
346 }
347 else
348 {
349 log_info("compressed mesh detected");
350 file.seekg(std::ios::beg);
351
352 std::stringstream stream;
353 char in[ZLIB_BUF_SIZE];
354 char out[ZLIB_BUF_SIZE];
355
356 z_stream zstream;
357
358 zstream.zalloc = Z_NULL;
359 zstream.zfree = Z_NULL;
360 zstream.opaque = Z_NULL;
361 zstream.avail_in = 0;
362 zstream.next_in = Z_NULL;
363
364 int result = inflateInit2(&zstream, 32+MAX_WBITS);
365 if (result != Z_OK) throw std::runtime_error("zlib init error");
366
367 do
368 {
369 file.read(in, sizeof(in));
370 zstream.next_in = (Bytef*)in;
371 zstream.avail_in = file.gcount();
372
373 if (zstream.avail_in == 0) break;
374
375 do
376 {
377 zstream.next_out = (Bytef*)out;
378 zstream.avail_out = sizeof(out);
379
380 result = inflate(&zstream, Z_NO_FLUSH);
381 switch (result)
382 {
383 case Z_NEED_DICT:
384 case Z_DATA_ERROR:
385 case Z_MEM_ERROR:
386 inflateEnd(&zstream);
387 throw std::runtime_error("zlib inflate error");
388 case Z_STREAM_ERROR:
389 throw std::runtime_error("zlib stream error");
390 }
391
392 int inflated = sizeof(out) - zstream.avail_out;
393 stream.write(out, inflated);
394 }
395 while (zstream.avail_out == 0);
396 }
397 while(result != Z_STREAM_END);
398
399 inflateEnd(&zstream);
400
401 import(stream);
402 }
403 }
404
405
406 void mesh::draw(scalar alpha) const
407 {
408 //glEnableClientState(GL_VERTEX_ARRAY);
409 //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
410
411 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
412
413 std::vector<object_ptr>::const_iterator it;
414 for (it = objects_.begin(); it != objects_.end(); ++it)
415 {
416 (*it)->draw(*this, alpha);
417 }
418 }
419
420
421 void mesh::set_material(int index) const
422 {
423 set_material(materials_[index]);
424 }
425
426 void mesh::set_material(const material& material) const
427 {
428 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse.data());
429 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient.data());
430 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular.data());
431 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material.emissive.data());
432 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
433 //glColor(material.diffuse);
434 }
435
436
437 void mesh::object::draw(const mesh& mesh, scalar alpha) const
438 {
439 //log_info("cool", verts.size());
440 //{
441 //image::reset_binding();
442 //std::vector<vector3>::const_iterator it;
443 //glBegin(GL_LINE_STRIP);
444 //for (it = verts.begin(); it != verts.end(); ++it)
445 //{
446 //glVertex(*it);
447 //}
448 //glEnd();
449 //}
450
451 //glPolygonMode(GL_BACK, GL_LINE);
452 //glVertexPointer(3, GL_SCALAR, 0, verts[0].data());
453 if (texture) texture->bind();
454 else image::reset_binding();
455
456 for (size_t i = 0; i < faces.size(); ++i)
457 {
458 const material_group& face = faces[i];
459 mesh.set_material(i);
460 //it->draw(alpha);
461 //std::vector<unsigned>::const_iterator jt;
462 int count = face.triangles.size();
463 for (int j = 0; j < count; j += 3)
464 {
465 glBegin(GL_TRIANGLES);
466 glTexCoord(face.triangles_uv[j]);
467 glVertex(verts[face.triangles[j]]);
468 glTexCoord(face.triangles_uv[j+1]);
469 glVertex(verts[face.triangles[j+1]]);
470 glTexCoord(face.triangles_uv[j+2]);
471 glVertex(verts[face.triangles[j+2]]);
472 glEnd();
473 }
474 }
475
476 std::vector<object_ptr>::const_iterator jt;
477 for (jt = kids.begin(); jt != kids.end(); ++jt)
478 {
479 (*jt)->draw(mesh, alpha);
480 }
481 }
482
483 void mesh::material_group::draw(scalar alpha) const
484 {
485 // TODO: setup material
486
487 /*
488 if (triangles.size() > 0)
489 {
490 //log_info("drawing triangles:", triangles.size()/3);
491 glTexCoordPointer(2, GL_SCALAR, 0, triangles_uv[0].data());
492 glDrawElements(GL_TRIANGLES,
493 triangles.size(), GL_UNSIGNED_INT,
494 &triangles[0]);
495 }
496 */
497 }
498
499
500 class mesh_resource_loader
501 {
502 public:
503
504 mesh_resource_loader()
505 {
506 resource::register_type<mesh>("ac", "models");
507 }
508
509 ~mesh_resource_loader()
510 {
511 resource::unregister_type("ac");
512 }
513 };
514
515 static mesh_resource_loader loader;
516
517
518 } // namespace moof
519
This page took 0.05219 seconds and 4 git commands to generate.