goat3d

diff src/mesh.h @ 1:e46529a5d057

some progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 17 Aug 2013 23:51:24 +0300
parents 2918358f5e6d
children cd71f0b92f44
line diff
     1.1 --- a/src/mesh.h	Sat Aug 17 16:10:26 2013 +0300
     1.2 +++ b/src/mesh.h	Sat Aug 17 23:51:24 2013 +0300
     1.3 @@ -1,8 +1,213 @@
     1.4  #ifndef MESH_H_
     1.5  #define MESH_H_
     1.6  
     1.7 -class Mesh {
     1.8 -	// TODO
     1.9 +#include <string>
    1.10 +#include <vector>
    1.11 +#include <vmath/vmath.h>
    1.12 +//#include "geom.h"
    1.13 +
    1.14 +enum {
    1.15 +	MESH_ATTR_VERTEX,
    1.16 +	MESH_ATTR_NORMAL,
    1.17 +	MESH_ATTR_TANGENT,
    1.18 +	MESH_ATTR_TEXCOORD,
    1.19 +	MESH_ATTR_COLOR,
    1.20 +	MESH_ATTR_BONEWEIGHTS,
    1.21 +	MESH_ATTR_BONEIDX,
    1.22 +
    1.23 +	NUM_MESH_ATTR
    1.24  };
    1.25  
    1.26 +// intersection mode flags
    1.27 +enum {
    1.28 +	ISECT_DEFAULT	= 0,	// default (whole mesh, all intersections)
    1.29 +	ISECT_FRONT		= 1,	// front-faces only
    1.30 +	ISECT_FACE		= 2,	// return intersected face pointer instead of mesh
    1.31 +	ISECT_VERTICES	= 4		// return (?) TODO
    1.32 +};
    1.33 +
    1.34 +class XFormNode;
    1.35 +
    1.36 +
    1.37 +class Triangle {
    1.38 +public:
    1.39 +	Vector3 v[3];
    1.40 +	Vector3 normal;
    1.41 +	bool normal_valid;
    1.42 +	int id;
    1.43 +
    1.44 +	Triangle();
    1.45 +	Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2);
    1.46 +	Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0);
    1.47 +
    1.48 +	/// calculate normal (quite expensive)
    1.49 +	void calc_normal();
    1.50 +	const Vector3 &get_normal() const;
    1.51 +
    1.52 +	void transform(const Matrix4x4 &xform);
    1.53 +
    1.54 +	void draw() const;
    1.55 +	void draw_wire() const;
    1.56 +
    1.57 +	/// calculate barycentric coordinates of a point
    1.58 +	Vector3 calc_barycentric(const Vector3 &pos) const;
    1.59 +
    1.60 +	//bool intersect(const Ray &ray, HitPoint *hit = 0) const;
    1.61 +};
    1.62 +
    1.63 +
    1.64 +class Mesh {
    1.65 +private:
    1.66 +	std::string name;
    1.67 +	unsigned int nverts, nfaces;
    1.68 +
    1.69 +	// current value for each attribute for the immedate mode
    1.70 +	// interface.
    1.71 +	Vector4 cur_val[NUM_MESH_ATTR];
    1.72 +
    1.73 +	unsigned int buffer_objects[NUM_MESH_ATTR + 1];
    1.74 +
    1.75 +	// vertex attribute data and buffer objects
    1.76 +	struct {
    1.77 +		int nelem;					// number of elements per attribute range: [1, 4]
    1.78 +		std::vector<float> data;
    1.79 +		unsigned int vbo;
    1.80 +		mutable bool vbo_valid;		// if this is false, the vbo needs updating from the data
    1.81 +		mutable bool data_valid;	// if this is false, the data needs to be pulled from the vbo
    1.82 +		//int sdr_loc;
    1.83 +	} vattr[NUM_MESH_ATTR];
    1.84 +
    1.85 +	static int global_sdr_loc[NUM_MESH_ATTR];
    1.86 +
    1.87 +	std::vector<XFormNode*> bones;	// bones affecting this mesh
    1.88 +
    1.89 +	// index data and buffer object
    1.90 +	std::vector<unsigned int> idata;
    1.91 +	unsigned int ibo;
    1.92 +	mutable bool ibo_valid;
    1.93 +	mutable bool idata_valid;
    1.94 +
    1.95 +	// index buffer object for wireframe rendering (constructed on demand)
    1.96 +	unsigned int wire_ibo;
    1.97 +	mutable bool wire_ibo_valid;
    1.98 +
    1.99 +	// axis-aligned bounding box
   1.100 +	/*mutable AABox aabb;
   1.101 +	mutable bool aabb_valid;
   1.102 +
   1.103 +	// bounding sphere
   1.104 +	mutable Sphere bsph;
   1.105 +	mutable bool bsph_valid;*/
   1.106 +
   1.107 +	// keeps the last intersected face
   1.108 +	mutable Triangle hitface;
   1.109 +	// keeps the last intersected vertex position
   1.110 +	mutable Vector3 hitvert;
   1.111 +
   1.112 +	void calc_aabb();
   1.113 +	void calc_bsph();
   1.114 +
   1.115 +	static unsigned int intersect_mode;
   1.116 +	static float vertex_sel_dist;
   1.117 +
   1.118 +	static float vis_vecsize;
   1.119 +
   1.120 +	/// update the VBOs after data has changed (invalid vbo/ibo)
   1.121 +	void update_buffers();
   1.122 +	/// construct/update the wireframe index buffer (called from draw_wire).
   1.123 +	void update_wire_ibo();
   1.124 +
   1.125 +
   1.126 +public:
   1.127 +	Mesh();
   1.128 +	~Mesh();
   1.129 +
   1.130 +	void set_name(const char *name);
   1.131 +	const char *get_name() const;
   1.132 +
   1.133 +	bool has_attrib(int attr) const;
   1.134 +
   1.135 +	// clears everything about this mesh, and returns to the newly constructed state
   1.136 +	void clear();
   1.137 +
   1.138 +	// access the vertex attribute data
   1.139 +	// if vdata == 0, space is just allocated
   1.140 +	float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
   1.141 +	float *get_attrib_data(int attrib);	// invalidates vbo
   1.142 +	const float *get_attrib_data(int attrib) const;
   1.143 +
   1.144 +	// simple access to any particular attribute
   1.145 +	void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
   1.146 +	Vector4 get_attrib(int attrib, int idx) const;
   1.147 +
   1.148 +	// ... same for index data
   1.149 +	unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
   1.150 +	unsigned int *get_index_data();	// invalidates ibo
   1.151 +	const unsigned int *get_index_data() const;
   1.152 +
   1.153 +	void append(const Mesh &mesh);
   1.154 +
   1.155 +	// immediate-mode style mesh construction interface
   1.156 +	void vertex(float x, float y, float z);
   1.157 +	void normal(float nx, float ny, float nz);
   1.158 +	void tangent(float tx, float ty, float tz);
   1.159 +	void texcoord(float u, float v, float w);
   1.160 +	void boneweights(float w1, float w2, float w3, float w4);
   1.161 +	void boneidx(int idx1, int idx2, int idx3, int idx4);
   1.162 +
   1.163 +	/* apply a transformation to the vertices and its inverse-transpose
   1.164 +	 * to the normals and tangents.
   1.165 +	 */
   1.166 +	void apply_xform(const Matrix4x4 &xform);
   1.167 +	void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
   1.168 +
   1.169 +	// adds a bone and returns its index
   1.170 +	int add_bone(XFormNode *bone);
   1.171 +	const XFormNode *get_bone(int idx) const;
   1.172 +	int get_bones_count() const;
   1.173 +
   1.174 +	// access the shader attribute locations
   1.175 +	static void set_attrib_location(int attr, int loc);
   1.176 +	static int get_attrib_location(int attr);
   1.177 +	static void clear_attrib_locations();
   1.178 +
   1.179 +	static void set_vis_vecsize(float sz);
   1.180 +	static float get_vis_vecsize();
   1.181 +
   1.182 +	void draw() const;
   1.183 +	void draw_wire() const;
   1.184 +	void draw_vertices() const;
   1.185 +	void draw_normals() const;
   1.186 +	void draw_tangents() const;
   1.187 +
   1.188 +#if 0
   1.189 +	/** get the bounding box in local space. The result will be cached, and subsequent
   1.190 +	 * calls will return the same box. The cache gets invalidated by any functions that can affect
   1.191 +	 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
   1.192 +	 * @{ */
   1.193 +	void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
   1.194 +	const AABox &get_aabbox() const;
   1.195 +	/// @}
   1.196 +
   1.197 +	/** get the bounding sphere in local space. The result will be cached, and subsequent
   1.198 +	 * calls will return the same box. The cache gets invalidated by any functions that can affect
   1.199 +	 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
   1.200 +	 * @{ */
   1.201 +	float get_bsphere(Vector3 *center, float *rad) const;
   1.202 +	const Sphere &get_bsphere() const;
   1.203 +
   1.204 +	static void set_intersect_mode(unsigned int mode);
   1.205 +	static unsigned int get_intersect_mode();
   1.206 +	static void set_vertex_select_distance(float dist);
   1.207 +	static float get_vertex_select_distance();
   1.208 +
   1.209 +	/** Find the intersection between the mesh and a ray.
   1.210 +	 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
   1.211 +	 *     If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
   1.212 +	 */
   1.213 +	bool intersect(const Ray &ray, HitPoint *hit = 0) const;
   1.214 +#endif
   1.215 +};
   1.216 +
   1.217 +
   1.218  #endif	// MESH_H_