ld33_umonster

diff src/mesh.cc @ 0:4a6683050e29

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