goat3d
diff src/mesh.cc @ 1:e46529a5d057
some progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 17 Aug 2013 23:51:24 +0300 |
parents | |
children | cd71f0b92f44 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.cc Sat Aug 17 23:51:24 2013 +0300 1.3 @@ -0,0 +1,973 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <float.h> 1.7 +#include <assert.h> 1.8 +#include "opengl.h" 1.9 +#include "mesh.h" 1.10 +#include "xform_node.h" 1.11 +//#include "logger.h" 1.12 + 1.13 +int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5 }; 1.14 +unsigned int Mesh::intersect_mode = ISECT_DEFAULT; 1.15 +float Mesh::vertex_sel_dist = 0.01; 1.16 +float Mesh::vis_vecsize = 1.0; 1.17 + 1.18 +Mesh::Mesh() 1.19 +{ 1.20 + clear(); 1.21 + 1.22 + glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects); 1.23 + 1.24 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.25 + vattr[i].vbo = buffer_objects[i]; 1.26 + } 1.27 + ibo = buffer_objects[NUM_MESH_ATTR]; 1.28 + wire_ibo = 0; 1.29 +} 1.30 + 1.31 +Mesh::~Mesh() 1.32 +{ 1.33 + glDeleteBuffers(NUM_MESH_ATTR + 1, buffer_objects); 1.34 + 1.35 + if(wire_ibo) { 1.36 + glDeleteBuffers(1, &wire_ibo); 1.37 + } 1.38 +} 1.39 + 1.40 +void Mesh::set_name(const char *name) 1.41 +{ 1.42 + this->name = name; 1.43 +} 1.44 + 1.45 +const char *Mesh::get_name() const 1.46 +{ 1.47 + return name.c_str(); 1.48 +} 1.49 + 1.50 +bool Mesh::has_attrib(int attr) const 1.51 +{ 1.52 + if(attr < 0 || attr >= NUM_MESH_ATTR) { 1.53 + return false; 1.54 + } 1.55 + 1.56 + // if neither of these is valid, then nobody has set this attribute 1.57 + return vattr[attr].vbo_valid || vattr[attr].data_valid; 1.58 +} 1.59 + 1.60 +void Mesh::clear() 1.61 +{ 1.62 + bones.clear(); 1.63 + 1.64 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.65 + vattr[i].nelem = 0; 1.66 + vattr[i].vbo_valid = false; 1.67 + vattr[i].data_valid = false; 1.68 + //vattr[i].sdr_loc = -1; 1.69 + vattr[i].data.clear(); 1.70 + } 1.71 + ibo_valid = false; 1.72 + idata.clear(); 1.73 + 1.74 + wire_ibo_valid = false; 1.75 + 1.76 + nverts = nfaces = 0; 1.77 + 1.78 + /*bsph_valid = false; 1.79 + aabb_valid = false;*/ 1.80 +} 1.81 + 1.82 +float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data) 1.83 +{ 1.84 + if(attrib < 0 || attrib >= NUM_MESH_ATTR) { 1.85 + fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib); 1.86 + return 0; 1.87 + } 1.88 + 1.89 + if(nverts && num != nverts) { 1.90 + fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts); 1.91 + return 0; 1.92 + } 1.93 + nverts = num; 1.94 + 1.95 + vattr[attrib].data.clear(); 1.96 + vattr[attrib].nelem = nelem; 1.97 + vattr[attrib].data.resize(num * nelem); 1.98 + 1.99 + if(data) { 1.100 + memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data); 1.101 + } 1.102 + 1.103 + vattr[attrib].data_valid = true; 1.104 + vattr[attrib].vbo_valid = false; 1.105 + return &vattr[attrib].data[0]; 1.106 +} 1.107 + 1.108 +float *Mesh::get_attrib_data(int attrib) 1.109 +{ 1.110 + if(attrib < 0 || attrib >= NUM_MESH_ATTR) { 1.111 + fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib); 1.112 + return 0; 1.113 + } 1.114 + 1.115 + vattr[attrib].vbo_valid = false; 1.116 + return (float*)((const Mesh*)this)->get_attrib_data(attrib); 1.117 +} 1.118 + 1.119 +const float *Mesh::get_attrib_data(int attrib) const 1.120 +{ 1.121 + if(attrib < 0 || attrib >= NUM_MESH_ATTR) { 1.122 + fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib); 1.123 + return 0; 1.124 + } 1.125 + 1.126 + if(!vattr[attrib].data_valid) { 1.127 +#if GL_ES_VERSION_2_0 1.128 + fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__); 1.129 + return 0; 1.130 +#else 1.131 + if(!vattr[attrib].vbo_valid) { 1.132 + fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib); 1.133 + return 0; 1.134 + } 1.135 + 1.136 + // local data copy is unavailable, grab the data from the vbo 1.137 + Mesh *m = (Mesh*)this; 1.138 + m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem); 1.139 + 1.140 + glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo); 1.141 + void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); 1.142 + memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float)); 1.143 + glUnmapBuffer(GL_ARRAY_BUFFER); 1.144 + 1.145 + vattr[attrib].data_valid = true; 1.146 +#endif 1.147 + } 1.148 + 1.149 + return &vattr[attrib].data[0]; 1.150 +} 1.151 + 1.152 +void Mesh::set_attrib(int attrib, int idx, const Vector4 &v) 1.153 +{ 1.154 + float *data = get_attrib_data(attrib); 1.155 + if(data) { 1.156 + data += idx * vattr[attrib].nelem; 1.157 + for(int i=0; i<vattr[attrib].nelem; i++) { 1.158 + data[i] = v[i]; 1.159 + } 1.160 + } 1.161 +} 1.162 + 1.163 +Vector4 Mesh::get_attrib(int attrib, int idx) const 1.164 +{ 1.165 + Vector4 v(0.0, 0.0, 0.0, 1.0); 1.166 + const float *data = get_attrib_data(attrib); 1.167 + if(data) { 1.168 + data += idx * vattr[attrib].nelem; 1.169 + for(int i=0; i<vattr[attrib].nelem; i++) { 1.170 + v[i] = data[i]; 1.171 + } 1.172 + } 1.173 + return v; 1.174 +} 1.175 + 1.176 +unsigned int *Mesh::set_index_data(int num, const unsigned int *indices) 1.177 +{ 1.178 + int nidx = nfaces * 3; 1.179 + if(nidx && num != nidx) { 1.180 + fprintf(stderr, "%s: index count missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx); 1.181 + return 0; 1.182 + } 1.183 + nfaces = num / 3; 1.184 + 1.185 + idata.clear(); 1.186 + idata.resize(num); 1.187 + 1.188 + if(indices) { 1.189 + memcpy(&idata[0], indices, num * sizeof *indices); 1.190 + } 1.191 + 1.192 + idata_valid = true; 1.193 + ibo_valid = false; 1.194 + 1.195 + return &idata[0]; 1.196 +} 1.197 + 1.198 +unsigned int *Mesh::get_index_data() 1.199 +{ 1.200 + ibo_valid = false; 1.201 + return (unsigned int*)((const Mesh*)this)->get_index_data(); 1.202 +} 1.203 + 1.204 +const unsigned int *Mesh::get_index_data() const 1.205 +{ 1.206 + if(!idata_valid) { 1.207 +#if GL_ES_VERSION_2_0 1.208 + fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__); 1.209 + return 0; 1.210 +#else 1.211 + if(!ibo_valid) { 1.212 + fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__); 1.213 + return 0; 1.214 + } 1.215 + 1.216 + // local data copy is unavailable, gram the data from the ibo 1.217 + Mesh *m = (Mesh*)this; 1.218 + int nidx = nfaces * 3; 1.219 + m->idata.resize(nidx); 1.220 + 1.221 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); 1.222 + void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY); 1.223 + memcpy(&m->idata[0], data, nidx * sizeof(unsigned int)); 1.224 + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); 1.225 + 1.226 + idata_valid = true; 1.227 +#endif 1.228 + } 1.229 + 1.230 + return &idata[0]; 1.231 +} 1.232 + 1.233 +void Mesh::append(const Mesh &mesh) 1.234 +{ 1.235 + unsigned int idxoffs = nverts; 1.236 + 1.237 + nverts += mesh.nverts; 1.238 + nfaces += mesh.nfaces; 1.239 + 1.240 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.241 + if(has_attrib(i) && mesh.has_attrib(i)) { 1.242 + // force validating the data arrays 1.243 + get_attrib_data(i); 1.244 + mesh.get_attrib_data(i); 1.245 + 1.246 + // append the mesh data 1.247 + vattr[i].data.insert(vattr[i].data.end(), mesh.vattr[i].data.begin(), mesh.vattr[i].data.end()); 1.248 + } 1.249 + } 1.250 + 1.251 + if(ibo_valid || idata_valid) { 1.252 + // make index arrays valid 1.253 + get_index_data(); 1.254 + mesh.get_index_data(); 1.255 + 1.256 + size_t orig_sz = idata.size(); 1.257 + 1.258 + idata.insert(idata.end(), mesh.idata.begin(), mesh.idata.end()); 1.259 + 1.260 + // fixup all the new indices 1.261 + for(size_t i=orig_sz; i<idata.size(); i++) { 1.262 + idata[i] += idxoffs; 1.263 + } 1.264 + } 1.265 + 1.266 + // fuck everything 1.267 + wire_ibo_valid = false; 1.268 + /*aabb_valid = false; 1.269 + bsph_valid = false;*/ 1.270 +} 1.271 + 1.272 +// assemble a complete vertex by adding all the useful attributes 1.273 +void Mesh::vertex(float x, float y, float z) 1.274 +{ 1.275 + cur_val[MESH_ATTR_VERTEX] = Vector4(x, y, z, 1.0f); 1.276 + vattr[MESH_ATTR_VERTEX].data_valid = true; 1.277 + vattr[MESH_ATTR_VERTEX].nelem = 3; 1.278 + 1.279 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.280 + if(vattr[i].data_valid) { 1.281 + for(int j=0; j<vattr[MESH_ATTR_VERTEX].nelem; j++) { 1.282 + vattr[i].data.push_back(cur_val[i][j]); 1.283 + } 1.284 + } 1.285 + vattr[i].vbo_valid = false; 1.286 + } 1.287 + 1.288 + if(idata_valid) { 1.289 + idata.clear(); 1.290 + } 1.291 + ibo_valid = idata_valid = false; 1.292 +} 1.293 + 1.294 +void Mesh::normal(float nx, float ny, float nz) 1.295 +{ 1.296 + cur_val[MESH_ATTR_NORMAL] = Vector4(nx, ny, nz, 1.0f); 1.297 + vattr[MESH_ATTR_NORMAL].data_valid = true; 1.298 + vattr[MESH_ATTR_NORMAL].nelem = 3; 1.299 +} 1.300 + 1.301 +void Mesh::tangent(float tx, float ty, float tz) 1.302 +{ 1.303 + cur_val[MESH_ATTR_TANGENT] = Vector4(tx, ty, tz, 1.0f); 1.304 + vattr[MESH_ATTR_TANGENT].data_valid = true; 1.305 + vattr[MESH_ATTR_TANGENT].nelem = 3; 1.306 +} 1.307 + 1.308 +void Mesh::texcoord(float u, float v, float w) 1.309 +{ 1.310 + cur_val[MESH_ATTR_TEXCOORD] = Vector4(u, v, w, 1.0f); 1.311 + vattr[MESH_ATTR_TEXCOORD].data_valid = true; 1.312 + vattr[MESH_ATTR_TEXCOORD].nelem = 3; 1.313 +} 1.314 + 1.315 +void Mesh::boneweights(float w1, float w2, float w3, float w4) 1.316 +{ 1.317 + cur_val[MESH_ATTR_BONEWEIGHTS] = Vector4(w1, w2, w3, w4); 1.318 + vattr[MESH_ATTR_BONEWEIGHTS].data_valid = true; 1.319 + vattr[MESH_ATTR_BONEWEIGHTS].nelem = 4; 1.320 +} 1.321 + 1.322 +void Mesh::boneidx(int idx1, int idx2, int idx3, int idx4) 1.323 +{ 1.324 + cur_val[MESH_ATTR_BONEIDX] = Vector4(idx1, idx2, idx3, idx4); 1.325 + vattr[MESH_ATTR_BONEIDX].data_valid = true; 1.326 + vattr[MESH_ATTR_BONEIDX].nelem = 4; 1.327 +} 1.328 + 1.329 +/// static function 1.330 +void Mesh::set_attrib_location(int attr, int loc) 1.331 +{ 1.332 + if(attr < 0 || attr >= NUM_MESH_ATTR) { 1.333 + return; 1.334 + } 1.335 + Mesh::global_sdr_loc[attr] = loc; 1.336 +} 1.337 + 1.338 +/// static function 1.339 +int Mesh::get_attrib_location(int attr) 1.340 +{ 1.341 + if(attr < 0 || attr >= NUM_MESH_ATTR) { 1.342 + return -1; 1.343 + } 1.344 + return Mesh::global_sdr_loc[attr]; 1.345 +} 1.346 + 1.347 +/// static function 1.348 +void Mesh::clear_attrib_locations() 1.349 +{ 1.350 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.351 + Mesh::global_sdr_loc[i] = -1; 1.352 + } 1.353 +} 1.354 + 1.355 +/// static function 1.356 +void Mesh::set_vis_vecsize(float sz) 1.357 +{ 1.358 + Mesh::vis_vecsize = sz; 1.359 +} 1.360 + 1.361 +float Mesh::get_vis_vecsize() 1.362 +{ 1.363 + return Mesh::vis_vecsize; 1.364 +} 1.365 + 1.366 +void Mesh::apply_xform(const Matrix4x4 &xform) 1.367 +{ 1.368 + Matrix4x4 dir_xform = xform; 1.369 + dir_xform[0][3] = dir_xform[1][3] = dir_xform[2][3] = 0.0f; 1.370 + dir_xform[3][0] = dir_xform[3][1] = dir_xform[3][2] = 0.0f; 1.371 + dir_xform[3][3] = 1.0f; 1.372 + 1.373 + apply_xform(xform, dir_xform); 1.374 +} 1.375 + 1.376 +void Mesh::apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform) 1.377 +{ 1.378 + for(unsigned int i=0; i<nverts; i++) { 1.379 + Vector4 v = get_attrib(MESH_ATTR_VERTEX, i); 1.380 + set_attrib(MESH_ATTR_VERTEX, i, v.transformed(xform)); 1.381 + 1.382 + if(has_attrib(MESH_ATTR_NORMAL)) { 1.383 + Vector3 n = get_attrib(MESH_ATTR_NORMAL, i); 1.384 + set_attrib(MESH_ATTR_NORMAL, i, n.transformed(dir_xform)); 1.385 + } 1.386 + if(has_attrib(MESH_ATTR_TANGENT)) { 1.387 + Vector3 t = get_attrib(MESH_ATTR_TANGENT, i); 1.388 + set_attrib(MESH_ATTR_TANGENT, i, t.transformed(dir_xform)); 1.389 + } 1.390 + } 1.391 +} 1.392 + 1.393 +int Mesh::add_bone(XFormNode *bone) 1.394 +{ 1.395 + int idx = bones.size(); 1.396 + bones.push_back(bone); 1.397 + return idx; 1.398 +} 1.399 + 1.400 +const XFormNode *Mesh::get_bone(int idx) const 1.401 +{ 1.402 + if(idx < 0 || idx >= (int)bones.size()) { 1.403 + return 0; 1.404 + } 1.405 + return bones[idx]; 1.406 +} 1.407 + 1.408 +int Mesh::get_bones_count() const 1.409 +{ 1.410 + return (int)bones.size(); 1.411 +} 1.412 + 1.413 +void Mesh::draw() const 1.414 +{ 1.415 + ((Mesh*)this)->update_buffers(); 1.416 + 1.417 + if(!vattr[MESH_ATTR_VERTEX].vbo_valid) { 1.418 + fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__); 1.419 + return; 1.420 + } 1.421 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.422 + fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__); 1.423 + return; 1.424 + } 1.425 + 1.426 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.427 + int loc = global_sdr_loc[i]; 1.428 + if(loc >= 0 && vattr[i].vbo_valid) { 1.429 + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); 1.430 + glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); 1.431 + glEnableVertexAttribArray(loc); 1.432 + } 1.433 + } 1.434 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.435 + 1.436 + if(ibo_valid) { 1.437 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); 1.438 + glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0); 1.439 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.440 + } else { 1.441 + glDrawArrays(GL_TRIANGLES, 0, nverts); 1.442 + } 1.443 + 1.444 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.445 + int loc = global_sdr_loc[i]; 1.446 + if(loc >= 0 && vattr[i].vbo_valid) { 1.447 + glDisableVertexAttribArray(loc); 1.448 + } 1.449 + } 1.450 +} 1.451 + 1.452 +void Mesh::draw_wire() const 1.453 +{ 1.454 + ((Mesh*)this)->update_wire_ibo(); 1.455 + 1.456 + if(!vattr[MESH_ATTR_VERTEX].vbo_valid || !wire_ibo_valid) { 1.457 + fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__); 1.458 + return; 1.459 + } 1.460 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.461 + fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__); 1.462 + return; 1.463 + } 1.464 + 1.465 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.466 + int loc = global_sdr_loc[i]; 1.467 + if(loc >= 0 && vattr[i].vbo_valid) { 1.468 + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); 1.469 + glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); 1.470 + glEnableVertexAttribArray(loc); 1.471 + } 1.472 + } 1.473 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.474 + 1.475 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo); 1.476 + glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0); 1.477 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.478 + 1.479 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.480 + int loc = global_sdr_loc[i]; 1.481 + if(loc >= 0 && vattr[i].vbo_valid) { 1.482 + glDisableVertexAttribArray(loc); 1.483 + } 1.484 + } 1.485 +} 1.486 + 1.487 +void Mesh::draw_vertices() const 1.488 +{ 1.489 + ((Mesh*)this)->update_buffers(); 1.490 + 1.491 + if(!vattr[MESH_ATTR_VERTEX].vbo_valid) { 1.492 + fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__); 1.493 + return; 1.494 + } 1.495 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.496 + fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__); 1.497 + return; 1.498 + } 1.499 + 1.500 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.501 + int loc = global_sdr_loc[i]; 1.502 + if(loc >= 0 && vattr[i].vbo_valid) { 1.503 + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); 1.504 + glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); 1.505 + glEnableVertexAttribArray(loc); 1.506 + } 1.507 + } 1.508 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.509 + 1.510 + glDrawArrays(GL_POINTS, 0, nverts); 1.511 + 1.512 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.513 + int loc = global_sdr_loc[i]; 1.514 + if(loc >= 0 && vattr[i].vbo_valid) { 1.515 + glDisableVertexAttribArray(loc); 1.516 + } 1.517 + } 1.518 +} 1.519 + 1.520 +void Mesh::draw_normals() const 1.521 +{ 1.522 +#ifdef USE_OLDGL 1.523 + int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; 1.524 + Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 1.525 + Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); 1.526 + 1.527 + if(!varr || !norm || vert_loc < 0) { 1.528 + return; 1.529 + } 1.530 + 1.531 + glBegin(GL_LINES); 1.532 + for(size_t i=0; i<nverts; i++) { 1.533 + glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z); 1.534 + Vector3 end = varr[i] + norm[i] * vis_vecsize; 1.535 + glVertexAttrib3f(vert_loc, end.x, end.y, end.z); 1.536 + } 1.537 + glEnd(); 1.538 + 1.539 +#endif // USE_OLDGL 1.540 +} 1.541 + 1.542 +void Mesh::draw_tangents() const 1.543 +{ 1.544 +#ifdef USE_OLDGL 1.545 + int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; 1.546 + Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 1.547 + Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT); 1.548 + 1.549 + if(!varr || !tang || vert_loc < 0) { 1.550 + return; 1.551 + } 1.552 + 1.553 + glBegin(GL_LINES); 1.554 + for(size_t i=0; i<nverts; i++) { 1.555 + glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z); 1.556 + Vector3 end = varr[i] + tang[i] * vis_vecsize; 1.557 + glVertexAttrib3f(vert_loc, end.x, end.y, end.z); 1.558 + } 1.559 + glEnd(); 1.560 + 1.561 +#endif // USE_OLDGL 1.562 +} 1.563 + 1.564 +#if 0 1.565 +void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const 1.566 +{ 1.567 + if(!aabb_valid) { 1.568 + ((Mesh*)this)->calc_aabb(); 1.569 + } 1.570 + *vmin = aabb.min; 1.571 + *vmax = aabb.max; 1.572 +} 1.573 + 1.574 +const AABox &Mesh::get_aabbox() const 1.575 +{ 1.576 + if(!aabb_valid) { 1.577 + ((Mesh*)this)->calc_aabb(); 1.578 + } 1.579 + return aabb; 1.580 +} 1.581 + 1.582 +float Mesh::get_bsphere(Vector3 *center, float *rad) const 1.583 +{ 1.584 + if(!bsph_valid) { 1.585 + ((Mesh*)this)->calc_bsph(); 1.586 + } 1.587 + *center = bsph.center; 1.588 + *rad = bsph.radius; 1.589 + return bsph.radius; 1.590 +} 1.591 + 1.592 +const Sphere &Mesh::get_bsphere() const 1.593 +{ 1.594 + if(!bsph_valid) { 1.595 + ((Mesh*)this)->calc_bsph(); 1.596 + } 1.597 + return bsph; 1.598 +} 1.599 + 1.600 +/// static function 1.601 +void Mesh::set_intersect_mode(unsigned int mode) 1.602 +{ 1.603 + Mesh::intersect_mode = mode; 1.604 +} 1.605 + 1.606 +/// static function 1.607 +unsigned int Mesh::get_intersect_mode() 1.608 +{ 1.609 + return Mesh::intersect_mode; 1.610 +} 1.611 + 1.612 +/// static function 1.613 +void Mesh::set_vertex_select_distance(float dist) 1.614 +{ 1.615 + Mesh::vertex_sel_dist = dist; 1.616 +} 1.617 + 1.618 +/// static function 1.619 +float Mesh::get_vertex_select_distance() 1.620 +{ 1.621 + return Mesh::vertex_sel_dist; 1.622 +} 1.623 + 1.624 +/*bool Mesh::intersect(const Ray &ray, HitPoint *hit) const 1.625 +{ 1.626 + assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE)); 1.627 + 1.628 + const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 1.629 + const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); 1.630 + if(!varr) { 1.631 + return false; 1.632 + } 1.633 + const unsigned int *idxarr = get_index_data(); 1.634 + 1.635 + // first test with the bounding box 1.636 + AABox box; 1.637 + get_aabbox(&box.min, &box.max); 1.638 + if(!box.intersect(ray)) { 1.639 + return false; 1.640 + } 1.641 + 1.642 + HitPoint nearest_hit; 1.643 + nearest_hit.dist = FLT_MAX; 1.644 + nearest_hit.obj = 0; 1.645 + 1.646 + if(Mesh::intersect_mode & ISECT_VERTICES) { 1.647 + // we asked for "intersections" with the vertices of the mesh 1.648 + long nearest_vidx = -1; 1.649 + float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist; 1.650 + 1.651 + for(unsigned int i=0; i<nverts; i++) { 1.652 + 1.653 + if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) { 1.654 + continue; 1.655 + } 1.656 + 1.657 + // project the vertex onto the ray line 1.658 + float t = dot_product(varr[i] - ray.origin, ray.dir); 1.659 + Vector3 vproj = ray.origin + ray.dir * t; 1.660 + 1.661 + float dist_sq = (vproj - varr[i]).length_sq(); 1.662 + if(dist_sq < thres_sq) { 1.663 + if(!hit) { 1.664 + return true; 1.665 + } 1.666 + if(t < nearest_hit.dist) { 1.667 + nearest_hit.dist = t; 1.668 + nearest_vidx = i; 1.669 + } 1.670 + } 1.671 + } 1.672 + 1.673 + if(nearest_vidx != -1) { 1.674 + hitvert = varr[nearest_vidx]; 1.675 + nearest_hit.obj = &hitvert; 1.676 + } 1.677 + 1.678 + } else { 1.679 + // regular intersection test with polygons 1.680 + 1.681 + for(unsigned int i=0; i<nfaces; i++) { 1.682 + Triangle face(i, varr, idxarr); 1.683 + 1.684 + // ignore back-facing polygons if the mode flags include ISECT_FRONT 1.685 + if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) { 1.686 + continue; 1.687 + } 1.688 + 1.689 + HitPoint fhit; 1.690 + if(face.intersect(ray, hit ? &fhit : 0)) { 1.691 + if(!hit) { 1.692 + return true; 1.693 + } 1.694 + if(fhit.dist < nearest_hit.dist) { 1.695 + nearest_hit = fhit; 1.696 + hitface = face; 1.697 + } 1.698 + } 1.699 + } 1.700 + } 1.701 + 1.702 + if(nearest_hit.obj) { 1.703 + if(hit) { 1.704 + *hit = nearest_hit; 1.705 + 1.706 + // if we are interested in the mesh and not the faces set obj to this 1.707 + if(Mesh::intersect_mode & ISECT_FACE) { 1.708 + hit->obj = &hitface; 1.709 + } else if(Mesh::intersect_mode & ISECT_VERTICES) { 1.710 + hit->obj = &hitvert; 1.711 + } else { 1.712 + hit->obj = this; 1.713 + } 1.714 + } 1.715 + return true; 1.716 + } 1.717 + return false; 1.718 +}*/ 1.719 + 1.720 + 1.721 +// ------ private member functions ------ 1.722 + 1.723 +void Mesh::calc_aabb() 1.724 +{ 1.725 + // the cast is to force calling the const version which doesn't invalidate 1.726 + if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { 1.727 + return; 1.728 + } 1.729 + 1.730 + aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX); 1.731 + aabb.max = -aabb.min; 1.732 + 1.733 + for(unsigned int i=0; i<nverts; i++) { 1.734 + Vector4 v = get_attrib(MESH_ATTR_VERTEX, i); 1.735 + for(int j=0; j<3; j++) { 1.736 + if(v[j] < aabb.min[j]) { 1.737 + aabb.min[j] = v[j]; 1.738 + } 1.739 + if(v[j] > aabb.max[j]) { 1.740 + aabb.max[j] = v[j]; 1.741 + } 1.742 + } 1.743 + } 1.744 + aabb_valid = true; 1.745 +} 1.746 + 1.747 +void Mesh::calc_bsph() 1.748 +{ 1.749 + // the cast is to force calling the const version which doesn't invalidate 1.750 + if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { 1.751 + return; 1.752 + } 1.753 + 1.754 + Vector3 v; 1.755 + bsph.center = Vector3(0, 0, 0); 1.756 + 1.757 + // first find the center 1.758 + for(unsigned int i=0; i<nverts; i++) { 1.759 + v = get_attrib(MESH_ATTR_VERTEX, i); 1.760 + bsph.center += v; 1.761 + } 1.762 + bsph.center /= (float)nverts; 1.763 + 1.764 + bsph.radius = 0.0f; 1.765 + for(unsigned int i=0; i<nverts; i++) { 1.766 + v = get_attrib(MESH_ATTR_VERTEX, i); 1.767 + float dist_sq = (v - bsph.center).length_sq(); 1.768 + if(dist_sq > bsph.radius) { 1.769 + bsph.radius = dist_sq; 1.770 + } 1.771 + } 1.772 + bsph.radius = sqrt(bsph.radius); 1.773 + 1.774 + bsph_valid = true; 1.775 +} 1.776 +#endif 1.777 + 1.778 +void Mesh::update_buffers() 1.779 +{ 1.780 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.781 + if(has_attrib(i) && !vattr[i].vbo_valid) { 1.782 + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); 1.783 + glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW); 1.784 + vattr[i].vbo_valid = true; 1.785 + } 1.786 + } 1.787 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.788 + 1.789 + if(idata_valid && !ibo_valid) { 1.790 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); 1.791 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW); 1.792 + ibo_valid = true; 1.793 + } 1.794 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.795 +} 1.796 + 1.797 +void Mesh::update_wire_ibo() 1.798 +{ 1.799 + update_buffers(); 1.800 + 1.801 + if(wire_ibo_valid) { 1.802 + return; 1.803 + } 1.804 + 1.805 + if(!wire_ibo) { 1.806 + glGenBuffers(1, &wire_ibo); 1.807 + } 1.808 + 1.809 + unsigned int *wire_idxarr = new unsigned int[nfaces * 6]; 1.810 + unsigned int *dest = wire_idxarr; 1.811 + 1.812 + if(ibo_valid) { 1.813 + // we're dealing with an indexed mesh 1.814 + const unsigned int *idxarr = ((const Mesh*)this)->get_index_data(); 1.815 + 1.816 + for(unsigned int i=0; i<nfaces; i++) { 1.817 + *dest++ = idxarr[0]; 1.818 + *dest++ = idxarr[1]; 1.819 + *dest++ = idxarr[1]; 1.820 + *dest++ = idxarr[2]; 1.821 + *dest++ = idxarr[2]; 1.822 + *dest++ = idxarr[0]; 1.823 + idxarr += 3; 1.824 + } 1.825 + } else { 1.826 + // not an indexed mesh ... 1.827 + for(unsigned int i=0; i<nfaces; i++) { 1.828 + int vidx = i * 3; 1.829 + *dest++ = vidx; 1.830 + *dest++ = vidx + 1; 1.831 + *dest++ = vidx + 1; 1.832 + *dest++ = vidx + 2; 1.833 + *dest++ = vidx + 2; 1.834 + *dest++ = vidx; 1.835 + } 1.836 + } 1.837 + 1.838 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo); 1.839 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW); 1.840 + delete [] wire_idxarr; 1.841 + wire_ibo_valid = true; 1.842 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.843 +} 1.844 + 1.845 + 1.846 +// ------ class Triangle ------ 1.847 +Triangle::Triangle() 1.848 +{ 1.849 + normal_valid = false; 1.850 + id = -1; 1.851 +} 1.852 + 1.853 +Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2) 1.854 +{ 1.855 + v[0] = v0; 1.856 + v[1] = v1; 1.857 + v[2] = v2; 1.858 + normal_valid = false; 1.859 + id = -1; 1.860 +} 1.861 + 1.862 +Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr) 1.863 +{ 1.864 + if(idxarr) { 1.865 + v[0] = varr[idxarr[n * 3]]; 1.866 + v[1] = varr[idxarr[n * 3 + 1]]; 1.867 + v[2] = varr[idxarr[n * 3 + 2]]; 1.868 + } else { 1.869 + v[0] = varr[n * 3]; 1.870 + v[1] = varr[n * 3 + 1]; 1.871 + v[2] = varr[n * 3 + 2]; 1.872 + } 1.873 + normal_valid = false; 1.874 + id = n; 1.875 +} 1.876 + 1.877 +void Triangle::calc_normal() 1.878 +{ 1.879 + normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized(); 1.880 + normal_valid = true; 1.881 +} 1.882 + 1.883 +const Vector3 &Triangle::get_normal() const 1.884 +{ 1.885 + if(!normal_valid) { 1.886 + ((Triangle*)this)->calc_normal(); 1.887 + } 1.888 + return normal; 1.889 +} 1.890 + 1.891 +void Triangle::transform(const Matrix4x4 &xform) 1.892 +{ 1.893 + v[0].transform(xform); 1.894 + v[1].transform(xform); 1.895 + v[2].transform(xform); 1.896 + normal_valid = false; 1.897 +} 1.898 + 1.899 +void Triangle::draw() const 1.900 +{ 1.901 + Vector3 n[3]; 1.902 + n[0] = get_normal(); 1.903 + n[1] = get_normal(); 1.904 + n[2] = get_normal(); 1.905 + 1.906 + int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); 1.907 + int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL); 1.908 + 1.909 + glEnableVertexAttribArray(vloc); 1.910 + glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); 1.911 + glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x); 1.912 + 1.913 + glDrawArrays(GL_TRIANGLES, 0, 3); 1.914 + 1.915 + glDisableVertexAttribArray(vloc); 1.916 + glDisableVertexAttribArray(nloc); 1.917 + CHECKGLERR; 1.918 +} 1.919 + 1.920 +void Triangle::draw_wire() const 1.921 +{ 1.922 + static const int idxarr[] = {0, 1, 1, 2, 2, 0}; 1.923 + int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); 1.924 + 1.925 + glEnableVertexAttribArray(vloc); 1.926 + glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); 1.927 + 1.928 + glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr); 1.929 + 1.930 + glDisableVertexAttribArray(vloc); 1.931 + CHECKGLERR; 1.932 +} 1.933 + 1.934 +Vector3 Triangle::calc_barycentric(const Vector3 &pos) const 1.935 +{ 1.936 + Vector3 norm = get_normal(); 1.937 + 1.938 + float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm)); 1.939 + if(area_sq < 1e-5) { 1.940 + return Vector3(0, 0, 0); 1.941 + } 1.942 + 1.943 + float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm)); 1.944 + float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm)); 1.945 + float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm)); 1.946 + 1.947 + return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq); 1.948 +} 1.949 + 1.950 +/*bool Triangle::intersect(const Ray &ray, HitPoint *hit) const 1.951 +{ 1.952 + Vector3 normal = get_normal(); 1.953 + 1.954 + float ndotdir = dot_product(ray.dir, normal); 1.955 + if(fabs(ndotdir) < 1e-4) { 1.956 + return false; 1.957 + } 1.958 + 1.959 + Vector3 vertdir = v[0] - ray.origin; 1.960 + float t = dot_product(normal, vertdir) / ndotdir; 1.961 + 1.962 + Vector3 pos = ray.origin + ray.dir * t; 1.963 + Vector3 bary = calc_barycentric(pos); 1.964 + 1.965 + if(bary.x + bary.y + bary.z > 1.00001) { 1.966 + return false; 1.967 + } 1.968 + 1.969 + if(hit) { 1.970 + hit->dist = t; 1.971 + hit->pos = ray.origin + ray.dir * t; 1.972 + hit->normal = normal; 1.973 + hit->obj = this; 1.974 + } 1.975 + return true; 1.976 +}*/