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