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