7 #include "gmath/gmath.h"
16 MESH_ATTR_BONEWEIGHTS,
22 // intersection mode flags
24 ISECT_DEFAULT = 0, // default (whole mesh, all intersections)
25 ISECT_FRONT = 1, // front-faces only
26 ISECT_FACE = 2, // return intersected face pointer instead of mesh
27 ISECT_VERTICES = 4 // return (?) TODO
41 Triangle(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2);
42 Triangle(int n, const Vec3 *varr, const unsigned int *idxarr = 0);
44 /// calculate normal (quite expensive)
46 const Vec3 &get_normal() const;
48 void transform(const Mat4 &xform);
51 void draw_wire() const;
53 /// calculate barycentric coordinates of a point
54 Vec3 calc_barycentric(const Vec3 &pos) const;
56 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
63 unsigned int nverts, nfaces;
65 // current value for each attribute for the immedate mode
67 Vec4 cur_val[NUM_MESH_ATTR];
69 unsigned int buffer_objects[NUM_MESH_ATTR + 1];
71 // vertex attribute data and buffer objects
73 int nelem; // number of elements per attribute range: [1, 4]
74 std::vector<float> data;
76 mutable bool vbo_valid; // if this is false, the vbo needs updating from the data
77 mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo
79 } vattr[NUM_MESH_ATTR];
81 static int global_sdr_loc[NUM_MESH_ATTR];
83 //std::vector<XFormNode*> bones; // bones affecting this mesh
85 // index data and buffer object
86 std::vector<unsigned int> idata;
88 mutable bool ibo_valid;
89 mutable bool idata_valid;
91 // index buffer object for wireframe rendering (constructed on demand)
92 unsigned int wire_ibo;
93 mutable bool wire_ibo_valid;
95 // axis-aligned bounding box
97 mutable bool aabb_valid;
101 mutable bool bsph_valid;
103 // keeps the last intersected face
104 mutable Triangle hitface;
105 // keeps the last intersected vertex position
106 mutable Vec3 hitvert;
111 static unsigned int intersect_mode;
112 static float vertex_sel_dist;
114 static float vis_vecsize;
116 /// update the VBOs after data has changed (invalid vbo/ibo)
117 void update_buffers();
118 /// construct/update the wireframe index buffer (called from draw_wire).
119 void update_wire_ibo();
122 bool pre_draw() const;
123 void post_draw() const;
127 static bool use_custom_sdr_attr;
132 Mesh(const Mesh &rhs);
133 Mesh &operator =(const Mesh &rhs);
134 bool clone(const Mesh &m);
136 void set_name(const char *name);
137 const char *get_name() const;
139 bool has_attrib(int attr) const;
140 bool is_indexed() const;
142 // clears everything about this mesh, and returns to the newly constructed state
145 // access the vertex attribute data
146 // if vdata == 0, space is just allocated
147 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
148 float *get_attrib_data(int attrib); // invalidates vbo
149 const float *get_attrib_data(int attrib) const;
151 // simple access to any particular attribute
152 void set_attrib(int attrib, int idx, const Vec4 &v); // invalidates vbo
153 Vec4 get_attrib(int attrib, int idx) const;
155 int get_attrib_count(int attrib) const;
157 // ... same for index data
158 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
159 unsigned int *get_index_data(); // invalidates ibo
160 const unsigned int *get_index_data() const;
162 int get_index_count() const;
164 void append(const Mesh &mesh);
166 // immediate-mode style mesh construction interface
167 void vertex(float x, float y, float z);
168 void normal(float nx, float ny, float nz);
169 void tangent(float tx, float ty, float tz);
170 void texcoord(float u, float v, float w);
171 void boneweights(float w1, float w2, float w3, float w4);
172 void boneidx(int idx1, int idx2, int idx3, int idx4);
174 int get_poly_count() const;
176 /* apply a transformation to the vertices and its inverse-transpose
177 * to the normals and tangents.
179 void apply_xform(const Mat4 &xform);
180 void apply_xform(const Mat4 &xform, const Mat4 &dir_xform);
182 void flip(); // both faces and normals
186 void explode(); // undo all vertex sharing
188 void calc_face_normals(); // this is only guaranteed to work on an exploded mesh
190 // adds a bone and returns its index
191 /*int add_bone(XFormNode *bone);
192 const XFormNode *get_bone(int idx) const;
193 int get_bones_count() const;*/
195 // access the shader attribute locations
196 static void set_attrib_location(int attr, int loc);
197 static int get_attrib_location(int attr);
198 static void clear_attrib_locations();
200 static void set_vis_vecsize(float sz);
201 static float get_vis_vecsize();
204 void draw_wire() const;
205 void draw_vertices() const;
206 void draw_normals() const;
207 void draw_tangents() const;
209 /** get the bounding box in local space. The result will be cached, and subsequent
210 * calls will return the same box. The cache gets invalidated by any functions that can affect
211 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
213 void get_aabbox(Vec3 *vmin, Vec3 *vmax) const;
214 const AABox &get_aabbox() const;
217 /** get the bounding sphere in local space. The result will be cached, and subsequent
218 * calls will return the same box. The cache gets invalidated by any functions that can affect
219 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
221 float get_bsphere(Vec3 *center, float *rad) const;
222 const Sphere &get_bsphere() const;
224 static void set_intersect_mode(unsigned int mode);
225 static unsigned int get_intersect_mode();
226 static void set_vertex_select_distance(float dist);
227 static float get_vertex_select_distance();
229 /** Find the intersection between the mesh and a ray.
230 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
231 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
233 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
235 // texture coordinate manipulation
236 void texcoord_apply_xform(const Mat4 &xform);
237 void texcoord_gen_plane(const Vec3 &norm, const Vec3 &tang);
238 void texcoord_gen_box();
239 void texcoord_gen_cylinder();
241 bool dump(const char *fname) const;
242 bool dump(FILE *fp) const;
243 bool dump_obj(const char *fname) const;
244 bool dump_obj(FILE *fp) const;