goat3d

diff src/mesh.h @ 8:cd71f0b92f44

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