vrshoot

view src/mesh.h @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
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 Mesh();
123 ~Mesh();
125 void set_name(const char *name);
126 const char *get_name() const;
128 bool has_attrib(int attr) const;
130 // clears everything about this mesh, and returns to the newly constructed state
131 void clear();
133 // access the vertex attribute data
134 // if vdata == 0, space is just allocated
135 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
136 float *get_attrib_data(int attrib); // invalidates vbo
137 const float *get_attrib_data(int attrib) const;
139 // simple access to any particular attribute
140 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
141 Vector4 get_attrib(int attrib, int idx) const;
143 int get_attrib_count(int attrib) const;
145 // ... same for index data
146 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
147 unsigned int *get_index_data(); // invalidates ibo
148 const unsigned int *get_index_data() const;
150 int get_index_count() const;
152 void append(const Mesh &mesh);
154 // immediate-mode style mesh construction interface
155 void vertex(float x, float y, float z);
156 void normal(float nx, float ny, float nz);
157 void tangent(float tx, float ty, float tz);
158 void texcoord(float u, float v, float w = 0.0f);
159 void boneweights(float w1, float w2, float w3, float w4);
160 void boneidx(int idx1, int idx2, int idx3, int idx4);
162 // optional face function to easily append index data
163 void face(unsigned int v0, unsigned int v1, unsigned int v2);
165 int get_poly_count() const;
167 /* apply a transformation to the vertices and its inverse-transpose
168 * to the normals and tangents.
169 */
170 void apply_xform(const Matrix4x4 &xform);
171 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
173 // adds a bone and returns its index
174 int add_bone(XFormNode *bone);
175 const XFormNode *get_bone(int idx) const;
176 int get_bones_count() const;
178 // access the shader attribute locations
179 static void set_attrib_location(int attr, int loc);
180 static int get_attrib_location(int attr);
181 static void clear_attrib_locations();
183 static void set_vis_vecsize(float sz);
184 static float get_vis_vecsize();
186 void draw() const;
187 void draw_wire() const;
188 void draw_vertices() const;
189 void draw_normals() const;
190 void draw_tangents() const;
192 /** get the bounding box in local space. The result will be cached, and subsequent
193 * calls will return the same box. The cache gets invalidated by any functions that can affect
194 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
195 * @{ */
196 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
197 const AABox &get_aabbox() const;
198 /// @}
200 /** get the bounding sphere 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 float get_bsphere(Vector3 *center, float *rad) const;
205 const Sphere &get_bsphere() const;
207 static void set_intersect_mode(unsigned int mode);
208 static unsigned int get_intersect_mode();
209 static void set_vertex_select_distance(float dist);
210 static float get_vertex_select_distance();
212 /** Find the intersection between the mesh and a ray.
213 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
214 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
215 */
216 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
217 };
219 #endif // MESH_H_