ld33_umonster
view src/mesh.cc @ 10:1b30bd381667
sweep curve mesh gen and dragon horns
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 27 Aug 2015 05:25:04 +0300 |
parents | 35349df5392d |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
4 #include <assert.h>
5 #include "opengl.h"
6 #include "mesh.h"
7 //#include "xform_node.h"
9 #define USE_OLDGL
11 bool Mesh::use_custom_sdr_attr = true;
12 int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 };
13 /*
14 (int)SDR_ATTR_VERTEX,
15 (int)SDR_ATTR_NORMAL,
16 (int)SDR_ATTR_TANGENT,
17 (int)SDR_ATTR_TEXCOORD,
18 (int)SDR_ATTR_COLOR,
19 -1, -1};
20 */
21 unsigned int Mesh::intersect_mode = ISECT_DEFAULT;
22 float Mesh::vertex_sel_dist = 0.01;
23 float Mesh::vis_vecsize = 1.0;
25 Mesh::Mesh()
26 {
27 clear();
29 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
31 for(int i=0; i<NUM_MESH_ATTR; i++) {
32 vattr[i].vbo = buffer_objects[i];
33 }
34 ibo = buffer_objects[NUM_MESH_ATTR];
35 wire_ibo = 0;
36 }
38 Mesh::~Mesh()
39 {
40 glDeleteBuffers(NUM_MESH_ATTR + 1, buffer_objects);
42 if(wire_ibo) {
43 glDeleteBuffers(1, &wire_ibo);
44 }
45 }
47 Mesh::Mesh(const Mesh &rhs)
48 {
49 clear();
51 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
53 for(int i=0; i<NUM_MESH_ATTR; i++) {
54 vattr[i].vbo = buffer_objects[i];
55 }
56 ibo = buffer_objects[NUM_MESH_ATTR];
57 wire_ibo = 0;
59 clone(rhs);
60 }
62 Mesh &Mesh::operator =(const Mesh &rhs)
63 {
64 if(&rhs != this) {
65 clone(rhs);
66 }
67 return *this;
68 }
70 bool Mesh::clone(const Mesh &m)
71 {
72 clear();
74 for(int i=0; i<NUM_MESH_ATTR; i++) {
75 if(m.has_attrib(i)) {
76 m.get_attrib_data(i); // force validation of the actual data on the source mesh
78 vattr[i].nelem = m.vattr[i].nelem;
79 vattr[i].data = m.vattr[i].data; // copy the actual data
80 vattr[i].data_valid = true;
81 }
82 }
84 if(m.is_indexed()) {
85 m.get_index_data(); // again, force validation
87 // copy the index data
88 idata = m.idata;
89 idata_valid = true;
90 }
92 name = m.name;
93 nverts = m.nverts;
94 nfaces = m.nfaces;
96 //bones = m.bones;
98 memcpy(cur_val, m.cur_val, sizeof cur_val);
100 aabb = m.aabb;
101 aabb_valid = m.aabb_valid;
102 bsph = m.bsph;
103 bsph_valid = m.bsph_valid;
105 hitface = m.hitface;
106 hitvert = m.hitvert;
108 intersect_mode = m.intersect_mode;
109 vertex_sel_dist = m.vertex_sel_dist;
110 vis_vecsize = m.vis_vecsize;
112 return true;
113 }
115 void Mesh::set_name(const char *name)
116 {
117 this->name = name;
118 }
120 const char *Mesh::get_name() const
121 {
122 return name.c_str();
123 }
125 bool Mesh::has_attrib(int attr) const
126 {
127 if(attr < 0 || attr >= NUM_MESH_ATTR) {
128 return false;
129 }
131 // if neither of these is valid, then nobody has set this attribute
132 return vattr[attr].vbo_valid || vattr[attr].data_valid;
133 }
135 bool Mesh::is_indexed() const
136 {
137 return ibo_valid || idata_valid;
138 }
140 void Mesh::clear()
141 {
142 //bones.clear();
144 for(int i=0; i<NUM_MESH_ATTR; i++) {
145 vattr[i].nelem = 0;
146 vattr[i].vbo_valid = false;
147 vattr[i].data_valid = false;
148 //vattr[i].sdr_loc = -1;
149 vattr[i].data.clear();
150 }
151 ibo_valid = idata_valid = false;
152 idata.clear();
154 wire_ibo_valid = false;
156 nverts = nfaces = 0;
158 bsph_valid = false;
159 aabb_valid = false;
160 }
162 float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data)
163 {
164 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
165 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
166 return 0;
167 }
169 if(nverts && num != nverts) {
170 fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
171 return 0;
172 }
173 nverts = num;
175 vattr[attrib].data.clear();
176 vattr[attrib].nelem = nelem;
177 vattr[attrib].data.resize(num * nelem);
179 if(data) {
180 memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data);
181 }
183 vattr[attrib].data_valid = true;
184 vattr[attrib].vbo_valid = false;
185 return &vattr[attrib].data[0];
186 }
188 float *Mesh::get_attrib_data(int attrib)
189 {
190 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
191 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
192 return 0;
193 }
195 vattr[attrib].vbo_valid = false;
196 return (float*)((const Mesh*)this)->get_attrib_data(attrib);
197 }
199 const float *Mesh::get_attrib_data(int attrib) const
200 {
201 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
202 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
203 return 0;
204 }
206 if(!vattr[attrib].data_valid) {
207 #if GL_ES_VERSION_2_0
208 fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
209 return 0;
210 #else
211 if(!vattr[attrib].vbo_valid) {
212 fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
213 return 0;
214 }
216 // local data copy is unavailable, grab the data from the vbo
217 Mesh *m = (Mesh*)this;
218 m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem);
220 glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo);
221 void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
222 memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float));
223 glUnmapBuffer(GL_ARRAY_BUFFER);
225 vattr[attrib].data_valid = true;
226 #endif
227 }
229 return &vattr[attrib].data[0];
230 }
232 void Mesh::set_attrib(int attrib, int idx, const Vector4 &v)
233 {
234 float *data = get_attrib_data(attrib);
235 if(data) {
236 data += idx * vattr[attrib].nelem;
237 for(int i=0; i<vattr[attrib].nelem; i++) {
238 data[i] = v[i];
239 }
240 }
241 }
243 Vector4 Mesh::get_attrib(int attrib, int idx) const
244 {
245 Vector4 v(0.0, 0.0, 0.0, 1.0);
246 const float *data = get_attrib_data(attrib);
247 if(data) {
248 data += idx * vattr[attrib].nelem;
249 for(int i=0; i<vattr[attrib].nelem; i++) {
250 v[i] = data[i];
251 }
252 }
253 return v;
254 }
256 int Mesh::get_attrib_count(int attrib) const
257 {
258 return has_attrib(attrib) ? nverts : 0;
259 }
262 unsigned int *Mesh::set_index_data(int num, const unsigned int *indices)
263 {
264 int nidx = nfaces * 3;
265 if(nidx && num != nidx) {
266 fprintf(stderr, "%s: index count missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
267 return 0;
268 }
269 nfaces = num / 3;
271 idata.clear();
272 idata.resize(num);
274 if(indices) {
275 memcpy(&idata[0], indices, num * sizeof *indices);
276 }
278 idata_valid = true;
279 ibo_valid = false;
281 return &idata[0];
282 }
284 unsigned int *Mesh::get_index_data()
285 {
286 ibo_valid = false;
287 return (unsigned int*)((const Mesh*)this)->get_index_data();
288 }
290 const unsigned int *Mesh::get_index_data() const
291 {
292 if(!idata_valid) {
293 #if GL_ES_VERSION_2_0
294 fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
295 return 0;
296 #else
297 if(!ibo_valid) {
298 fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__);
299 return 0;
300 }
302 // local data copy is unavailable, gram the data from the ibo
303 Mesh *m = (Mesh*)this;
304 int nidx = nfaces * 3;
305 m->idata.resize(nidx);
307 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
308 void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
309 memcpy(&m->idata[0], data, nidx * sizeof(unsigned int));
310 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
312 idata_valid = true;
313 #endif
314 }
316 return &idata[0];
317 }
319 int Mesh::get_index_count() const
320 {
321 return nfaces * 3;
322 }
324 void Mesh::append(const Mesh &mesh)
325 {
326 unsigned int idxoffs = nverts;
328 if(!nverts) {
329 clone(mesh);
330 return;
331 }
333 nverts += mesh.nverts;
334 nfaces += mesh.nfaces;
336 for(int i=0; i<NUM_MESH_ATTR; i++) {
337 if(has_attrib(i) && mesh.has_attrib(i)) {
338 // force validating the data arrays
339 get_attrib_data(i);
340 mesh.get_attrib_data(i);
342 // append the mesh data
343 vattr[i].data.insert(vattr[i].data.end(), mesh.vattr[i].data.begin(), mesh.vattr[i].data.end());
344 }
345 }
347 if(ibo_valid || idata_valid) {
348 // make index arrays valid
349 get_index_data();
350 mesh.get_index_data();
352 size_t orig_sz = idata.size();
354 idata.insert(idata.end(), mesh.idata.begin(), mesh.idata.end());
356 // fixup all the new indices
357 for(size_t i=orig_sz; i<idata.size(); i++) {
358 idata[i] += idxoffs;
359 }
360 }
362 // fuck everything
363 wire_ibo_valid = false;
364 aabb_valid = false;
365 bsph_valid = false;
366 }
368 // assemble a complete vertex by adding all the useful attributes
369 void Mesh::vertex(float x, float y, float z)
370 {
371 cur_val[MESH_ATTR_VERTEX] = Vector4(x, y, z, 1.0f);
372 vattr[MESH_ATTR_VERTEX].data_valid = true;
373 vattr[MESH_ATTR_VERTEX].nelem = 3;
375 for(int i=0; i<NUM_MESH_ATTR; i++) {
376 if(vattr[i].data_valid) {
377 for(int j=0; j<vattr[MESH_ATTR_VERTEX].nelem; j++) {
378 vattr[i].data.push_back(cur_val[i][j]);
379 }
380 }
381 vattr[i].vbo_valid = false;
382 }
384 if(idata_valid) {
385 idata.clear();
386 }
387 ibo_valid = idata_valid = false;
388 }
390 void Mesh::normal(float nx, float ny, float nz)
391 {
392 cur_val[MESH_ATTR_NORMAL] = Vector4(nx, ny, nz, 1.0f);
393 vattr[MESH_ATTR_NORMAL].data_valid = true;
394 vattr[MESH_ATTR_NORMAL].nelem = 3;
395 }
397 void Mesh::tangent(float tx, float ty, float tz)
398 {
399 cur_val[MESH_ATTR_TANGENT] = Vector4(tx, ty, tz, 1.0f);
400 vattr[MESH_ATTR_TANGENT].data_valid = true;
401 vattr[MESH_ATTR_TANGENT].nelem = 3;
402 }
404 void Mesh::texcoord(float u, float v, float w)
405 {
406 cur_val[MESH_ATTR_TEXCOORD] = Vector4(u, v, w, 1.0f);
407 vattr[MESH_ATTR_TEXCOORD].data_valid = true;
408 vattr[MESH_ATTR_TEXCOORD].nelem = 3;
409 }
411 void Mesh::boneweights(float w1, float w2, float w3, float w4)
412 {
413 cur_val[MESH_ATTR_BONEWEIGHTS] = Vector4(w1, w2, w3, w4);
414 vattr[MESH_ATTR_BONEWEIGHTS].data_valid = true;
415 vattr[MESH_ATTR_BONEWEIGHTS].nelem = 4;
416 }
418 void Mesh::boneidx(int idx1, int idx2, int idx3, int idx4)
419 {
420 cur_val[MESH_ATTR_BONEIDX] = Vector4(idx1, idx2, idx3, idx4);
421 vattr[MESH_ATTR_BONEIDX].data_valid = true;
422 vattr[MESH_ATTR_BONEIDX].nelem = 4;
423 }
425 int Mesh::get_poly_count() const
426 {
427 if(nfaces) {
428 return nfaces;
429 }
430 if(nverts) {
431 return nverts / 3;
432 }
433 return 0;
434 }
436 /// static function
437 void Mesh::set_attrib_location(int attr, int loc)
438 {
439 if(attr < 0 || attr >= NUM_MESH_ATTR) {
440 return;
441 }
442 Mesh::global_sdr_loc[attr] = loc;
443 }
445 /// static function
446 int Mesh::get_attrib_location(int attr)
447 {
448 if(attr < 0 || attr >= NUM_MESH_ATTR) {
449 return -1;
450 }
451 return Mesh::global_sdr_loc[attr];
452 }
454 /// static function
455 void Mesh::clear_attrib_locations()
456 {
457 for(int i=0; i<NUM_MESH_ATTR; i++) {
458 Mesh::global_sdr_loc[i] = -1;
459 }
460 }
462 /// static function
463 void Mesh::set_vis_vecsize(float sz)
464 {
465 Mesh::vis_vecsize = sz;
466 }
468 float Mesh::get_vis_vecsize()
469 {
470 return Mesh::vis_vecsize;
471 }
473 void Mesh::apply_xform(const Matrix4x4 &xform)
474 {
475 Matrix4x4 dir_xform;// = xform.inverse().transposed();
476 dir_xform[0][3] = dir_xform[1][3] = dir_xform[2][3] = 0.0f;
477 dir_xform[3][0] = dir_xform[3][1] = dir_xform[3][2] = 0.0f;
478 dir_xform[3][3] = 1.0f;
480 apply_xform(xform, dir_xform);
481 }
483 void Mesh::apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform)
484 {
485 for(unsigned int i=0; i<nverts; i++) {
486 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
487 set_attrib(MESH_ATTR_VERTEX, i, v.transformed(xform));
489 if(has_attrib(MESH_ATTR_NORMAL)) {
490 Vector3 n = get_attrib(MESH_ATTR_NORMAL, i);
491 set_attrib(MESH_ATTR_NORMAL, i, n.transformed(dir_xform));
492 }
493 if(has_attrib(MESH_ATTR_TANGENT)) {
494 Vector3 t = get_attrib(MESH_ATTR_TANGENT, i);
495 set_attrib(MESH_ATTR_TANGENT, i, t.transformed(dir_xform));
496 }
497 }
498 }
500 void Mesh::flip()
501 {
502 flip_faces();
503 flip_normals();
504 }
506 void Mesh::flip_faces()
507 {
508 if(is_indexed()) {
509 unsigned int *indices = get_index_data();
510 if(!indices) return;
512 int idxnum = get_index_count();
513 for(int i=0; i<idxnum; i+=3) {
514 unsigned int tmp = indices[i + 2];
515 indices[i + 2] = indices[i + 1];
516 indices[i + 1] = tmp;
517 }
519 } else {
520 Vector3 *verts = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
521 if(!verts) return;
523 int vnum = get_attrib_count(MESH_ATTR_VERTEX);
524 for(int i=0; i<vnum; i+=3) {
525 Vector3 tmp = verts[i + 2];
526 verts[i + 2] = verts[i + 1];
527 verts[i + 1] = tmp;
528 }
529 }
530 }
532 void Mesh::flip_normals()
533 {
534 Vector3 *normals = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
535 if(!normals) return;
537 int num = get_attrib_count(MESH_ATTR_NORMAL);
538 for(int i=0; i<num; i++) {
539 normals[i] = -normals[i];
540 }
541 }
543 /*
544 int Mesh::add_bone(XFormNode *bone)
545 {
546 int idx = bones.size();
547 bones.push_back(bone);
548 return idx;
549 }
551 const XFormNode *Mesh::get_bone(int idx) const
552 {
553 if(idx < 0 || idx >= (int)bones.size()) {
554 return 0;
555 }
556 return bones[idx];
557 }
559 int Mesh::get_bones_count() const
560 {
561 return (int)bones.size();
562 }
563 */
565 bool Mesh::pre_draw() const
566 {
567 cur_sdr = 0;
568 if(glcaps.shaders) {
569 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
570 }
572 ((Mesh*)this)->update_buffers();
574 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
575 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
576 return false;
577 }
579 if(cur_sdr && use_custom_sdr_attr) {
580 // rendering with shaders
581 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
582 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
583 return false;
584 }
586 for(int i=0; i<NUM_MESH_ATTR; i++) {
587 int loc = global_sdr_loc[i];
588 if(loc >= 0 && vattr[i].vbo_valid) {
589 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
590 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
591 glEnableVertexAttribArray(loc);
592 }
593 }
594 } else {
595 #ifndef GL_ES_VERSION_2_0
596 // rendering with fixed-function (not available in GLES2)
597 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
598 glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
599 glEnableClientState(GL_VERTEX_ARRAY);
601 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
602 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
603 glNormalPointer(GL_FLOAT, 0, 0);
604 glEnableClientState(GL_NORMAL_ARRAY);
605 }
606 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
607 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
608 glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
609 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
610 }
611 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
612 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
613 glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
614 glEnableClientState(GL_COLOR_ARRAY);
615 }
616 #endif
617 }
618 glBindBuffer(GL_ARRAY_BUFFER, 0);
620 return true;
621 }
623 void Mesh::draw() const
624 {
625 if(!pre_draw()) return;
627 if(ibo_valid) {
628 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
629 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
630 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
631 } else {
632 glDrawArrays(GL_TRIANGLES, 0, nverts);
633 }
635 post_draw();
636 }
638 void Mesh::post_draw() const
639 {
640 if(cur_sdr && use_custom_sdr_attr) {
641 // rendered with shaders
642 for(int i=0; i<NUM_MESH_ATTR; i++) {
643 int loc = global_sdr_loc[i];
644 if(loc >= 0 && vattr[i].vbo_valid) {
645 glDisableVertexAttribArray(loc);
646 }
647 }
648 } else {
649 #ifndef GL_ES_VERSION_2_0
650 // rendered with fixed-function
651 glDisableClientState(GL_VERTEX_ARRAY);
652 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
653 glDisableClientState(GL_NORMAL_ARRAY);
654 }
655 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
656 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
657 }
658 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
659 glDisableClientState(GL_COLOR_ARRAY);
660 }
661 #endif
662 }
663 }
665 void Mesh::draw_wire() const
666 {
667 if(!pre_draw()) return;
669 ((Mesh*)this)->update_wire_ibo();
671 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
672 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
673 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
675 post_draw();
676 }
678 void Mesh::draw_vertices() const
679 {
680 if(!pre_draw()) return;
682 glDrawArrays(GL_POINTS, 0, nverts);
684 post_draw();
685 }
687 void Mesh::draw_normals() const
688 {
689 #ifdef USE_OLDGL
690 int cur_sdr = 0;
691 if(glcaps.shaders) {
692 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
693 }
695 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
696 Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
697 if(!varr || !norm) {
698 return;
699 }
701 glBegin(GL_LINES);
702 if(cur_sdr && use_custom_sdr_attr) {
703 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
704 if(vert_loc < 0) {
705 glEnd();
706 return;
707 }
709 for(size_t i=0; i<nverts; i++) {
710 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
711 Vector3 end = varr[i] + norm[i] * vis_vecsize;
712 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
713 }
714 } else {
715 for(size_t i=0; i<nverts; i++) {
716 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
717 Vector3 end = varr[i] + norm[i] * vis_vecsize;
718 glVertex3f(end.x, end.y, end.z);
719 }
720 }
721 glEnd();
722 #endif // USE_OLDGL
723 }
725 void Mesh::draw_tangents() const
726 {
727 #ifdef USE_OLDGL
728 int cur_sdr = 0;
729 if(glcaps.shaders) {
730 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
731 }
733 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
734 Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT);
735 if(!varr || !tang) {
736 return;
737 }
739 glBegin(GL_LINES);
740 if(cur_sdr && use_custom_sdr_attr) {
741 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
742 if(vert_loc < 0) {
743 glEnd();
744 return;
745 }
747 for(size_t i=0; i<nverts; i++) {
748 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
749 Vector3 end = varr[i] + tang[i] * vis_vecsize;
750 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
751 }
752 } else {
753 for(size_t i=0; i<nverts; i++) {
754 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
755 Vector3 end = varr[i] + tang[i] * vis_vecsize;
756 glVertex3f(end.x, end.y, end.z);
757 }
758 }
759 glEnd();
760 #endif // USE_OLDGL
761 }
763 void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const
764 {
765 if(!aabb_valid) {
766 ((Mesh*)this)->calc_aabb();
767 }
768 *vmin = aabb.min;
769 *vmax = aabb.max;
770 }
772 const AABox &Mesh::get_aabbox() const
773 {
774 if(!aabb_valid) {
775 ((Mesh*)this)->calc_aabb();
776 }
777 return aabb;
778 }
780 float Mesh::get_bsphere(Vector3 *center, float *rad) const
781 {
782 if(!bsph_valid) {
783 ((Mesh*)this)->calc_bsph();
784 }
785 *center = bsph.center;
786 *rad = bsph.radius;
787 return bsph.radius;
788 }
790 const Sphere &Mesh::get_bsphere() const
791 {
792 if(!bsph_valid) {
793 ((Mesh*)this)->calc_bsph();
794 }
795 return bsph;
796 }
798 /// static function
799 void Mesh::set_intersect_mode(unsigned int mode)
800 {
801 Mesh::intersect_mode = mode;
802 }
804 /// static function
805 unsigned int Mesh::get_intersect_mode()
806 {
807 return Mesh::intersect_mode;
808 }
810 /// static function
811 void Mesh::set_vertex_select_distance(float dist)
812 {
813 Mesh::vertex_sel_dist = dist;
814 }
816 /// static function
817 float Mesh::get_vertex_select_distance()
818 {
819 return Mesh::vertex_sel_dist;
820 }
822 bool Mesh::intersect(const Ray &ray, HitPoint *hit) const
823 {
824 assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE));
826 const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
827 const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
828 if(!varr) {
829 return false;
830 }
831 const unsigned int *idxarr = get_index_data();
833 // first test with the bounding box
834 AABox box;
835 get_aabbox(&box.min, &box.max);
836 if(!box.intersect(ray)) {
837 return false;
838 }
840 HitPoint nearest_hit;
841 nearest_hit.dist = FLT_MAX;
842 nearest_hit.obj = 0;
844 if(Mesh::intersect_mode & ISECT_VERTICES) {
845 // we asked for "intersections" with the vertices of the mesh
846 long nearest_vidx = -1;
847 float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist;
849 for(unsigned int i=0; i<nverts; i++) {
851 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) {
852 continue;
853 }
855 // project the vertex onto the ray line
856 float t = dot_product(varr[i] - ray.origin, ray.dir);
857 Vector3 vproj = ray.origin + ray.dir * t;
859 float dist_sq = (vproj - varr[i]).length_sq();
860 if(dist_sq < thres_sq) {
861 if(!hit) {
862 return true;
863 }
864 if(t < nearest_hit.dist) {
865 nearest_hit.dist = t;
866 nearest_vidx = i;
867 }
868 }
869 }
871 if(nearest_vidx != -1) {
872 hitvert = varr[nearest_vidx];
873 nearest_hit.obj = &hitvert;
874 }
876 } else {
877 // regular intersection test with polygons
879 for(unsigned int i=0; i<nfaces; i++) {
880 Triangle face(i, varr, idxarr);
882 // ignore back-facing polygons if the mode flags include ISECT_FRONT
883 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) {
884 continue;
885 }
887 HitPoint fhit;
888 if(face.intersect(ray, hit ? &fhit : 0)) {
889 if(!hit) {
890 return true;
891 }
892 if(fhit.dist < nearest_hit.dist) {
893 nearest_hit = fhit;
894 hitface = face;
895 }
896 }
897 }
898 }
900 if(nearest_hit.obj) {
901 if(hit) {
902 *hit = nearest_hit;
904 // if we are interested in the mesh and not the faces set obj to this
905 if(Mesh::intersect_mode & ISECT_FACE) {
906 hit->obj = &hitface;
907 } else if(Mesh::intersect_mode & ISECT_VERTICES) {
908 hit->obj = &hitvert;
909 } else {
910 hit->obj = this;
911 }
912 }
913 return true;
914 }
915 return false;
916 }
919 // texture coordinate manipulation
920 void Mesh::texcoord_apply_xform(const Matrix4x4 &xform)
921 {
922 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
923 return;
924 }
926 for(unsigned int i=0; i<nverts; i++) {
927 Vector4 tc = get_attrib(MESH_ATTR_TEXCOORD, i);
928 set_attrib(MESH_ATTR_TEXCOORD, i, tc.transformed(xform));
929 }
930 }
932 void Mesh::texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang)
933 {
934 if(!nverts) return;
936 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
937 // allocate texture coordinate attribute array
938 set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
939 }
941 Vector3 n = norm.normalized();
942 Vector3 b = cross_product(n, tang).normalized();
943 Vector3 t = cross_product(b, n);
945 for(unsigned int i=0; i<nverts; i++) {
946 Vector3 pos = get_attrib(MESH_ATTR_VERTEX, i);
948 // distance along the tangent direction
949 float u = dot_product(pos, t);
950 // distance along the bitangent direction
951 float v = dot_product(pos, b);
953 set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(u, v, 0, 1));
954 }
955 }
957 void Mesh::texcoord_gen_box()
958 {
959 if(!nverts || !has_attrib(MESH_ATTR_NORMAL)) return;
961 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
962 // allocate texture coordinate attribute array
963 set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
964 }
966 for(unsigned int i=0; i<nverts; i++) {
967 Vector3 pos = Vector3(get_attrib(MESH_ATTR_VERTEX, i)) * 0.5 + Vector3(0.5, 0.5, 0.5);
968 Vector3 norm = get_attrib(MESH_ATTR_NORMAL, i);
970 float abs_nx = fabs(norm.x);
971 float abs_ny = fabs(norm.y);
972 float abs_nz = fabs(norm.z);
973 int dom = abs_nx > abs_ny && abs_nx > abs_nz ? 0 : (abs_ny > abs_nz ? 1 : 2);
975 float uv[2], *uvptr = uv;
976 for(int j=0; j<3; j++) {
977 if(j == dom) continue; // skip dominant axis
979 *uvptr++ = pos[j];
980 }
981 set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(uv[0], uv[1], 0, 1));
982 }
983 }
985 void Mesh::texcoord_gen_cylinder()
986 {
987 if(!nverts) return;
989 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
990 // allocate texture coordinate attribute array
991 set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
992 }
994 for(unsigned int i=0; i<nverts; i++) {
995 Vector3 pos = get_attrib(MESH_ATTR_VERTEX, i);
997 float rho = sqrt(pos.x * pos.x + pos.z * pos.z);
998 float theta = rho == 0.0 ? 0.0 : atan2(pos.z / rho, pos.x / rho);
1000 float u = theta / (2.0 * M_PI) + 0.5;
1001 float v = pos.y;
1003 set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(u, v, 0, 1));
1004 }
1005 }
1008 void Mesh::dump(FILE *fp) const
1009 {
1010 if(!has_attrib(MESH_ATTR_VERTEX)) {
1011 return;
1012 }
1014 fprintf(fp, "VERTEX ATTRIBUTES\n");
1015 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid" };
1016 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1018 for(int i=0; i<(int)nverts; i++) {
1019 fprintf(fp, "%5u:", i);
1020 for(int j=0; j<NUM_MESH_ATTR; j++) {
1021 if(has_attrib(j)) {
1022 Vector4 v = get_attrib(j, i);
1023 int nelem = vattr[j].nelem;
1024 fprintf(fp, elemfmt[nelem], label[j], v.x, v.y, v.z, v.w);
1025 }
1026 }
1027 fputc('\n', fp);
1028 }
1030 if(is_indexed()) {
1031 const unsigned int *idx = get_index_data();
1032 int numidx = get_index_count();
1033 int numtri = numidx / 3;
1034 assert(numidx % 3 == 0);
1036 fprintf(fp, "FACES\n");
1038 for(int i=0; i<numtri; i++) {
1039 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1040 idx += 3;
1041 }
1042 }
1043 }
1045 // ------ private member functions ------
1047 void Mesh::calc_aabb()
1048 {
1049 // the cast is to force calling the const version which doesn't invalidate
1050 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
1051 return;
1052 }
1054 aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
1055 aabb.max = -aabb.min;
1057 for(unsigned int i=0; i<nverts; i++) {
1058 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
1059 for(int j=0; j<3; j++) {
1060 if(v[j] < aabb.min[j]) {
1061 aabb.min[j] = v[j];
1062 }
1063 if(v[j] > aabb.max[j]) {
1064 aabb.max[j] = v[j];
1065 }
1066 }
1067 }
1068 aabb_valid = true;
1069 }
1071 void Mesh::calc_bsph()
1072 {
1073 // the cast is to force calling the const version which doesn't invalidate
1074 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
1075 return;
1076 }
1078 Vector3 v;
1079 bsph.center = Vector3(0, 0, 0);
1081 // first find the center
1082 for(unsigned int i=0; i<nverts; i++) {
1083 v = get_attrib(MESH_ATTR_VERTEX, i);
1084 bsph.center += v;
1085 }
1086 bsph.center /= (float)nverts;
1088 bsph.radius = 0.0f;
1089 for(unsigned int i=0; i<nverts; i++) {
1090 v = get_attrib(MESH_ATTR_VERTEX, i);
1091 float dist_sq = (v - bsph.center).length_sq();
1092 if(dist_sq > bsph.radius) {
1093 bsph.radius = dist_sq;
1094 }
1095 }
1096 bsph.radius = sqrt(bsph.radius);
1098 bsph_valid = true;
1099 }
1101 void Mesh::update_buffers()
1102 {
1103 for(int i=0; i<NUM_MESH_ATTR; i++) {
1104 if(has_attrib(i) && !vattr[i].vbo_valid) {
1105 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
1106 glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW);
1107 vattr[i].vbo_valid = true;
1108 }
1109 }
1110 glBindBuffer(GL_ARRAY_BUFFER, 0);
1112 if(idata_valid && !ibo_valid) {
1113 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
1114 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW);
1115 ibo_valid = true;
1116 }
1117 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1118 }
1120 void Mesh::update_wire_ibo()
1121 {
1122 update_buffers();
1124 if(wire_ibo_valid) {
1125 return;
1126 }
1128 if(!wire_ibo) {
1129 glGenBuffers(1, &wire_ibo);
1130 }
1132 unsigned int *wire_idxarr = new unsigned int[nfaces * 6];
1133 unsigned int *dest = wire_idxarr;
1135 if(ibo_valid) {
1136 // we're dealing with an indexed mesh
1137 const unsigned int *idxarr = ((const Mesh*)this)->get_index_data();
1139 for(unsigned int i=0; i<nfaces; i++) {
1140 *dest++ = idxarr[0];
1141 *dest++ = idxarr[1];
1142 *dest++ = idxarr[1];
1143 *dest++ = idxarr[2];
1144 *dest++ = idxarr[2];
1145 *dest++ = idxarr[0];
1146 idxarr += 3;
1147 }
1148 } else {
1149 // not an indexed mesh ...
1150 for(unsigned int i=0; i<nfaces; i++) {
1151 int vidx = i * 3;
1152 *dest++ = vidx;
1153 *dest++ = vidx + 1;
1154 *dest++ = vidx + 1;
1155 *dest++ = vidx + 2;
1156 *dest++ = vidx + 2;
1157 *dest++ = vidx;
1158 }
1159 }
1161 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
1162 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
1163 delete [] wire_idxarr;
1164 wire_ibo_valid = true;
1165 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1166 }
1169 // ------ class Triangle ------
1170 Triangle::Triangle()
1171 {
1172 normal_valid = false;
1173 id = -1;
1174 }
1176 Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
1177 {
1178 v[0] = v0;
1179 v[1] = v1;
1180 v[2] = v2;
1181 normal_valid = false;
1182 id = -1;
1183 }
1185 Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr)
1186 {
1187 if(idxarr) {
1188 v[0] = varr[idxarr[n * 3]];
1189 v[1] = varr[idxarr[n * 3 + 1]];
1190 v[2] = varr[idxarr[n * 3 + 2]];
1191 } else {
1192 v[0] = varr[n * 3];
1193 v[1] = varr[n * 3 + 1];
1194 v[2] = varr[n * 3 + 2];
1195 }
1196 normal_valid = false;
1197 id = n;
1198 }
1200 void Triangle::calc_normal()
1201 {
1202 normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized();
1203 normal_valid = true;
1204 }
1206 const Vector3 &Triangle::get_normal() const
1207 {
1208 if(!normal_valid) {
1209 ((Triangle*)this)->calc_normal();
1210 }
1211 return normal;
1212 }
1214 void Triangle::transform(const Matrix4x4 &xform)
1215 {
1216 v[0].transform(xform);
1217 v[1].transform(xform);
1218 v[2].transform(xform);
1219 normal_valid = false;
1220 }
1222 void Triangle::draw() const
1223 {
1224 Vector3 n[3];
1225 n[0] = get_normal();
1226 n[1] = get_normal();
1227 n[2] = get_normal();
1229 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
1230 int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL);
1232 glEnableVertexAttribArray(vloc);
1233 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
1234 glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x);
1236 glDrawArrays(GL_TRIANGLES, 0, 3);
1238 glDisableVertexAttribArray(vloc);
1239 glDisableVertexAttribArray(nloc);
1240 }
1242 void Triangle::draw_wire() const
1243 {
1244 static const int idxarr[] = {0, 1, 1, 2, 2, 0};
1245 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
1247 glEnableVertexAttribArray(vloc);
1248 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
1250 glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr);
1252 glDisableVertexAttribArray(vloc);
1253 }
1255 Vector3 Triangle::calc_barycentric(const Vector3 &pos) const
1256 {
1257 Vector3 norm = get_normal();
1259 float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm));
1260 if(area_sq < 1e-5) {
1261 return Vector3(0, 0, 0);
1262 }
1264 float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm));
1265 float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm));
1266 float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm));
1268 return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq);
1269 }
1271 bool Triangle::intersect(const Ray &ray, HitPoint *hit) const
1272 {
1273 Vector3 normal = get_normal();
1275 float ndotdir = dot_product(ray.dir, normal);
1276 if(fabs(ndotdir) < 1e-4) {
1277 return false;
1278 }
1280 Vector3 vertdir = v[0] - ray.origin;
1281 float t = dot_product(normal, vertdir) / ndotdir;
1283 Vector3 pos = ray.origin + ray.dir * t;
1284 Vector3 bary = calc_barycentric(pos);
1286 if(bary.x + bary.y + bary.z > 1.00001) {
1287 return false;
1288 }
1290 if(hit) {
1291 hit->dist = t;
1292 hit->pos = ray.origin + ray.dir * t;
1293 hit->normal = normal;
1294 hit->obj = this;
1295 }
1296 return true;
1297 }