goat3dgfx
diff src/mesh.cc @ 0:1873dfd13f2d
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 14 Nov 2013 05:27:09 +0200 |
parents | |
children | 7d6b667821cf |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.cc Thu Nov 14 05:27:09 2013 +0200 1.3 @@ -0,0 +1,971 @@ 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 + error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib); 1.86 + return 0; 1.87 + } 1.88 + 1.89 + if(nverts && num != nverts) { 1.90 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%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 + error_log("%s: invalid vertex buffer\n", __FUNCTION__); 1.419 + return; 1.420 + } 1.421 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.422 + error_log("%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 + error_log("%s: invalid vertex buffer\n", __FUNCTION__); 1.458 + return; 1.459 + } 1.460 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.461 + error_log("%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 + error_log("%s: invalid vertex buffer\n", __FUNCTION__); 1.493 + return; 1.494 + } 1.495 + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { 1.496 + error_log("%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 +void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const 1.565 +{ 1.566 + if(!aabb_valid) { 1.567 + ((Mesh*)this)->calc_aabb(); 1.568 + } 1.569 + *vmin = aabb.min; 1.570 + *vmax = aabb.max; 1.571 +} 1.572 + 1.573 +const AABox &Mesh::get_aabbox() const 1.574 +{ 1.575 + if(!aabb_valid) { 1.576 + ((Mesh*)this)->calc_aabb(); 1.577 + } 1.578 + return aabb; 1.579 +} 1.580 + 1.581 +float Mesh::get_bsphere(Vector3 *center, float *rad) const 1.582 +{ 1.583 + if(!bsph_valid) { 1.584 + ((Mesh*)this)->calc_bsph(); 1.585 + } 1.586 + *center = bsph.center; 1.587 + *rad = bsph.radius; 1.588 + return bsph.radius; 1.589 +} 1.590 + 1.591 +const Sphere &Mesh::get_bsphere() const 1.592 +{ 1.593 + if(!bsph_valid) { 1.594 + ((Mesh*)this)->calc_bsph(); 1.595 + } 1.596 + return bsph; 1.597 +} 1.598 + 1.599 +/// static function 1.600 +void Mesh::set_intersect_mode(unsigned int mode) 1.601 +{ 1.602 + Mesh::intersect_mode = mode; 1.603 +} 1.604 + 1.605 +/// static function 1.606 +unsigned int Mesh::get_intersect_mode() 1.607 +{ 1.608 + return Mesh::intersect_mode; 1.609 +} 1.610 + 1.611 +/// static function 1.612 +void Mesh::set_vertex_select_distance(float dist) 1.613 +{ 1.614 + Mesh::vertex_sel_dist = dist; 1.615 +} 1.616 + 1.617 +/// static function 1.618 +float Mesh::get_vertex_select_distance() 1.619 +{ 1.620 + return Mesh::vertex_sel_dist; 1.621 +} 1.622 + 1.623 +bool Mesh::intersect(const Ray &ray, HitPoint *hit) const 1.624 +{ 1.625 + assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE)); 1.626 + 1.627 + const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 1.628 + const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); 1.629 + if(!varr) { 1.630 + return false; 1.631 + } 1.632 + const unsigned int *idxarr = get_index_data(); 1.633 + 1.634 + // first test with the bounding box 1.635 + AABox box; 1.636 + get_aabbox(&box.min, &box.max); 1.637 + if(!box.intersect(ray)) { 1.638 + return false; 1.639 + } 1.640 + 1.641 + HitPoint nearest_hit; 1.642 + nearest_hit.dist = FLT_MAX; 1.643 + nearest_hit.obj = 0; 1.644 + 1.645 + if(Mesh::intersect_mode & ISECT_VERTICES) { 1.646 + // we asked for "intersections" with the vertices of the mesh 1.647 + long nearest_vidx = -1; 1.648 + float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist; 1.649 + 1.650 + for(unsigned int i=0; i<nverts; i++) { 1.651 + 1.652 + if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) { 1.653 + continue; 1.654 + } 1.655 + 1.656 + // project the vertex onto the ray line 1.657 + float t = dot_product(varr[i] - ray.origin, ray.dir); 1.658 + Vector3 vproj = ray.origin + ray.dir * t; 1.659 + 1.660 + float dist_sq = (vproj - varr[i]).length_sq(); 1.661 + if(dist_sq < thres_sq) { 1.662 + if(!hit) { 1.663 + return true; 1.664 + } 1.665 + if(t < nearest_hit.dist) { 1.666 + nearest_hit.dist = t; 1.667 + nearest_vidx = i; 1.668 + } 1.669 + } 1.670 + } 1.671 + 1.672 + if(nearest_vidx != -1) { 1.673 + hitvert = varr[nearest_vidx]; 1.674 + nearest_hit.obj = &hitvert; 1.675 + } 1.676 + 1.677 + } else { 1.678 + // regular intersection test with polygons 1.679 + 1.680 + for(unsigned int i=0; i<nfaces; i++) { 1.681 + Triangle face(i, varr, idxarr); 1.682 + 1.683 + // ignore back-facing polygons if the mode flags include ISECT_FRONT 1.684 + if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) { 1.685 + continue; 1.686 + } 1.687 + 1.688 + HitPoint fhit; 1.689 + if(face.intersect(ray, hit ? &fhit : 0)) { 1.690 + if(!hit) { 1.691 + return true; 1.692 + } 1.693 + if(fhit.dist < nearest_hit.dist) { 1.694 + nearest_hit = fhit; 1.695 + hitface = face; 1.696 + } 1.697 + } 1.698 + } 1.699 + } 1.700 + 1.701 + if(nearest_hit.obj) { 1.702 + if(hit) { 1.703 + *hit = nearest_hit; 1.704 + 1.705 + // if we are interested in the mesh and not the faces set obj to this 1.706 + if(Mesh::intersect_mode & ISECT_FACE) { 1.707 + hit->obj = &hitface; 1.708 + } else if(Mesh::intersect_mode & ISECT_VERTICES) { 1.709 + hit->obj = &hitvert; 1.710 + } else { 1.711 + hit->obj = this; 1.712 + } 1.713 + } 1.714 + return true; 1.715 + } 1.716 + return false; 1.717 +} 1.718 + 1.719 + 1.720 +// ------ private member functions ------ 1.721 + 1.722 +void Mesh::calc_aabb() 1.723 +{ 1.724 + // the cast is to force calling the const version which doesn't invalidate 1.725 + if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { 1.726 + return; 1.727 + } 1.728 + 1.729 + aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX); 1.730 + aabb.max = -aabb.min; 1.731 + 1.732 + for(unsigned int i=0; i<nverts; i++) { 1.733 + Vector4 v = get_attrib(MESH_ATTR_VERTEX, i); 1.734 + for(int j=0; j<3; j++) { 1.735 + if(v[j] < aabb.min[j]) { 1.736 + aabb.min[j] = v[j]; 1.737 + } 1.738 + if(v[j] > aabb.max[j]) { 1.739 + aabb.max[j] = v[j]; 1.740 + } 1.741 + } 1.742 + } 1.743 + aabb_valid = true; 1.744 +} 1.745 + 1.746 +void Mesh::calc_bsph() 1.747 +{ 1.748 + // the cast is to force calling the const version which doesn't invalidate 1.749 + if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { 1.750 + return; 1.751 + } 1.752 + 1.753 + Vector3 v; 1.754 + bsph.center = Vector3(0, 0, 0); 1.755 + 1.756 + // first find the center 1.757 + for(unsigned int i=0; i<nverts; i++) { 1.758 + v = get_attrib(MESH_ATTR_VERTEX, i); 1.759 + bsph.center += v; 1.760 + } 1.761 + bsph.center /= (float)nverts; 1.762 + 1.763 + bsph.radius = 0.0f; 1.764 + for(unsigned int i=0; i<nverts; i++) { 1.765 + v = get_attrib(MESH_ATTR_VERTEX, i); 1.766 + float dist_sq = (v - bsph.center).length_sq(); 1.767 + if(dist_sq > bsph.radius) { 1.768 + bsph.radius = dist_sq; 1.769 + } 1.770 + } 1.771 + bsph.radius = sqrt(bsph.radius); 1.772 + 1.773 + bsph_valid = true; 1.774 +} 1.775 + 1.776 +void Mesh::update_buffers() 1.777 +{ 1.778 + for(int i=0; i<NUM_MESH_ATTR; i++) { 1.779 + if(has_attrib(i) && !vattr[i].vbo_valid) { 1.780 + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); 1.781 + glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW); 1.782 + vattr[i].vbo_valid = true; 1.783 + } 1.784 + } 1.785 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.786 + 1.787 + if(idata_valid && !ibo_valid) { 1.788 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); 1.789 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW); 1.790 + ibo_valid = true; 1.791 + } 1.792 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.793 +} 1.794 + 1.795 +void Mesh::update_wire_ibo() 1.796 +{ 1.797 + update_buffers(); 1.798 + 1.799 + if(wire_ibo_valid) { 1.800 + return; 1.801 + } 1.802 + 1.803 + if(!wire_ibo) { 1.804 + glGenBuffers(1, &wire_ibo); 1.805 + } 1.806 + 1.807 + unsigned int *wire_idxarr = new unsigned int[nfaces * 6]; 1.808 + unsigned int *dest = wire_idxarr; 1.809 + 1.810 + if(ibo_valid) { 1.811 + // we're dealing with an indexed mesh 1.812 + const unsigned int *idxarr = ((const Mesh*)this)->get_index_data(); 1.813 + 1.814 + for(unsigned int i=0; i<nfaces; i++) { 1.815 + *dest++ = idxarr[0]; 1.816 + *dest++ = idxarr[1]; 1.817 + *dest++ = idxarr[1]; 1.818 + *dest++ = idxarr[2]; 1.819 + *dest++ = idxarr[2]; 1.820 + *dest++ = idxarr[0]; 1.821 + idxarr += 3; 1.822 + } 1.823 + } else { 1.824 + // not an indexed mesh ... 1.825 + for(unsigned int i=0; i<nfaces; i++) { 1.826 + int vidx = i * 3; 1.827 + *dest++ = vidx; 1.828 + *dest++ = vidx + 1; 1.829 + *dest++ = vidx + 1; 1.830 + *dest++ = vidx + 2; 1.831 + *dest++ = vidx + 2; 1.832 + *dest++ = vidx; 1.833 + } 1.834 + } 1.835 + 1.836 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo); 1.837 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW); 1.838 + delete [] wire_idxarr; 1.839 + wire_ibo_valid = true; 1.840 + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.841 +} 1.842 + 1.843 + 1.844 +// ------ class Triangle ------ 1.845 +Triangle::Triangle() 1.846 +{ 1.847 + normal_valid = false; 1.848 + id = -1; 1.849 +} 1.850 + 1.851 +Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2) 1.852 +{ 1.853 + v[0] = v0; 1.854 + v[1] = v1; 1.855 + v[2] = v2; 1.856 + normal_valid = false; 1.857 + id = -1; 1.858 +} 1.859 + 1.860 +Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr) 1.861 +{ 1.862 + if(idxarr) { 1.863 + v[0] = varr[idxarr[n * 3]]; 1.864 + v[1] = varr[idxarr[n * 3 + 1]]; 1.865 + v[2] = varr[idxarr[n * 3 + 2]]; 1.866 + } else { 1.867 + v[0] = varr[n * 3]; 1.868 + v[1] = varr[n * 3 + 1]; 1.869 + v[2] = varr[n * 3 + 2]; 1.870 + } 1.871 + normal_valid = false; 1.872 + id = n; 1.873 +} 1.874 + 1.875 +void Triangle::calc_normal() 1.876 +{ 1.877 + normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized(); 1.878 + normal_valid = true; 1.879 +} 1.880 + 1.881 +const Vector3 &Triangle::get_normal() const 1.882 +{ 1.883 + if(!normal_valid) { 1.884 + ((Triangle*)this)->calc_normal(); 1.885 + } 1.886 + return normal; 1.887 +} 1.888 + 1.889 +void Triangle::transform(const Matrix4x4 &xform) 1.890 +{ 1.891 + v[0].transform(xform); 1.892 + v[1].transform(xform); 1.893 + v[2].transform(xform); 1.894 + normal_valid = false; 1.895 +} 1.896 + 1.897 +void Triangle::draw() const 1.898 +{ 1.899 + Vector3 n[3]; 1.900 + n[0] = get_normal(); 1.901 + n[1] = get_normal(); 1.902 + n[2] = get_normal(); 1.903 + 1.904 + int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); 1.905 + int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL); 1.906 + 1.907 + glEnableVertexAttribArray(vloc); 1.908 + glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); 1.909 + glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x); 1.910 + 1.911 + glDrawArrays(GL_TRIANGLES, 0, 3); 1.912 + 1.913 + glDisableVertexAttribArray(vloc); 1.914 + glDisableVertexAttribArray(nloc); 1.915 + CHECKGLERR; 1.916 +} 1.917 + 1.918 +void Triangle::draw_wire() const 1.919 +{ 1.920 + static const int idxarr[] = {0, 1, 1, 2, 2, 0}; 1.921 + int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); 1.922 + 1.923 + glEnableVertexAttribArray(vloc); 1.924 + glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); 1.925 + 1.926 + glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr); 1.927 + 1.928 + glDisableVertexAttribArray(vloc); 1.929 + CHECKGLERR; 1.930 +} 1.931 + 1.932 +Vector3 Triangle::calc_barycentric(const Vector3 &pos) const 1.933 +{ 1.934 + Vector3 norm = get_normal(); 1.935 + 1.936 + float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm)); 1.937 + if(area_sq < 1e-5) { 1.938 + return Vector3(0, 0, 0); 1.939 + } 1.940 + 1.941 + float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm)); 1.942 + float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm)); 1.943 + float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm)); 1.944 + 1.945 + return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq); 1.946 +} 1.947 + 1.948 +bool Triangle::intersect(const Ray &ray, HitPoint *hit) const 1.949 +{ 1.950 + Vector3 normal = get_normal(); 1.951 + 1.952 + float ndotdir = dot_product(ray.dir, normal); 1.953 + if(fabs(ndotdir) < 1e-4) { 1.954 + return false; 1.955 + } 1.956 + 1.957 + Vector3 vertdir = v[0] - ray.origin; 1.958 + float t = dot_product(normal, vertdir) / ndotdir; 1.959 + 1.960 + Vector3 pos = ray.origin + ray.dir * t; 1.961 + Vector3 bary = calc_barycentric(pos); 1.962 + 1.963 + if(bary.x + bary.y + bary.z > 1.00001) { 1.964 + return false; 1.965 + } 1.966 + 1.967 + if(hit) { 1.968 + hit->dist = t; 1.969 + hit->pos = ray.origin + ray.dir * t; 1.970 + hit->normal = normal; 1.971 + hit->obj = this; 1.972 + } 1.973 + return true; 1.974 +}