coeng

view src/mesh.cc @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +0200
parents
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 "shader.h"
9 int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5 };
10 unsigned int Mesh::intersect_mode = ISECT_DEFAULT;
11 float Mesh::vertex_sel_dist = 0.01;
12 float Mesh::vis_vecsize = 1.0;
14 Mesh::Mesh()
15 {
16 clear();
18 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
20 for(int i=0; i<NUM_MESH_ATTR; i++) {
21 vattr[i].vbo = buffer_objects[i];
22 }
23 ibo = buffer_objects[NUM_MESH_ATTR];
24 wire_ibo = 0;
25 }
27 Mesh::~Mesh()
28 {
29 glDeleteBuffers(NUM_MESH_ATTR + 1, buffer_objects);
31 if(wire_ibo) {
32 glDeleteBuffers(1, &wire_ibo);
33 }
34 }
36 void Mesh::set_name(const char *name)
37 {
38 this->name = name;
39 }
41 const char *Mesh::get_name() const
42 {
43 return name.c_str();
44 }
46 bool Mesh::has_attrib(int attr) const
47 {
48 if(attr < 0 || attr >= NUM_MESH_ATTR) {
49 return false;
50 }
52 // if neither of these is valid, then nobody has set this attribute
53 return vattr[attr].vbo_valid || vattr[attr].data_valid;
54 }
56 void Mesh::clear()
57 {
58 //bones.clear();
60 for(int i=0; i<NUM_MESH_ATTR; i++) {
61 vattr[i].nelem = 0;
62 vattr[i].vbo_valid = false;
63 vattr[i].data_valid = false;
64 //vattr[i].sdr_loc = -1;
65 vattr[i].data.clear();
66 }
67 ibo_valid = idata_valid = false;
68 idata.clear();
70 wire_ibo_valid = false;
72 nverts = nfaces = 0;
74 bsph_valid = false;
75 aabb_valid = false;
76 }
78 float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data)
79 {
80 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
81 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
82 return 0;
83 }
85 if(nverts && num != nverts) {
86 fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
87 return 0;
88 }
89 nverts = num;
91 vattr[attrib].data.clear();
92 vattr[attrib].nelem = nelem;
93 vattr[attrib].data.resize(num * nelem);
95 if(data) {
96 memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data);
97 }
99 vattr[attrib].data_valid = true;
100 vattr[attrib].vbo_valid = false;
101 return &vattr[attrib].data[0];
102 }
104 float *Mesh::get_attrib_data(int attrib)
105 {
106 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
107 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
108 return 0;
109 }
111 vattr[attrib].vbo_valid = false;
112 return (float*)((const Mesh*)this)->get_attrib_data(attrib);
113 }
115 const float *Mesh::get_attrib_data(int attrib) const
116 {
117 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
118 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
119 return 0;
120 }
122 if(!vattr[attrib].data_valid) {
123 #if GL_ES_VERSION_2_0
124 fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
125 return 0;
126 #else
127 if(!vattr[attrib].vbo_valid) {
128 fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
129 return 0;
130 }
132 // local data copy is unavailable, grab the data from the vbo
133 Mesh *m = (Mesh*)this;
134 m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem);
136 glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo);
137 void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
138 memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float));
139 glUnmapBuffer(GL_ARRAY_BUFFER);
141 vattr[attrib].data_valid = true;
142 #endif
143 }
145 return &vattr[attrib].data[0];
146 }
148 void Mesh::set_attrib(int attrib, int idx, const Vector4 &v)
149 {
150 float *data = get_attrib_data(attrib);
151 if(data) {
152 data += idx * vattr[attrib].nelem;
153 for(int i=0; i<vattr[attrib].nelem; i++) {
154 data[i] = v[i];
155 }
156 }
157 }
159 Vector4 Mesh::get_attrib(int attrib, int idx) const
160 {
161 Vector4 v(0.0, 0.0, 0.0, 1.0);
162 const float *data = get_attrib_data(attrib);
163 if(data) {
164 data += idx * vattr[attrib].nelem;
165 for(int i=0; i<vattr[attrib].nelem; i++) {
166 v[i] = data[i];
167 }
168 }
169 return v;
170 }
172 unsigned int *Mesh::set_index_data(int num, const unsigned int *indices)
173 {
174 int nidx = nfaces * 3;
175 if(nidx && num != nidx) {
176 fprintf(stderr, "%s: index count missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
177 return 0;
178 }
179 nfaces = num / 3;
181 idata.clear();
182 idata.resize(num);
184 if(indices) {
185 memcpy(&idata[0], indices, num * sizeof *indices);
186 }
188 idata_valid = true;
189 ibo_valid = false;
191 return &idata[0];
192 }
194 unsigned int *Mesh::get_index_data()
195 {
196 ibo_valid = false;
197 return (unsigned int*)((const Mesh*)this)->get_index_data();
198 }
200 const unsigned int *Mesh::get_index_data() const
201 {
202 if(!idata_valid) {
203 #if GL_ES_VERSION_2_0
204 fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
205 return 0;
206 #else
207 if(!ibo_valid) {
208 fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__);
209 return 0;
210 }
212 // local data copy is unavailable, gram the data from the ibo
213 Mesh *m = (Mesh*)this;
214 int nidx = nfaces * 3;
215 m->idata.resize(nidx);
217 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
218 void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
219 memcpy(&m->idata[0], data, nidx * sizeof(unsigned int));
220 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
222 idata_valid = true;
223 #endif
224 }
226 return &idata[0];
227 }
229 void Mesh::append(const Mesh &mesh)
230 {
231 unsigned int idxoffs = nverts;
233 nverts += mesh.nverts;
234 nfaces += mesh.nfaces;
236 for(int i=0; i<NUM_MESH_ATTR; i++) {
237 if(has_attrib(i) && mesh.has_attrib(i)) {
238 // force validating the data arrays
239 get_attrib_data(i);
240 mesh.get_attrib_data(i);
242 // append the mesh data
243 vattr[i].data.insert(vattr[i].data.end(), mesh.vattr[i].data.begin(), mesh.vattr[i].data.end());
244 }
245 }
247 if(ibo_valid || idata_valid) {
248 // make index arrays valid
249 get_index_data();
250 mesh.get_index_data();
252 size_t orig_sz = idata.size();
254 idata.insert(idata.end(), mesh.idata.begin(), mesh.idata.end());
256 // fixup all the new indices
257 for(size_t i=orig_sz; i<idata.size(); i++) {
258 idata[i] += idxoffs;
259 }
260 }
262 // fuck everything
263 wire_ibo_valid = false;
264 aabb_valid = false;
265 bsph_valid = false;
266 }
268 // assemble a complete vertex by adding all the useful attributes
269 void Mesh::vertex(float x, float y, float z)
270 {
271 cur_val[MESH_ATTR_VERTEX] = Vector4(x, y, z, 1.0f);
272 vattr[MESH_ATTR_VERTEX].data_valid = true;
273 vattr[MESH_ATTR_VERTEX].nelem = 3;
275 for(int i=0; i<NUM_MESH_ATTR; i++) {
276 if(vattr[i].data_valid) {
277 for(int j=0; j<vattr[MESH_ATTR_VERTEX].nelem; j++) {
278 vattr[i].data.push_back(cur_val[i][j]);
279 }
280 }
281 vattr[i].vbo_valid = false;
282 }
284 if(idata_valid) {
285 idata.clear();
286 }
287 ibo_valid = idata_valid = false;
288 }
290 void Mesh::normal(float nx, float ny, float nz)
291 {
292 cur_val[MESH_ATTR_NORMAL] = Vector4(nx, ny, nz, 1.0f);
293 vattr[MESH_ATTR_NORMAL].data_valid = true;
294 vattr[MESH_ATTR_NORMAL].nelem = 3;
295 }
297 void Mesh::tangent(float tx, float ty, float tz)
298 {
299 cur_val[MESH_ATTR_TANGENT] = Vector4(tx, ty, tz, 1.0f);
300 vattr[MESH_ATTR_TANGENT].data_valid = true;
301 vattr[MESH_ATTR_TANGENT].nelem = 3;
302 }
304 void Mesh::texcoord(float u, float v, float w)
305 {
306 cur_val[MESH_ATTR_TEXCOORD] = Vector4(u, v, w, 1.0f);
307 vattr[MESH_ATTR_TEXCOORD].data_valid = true;
308 vattr[MESH_ATTR_TEXCOORD].nelem = 3;
309 }
311 void Mesh::boneweights(float w1, float w2, float w3, float w4)
312 {
313 cur_val[MESH_ATTR_BONEWEIGHTS] = Vector4(w1, w2, w3, w4);
314 vattr[MESH_ATTR_BONEWEIGHTS].data_valid = true;
315 vattr[MESH_ATTR_BONEWEIGHTS].nelem = 4;
316 }
318 void Mesh::boneidx(int idx1, int idx2, int idx3, int idx4)
319 {
320 cur_val[MESH_ATTR_BONEIDX] = Vector4(idx1, idx2, idx3, idx4);
321 vattr[MESH_ATTR_BONEIDX].data_valid = true;
322 vattr[MESH_ATTR_BONEIDX].nelem = 4;
323 }
325 /// static function
326 void Mesh::set_attrib_location(int attr, int loc)
327 {
328 if(attr < 0 || attr >= NUM_MESH_ATTR) {
329 return;
330 }
331 Mesh::global_sdr_loc[attr] = loc;
332 }
334 /// static function
335 int Mesh::get_attrib_location(int attr)
336 {
337 if(attr < 0 || attr >= NUM_MESH_ATTR) {
338 return -1;
339 }
340 return Mesh::global_sdr_loc[attr];
341 }
343 /// static function
344 void Mesh::clear_attrib_locations()
345 {
346 for(int i=0; i<NUM_MESH_ATTR; i++) {
347 Mesh::global_sdr_loc[i] = -1;
348 }
349 }
351 /// static function
352 void Mesh::set_vis_vecsize(float sz)
353 {
354 Mesh::vis_vecsize = sz;
355 }
357 float Mesh::get_vis_vecsize()
358 {
359 return Mesh::vis_vecsize;
360 }
362 void Mesh::apply_xform(const Matrix4x4 &xform)
363 {
364 Matrix4x4 dir_xform = xform;
365 dir_xform[0][3] = dir_xform[1][3] = dir_xform[2][3] = 0.0f;
366 dir_xform[3][0] = dir_xform[3][1] = dir_xform[3][2] = 0.0f;
367 dir_xform[3][3] = 1.0f;
369 apply_xform(xform, dir_xform);
370 }
372 void Mesh::apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform)
373 {
374 for(unsigned int i=0; i<nverts; i++) {
375 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
376 set_attrib(MESH_ATTR_VERTEX, i, v.transformed(xform));
378 if(has_attrib(MESH_ATTR_NORMAL)) {
379 Vector3 n = get_attrib(MESH_ATTR_NORMAL, i);
380 set_attrib(MESH_ATTR_NORMAL, i, n.transformed(dir_xform));
381 }
382 if(has_attrib(MESH_ATTR_TANGENT)) {
383 Vector3 t = get_attrib(MESH_ATTR_TANGENT, i);
384 set_attrib(MESH_ATTR_TANGENT, i, t.transformed(dir_xform));
385 }
386 }
387 }
389 /*
390 int Mesh::add_bone(XFormNode *bone)
391 {
392 int idx = bones.size();
393 bones.push_back(bone);
394 return idx;
395 }
397 const XFormNode *Mesh::get_bone(int idx) const
398 {
399 if(idx < 0 || idx >= (int)bones.size()) {
400 return 0;
401 }
402 return bones[idx];
403 }
405 int Mesh::get_bones_count() const
406 {
407 return (int)bones.size();
408 }
409 */
411 void Mesh::draw() const
412 {
413 const ShaderProg *cur_sdr = get_current_shader();
414 #ifdef GL_ES_VERSION_2_0
415 if(!cur_sdr) {
416 fprintf(stderr, "%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__);
417 return;
418 }
419 #endif
421 ((Mesh*)this)->update_buffers();
423 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
424 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
425 return;
426 }
428 if(cur_sdr) {
429 // rendering with shaders
430 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
431 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
432 return;
433 }
435 for(int i=0; i<NUM_MESH_ATTR; i++) {
436 int loc = global_sdr_loc[i];
437 if(loc >= 0 && vattr[i].vbo_valid) {
438 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
439 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
440 glEnableVertexAttribArray(loc);
441 }
442 }
443 } else {
444 #ifndef GL_ES_VERSION_2_0
445 // rendering with fixed-function (not available in GLES2)
446 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
447 glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
448 glEnableClientState(GL_VERTEX_ARRAY);
450 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
451 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
452 glNormalPointer(GL_FLOAT, 0, 0);
453 glEnableClientState(GL_NORMAL_ARRAY);
454 }
455 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
456 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
457 glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
458 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
459 }
460 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
461 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
462 glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
463 glEnableClientState(GL_COLOR_ARRAY);
464 }
465 #endif
466 }
467 glBindBuffer(GL_ARRAY_BUFFER, 0);
469 if(ibo_valid) {
470 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
471 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
472 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
473 } else {
474 glDrawArrays(GL_TRIANGLES, 0, nverts);
475 }
477 if(cur_sdr) {
478 // rendered with shaders
479 for(int i=0; i<NUM_MESH_ATTR; i++) {
480 int loc = global_sdr_loc[i];
481 if(loc >= 0 && vattr[i].vbo_valid) {
482 glDisableVertexAttribArray(loc);
483 }
484 }
485 } else {
486 #ifndef GL_ES_VERSION_2_0
487 // rendered with fixed-function
488 glDisableClientState(GL_VERTEX_ARRAY);
489 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
490 glDisableClientState(GL_NORMAL_ARRAY);
491 }
492 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
493 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
494 }
495 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
496 glDisableClientState(GL_COLOR_ARRAY);
497 }
498 #endif
499 }
500 }
502 void Mesh::draw_wire() const
503 {
504 ((Mesh*)this)->update_wire_ibo();
506 if(!vattr[MESH_ATTR_VERTEX].vbo_valid || !wire_ibo_valid) {
507 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
508 return;
509 }
510 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
511 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
512 return;
513 }
515 for(int i=0; i<NUM_MESH_ATTR; i++) {
516 int loc = global_sdr_loc[i];
517 if(loc >= 0 && vattr[i].vbo_valid) {
518 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
519 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
520 glEnableVertexAttribArray(loc);
521 }
522 }
523 glBindBuffer(GL_ARRAY_BUFFER, 0);
525 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
526 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
527 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
529 for(int i=0; i<NUM_MESH_ATTR; i++) {
530 int loc = global_sdr_loc[i];
531 if(loc >= 0 && vattr[i].vbo_valid) {
532 glDisableVertexAttribArray(loc);
533 }
534 }
535 }
537 void Mesh::draw_vertices() const
538 {
539 ((Mesh*)this)->update_buffers();
541 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
542 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
543 return;
544 }
545 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
546 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
547 return;
548 }
550 for(int i=0; i<NUM_MESH_ATTR; i++) {
551 int loc = global_sdr_loc[i];
552 if(loc >= 0 && vattr[i].vbo_valid) {
553 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
554 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
555 glEnableVertexAttribArray(loc);
556 }
557 }
558 glBindBuffer(GL_ARRAY_BUFFER, 0);
560 glDrawArrays(GL_POINTS, 0, nverts);
562 for(int i=0; i<NUM_MESH_ATTR; i++) {
563 int loc = global_sdr_loc[i];
564 if(loc >= 0 && vattr[i].vbo_valid) {
565 glDisableVertexAttribArray(loc);
566 }
567 }
568 }
570 void Mesh::draw_normals() const
571 {
572 #ifdef USE_OLDGL
573 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
574 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
575 Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
577 if(!varr || !norm || vert_loc < 0) {
578 return;
579 }
581 glBegin(GL_LINES);
582 for(size_t i=0; i<nverts; i++) {
583 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
584 Vector3 end = varr[i] + norm[i] * vis_vecsize;
585 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
586 }
587 glEnd();
589 #endif // USE_OLDGL
590 }
592 void Mesh::draw_tangents() const
593 {
594 #ifdef USE_OLDGL
595 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
596 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
597 Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT);
599 if(!varr || !tang || vert_loc < 0) {
600 return;
601 }
603 glBegin(GL_LINES);
604 for(size_t i=0; i<nverts; i++) {
605 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
606 Vector3 end = varr[i] + tang[i] * vis_vecsize;
607 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
608 }
609 glEnd();
611 #endif // USE_OLDGL
612 }
614 void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const
615 {
616 if(!aabb_valid) {
617 ((Mesh*)this)->calc_aabb();
618 }
619 *vmin = aabb.min;
620 *vmax = aabb.max;
621 }
623 const AABox &Mesh::get_aabbox() const
624 {
625 if(!aabb_valid) {
626 ((Mesh*)this)->calc_aabb();
627 }
628 return aabb;
629 }
631 float Mesh::get_bsphere(Vector3 *center, float *rad) const
632 {
633 if(!bsph_valid) {
634 ((Mesh*)this)->calc_bsph();
635 }
636 *center = bsph.center;
637 *rad = bsph.radius;
638 return bsph.radius;
639 }
641 const Sphere &Mesh::get_bsphere() const
642 {
643 if(!bsph_valid) {
644 ((Mesh*)this)->calc_bsph();
645 }
646 return bsph;
647 }
649 /// static function
650 void Mesh::set_intersect_mode(unsigned int mode)
651 {
652 Mesh::intersect_mode = mode;
653 }
655 /// static function
656 unsigned int Mesh::get_intersect_mode()
657 {
658 return Mesh::intersect_mode;
659 }
661 /// static function
662 void Mesh::set_vertex_select_distance(float dist)
663 {
664 Mesh::vertex_sel_dist = dist;
665 }
667 /// static function
668 float Mesh::get_vertex_select_distance()
669 {
670 return Mesh::vertex_sel_dist;
671 }
673 bool Mesh::intersect(const Ray &ray, HitPoint *hit) const
674 {
675 assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE));
677 const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
678 const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
679 if(!varr) {
680 return false;
681 }
682 const unsigned int *idxarr = get_index_data();
684 // first test with the bounding box
685 AABox box;
686 get_aabbox(&box.min, &box.max);
687 if(!box.intersect(ray)) {
688 return false;
689 }
691 HitPoint nearest_hit;
692 nearest_hit.dist = FLT_MAX;
693 nearest_hit.shape = 0;
695 if(Mesh::intersect_mode & ISECT_VERTICES) {
696 // we asked for "intersections" with the vertices of the mesh
697 long nearest_vidx = -1;
698 float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist;
700 for(unsigned int i=0; i<nverts; i++) {
702 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) {
703 continue;
704 }
706 // project the vertex onto the ray line
707 float t = dot_product(varr[i] - ray.origin, ray.dir);
708 Vector3 vproj = ray.origin + ray.dir * t;
710 float dist_sq = (vproj - varr[i]).length_sq();
711 if(dist_sq < thres_sq) {
712 if(!hit) {
713 return true;
714 }
715 if(t < nearest_hit.dist) {
716 nearest_hit.dist = t;
717 nearest_vidx = i;
718 }
719 }
720 }
722 if(nearest_vidx != -1) {
723 hitvert = varr[nearest_vidx];
724 nearest_hit.data = (void*)&hitvert;
725 }
727 } else {
728 // regular intersection test with polygons
730 for(unsigned int i=0; i<nfaces; i++) {
731 Triangle face(i, varr, idxarr);
733 // ignore back-facing polygons if the mode flags include ISECT_FRONT
734 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) {
735 continue;
736 }
738 HitPoint fhit;
739 if(face.intersect(ray, hit ? &fhit : 0)) {
740 if(!hit) {
741 return true;
742 }
743 if(fhit.dist < nearest_hit.dist) {
744 nearest_hit = fhit;
745 hitface = face;
746 }
747 }
748 }
749 }
751 if(nearest_hit.shape) {
752 if(hit) {
753 *hit = nearest_hit;
755 // if we are interested in the mesh and not the faces set obj to this
756 if(Mesh::intersect_mode & ISECT_FACE) {
757 hit->data = &hitface;
758 } else if(Mesh::intersect_mode & ISECT_VERTICES) {
759 hit->data = &hitvert;
760 } else {
761 hit->data = (void*)this;
762 }
763 }
764 return true;
765 }
766 return false;
767 }
770 // ------ private member functions ------
772 void Mesh::calc_aabb()
773 {
774 // the cast is to force calling the const version which doesn't invalidate
775 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
776 return;
777 }
779 aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
780 aabb.max = -aabb.min;
782 for(unsigned int i=0; i<nverts; i++) {
783 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
784 for(int j=0; j<3; j++) {
785 if(v[j] < aabb.min[j]) {
786 aabb.min[j] = v[j];
787 }
788 if(v[j] > aabb.max[j]) {
789 aabb.max[j] = v[j];
790 }
791 }
792 }
793 aabb_valid = true;
794 }
796 void Mesh::calc_bsph()
797 {
798 // the cast is to force calling the const version which doesn't invalidate
799 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
800 return;
801 }
803 Vector3 v;
804 bsph.center = Vector3(0, 0, 0);
806 // first find the center
807 for(unsigned int i=0; i<nverts; i++) {
808 v = get_attrib(MESH_ATTR_VERTEX, i);
809 bsph.center += v;
810 }
811 bsph.center /= (float)nverts;
813 bsph.radius = 0.0f;
814 for(unsigned int i=0; i<nverts; i++) {
815 v = get_attrib(MESH_ATTR_VERTEX, i);
816 float dist_sq = (v - bsph.center).length_sq();
817 if(dist_sq > bsph.radius) {
818 bsph.radius = dist_sq;
819 }
820 }
821 bsph.radius = sqrt(bsph.radius);
823 bsph_valid = true;
824 }
826 void Mesh::update_buffers()
827 {
828 for(int i=0; i<NUM_MESH_ATTR; i++) {
829 if(has_attrib(i) && !vattr[i].vbo_valid) {
830 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
831 glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW);
832 vattr[i].vbo_valid = true;
833 }
834 }
835 glBindBuffer(GL_ARRAY_BUFFER, 0);
837 if(idata_valid && !ibo_valid) {
838 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
839 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW);
840 ibo_valid = true;
841 }
842 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
843 }
845 void Mesh::update_wire_ibo()
846 {
847 update_buffers();
849 if(wire_ibo_valid) {
850 return;
851 }
853 if(!wire_ibo) {
854 glGenBuffers(1, &wire_ibo);
855 }
857 unsigned int *wire_idxarr = new unsigned int[nfaces * 6];
858 unsigned int *dest = wire_idxarr;
860 if(ibo_valid) {
861 // we're dealing with an indexed mesh
862 const unsigned int *idxarr = ((const Mesh*)this)->get_index_data();
864 for(unsigned int i=0; i<nfaces; i++) {
865 *dest++ = idxarr[0];
866 *dest++ = idxarr[1];
867 *dest++ = idxarr[1];
868 *dest++ = idxarr[2];
869 *dest++ = idxarr[2];
870 *dest++ = idxarr[0];
871 idxarr += 3;
872 }
873 } else {
874 // not an indexed mesh ...
875 for(unsigned int i=0; i<nfaces; i++) {
876 int vidx = i * 3;
877 *dest++ = vidx;
878 *dest++ = vidx + 1;
879 *dest++ = vidx + 1;
880 *dest++ = vidx + 2;
881 *dest++ = vidx + 2;
882 *dest++ = vidx;
883 }
884 }
886 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
887 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
888 delete [] wire_idxarr;
889 wire_ibo_valid = true;
890 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
891 }
894 // ------ class Triangle ------
895 Triangle::Triangle()
896 {
897 normal_valid = false;
898 id = -1;
899 }
901 Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
902 {
903 v[0] = v0;
904 v[1] = v1;
905 v[2] = v2;
906 normal_valid = false;
907 id = -1;
908 }
910 Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr)
911 {
912 if(idxarr) {
913 v[0] = varr[idxarr[n * 3]];
914 v[1] = varr[idxarr[n * 3 + 1]];
915 v[2] = varr[idxarr[n * 3 + 2]];
916 } else {
917 v[0] = varr[n * 3];
918 v[1] = varr[n * 3 + 1];
919 v[2] = varr[n * 3 + 2];
920 }
921 normal_valid = false;
922 id = n;
923 }
925 void Triangle::calc_normal()
926 {
927 normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized();
928 normal_valid = true;
929 }
931 const Vector3 &Triangle::get_normal() const
932 {
933 if(!normal_valid) {
934 ((Triangle*)this)->calc_normal();
935 }
936 return normal;
937 }
939 void Triangle::transform(const Matrix4x4 &xform)
940 {
941 v[0].transform(xform);
942 v[1].transform(xform);
943 v[2].transform(xform);
944 normal_valid = false;
945 }
947 void Triangle::draw() const
948 {
949 Vector3 n[3];
950 n[0] = get_normal();
951 n[1] = get_normal();
952 n[2] = get_normal();
954 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
955 int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL);
957 glEnableVertexAttribArray(vloc);
958 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
959 glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x);
961 glDrawArrays(GL_TRIANGLES, 0, 3);
963 glDisableVertexAttribArray(vloc);
964 glDisableVertexAttribArray(nloc);
965 }
967 void Triangle::draw_wire() const
968 {
969 static const int idxarr[] = {0, 1, 1, 2, 2, 0};
970 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
972 glEnableVertexAttribArray(vloc);
973 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
975 glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr);
977 glDisableVertexAttribArray(vloc);
978 }
980 Vector3 Triangle::calc_barycentric(const Vector3 &pos) const
981 {
982 Vector3 norm = get_normal();
984 float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm));
985 if(area_sq < 1e-5) {
986 return Vector3(0, 0, 0);
987 }
989 float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm));
990 float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm));
991 float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm));
993 return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq);
994 }
996 bool Triangle::intersect(const Ray &ray, HitPoint *hit) const
997 {
998 Vector3 normal = get_normal();
1000 float ndotdir = dot_product(ray.dir, normal);
1001 if(fabs(ndotdir) < 1e-4) {
1002 return false;
1005 Vector3 vertdir = v[0] - ray.origin;
1006 float t = dot_product(normal, vertdir) / ndotdir;
1008 Vector3 pos = ray.origin + ray.dir * t;
1009 Vector3 bary = calc_barycentric(pos);
1011 if(bary.x + bary.y + bary.z > 1.00001) {
1012 return false;
1015 if(hit) {
1016 hit->dist = t;
1017 hit->pos = ray.origin + ray.dir * t;
1018 hit->normal = normal;
1019 hit->data = (void*)this;
1021 return true;