nuclear@13: #include nuclear@13: #include nuclear@13: #include nuclear@13: #include nuclear@13: #include "opengl.h" nuclear@13: #include "mesh.h" nuclear@13: #include "xform_node.h" nuclear@13: #include "shader.h" nuclear@13: #include "logger.h" nuclear@13: nuclear@13: int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5 }; nuclear@13: unsigned int Mesh::intersect_mode = ISECT_DEFAULT; nuclear@13: float Mesh::vertex_sel_dist = 0.01; nuclear@13: float Mesh::vis_vecsize = 1.0; nuclear@13: nuclear@13: Mesh::Mesh() nuclear@13: { nuclear@13: clear(); nuclear@13: nuclear@13: glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects); nuclear@13: nuclear@13: for(int i=0; iname = name; nuclear@13: } nuclear@13: nuclear@13: const char *Mesh::get_name() const nuclear@13: { nuclear@13: return name.c_str(); nuclear@13: } nuclear@13: nuclear@13: bool Mesh::has_attrib(int attr) const nuclear@13: { nuclear@13: if(attr < 0 || attr >= NUM_MESH_ATTR) { nuclear@13: return false; nuclear@13: } nuclear@13: nuclear@13: // if neither of these is valid, then nobody has set this attribute nuclear@13: return vattr[attr].vbo_valid || vattr[attr].data_valid; nuclear@13: } nuclear@13: nuclear@13: void Mesh::clear() nuclear@13: { nuclear@13: bones.clear(); nuclear@13: nuclear@13: for(int i=0; i= NUM_MESH_ATTR) { nuclear@13: error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib); nuclear@13: return 0; nuclear@13: } nuclear@13: nuclear@13: if(nverts && num != nverts) { nuclear@13: error_log("%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts); nuclear@13: return 0; nuclear@13: } nuclear@13: nverts = num; nuclear@13: nuclear@13: vattr[attrib].data.clear(); nuclear@13: vattr[attrib].nelem = nelem; nuclear@13: vattr[attrib].data.resize(num * nelem); nuclear@13: nuclear@13: if(data) { nuclear@13: memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data); nuclear@13: } nuclear@13: nuclear@13: vattr[attrib].data_valid = true; nuclear@13: vattr[attrib].vbo_valid = false; nuclear@13: return &vattr[attrib].data[0]; nuclear@13: } nuclear@13: nuclear@13: float *Mesh::get_attrib_data(int attrib) nuclear@13: { nuclear@13: if(attrib < 0 || attrib >= NUM_MESH_ATTR) { nuclear@13: error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib); nuclear@13: return 0; nuclear@13: } nuclear@13: nuclear@13: vattr[attrib].vbo_valid = false; nuclear@13: return (float*)((const Mesh*)this)->get_attrib_data(attrib); nuclear@13: } nuclear@13: nuclear@13: const float *Mesh::get_attrib_data(int attrib) const nuclear@13: { nuclear@13: if(attrib < 0 || attrib >= NUM_MESH_ATTR) { nuclear@13: error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib); nuclear@13: return 0; nuclear@13: } nuclear@13: nuclear@13: if(!vattr[attrib].data_valid) { nuclear@13: #if GL_ES_VERSION_2_0 nuclear@13: error_log("%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__); nuclear@13: return 0; nuclear@13: #else nuclear@13: if(!vattr[attrib].vbo_valid) { nuclear@13: error_log("%s: unavailable attrib: %d\n", __FUNCTION__, attrib); nuclear@13: return 0; nuclear@13: } nuclear@13: nuclear@13: // local data copy is unavailable, grab the data from the vbo nuclear@13: Mesh *m = (Mesh*)this; nuclear@13: m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem); nuclear@13: nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo); nuclear@13: void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); nuclear@13: memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float)); nuclear@13: glUnmapBuffer(GL_ARRAY_BUFFER); nuclear@13: nuclear@13: vattr[attrib].data_valid = true; nuclear@13: #endif nuclear@13: } nuclear@13: nuclear@13: return &vattr[attrib].data[0]; nuclear@13: } nuclear@13: nuclear@13: void Mesh::set_attrib(int attrib, int idx, const Vector4 &v) nuclear@13: { nuclear@13: float *data = get_attrib_data(attrib); nuclear@13: if(data) { nuclear@13: data += idx * vattr[attrib].nelem; nuclear@13: for(int i=0; iget_index_data(); nuclear@13: } nuclear@13: nuclear@13: const unsigned int *Mesh::get_index_data() const nuclear@13: { nuclear@13: if(!idata_valid) { nuclear@13: #if GL_ES_VERSION_2_0 nuclear@13: error_log("%s: can't read back index data in CrippledGL ES\n", __FUNCTION__); nuclear@13: return 0; nuclear@13: #else nuclear@13: if(!ibo_valid) { nuclear@13: error_log("%s: indices unavailable\n", __FUNCTION__); nuclear@13: return 0; nuclear@13: } nuclear@13: nuclear@13: // local data copy is unavailable, gram the data from the ibo nuclear@13: Mesh *m = (Mesh*)this; nuclear@13: int nidx = nfaces * 3; nuclear@13: m->idata.resize(nidx); nuclear@13: nuclear@13: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); nuclear@13: void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY); nuclear@13: memcpy(&m->idata[0], data, nidx * sizeof(unsigned int)); nuclear@13: glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); nuclear@13: nuclear@13: idata_valid = true; nuclear@13: #endif nuclear@13: } nuclear@13: nuclear@13: return &idata[0]; nuclear@13: } nuclear@13: nuclear@13: void Mesh::append(const Mesh &mesh) nuclear@13: { nuclear@13: unsigned int idxoffs = nverts; nuclear@13: nuclear@13: nverts += mesh.nverts; nuclear@13: nfaces += mesh.nfaces; nuclear@13: nuclear@13: for(int i=0; i= NUM_MESH_ATTR) { nuclear@13: return; nuclear@13: } nuclear@13: Mesh::global_sdr_loc[attr] = loc; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: int Mesh::get_attrib_location(int attr) nuclear@13: { nuclear@13: if(attr < 0 || attr >= NUM_MESH_ATTR) { nuclear@13: return -1; nuclear@13: } nuclear@13: return Mesh::global_sdr_loc[attr]; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: void Mesh::clear_attrib_locations() nuclear@13: { nuclear@13: for(int i=0; i= (int)bones.size()) { nuclear@13: return 0; nuclear@13: } nuclear@13: return bones[idx]; nuclear@13: } nuclear@13: nuclear@13: int Mesh::get_bones_count() const nuclear@13: { nuclear@13: return (int)bones.size(); nuclear@13: } nuclear@13: nuclear@13: void Mesh::draw() const nuclear@13: { nuclear@13: const ShaderProg *cur_sdr = get_current_shader(); nuclear@13: #ifdef GL_ES_VERSION_2_0 nuclear@13: if(!cur_sdr) { nuclear@13: error_log("%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: #endif nuclear@13: nuclear@13: ((Mesh*)this)->update_buffers(); nuclear@13: nuclear@13: if(!vattr[MESH_ATTR_VERTEX].vbo_valid) { nuclear@13: error_log("%s: invalid vertex buffer\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: if(cur_sdr) { nuclear@13: // rendering with shaders nuclear@13: if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { nuclear@13: error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); nuclear@13: glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); nuclear@13: glEnableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: } else { nuclear@13: #ifndef GL_ES_VERSION_2_0 nuclear@13: // rendering with fixed-function (not available in GLES2) nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo); nuclear@13: glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0); nuclear@13: glEnableClientState(GL_VERTEX_ARRAY); nuclear@13: nuclear@13: if(vattr[MESH_ATTR_NORMAL].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo); nuclear@13: glNormalPointer(GL_FLOAT, 0, 0); nuclear@13: glEnableClientState(GL_NORMAL_ARRAY); nuclear@13: } nuclear@13: if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo); nuclear@13: glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0); nuclear@13: glEnableClientState(GL_TEXTURE_COORD_ARRAY); nuclear@13: } nuclear@13: if(vattr[MESH_ATTR_COLOR].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo); nuclear@13: glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0); nuclear@13: glEnableClientState(GL_COLOR_ARRAY); nuclear@13: } nuclear@13: #endif nuclear@13: } nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, 0); nuclear@13: nuclear@13: if(ibo_valid) { nuclear@13: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); nuclear@13: glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0); nuclear@13: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); nuclear@13: } else { nuclear@13: glDrawArrays(GL_TRIANGLES, 0, nverts); nuclear@13: } nuclear@13: nuclear@13: if(cur_sdr) { nuclear@13: // rendered with shaders nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glDisableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: } else { nuclear@13: #ifndef GL_ES_VERSION_2_0 nuclear@13: // rendered with fixed-function nuclear@13: glDisableClientState(GL_VERTEX_ARRAY); nuclear@13: if(vattr[MESH_ATTR_NORMAL].vbo_valid) { nuclear@13: glDisableClientState(GL_NORMAL_ARRAY); nuclear@13: } nuclear@13: if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) { nuclear@13: glDisableClientState(GL_TEXTURE_COORD_ARRAY); nuclear@13: } nuclear@13: if(vattr[MESH_ATTR_COLOR].vbo_valid) { nuclear@13: glDisableClientState(GL_COLOR_ARRAY); nuclear@13: } nuclear@13: #endif nuclear@13: } nuclear@13: } nuclear@13: nuclear@13: void Mesh::draw_wire() const nuclear@13: { nuclear@13: ((Mesh*)this)->update_wire_ibo(); nuclear@13: nuclear@13: if(!vattr[MESH_ATTR_VERTEX].vbo_valid || !wire_ibo_valid) { nuclear@13: error_log("%s: invalid vertex buffer\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { nuclear@13: error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); nuclear@13: glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); nuclear@13: glEnableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, 0); nuclear@13: nuclear@13: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo); nuclear@13: glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0); nuclear@13: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); nuclear@13: nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glDisableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: nuclear@13: void Mesh::draw_vertices() const nuclear@13: { nuclear@13: ((Mesh*)this)->update_buffers(); nuclear@13: nuclear@13: if(!vattr[MESH_ATTR_VERTEX].vbo_valid) { nuclear@13: error_log("%s: invalid vertex buffer\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { nuclear@13: error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__); nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); nuclear@13: glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); nuclear@13: glEnableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: glBindBuffer(GL_ARRAY_BUFFER, 0); nuclear@13: nuclear@13: glDrawArrays(GL_POINTS, 0, nverts); nuclear@13: nuclear@13: for(int i=0; i= 0 && vattr[i].vbo_valid) { nuclear@13: glDisableVertexAttribArray(loc); nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: nuclear@13: void Mesh::draw_normals() const nuclear@13: { nuclear@13: #ifdef USE_OLDGL nuclear@13: int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; nuclear@13: Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); nuclear@13: Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); nuclear@13: nuclear@13: if(!varr || !norm || vert_loc < 0) { nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: glBegin(GL_LINES); nuclear@13: for(size_t i=0; icalc_aabb(); nuclear@13: } nuclear@13: *vmin = aabb.min; nuclear@13: *vmax = aabb.max; nuclear@13: } nuclear@13: nuclear@13: const AABox &Mesh::get_aabbox() const nuclear@13: { nuclear@13: if(!aabb_valid) { nuclear@13: ((Mesh*)this)->calc_aabb(); nuclear@13: } nuclear@13: return aabb; nuclear@13: } nuclear@13: nuclear@13: float Mesh::get_bsphere(Vector3 *center, float *rad) const nuclear@13: { nuclear@13: if(!bsph_valid) { nuclear@13: ((Mesh*)this)->calc_bsph(); nuclear@13: } nuclear@13: *center = bsph.center; nuclear@13: *rad = bsph.radius; nuclear@13: return bsph.radius; nuclear@13: } nuclear@13: nuclear@13: const Sphere &Mesh::get_bsphere() const nuclear@13: { nuclear@13: if(!bsph_valid) { nuclear@13: ((Mesh*)this)->calc_bsph(); nuclear@13: } nuclear@13: return bsph; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: void Mesh::set_intersect_mode(unsigned int mode) nuclear@13: { nuclear@13: Mesh::intersect_mode = mode; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: unsigned int Mesh::get_intersect_mode() nuclear@13: { nuclear@13: return Mesh::intersect_mode; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: void Mesh::set_vertex_select_distance(float dist) nuclear@13: { nuclear@13: Mesh::vertex_sel_dist = dist; nuclear@13: } nuclear@13: nuclear@13: /// static function nuclear@13: float Mesh::get_vertex_select_distance() nuclear@13: { nuclear@13: return Mesh::vertex_sel_dist; nuclear@13: } nuclear@13: nuclear@13: bool Mesh::intersect(const Ray &ray, HitPoint *hit) const nuclear@13: { nuclear@13: assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE)); nuclear@13: nuclear@13: const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); nuclear@13: const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); nuclear@13: if(!varr) { nuclear@13: return false; nuclear@13: } nuclear@13: const unsigned int *idxarr = get_index_data(); nuclear@13: nuclear@13: // first test with the bounding box nuclear@13: AABox box; nuclear@13: get_aabbox(&box.min, &box.max); nuclear@13: if(!box.intersect(ray)) { nuclear@13: return false; nuclear@13: } nuclear@13: nuclear@13: HitPoint nearest_hit; nuclear@13: nearest_hit.dist = FLT_MAX; nuclear@13: nearest_hit.obj = 0; nuclear@13: nuclear@13: if(Mesh::intersect_mode & ISECT_VERTICES) { nuclear@13: // we asked for "intersections" with the vertices of the mesh nuclear@13: long nearest_vidx = -1; nuclear@13: float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist; nuclear@13: nuclear@13: for(unsigned int i=0; i 0) { nuclear@13: continue; nuclear@13: } nuclear@13: nuclear@13: // project the vertex onto the ray line nuclear@13: float t = dot_product(varr[i] - ray.origin, ray.dir); nuclear@13: Vector3 vproj = ray.origin + ray.dir * t; nuclear@13: nuclear@13: float dist_sq = (vproj - varr[i]).length_sq(); nuclear@13: if(dist_sq < thres_sq) { nuclear@13: if(!hit) { nuclear@13: return true; nuclear@13: } nuclear@13: if(t < nearest_hit.dist) { nuclear@13: nearest_hit.dist = t; nuclear@13: nearest_vidx = i; nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: nuclear@13: if(nearest_vidx != -1) { nuclear@13: hitvert = varr[nearest_vidx]; nuclear@13: nearest_hit.obj = &hitvert; nuclear@13: } nuclear@13: nuclear@13: } else { nuclear@13: // regular intersection test with polygons nuclear@13: nuclear@13: for(unsigned int i=0; i 0) { nuclear@13: continue; nuclear@13: } nuclear@13: nuclear@13: HitPoint fhit; nuclear@13: if(face.intersect(ray, hit ? &fhit : 0)) { nuclear@13: if(!hit) { nuclear@13: return true; nuclear@13: } nuclear@13: if(fhit.dist < nearest_hit.dist) { nuclear@13: nearest_hit = fhit; nuclear@13: hitface = face; nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: nuclear@13: if(nearest_hit.obj) { nuclear@13: if(hit) { nuclear@13: *hit = nearest_hit; nuclear@13: nuclear@13: // if we are interested in the mesh and not the faces set obj to this nuclear@13: if(Mesh::intersect_mode & ISECT_FACE) { nuclear@13: hit->obj = &hitface; nuclear@13: } else if(Mesh::intersect_mode & ISECT_VERTICES) { nuclear@13: hit->obj = &hitvert; nuclear@13: } else { nuclear@13: hit->obj = this; nuclear@13: } nuclear@13: } nuclear@13: return true; nuclear@13: } nuclear@13: return false; nuclear@13: } nuclear@13: nuclear@13: nuclear@13: // ------ private member functions ------ nuclear@13: nuclear@13: void Mesh::calc_aabb() nuclear@13: { nuclear@13: // the cast is to force calling the const version which doesn't invalidate nuclear@13: if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX); nuclear@13: aabb.max = -aabb.min; nuclear@13: nuclear@13: for(unsigned int i=0; i aabb.max[j]) { nuclear@13: aabb.max[j] = v[j]; nuclear@13: } nuclear@13: } nuclear@13: } nuclear@13: aabb_valid = true; nuclear@13: } nuclear@13: nuclear@13: void Mesh::calc_bsph() nuclear@13: { nuclear@13: // the cast is to force calling the const version which doesn't invalidate nuclear@13: if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) { nuclear@13: return; nuclear@13: } nuclear@13: nuclear@13: Vector3 v; nuclear@13: bsph.center = Vector3(0, 0, 0); nuclear@13: nuclear@13: // first find the center nuclear@13: for(unsigned int i=0; i bsph.radius) { nuclear@13: bsph.radius = dist_sq; nuclear@13: } nuclear@13: } nuclear@13: bsph.radius = sqrt(bsph.radius); nuclear@13: nuclear@13: bsph_valid = true; nuclear@13: } nuclear@13: nuclear@13: void Mesh::update_buffers() nuclear@13: { nuclear@13: for(int i=0; iget_index_data(); nuclear@13: nuclear@13: for(unsigned int i=0; icalc_normal(); nuclear@13: } nuclear@13: return normal; nuclear@13: } nuclear@13: nuclear@13: void Triangle::transform(const Matrix4x4 &xform) nuclear@13: { nuclear@13: v[0].transform(xform); nuclear@13: v[1].transform(xform); nuclear@13: v[2].transform(xform); nuclear@13: normal_valid = false; nuclear@13: } nuclear@13: nuclear@13: void Triangle::draw() const nuclear@13: { nuclear@13: Vector3 n[3]; nuclear@13: n[0] = get_normal(); nuclear@13: n[1] = get_normal(); nuclear@13: n[2] = get_normal(); nuclear@13: nuclear@13: int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); nuclear@13: int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL); nuclear@13: nuclear@13: glEnableVertexAttribArray(vloc); nuclear@13: glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); nuclear@13: glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x); nuclear@13: nuclear@13: glDrawArrays(GL_TRIANGLES, 0, 3); nuclear@13: nuclear@13: glDisableVertexAttribArray(vloc); nuclear@13: glDisableVertexAttribArray(nloc); nuclear@13: CHECKGLERR; nuclear@13: } nuclear@13: nuclear@13: void Triangle::draw_wire() const nuclear@13: { nuclear@13: static const int idxarr[] = {0, 1, 1, 2, 2, 0}; nuclear@13: int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX); nuclear@13: nuclear@13: glEnableVertexAttribArray(vloc); nuclear@13: glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x); nuclear@13: nuclear@13: glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr); nuclear@13: nuclear@13: glDisableVertexAttribArray(vloc); nuclear@13: CHECKGLERR; nuclear@13: } nuclear@13: nuclear@13: Vector3 Triangle::calc_barycentric(const Vector3 &pos) const nuclear@13: { nuclear@13: Vector3 norm = get_normal(); nuclear@13: nuclear@13: float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm)); nuclear@13: if(area_sq < 1e-5) { nuclear@13: return Vector3(0, 0, 0); nuclear@13: } nuclear@13: nuclear@13: float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm)); nuclear@13: float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm)); nuclear@13: float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm)); nuclear@13: nuclear@13: return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq); nuclear@13: } nuclear@13: nuclear@13: bool Triangle::intersect(const Ray &ray, HitPoint *hit) const nuclear@13: { nuclear@13: Vector3 normal = get_normal(); nuclear@13: nuclear@13: float ndotdir = dot_product(ray.dir, normal); nuclear@13: if(fabs(ndotdir) < 1e-4) { nuclear@13: return false; nuclear@13: } nuclear@13: nuclear@13: Vector3 vertdir = v[0] - ray.origin; nuclear@13: float t = dot_product(normal, vertdir) / ndotdir; nuclear@13: nuclear@13: Vector3 pos = ray.origin + ray.dir * t; nuclear@13: Vector3 bary = calc_barycentric(pos); nuclear@13: nuclear@13: if(bary.x + bary.y + bary.z > 1.00001) { nuclear@13: return false; nuclear@13: } nuclear@13: nuclear@13: if(hit) { nuclear@13: hit->dist = t; nuclear@13: hit->pos = ray.origin + ray.dir * t; nuclear@13: hit->normal = normal; nuclear@13: hit->obj = this; nuclear@13: } nuclear@13: return true; nuclear@13: }