tavli

view src/mesh.h @ 21:c3fbf9616dbd

slot bounds, and ray testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 02 Jul 2015 00:01:39 +0300
parents a0d30f6f20d4
children
line source
1 #ifndef MESH_H_
2 #define MESH_H_
4 #include <string>
5 #include <vector>
6 #include "vmath/vmath.h"
7 #include "geom.h"
9 enum {
10 MESH_ATTR_VERTEX,
11 MESH_ATTR_NORMAL,
12 MESH_ATTR_TANGENT,
13 MESH_ATTR_TEXCOORD,
14 MESH_ATTR_COLOR,
15 MESH_ATTR_BONEWEIGHTS,
16 MESH_ATTR_BONEIDX,
18 NUM_MESH_ATTR
19 };
21 // intersection mode flags
22 enum {
23 ISECT_DEFAULT = 0, // default (whole mesh, all intersections)
24 ISECT_FRONT = 1, // front-faces only
25 ISECT_FACE = 2, // return intersected face pointer instead of mesh
26 ISECT_VERTICES = 4 // return (?) TODO
27 };
29 //class XFormNode;
32 class Triangle {
33 public:
34 Vector3 v[3];
35 Vector3 normal;
36 bool normal_valid;
37 int id;
39 Triangle();
40 Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2);
41 Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0);
43 /// calculate normal (quite expensive)
44 void calc_normal();
45 const Vector3 &get_normal() const;
47 void transform(const Matrix4x4 &xform);
49 void draw() const;
50 void draw_wire() const;
52 /// calculate barycentric coordinates of a point
53 Vector3 calc_barycentric(const Vector3 &pos) const;
55 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
56 };
59 class Mesh {
60 private:
61 std::string name;
62 unsigned int nverts, nfaces;
64 // current value for each attribute for the immedate mode
65 // interface.
66 Vector4 cur_val[NUM_MESH_ATTR];
68 unsigned int buffer_objects[NUM_MESH_ATTR + 1];
70 // vertex attribute data and buffer objects
71 struct {
72 int nelem; // number of elements per attribute range: [1, 4]
73 std::vector<float> data;
74 unsigned int vbo;
75 mutable bool vbo_valid; // if this is false, the vbo needs updating from the data
76 mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo
77 //int sdr_loc;
78 } vattr[NUM_MESH_ATTR];
80 static int global_sdr_loc[NUM_MESH_ATTR];
82 //std::vector<XFormNode*> bones; // bones affecting this mesh
84 // index data and buffer object
85 std::vector<unsigned int> idata;
86 unsigned int ibo;
87 mutable bool ibo_valid;
88 mutable bool idata_valid;
90 // index buffer object for wireframe rendering (constructed on demand)
91 unsigned int wire_ibo;
92 mutable bool wire_ibo_valid;
94 // axis-aligned bounding box
95 mutable AABox aabb;
96 mutable bool aabb_valid;
98 // bounding sphere
99 mutable Sphere bsph;
100 mutable bool bsph_valid;
102 // keeps the last intersected face
103 mutable Triangle hitface;
104 // keeps the last intersected vertex position
105 mutable Vector3 hitvert;
107 void calc_aabb();
108 void calc_bsph();
110 static unsigned int intersect_mode;
111 static float vertex_sel_dist;
113 static float vis_vecsize;
115 /// update the VBOs after data has changed (invalid vbo/ibo)
116 void update_buffers();
117 /// construct/update the wireframe index buffer (called from draw_wire).
118 void update_wire_ibo();
121 public:
122 static bool use_custom_sdr_attr;
124 Mesh();
125 ~Mesh();
127 Mesh(const Mesh &rhs);
128 Mesh &operator =(const Mesh &rhs);
129 bool clone(const Mesh &m);
131 void set_name(const char *name);
132 const char *get_name() const;
134 bool has_attrib(int attr) const;
135 bool is_indexed() const;
137 // clears everything about this mesh, and returns to the newly constructed state
138 void clear();
140 // access the vertex attribute data
141 // if vdata == 0, space is just allocated
142 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
143 float *get_attrib_data(int attrib); // invalidates vbo
144 const float *get_attrib_data(int attrib) const;
146 // simple access to any particular attribute
147 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
148 Vector4 get_attrib(int attrib, int idx) const;
150 int get_attrib_count(int attrib) const;
152 // ... same for index data
153 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
154 unsigned int *get_index_data(); // invalidates ibo
155 const unsigned int *get_index_data() const;
157 int get_index_count() const;
159 void append(const Mesh &mesh);
161 // immediate-mode style mesh construction interface
162 void vertex(float x, float y, float z);
163 void normal(float nx, float ny, float nz);
164 void tangent(float tx, float ty, float tz);
165 void texcoord(float u, float v, float w);
166 void boneweights(float w1, float w2, float w3, float w4);
167 void boneidx(int idx1, int idx2, int idx3, int idx4);
169 int get_poly_count() const;
171 /* apply a transformation to the vertices and its inverse-transpose
172 * to the normals and tangents.
173 */
174 void apply_xform(const Matrix4x4 &xform);
175 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
177 void flip(); // both faces and normals
178 void flip_faces();
179 void flip_normals();
181 // adds a bone and returns its index
182 /*int add_bone(XFormNode *bone);
183 const XFormNode *get_bone(int idx) const;
184 int get_bones_count() const;*/
186 // access the shader attribute locations
187 static void set_attrib_location(int attr, int loc);
188 static int get_attrib_location(int attr);
189 static void clear_attrib_locations();
191 static void set_vis_vecsize(float sz);
192 static float get_vis_vecsize();
194 void draw() const;
195 void draw_wire() const;
196 void draw_vertices() const;
197 void draw_normals() const;
198 void draw_tangents() const;
200 /** get the bounding box in local space. The result will be cached, and subsequent
201 * calls will return the same box. The cache gets invalidated by any functions that can affect
202 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
203 * @{ */
204 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
205 const AABox &get_aabbox() const;
206 /// @}
208 /** get the bounding sphere in local space. The result will be cached, and subsequent
209 * calls will return the same box. The cache gets invalidated by any functions that can affect
210 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
211 * @{ */
212 float get_bsphere(Vector3 *center, float *rad) const;
213 const Sphere &get_bsphere() const;
215 static void set_intersect_mode(unsigned int mode);
216 static unsigned int get_intersect_mode();
217 static void set_vertex_select_distance(float dist);
218 static float get_vertex_select_distance();
220 /** Find the intersection between the mesh and a ray.
221 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
222 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
223 */
224 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
226 // texture coordinate manipulation
227 void texcoord_apply_xform(const Matrix4x4 &xform);
228 void texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang);
229 void texcoord_gen_box();
230 };
232 #endif // MESH_H_