tavli

view src/mesh.h @ 11:a8e26f163f99

poulia
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Jun 2015 08:01:51 +0300
parents 52e0dd47753b
children b1a195c3ee16
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 Mesh(const Mesh &rhs);
126 Mesh &operator =(const Mesh &rhs);
127 bool clone(const Mesh &m);
129 void set_name(const char *name);
130 const char *get_name() const;
132 bool has_attrib(int attr) const;
133 bool is_indexed() const;
135 // clears everything about this mesh, and returns to the newly constructed state
136 void clear();
138 // access the vertex attribute data
139 // if vdata == 0, space is just allocated
140 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
141 float *get_attrib_data(int attrib); // invalidates vbo
142 const float *get_attrib_data(int attrib) const;
144 // simple access to any particular attribute
145 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
146 Vector4 get_attrib(int attrib, int idx) const;
148 int get_attrib_count(int attrib) const;
150 // ... same for index data
151 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
152 unsigned int *get_index_data(); // invalidates ibo
153 const unsigned int *get_index_data() const;
155 int get_index_count() const;
157 void append(const Mesh &mesh);
159 // immediate-mode style mesh construction interface
160 void vertex(float x, float y, float z);
161 void normal(float nx, float ny, float nz);
162 void tangent(float tx, float ty, float tz);
163 void texcoord(float u, float v, float w);
164 void boneweights(float w1, float w2, float w3, float w4);
165 void boneidx(int idx1, int idx2, int idx3, int idx4);
167 int get_poly_count() const;
169 /* apply a transformation to the vertices and its inverse-transpose
170 * to the normals and tangents.
171 */
172 void apply_xform(const Matrix4x4 &xform);
173 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
175 void flip(); // both faces and normals
176 void flip_faces();
177 void flip_normals();
179 // adds a bone and returns its index
180 /*int add_bone(XFormNode *bone);
181 const XFormNode *get_bone(int idx) const;
182 int get_bones_count() const;*/
184 // access the shader attribute locations
185 static void set_attrib_location(int attr, int loc);
186 static int get_attrib_location(int attr);
187 static void clear_attrib_locations();
189 static void set_vis_vecsize(float sz);
190 static float get_vis_vecsize();
192 void draw() const;
193 void draw_wire() const;
194 void draw_vertices() const;
195 void draw_normals() const;
196 void draw_tangents() const;
198 /** get the bounding box in local space. The result will be cached, and subsequent
199 * calls will return the same box. The cache gets invalidated by any functions that can affect
200 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
201 * @{ */
202 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
203 const AABox &get_aabbox() const;
204 /// @}
206 /** get the bounding sphere in local space. The result will be cached, and subsequent
207 * calls will return the same box. The cache gets invalidated by any functions that can affect
208 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
209 * @{ */
210 float get_bsphere(Vector3 *center, float *rad) const;
211 const Sphere &get_bsphere() const;
213 static void set_intersect_mode(unsigned int mode);
214 static unsigned int get_intersect_mode();
215 static void set_vertex_select_distance(float dist);
216 static float get_vertex_select_distance();
218 /** Find the intersection between the mesh and a ray.
219 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
220 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
221 */
222 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
224 // texture coordinate manipulation
225 void texcoord_apply_xform(const Matrix4x4 &xform);
226 void texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang);
227 void texcoord_gen_box();
228 };
230 #endif // MESH_H_