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