conworlds

diff src/mesh.cc @ 13:283cdfa7dda2

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