tavli
view src/mesh.cc @ 0:52e0dd47753b
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 21 Jun 2015 06:30:39 +0300 |
parents | |
children | 3fcd7b4d631f |
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"
8 #include "shader.h"
10 int Mesh::global_sdr_loc[NUM_MESH_ATTR] = {
11 (int)SDR_ATTR_VERTEX,
12 (int)SDR_ATTR_NORMAL,
13 (int)SDR_ATTR_TANGENT,
14 (int)SDR_ATTR_TEXCOORD,
15 (int)SDR_ATTR_COLOR,
16 -1, -1};
17 unsigned int Mesh::intersect_mode = ISECT_DEFAULT;
18 float Mesh::vertex_sel_dist = 0.01;
19 float Mesh::vis_vecsize = 1.0;
21 Mesh::Mesh()
22 {
23 clear();
25 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
27 for(int i=0; i<NUM_MESH_ATTR; i++) {
28 vattr[i].vbo = buffer_objects[i];
29 }
30 ibo = buffer_objects[NUM_MESH_ATTR];
31 wire_ibo = 0;
32 }
34 Mesh::~Mesh()
35 {
36 glDeleteBuffers(NUM_MESH_ATTR + 1, buffer_objects);
38 if(wire_ibo) {
39 glDeleteBuffers(1, &wire_ibo);
40 }
41 }
43 Mesh::Mesh(const Mesh &rhs)
44 {
45 clear();
47 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
49 for(int i=0; i<NUM_MESH_ATTR; i++) {
50 vattr[i].vbo = buffer_objects[i];
51 }
52 ibo = buffer_objects[NUM_MESH_ATTR];
53 wire_ibo = 0;
55 clone(rhs);
56 }
58 Mesh &Mesh::operator =(const Mesh &rhs)
59 {
60 if(&rhs != this) {
61 clone(rhs);
62 }
63 return *this;
64 }
66 bool Mesh::clone(const Mesh &m)
67 {
68 clear();
70 for(int i=0; i<NUM_MESH_ATTR; i++) {
71 if(m.has_attrib(i)) {
72 m.get_attrib_data(i); // force validation of the actual data on the source mesh
74 vattr[i].nelem = m.vattr[i].nelem;
75 vattr[i].data = m.vattr[i].data; // copy the actual data
76 vattr[i].data_valid = true;
77 }
78 }
80 if(m.is_indexed()) {
81 m.get_index_data(); // again, force validation
83 // copy the index data
84 idata = m.idata;
85 idata_valid = true;
86 }
88 name = m.name;
89 nverts = m.nverts;
90 nfaces = m.nfaces;
92 //bones = m.bones;
94 memcpy(cur_val, m.cur_val, sizeof cur_val);
96 aabb = m.aabb;
97 aabb_valid = m.aabb_valid;
98 bsph = m.bsph;
99 bsph_valid = m.bsph_valid;
101 hitface = m.hitface;
102 hitvert = m.hitvert;
104 intersect_mode = m.intersect_mode;
105 vertex_sel_dist = m.vertex_sel_dist;
106 vis_vecsize = m.vis_vecsize;
108 return true;
109 }
111 void Mesh::set_name(const char *name)
112 {
113 this->name = name;
114 }
116 const char *Mesh::get_name() const
117 {
118 return name.c_str();
119 }
121 bool Mesh::has_attrib(int attr) const
122 {
123 if(attr < 0 || attr >= NUM_MESH_ATTR) {
124 return false;
125 }
127 // if neither of these is valid, then nobody has set this attribute
128 return vattr[attr].vbo_valid || vattr[attr].data_valid;
129 }
131 bool Mesh::is_indexed() const
132 {
133 return ibo_valid || idata_valid;
134 }
136 void Mesh::clear()
137 {
138 //bones.clear();
140 for(int i=0; i<NUM_MESH_ATTR; i++) {
141 vattr[i].nelem = 0;
142 vattr[i].vbo_valid = false;
143 vattr[i].data_valid = false;
144 //vattr[i].sdr_loc = -1;
145 vattr[i].data.clear();
146 }
147 ibo_valid = idata_valid = false;
148 idata.clear();
150 wire_ibo_valid = false;
152 nverts = nfaces = 0;
154 bsph_valid = false;
155 aabb_valid = false;
156 }
158 float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data)
159 {
160 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
161 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
162 return 0;
163 }
165 if(nverts && num != nverts) {
166 fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
167 return 0;
168 }
169 nverts = num;
171 vattr[attrib].data.clear();
172 vattr[attrib].nelem = nelem;
173 vattr[attrib].data.resize(num * nelem);
175 if(data) {
176 memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data);
177 }
179 vattr[attrib].data_valid = true;
180 vattr[attrib].vbo_valid = false;
181 return &vattr[attrib].data[0];
182 }
184 float *Mesh::get_attrib_data(int attrib)
185 {
186 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
187 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
188 return 0;
189 }
191 vattr[attrib].vbo_valid = false;
192 return (float*)((const Mesh*)this)->get_attrib_data(attrib);
193 }
195 const float *Mesh::get_attrib_data(int attrib) const
196 {
197 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
198 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
199 return 0;
200 }
202 if(!vattr[attrib].data_valid) {
203 #if GL_ES_VERSION_2_0
204 fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
205 return 0;
206 #else
207 if(!vattr[attrib].vbo_valid) {
208 fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
209 return 0;
210 }
212 // local data copy is unavailable, grab the data from the vbo
213 Mesh *m = (Mesh*)this;
214 m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem);
216 glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo);
217 void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
218 memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float));
219 glUnmapBuffer(GL_ARRAY_BUFFER);
221 vattr[attrib].data_valid = true;
222 #endif
223 }
225 return &vattr[attrib].data[0];
226 }
228 void Mesh::set_attrib(int attrib, int idx, const Vector4 &v)
229 {
230 float *data = get_attrib_data(attrib);
231 if(data) {
232 data += idx * vattr[attrib].nelem;
233 for(int i=0; i<vattr[attrib].nelem; i++) {
234 data[i] = v[i];
235 }
236 }
237 }
239 Vector4 Mesh::get_attrib(int attrib, int idx) const
240 {
241 Vector4 v(0.0, 0.0, 0.0, 1.0);
242 const float *data = get_attrib_data(attrib);
243 if(data) {
244 data += idx * vattr[attrib].nelem;
245 for(int i=0; i<vattr[attrib].nelem; i++) {
246 v[i] = data[i];
247 }
248 }
249 return v;
250 }
252 int Mesh::get_attrib_count(int attrib) const
253 {
254 return has_attrib(attrib) ? nverts : 0;
255 }
258 unsigned int *Mesh::set_index_data(int num, const unsigned int *indices)
259 {
260 int nidx = nfaces * 3;
261 if(nidx && num != nidx) {
262 fprintf(stderr, "%s: index count missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
263 return 0;
264 }
265 nfaces = num / 3;
267 idata.clear();
268 idata.resize(num);
270 if(indices) {
271 memcpy(&idata[0], indices, num * sizeof *indices);
272 }
274 idata_valid = true;
275 ibo_valid = false;
277 return &idata[0];
278 }
280 unsigned int *Mesh::get_index_data()
281 {
282 ibo_valid = false;
283 return (unsigned int*)((const Mesh*)this)->get_index_data();
284 }
286 const unsigned int *Mesh::get_index_data() const
287 {
288 if(!idata_valid) {
289 #if GL_ES_VERSION_2_0
290 fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
291 return 0;
292 #else
293 if(!ibo_valid) {
294 fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__);
295 return 0;
296 }
298 // local data copy is unavailable, gram the data from the ibo
299 Mesh *m = (Mesh*)this;
300 int nidx = nfaces * 3;
301 m->idata.resize(nidx);
303 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
304 void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
305 memcpy(&m->idata[0], data, nidx * sizeof(unsigned int));
306 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
308 idata_valid = true;
309 #endif
310 }
312 return &idata[0];
313 }
315 int Mesh::get_index_count() const
316 {
317 return nfaces * 3;
318 }
320 void Mesh::append(const Mesh &mesh)
321 {
322 unsigned int idxoffs = nverts;
324 nverts += mesh.nverts;
325 nfaces += mesh.nfaces;
327 for(int i=0; i<NUM_MESH_ATTR; i++) {
328 if(has_attrib(i) && mesh.has_attrib(i)) {
329 // force validating the data arrays
330 get_attrib_data(i);
331 mesh.get_attrib_data(i);
333 // append the mesh data
334 vattr[i].data.insert(vattr[i].data.end(), mesh.vattr[i].data.begin(), mesh.vattr[i].data.end());
335 }
336 }
338 if(ibo_valid || idata_valid) {
339 // make index arrays valid
340 get_index_data();
341 mesh.get_index_data();
343 size_t orig_sz = idata.size();
345 idata.insert(idata.end(), mesh.idata.begin(), mesh.idata.end());
347 // fixup all the new indices
348 for(size_t i=orig_sz; i<idata.size(); i++) {
349 idata[i] += idxoffs;
350 }
351 }
353 // fuck everything
354 wire_ibo_valid = false;
355 aabb_valid = false;
356 bsph_valid = false;
357 }
359 // assemble a complete vertex by adding all the useful attributes
360 void Mesh::vertex(float x, float y, float z)
361 {
362 cur_val[MESH_ATTR_VERTEX] = Vector4(x, y, z, 1.0f);
363 vattr[MESH_ATTR_VERTEX].data_valid = true;
364 vattr[MESH_ATTR_VERTEX].nelem = 3;
366 for(int i=0; i<NUM_MESH_ATTR; i++) {
367 if(vattr[i].data_valid) {
368 for(int j=0; j<vattr[MESH_ATTR_VERTEX].nelem; j++) {
369 vattr[i].data.push_back(cur_val[i][j]);
370 }
371 }
372 vattr[i].vbo_valid = false;
373 }
375 if(idata_valid) {
376 idata.clear();
377 }
378 ibo_valid = idata_valid = false;
379 }
381 void Mesh::normal(float nx, float ny, float nz)
382 {
383 cur_val[MESH_ATTR_NORMAL] = Vector4(nx, ny, nz, 1.0f);
384 vattr[MESH_ATTR_NORMAL].data_valid = true;
385 vattr[MESH_ATTR_NORMAL].nelem = 3;
386 }
388 void Mesh::tangent(float tx, float ty, float tz)
389 {
390 cur_val[MESH_ATTR_TANGENT] = Vector4(tx, ty, tz, 1.0f);
391 vattr[MESH_ATTR_TANGENT].data_valid = true;
392 vattr[MESH_ATTR_TANGENT].nelem = 3;
393 }
395 void Mesh::texcoord(float u, float v, float w)
396 {
397 cur_val[MESH_ATTR_TEXCOORD] = Vector4(u, v, w, 1.0f);
398 vattr[MESH_ATTR_TEXCOORD].data_valid = true;
399 vattr[MESH_ATTR_TEXCOORD].nelem = 3;
400 }
402 void Mesh::boneweights(float w1, float w2, float w3, float w4)
403 {
404 cur_val[MESH_ATTR_BONEWEIGHTS] = Vector4(w1, w2, w3, w4);
405 vattr[MESH_ATTR_BONEWEIGHTS].data_valid = true;
406 vattr[MESH_ATTR_BONEWEIGHTS].nelem = 4;
407 }
409 void Mesh::boneidx(int idx1, int idx2, int idx3, int idx4)
410 {
411 cur_val[MESH_ATTR_BONEIDX] = Vector4(idx1, idx2, idx3, idx4);
412 vattr[MESH_ATTR_BONEIDX].data_valid = true;
413 vattr[MESH_ATTR_BONEIDX].nelem = 4;
414 }
416 int Mesh::get_poly_count() const
417 {
418 if(nfaces) {
419 return nfaces;
420 }
421 if(nverts) {
422 return nverts / 3;
423 }
424 return 0;
425 }
427 /// static function
428 void Mesh::set_attrib_location(int attr, int loc)
429 {
430 if(attr < 0 || attr >= NUM_MESH_ATTR) {
431 return;
432 }
433 Mesh::global_sdr_loc[attr] = loc;
434 }
436 /// static function
437 int Mesh::get_attrib_location(int attr)
438 {
439 if(attr < 0 || attr >= NUM_MESH_ATTR) {
440 return -1;
441 }
442 return Mesh::global_sdr_loc[attr];
443 }
445 /// static function
446 void Mesh::clear_attrib_locations()
447 {
448 for(int i=0; i<NUM_MESH_ATTR; i++) {
449 Mesh::global_sdr_loc[i] = -1;
450 }
451 }
453 /// static function
454 void Mesh::set_vis_vecsize(float sz)
455 {
456 Mesh::vis_vecsize = sz;
457 }
459 float Mesh::get_vis_vecsize()
460 {
461 return Mesh::vis_vecsize;
462 }
464 void Mesh::apply_xform(const Matrix4x4 &xform)
465 {
466 Matrix4x4 dir_xform = xform;
467 dir_xform[0][3] = dir_xform[1][3] = dir_xform[2][3] = 0.0f;
468 dir_xform[3][0] = dir_xform[3][1] = dir_xform[3][2] = 0.0f;
469 dir_xform[3][3] = 1.0f;
471 apply_xform(xform, dir_xform);
472 }
474 void Mesh::apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform)
475 {
476 for(unsigned int i=0; i<nverts; i++) {
477 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
478 set_attrib(MESH_ATTR_VERTEX, i, v.transformed(xform));
480 if(has_attrib(MESH_ATTR_NORMAL)) {
481 Vector3 n = get_attrib(MESH_ATTR_NORMAL, i);
482 set_attrib(MESH_ATTR_NORMAL, i, n.transformed(dir_xform));
483 }
484 if(has_attrib(MESH_ATTR_TANGENT)) {
485 Vector3 t = get_attrib(MESH_ATTR_TANGENT, i);
486 set_attrib(MESH_ATTR_TANGENT, i, t.transformed(dir_xform));
487 }
488 }
489 }
491 void Mesh::flip()
492 {
493 flip_faces();
494 flip_normals();
495 }
497 void Mesh::flip_faces()
498 {
499 if(is_indexed()) {
500 unsigned int *indices = get_index_data();
501 if(!indices) return;
503 int idxnum = get_index_count();
504 for(int i=0; i<idxnum; i+=3) {
505 unsigned int tmp = indices[i + 2];
506 indices[i + 2] = indices[i + 1];
507 indices[i + 1] = tmp;
508 }
510 } else {
511 Vector3 *verts = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
512 if(!verts) return;
514 int vnum = get_attrib_count(MESH_ATTR_VERTEX);
515 for(int i=0; i<vnum; i+=3) {
516 Vector3 tmp = verts[i + 2];
517 verts[i + 2] = verts[i + 1];
518 verts[i + 1] = tmp;
519 }
520 }
521 }
523 void Mesh::flip_normals()
524 {
525 Vector3 *normals = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
526 if(!normals) return;
528 int num = get_attrib_count(MESH_ATTR_NORMAL);
529 for(int i=0; i<num; i++) {
530 normals[i] = -normals[i];
531 }
532 }
534 /*
535 int Mesh::add_bone(XFormNode *bone)
536 {
537 int idx = bones.size();
538 bones.push_back(bone);
539 return idx;
540 }
542 const XFormNode *Mesh::get_bone(int idx) const
543 {
544 if(idx < 0 || idx >= (int)bones.size()) {
545 return 0;
546 }
547 return bones[idx];
548 }
550 int Mesh::get_bones_count() const
551 {
552 return (int)bones.size();
553 }
554 */
556 void Mesh::draw() const
557 {
558 #ifdef GL_ES_VERSION_2_0
559 if(!SdrProg::active) {
560 fprintf(stderr, "%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__);
561 return;
562 }
563 #endif
565 ((Mesh*)this)->update_buffers();
567 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
568 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
569 return;
570 }
572 if(SdrProg::active) {
573 // rendering with shaders
574 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
575 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
576 return;
577 }
579 for(int i=0; i<NUM_MESH_ATTR; i++) {
580 int loc = global_sdr_loc[i];
581 if(loc >= 0 && vattr[i].vbo_valid) {
582 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
583 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
584 glEnableVertexAttribArray(loc);
585 }
586 }
587 } else {
588 #ifndef GL_ES_VERSION_2_0
589 // rendering with fixed-function (not available in GLES2)
590 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
591 glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
592 glEnableClientState(GL_VERTEX_ARRAY);
594 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
595 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
596 glNormalPointer(GL_FLOAT, 0, 0);
597 glEnableClientState(GL_NORMAL_ARRAY);
598 }
599 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
600 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
601 glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
602 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
603 }
604 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
605 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
606 glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
607 glEnableClientState(GL_COLOR_ARRAY);
608 }
609 #endif
610 }
611 glBindBuffer(GL_ARRAY_BUFFER, 0);
613 if(ibo_valid) {
614 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
615 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
616 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
617 } else {
618 glDrawArrays(GL_TRIANGLES, 0, nverts);
619 }
621 if(SdrProg::active) {
622 // rendered with shaders
623 for(int i=0; i<NUM_MESH_ATTR; i++) {
624 int loc = global_sdr_loc[i];
625 if(loc >= 0 && vattr[i].vbo_valid) {
626 glDisableVertexAttribArray(loc);
627 }
628 }
629 } else {
630 #ifndef GL_ES_VERSION_2_0
631 // rendered with fixed-function
632 glDisableClientState(GL_VERTEX_ARRAY);
633 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
634 glDisableClientState(GL_NORMAL_ARRAY);
635 }
636 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
637 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
638 }
639 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
640 glDisableClientState(GL_COLOR_ARRAY);
641 }
642 #endif
643 }
644 }
646 void Mesh::draw_wire() const
647 {
648 ((Mesh*)this)->update_wire_ibo();
650 if(!vattr[MESH_ATTR_VERTEX].vbo_valid || !wire_ibo_valid) {
651 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
652 return;
653 }
654 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
655 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
656 return;
657 }
659 for(int i=0; i<NUM_MESH_ATTR; i++) {
660 int loc = global_sdr_loc[i];
661 if(loc >= 0 && vattr[i].vbo_valid) {
662 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
663 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
664 glEnableVertexAttribArray(loc);
665 }
666 }
667 glBindBuffer(GL_ARRAY_BUFFER, 0);
669 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
670 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
671 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
673 for(int i=0; i<NUM_MESH_ATTR; i++) {
674 int loc = global_sdr_loc[i];
675 if(loc >= 0 && vattr[i].vbo_valid) {
676 glDisableVertexAttribArray(loc);
677 }
678 }
679 }
681 void Mesh::draw_vertices() const
682 {
683 ((Mesh*)this)->update_buffers();
685 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
686 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
687 return;
688 }
689 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
690 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
691 return;
692 }
694 for(int i=0; i<NUM_MESH_ATTR; i++) {
695 int loc = global_sdr_loc[i];
696 if(loc >= 0 && vattr[i].vbo_valid) {
697 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
698 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
699 glEnableVertexAttribArray(loc);
700 }
701 }
702 glBindBuffer(GL_ARRAY_BUFFER, 0);
704 glDrawArrays(GL_POINTS, 0, nverts);
706 for(int i=0; i<NUM_MESH_ATTR; i++) {
707 int loc = global_sdr_loc[i];
708 if(loc >= 0 && vattr[i].vbo_valid) {
709 glDisableVertexAttribArray(loc);
710 }
711 }
712 }
714 void Mesh::draw_normals() const
715 {
716 #ifdef USE_OLDGL
717 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
718 Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
719 if(!varr || !norm) {
720 return;
721 }
723 glBegin(GL_LINES);
724 if(get_current_shader()) {
725 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
726 if(vert_loc < 0) {
727 glEnd();
728 return;
729 }
731 for(size_t i=0; i<nverts; i++) {
732 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
733 Vector3 end = varr[i] + norm[i] * vis_vecsize;
734 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
735 }
736 } else {
737 for(size_t i=0; i<nverts; i++) {
738 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
739 Vector3 end = varr[i] + norm[i] * vis_vecsize;
740 glVertex3f(end.x, end.y, end.z);
741 }
742 }
743 glEnd();
744 #endif // USE_OLDGL
745 }
747 void Mesh::draw_tangents() const
748 {
749 #ifdef USE_OLDGL
750 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
751 Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT);
752 if(!varr || !tang) {
753 return;
754 }
756 glBegin(GL_LINES);
757 if(get_current_shader()) {
758 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
759 if(vert_loc < 0) {
760 glEnd();
761 return;
762 }
764 for(size_t i=0; i<nverts; i++) {
765 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
766 Vector3 end = varr[i] + tang[i] * vis_vecsize;
767 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
768 }
769 } else {
770 for(size_t i=0; i<nverts; i++) {
771 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
772 Vector3 end = varr[i] + tang[i] * vis_vecsize;
773 glVertex3f(end.x, end.y, end.z);
774 }
775 }
776 glEnd();
777 #endif // USE_OLDGL
778 }
780 void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const
781 {
782 if(!aabb_valid) {
783 ((Mesh*)this)->calc_aabb();
784 }
785 *vmin = aabb.min;
786 *vmax = aabb.max;
787 }
789 const AABox &Mesh::get_aabbox() const
790 {
791 if(!aabb_valid) {
792 ((Mesh*)this)->calc_aabb();
793 }
794 return aabb;
795 }
797 float Mesh::get_bsphere(Vector3 *center, float *rad) const
798 {
799 if(!bsph_valid) {
800 ((Mesh*)this)->calc_bsph();
801 }
802 *center = bsph.center;
803 *rad = bsph.radius;
804 return bsph.radius;
805 }
807 const Sphere &Mesh::get_bsphere() const
808 {
809 if(!bsph_valid) {
810 ((Mesh*)this)->calc_bsph();
811 }
812 return bsph;
813 }
815 /// static function
816 void Mesh::set_intersect_mode(unsigned int mode)
817 {
818 Mesh::intersect_mode = mode;
819 }
821 /// static function
822 unsigned int Mesh::get_intersect_mode()
823 {
824 return Mesh::intersect_mode;
825 }
827 /// static function
828 void Mesh::set_vertex_select_distance(float dist)
829 {
830 Mesh::vertex_sel_dist = dist;
831 }
833 /// static function
834 float Mesh::get_vertex_select_distance()
835 {
836 return Mesh::vertex_sel_dist;
837 }
839 bool Mesh::intersect(const Ray &ray, HitPoint *hit) const
840 {
841 assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE));
843 const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
844 const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
845 if(!varr) {
846 return false;
847 }
848 const unsigned int *idxarr = get_index_data();
850 // first test with the bounding box
851 AABox box;
852 get_aabbox(&box.min, &box.max);
853 if(!box.intersect(ray)) {
854 return false;
855 }
857 HitPoint nearest_hit;
858 nearest_hit.dist = FLT_MAX;
859 nearest_hit.obj = 0;
861 if(Mesh::intersect_mode & ISECT_VERTICES) {
862 // we asked for "intersections" with the vertices of the mesh
863 long nearest_vidx = -1;
864 float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist;
866 for(unsigned int i=0; i<nverts; i++) {
868 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) {
869 continue;
870 }
872 // project the vertex onto the ray line
873 float t = dot_product(varr[i] - ray.origin, ray.dir);
874 Vector3 vproj = ray.origin + ray.dir * t;
876 float dist_sq = (vproj - varr[i]).length_sq();
877 if(dist_sq < thres_sq) {
878 if(!hit) {
879 return true;
880 }
881 if(t < nearest_hit.dist) {
882 nearest_hit.dist = t;
883 nearest_vidx = i;
884 }
885 }
886 }
888 if(nearest_vidx != -1) {
889 hitvert = varr[nearest_vidx];
890 nearest_hit.obj = &hitvert;
891 }
893 } else {
894 // regular intersection test with polygons
896 for(unsigned int i=0; i<nfaces; i++) {
897 Triangle face(i, varr, idxarr);
899 // ignore back-facing polygons if the mode flags include ISECT_FRONT
900 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) {
901 continue;
902 }
904 HitPoint fhit;
905 if(face.intersect(ray, hit ? &fhit : 0)) {
906 if(!hit) {
907 return true;
908 }
909 if(fhit.dist < nearest_hit.dist) {
910 nearest_hit = fhit;
911 hitface = face;
912 }
913 }
914 }
915 }
917 if(nearest_hit.obj) {
918 if(hit) {
919 *hit = nearest_hit;
921 // if we are interested in the mesh and not the faces set obj to this
922 if(Mesh::intersect_mode & ISECT_FACE) {
923 hit->obj = &hitface;
924 } else if(Mesh::intersect_mode & ISECT_VERTICES) {
925 hit->obj = &hitvert;
926 } else {
927 hit->obj = this;
928 }
929 }
930 return true;
931 }
932 return false;
933 }
936 // ------ private member functions ------
938 void Mesh::calc_aabb()
939 {
940 // the cast is to force calling the const version which doesn't invalidate
941 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
942 return;
943 }
945 aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
946 aabb.max = -aabb.min;
948 for(unsigned int i=0; i<nverts; i++) {
949 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
950 for(int j=0; j<3; j++) {
951 if(v[j] < aabb.min[j]) {
952 aabb.min[j] = v[j];
953 }
954 if(v[j] > aabb.max[j]) {
955 aabb.max[j] = v[j];
956 }
957 }
958 }
959 aabb_valid = true;
960 }
962 void Mesh::calc_bsph()
963 {
964 // the cast is to force calling the const version which doesn't invalidate
965 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
966 return;
967 }
969 Vector3 v;
970 bsph.center = Vector3(0, 0, 0);
972 // first find the center
973 for(unsigned int i=0; i<nverts; i++) {
974 v = get_attrib(MESH_ATTR_VERTEX, i);
975 bsph.center += v;
976 }
977 bsph.center /= (float)nverts;
979 bsph.radius = 0.0f;
980 for(unsigned int i=0; i<nverts; i++) {
981 v = get_attrib(MESH_ATTR_VERTEX, i);
982 float dist_sq = (v - bsph.center).length_sq();
983 if(dist_sq > bsph.radius) {
984 bsph.radius = dist_sq;
985 }
986 }
987 bsph.radius = sqrt(bsph.radius);
989 bsph_valid = true;
990 }
992 void Mesh::update_buffers()
993 {
994 for(int i=0; i<NUM_MESH_ATTR; i++) {
995 if(has_attrib(i) && !vattr[i].vbo_valid) {
996 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
997 glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW);
998 vattr[i].vbo_valid = true;
999 }
1000 }
1001 glBindBuffer(GL_ARRAY_BUFFER, 0);
1003 if(idata_valid && !ibo_valid) {
1004 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
1005 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW);
1006 ibo_valid = true;
1007 }
1008 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1009 }
1011 void Mesh::update_wire_ibo()
1012 {
1013 update_buffers();
1015 if(wire_ibo_valid) {
1016 return;
1017 }
1019 if(!wire_ibo) {
1020 glGenBuffers(1, &wire_ibo);
1021 }
1023 unsigned int *wire_idxarr = new unsigned int[nfaces * 6];
1024 unsigned int *dest = wire_idxarr;
1026 if(ibo_valid) {
1027 // we're dealing with an indexed mesh
1028 const unsigned int *idxarr = ((const Mesh*)this)->get_index_data();
1030 for(unsigned int i=0; i<nfaces; i++) {
1031 *dest++ = idxarr[0];
1032 *dest++ = idxarr[1];
1033 *dest++ = idxarr[1];
1034 *dest++ = idxarr[2];
1035 *dest++ = idxarr[2];
1036 *dest++ = idxarr[0];
1037 idxarr += 3;
1038 }
1039 } else {
1040 // not an indexed mesh ...
1041 for(unsigned int i=0; i<nfaces; i++) {
1042 int vidx = i * 3;
1043 *dest++ = vidx;
1044 *dest++ = vidx + 1;
1045 *dest++ = vidx + 1;
1046 *dest++ = vidx + 2;
1047 *dest++ = vidx + 2;
1048 *dest++ = vidx;
1049 }
1050 }
1052 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
1053 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
1054 delete [] wire_idxarr;
1055 wire_ibo_valid = true;
1056 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1057 }
1060 // ------ class Triangle ------
1061 Triangle::Triangle()
1062 {
1063 normal_valid = false;
1064 id = -1;
1065 }
1067 Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
1068 {
1069 v[0] = v0;
1070 v[1] = v1;
1071 v[2] = v2;
1072 normal_valid = false;
1073 id = -1;
1074 }
1076 Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr)
1077 {
1078 if(idxarr) {
1079 v[0] = varr[idxarr[n * 3]];
1080 v[1] = varr[idxarr[n * 3 + 1]];
1081 v[2] = varr[idxarr[n * 3 + 2]];
1082 } else {
1083 v[0] = varr[n * 3];
1084 v[1] = varr[n * 3 + 1];
1085 v[2] = varr[n * 3 + 2];
1086 }
1087 normal_valid = false;
1088 id = n;
1089 }
1091 void Triangle::calc_normal()
1092 {
1093 normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized();
1094 normal_valid = true;
1095 }
1097 const Vector3 &Triangle::get_normal() const
1098 {
1099 if(!normal_valid) {
1100 ((Triangle*)this)->calc_normal();
1101 }
1102 return normal;
1103 }
1105 void Triangle::transform(const Matrix4x4 &xform)
1106 {
1107 v[0].transform(xform);
1108 v[1].transform(xform);
1109 v[2].transform(xform);
1110 normal_valid = false;
1111 }
1113 void Triangle::draw() const
1114 {
1115 Vector3 n[3];
1116 n[0] = get_normal();
1117 n[1] = get_normal();
1118 n[2] = get_normal();
1120 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
1121 int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL);
1123 glEnableVertexAttribArray(vloc);
1124 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
1125 glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x);
1127 glDrawArrays(GL_TRIANGLES, 0, 3);
1129 glDisableVertexAttribArray(vloc);
1130 glDisableVertexAttribArray(nloc);
1131 CHECK_GLERROR;
1132 }
1134 void Triangle::draw_wire() const
1135 {
1136 static const int idxarr[] = {0, 1, 1, 2, 2, 0};
1137 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
1139 glEnableVertexAttribArray(vloc);
1140 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
1142 glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr);
1144 glDisableVertexAttribArray(vloc);
1145 CHECK_GLERROR;
1146 }
1148 Vector3 Triangle::calc_barycentric(const Vector3 &pos) const
1149 {
1150 Vector3 norm = get_normal();
1152 float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm));
1153 if(area_sq < 1e-5) {
1154 return Vector3(0, 0, 0);
1155 }
1157 float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm));
1158 float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm));
1159 float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm));
1161 return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq);
1162 }
1164 bool Triangle::intersect(const Ray &ray, HitPoint *hit) const
1165 {
1166 Vector3 normal = get_normal();
1168 float ndotdir = dot_product(ray.dir, normal);
1169 if(fabs(ndotdir) < 1e-4) {
1170 return false;
1171 }
1173 Vector3 vertdir = v[0] - ray.origin;
1174 float t = dot_product(normal, vertdir) / ndotdir;
1176 Vector3 pos = ray.origin + ray.dir * t;
1177 Vector3 bary = calc_barycentric(pos);
1179 if(bary.x + bary.y + bary.z > 1.00001) {
1180 return false;
1181 }
1183 if(hit) {
1184 hit->dist = t;
1185 hit->pos = ray.origin + ray.dir * t;
1186 hit->normal = normal;
1187 hit->obj = this;
1188 }
1189 return true;
1190 }