7 #include "gmath/gmath.h"
16 MESH_ATTR_BONEWEIGHTS,
23 // intersection mode flags
25 ISECT_DEFAULT = 0, // default (whole mesh, all intersections)
26 ISECT_FRONT = 1, // front-faces only
27 ISECT_FACE = 2, // return intersected face pointer instead of mesh
28 ISECT_VERTICES = 4 // return (?) TODO
42 Triangle(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2);
43 Triangle(int n, const Vec3 *varr, const unsigned int *idxarr = 0);
45 /// calculate normal (quite expensive)
47 const Vec3 &get_normal() const;
49 void transform(const Mat4 &xform);
52 void draw_wire() const;
54 /// calculate barycentric coordinates of a point
55 Vec3 calc_barycentric(const Vec3 &pos) const;
57 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
64 unsigned int nverts, nfaces;
66 // current value for each attribute for the immedate mode
68 Vec4 cur_val[NUM_MESH_ATTR];
70 unsigned int buffer_objects[NUM_MESH_ATTR + 1];
72 // vertex attribute data and buffer objects
74 int nelem; // number of elements per attribute range: [1, 4]
75 std::vector<float> data;
77 mutable bool vbo_valid; // if this is false, the vbo needs updating from the data
78 mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo
80 } vattr[NUM_MESH_ATTR];
82 static int global_sdr_loc[NUM_MESH_ATTR];
84 //std::vector<XFormNode*> bones; // bones affecting this mesh
86 // index data and buffer object
87 std::vector<unsigned int> idata;
89 mutable bool ibo_valid;
90 mutable bool idata_valid;
92 // index buffer object for wireframe rendering (constructed on demand)
93 unsigned int wire_ibo;
94 mutable bool wire_ibo_valid;
96 // axis-aligned bounding box
98 mutable bool aabb_valid;
102 mutable bool bsph_valid;
104 // keeps the last intersected face
105 mutable Triangle hitface;
106 // keeps the last intersected vertex position
107 mutable Vec3 hitvert;
112 static unsigned int intersect_mode;
113 static float vertex_sel_dist;
115 static float vis_vecsize;
117 /// update the VBOs after data has changed (invalid vbo/ibo)
118 void update_buffers();
119 /// construct/update the wireframe index buffer (called from draw_wire).
120 void update_wire_ibo();
123 bool pre_draw() const;
124 void post_draw() const;
128 static bool use_custom_sdr_attr;
133 Mesh(const Mesh &rhs);
134 Mesh &operator =(const Mesh &rhs);
135 bool clone(const Mesh &m);
137 void set_name(const char *name);
138 const char *get_name() const;
140 bool has_attrib(int attr) const;
141 bool is_indexed() const;
143 // clears everything about this mesh, and returns to the newly constructed state
146 // access the vertex attribute data
147 // if vdata == 0, space is just allocated
148 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
149 float *get_attrib_data(int attrib); // invalidates vbo
150 const float *get_attrib_data(int attrib) const;
152 // simple access to any particular attribute
153 void set_attrib(int attrib, int idx, const Vec4 &v); // invalidates vbo
154 Vec4 get_attrib(int attrib, int idx) const;
156 int get_attrib_count(int attrib) const;
158 // ... same for index data
159 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
160 unsigned int *get_index_data(); // invalidates ibo
161 const unsigned int *get_index_data() const;
163 int get_index_count() const;
165 void append(const Mesh &mesh);
167 // immediate-mode style mesh construction interface
168 void vertex(float x, float y, float z);
169 void normal(float nx, float ny, float nz);
170 void tangent(float tx, float ty, float tz);
171 void texcoord(float u, float v, float w);
172 void boneweights(float w1, float w2, float w3, float w4);
173 void boneidx(int idx1, int idx2, int idx3, int idx4);
175 int get_poly_count() const;
177 /* apply a transformation to the vertices and its inverse-transpose
178 * to the normals and tangents.
180 void apply_xform(const Mat4 &xform);
181 void apply_xform(const Mat4 &xform, const Mat4 &dir_xform);
183 void flip(); // both faces and normals
187 void explode(); // undo all vertex sharing
189 void calc_face_normals(); // this is only guaranteed to work on an exploded mesh
191 // adds a bone and returns its index
192 /*int add_bone(XFormNode *bone);
193 const XFormNode *get_bone(int idx) const;
194 int get_bones_count() const;*/
196 // access the shader attribute locations
197 static void set_attrib_location(int attr, int loc);
198 static int get_attrib_location(int attr);
199 static void clear_attrib_locations();
201 static void set_vis_vecsize(float sz);
202 static float get_vis_vecsize();
205 void draw_wire() const;
206 void draw_vertices() const;
207 void draw_normals() const;
208 void draw_tangents() const;
210 /** get the bounding box in local space. The result will be cached, and subsequent
211 * calls will return the same box. The cache gets invalidated by any functions that can affect
212 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
214 void get_aabbox(Vec3 *vmin, Vec3 *vmax) const;
215 const AABox &get_aabbox() const;
218 /** get the bounding sphere in local space. The result will be cached, and subsequent
219 * calls will return the same box. The cache gets invalidated by any functions that can affect
220 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
222 float get_bsphere(Vec3 *center, float *rad) const;
223 const Sphere &get_bsphere() const;
226 static void set_intersect_mode(unsigned int mode);
227 static unsigned int get_intersect_mode();
228 static void set_vertex_select_distance(float dist);
229 static float get_vertex_select_distance();
231 /** Find the intersection between the mesh and a ray.
232 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
233 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
235 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
237 // texture coordinate manipulation
238 void texcoord_apply_xform(const Mat4 &xform);
239 void texcoord_gen_plane(const Vec3 &norm, const Vec3 &tang);
240 void texcoord_gen_box();
241 void texcoord_gen_cylinder();
243 bool dump(const char *fname) const;
244 bool dump(FILE *fp) const;
245 bool dump_obj(const char *fname) const;
246 bool dump_obj(FILE *fp) const;