coeng

view src/mesh.h @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +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 };
30 class Triangle {
31 public:
32 Vector3 v[3];
33 Vector3 normal;
34 bool normal_valid;
35 int id;
37 Triangle();
38 Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2);
39 Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0);
41 /// calculate normal (quite expensive)
42 void calc_normal();
43 const Vector3 &get_normal() const;
45 void transform(const Matrix4x4 &xform);
47 void draw() const;
48 void draw_wire() const;
50 /// calculate barycentric coordinates of a point
51 Vector3 calc_barycentric(const Vector3 &pos) const;
53 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
54 };
57 class Mesh {
58 private:
59 std::string name;
60 unsigned int nverts, nfaces;
62 // current value for each attribute for the immedate mode
63 // interface.
64 Vector4 cur_val[NUM_MESH_ATTR];
66 unsigned int buffer_objects[NUM_MESH_ATTR + 1];
68 // vertex attribute data and buffer objects
69 struct {
70 int nelem; // number of elements per attribute range: [1, 4]
71 std::vector<float> data;
72 unsigned int vbo;
73 mutable bool vbo_valid; // if this is false, the vbo needs updating from the data
74 mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo
75 //int sdr_loc;
76 } vattr[NUM_MESH_ATTR];
78 static int global_sdr_loc[NUM_MESH_ATTR];
80 //std::vector<XFormNode*> bones; // bones affecting this mesh
82 // index data and buffer object
83 std::vector<unsigned int> idata;
84 unsigned int ibo;
85 mutable bool ibo_valid;
86 mutable bool idata_valid;
88 // index buffer object for wireframe rendering (constructed on demand)
89 unsigned int wire_ibo;
90 mutable bool wire_ibo_valid;
92 // axis-aligned bounding box
93 mutable AABox aabb;
94 mutable bool aabb_valid;
96 // bounding sphere
97 mutable Sphere bsph;
98 mutable bool bsph_valid;
100 // keeps the last intersected face
101 mutable Triangle hitface;
102 // keeps the last intersected vertex position
103 mutable Vector3 hitvert;
105 void calc_aabb();
106 void calc_bsph();
108 static unsigned int intersect_mode;
109 static float vertex_sel_dist;
111 static float vis_vecsize;
113 /// update the VBOs after data has changed (invalid vbo/ibo)
114 void update_buffers();
115 /// construct/update the wireframe index buffer (called from draw_wire).
116 void update_wire_ibo();
119 public:
120 Mesh();
121 ~Mesh();
123 void set_name(const char *name);
124 const char *get_name() const;
126 bool has_attrib(int attr) const;
128 // clears everything about this mesh, and returns to the newly constructed state
129 void clear();
131 // access the vertex attribute data
132 // if vdata == 0, space is just allocated
133 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
134 float *get_attrib_data(int attrib); // invalidates vbo
135 const float *get_attrib_data(int attrib) const;
137 // simple access to any particular attribute
138 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
139 Vector4 get_attrib(int attrib, int idx) const;
141 // ... same for index data
142 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
143 unsigned int *get_index_data(); // invalidates ibo
144 const unsigned int *get_index_data() const;
146 void append(const Mesh &mesh);
148 // immediate-mode style mesh construction interface
149 void vertex(float x, float y, float z);
150 void normal(float nx, float ny, float nz);
151 void tangent(float tx, float ty, float tz);
152 void texcoord(float u, float v, float w);
153 void boneweights(float w1, float w2, float w3, float w4);
154 void boneidx(int idx1, int idx2, int idx3, int idx4);
156 /* apply a transformation to the vertices and its inverse-transpose
157 * to the normals and tangents.
158 */
159 void apply_xform(const Matrix4x4 &xform);
160 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
162 // adds a bone and returns its index
163 /*int add_bone(XFormNode *bone);
164 const XFormNode *get_bone(int idx) const;
165 int get_bones_count() const;
166 */
168 // access the shader attribute locations
169 static void set_attrib_location(int attr, int loc);
170 static int get_attrib_location(int attr);
171 static void clear_attrib_locations();
173 static void set_vis_vecsize(float sz);
174 static float get_vis_vecsize();
176 void draw() const;
177 void draw_wire() const;
178 void draw_vertices() const;
179 void draw_normals() const;
180 void draw_tangents() const;
182 /** get the bounding box in local space. The result will be cached, and subsequent
183 * calls will return the same box. The cache gets invalidated by any functions that can affect
184 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
185 * @{ */
186 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
187 const AABox &get_aabbox() const;
188 /// @}
190 /** get the bounding sphere in local space. The result will be cached, and subsequent
191 * calls will return the same box. The cache gets invalidated by any functions that can affect
192 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
193 * @{ */
194 float get_bsphere(Vector3 *center, float *rad) const;
195 const Sphere &get_bsphere() const;
197 static void set_intersect_mode(unsigned int mode);
198 static unsigned int get_intersect_mode();
199 static void set_vertex_select_distance(float dist);
200 static float get_vertex_select_distance();
202 /** Find the intersection between the mesh and a ray.
203 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
204 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
205 */
206 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
207 };
209 #endif // MESH_H_