goat3dgfx

view src/mesh.h @ 29:9d581abd0bfb

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 02:18:15 +0200
parents 1873dfd13f2d
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 namespace goatgfx {
11 enum {
12 MESH_ATTR_VERTEX,
13 MESH_ATTR_NORMAL,
14 MESH_ATTR_TANGENT,
15 MESH_ATTR_TEXCOORD,
16 MESH_ATTR_COLOR,
17 MESH_ATTR_BONEWEIGHTS,
18 MESH_ATTR_BONEIDX,
20 NUM_MESH_ATTR
21 };
23 // intersection mode flags
24 enum {
25 ISECT_DEFAULT = 0, // default (whole mesh, all intersections)
26 ISECT_FRONT = 1, // front-faces only
27 ISECT_FACE = 2, // return intersected face pointer instead of mesh
28 ISECT_VERTICES = 4 // return (?) TODO
29 };
31 class XFormNode;
34 class Triangle {
35 public:
36 Vector3 v[3];
37 Vector3 normal;
38 bool normal_valid;
39 int id;
41 Triangle();
42 Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2);
43 Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0);
45 /// calculate normal (quite expensive)
46 void calc_normal();
47 const Vector3 &get_normal() const;
49 void transform(const Matrix4x4 &xform);
51 void draw() const;
52 void draw_wire() const;
54 /// calculate barycentric coordinates of a point
55 Vector3 calc_barycentric(const Vector3 &pos) const;
57 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
58 };
61 class Mesh {
62 private:
63 std::string name;
64 unsigned int nverts, nfaces;
66 // current value for each attribute for the immedate mode
67 // interface.
68 Vector4 cur_val[NUM_MESH_ATTR];
70 unsigned int buffer_objects[NUM_MESH_ATTR + 1];
72 // vertex attribute data and buffer objects
73 struct {
74 int nelem; // number of elements per attribute range: [1, 4]
75 std::vector<float> data;
76 unsigned int vbo;
77 mutable bool vbo_valid; // if this is false, the vbo needs updating from the data
78 mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo
79 //int sdr_loc;
80 } vattr[NUM_MESH_ATTR];
82 static int global_sdr_loc[NUM_MESH_ATTR];
84 std::vector<XFormNode*> bones; // bones affecting this mesh
86 // index data and buffer object
87 std::vector<unsigned int> idata;
88 unsigned int ibo;
89 mutable bool ibo_valid;
90 mutable bool idata_valid;
92 // index buffer object for wireframe rendering (constructed on demand)
93 unsigned int wire_ibo;
94 mutable bool wire_ibo_valid;
96 // axis-aligned bounding box
97 mutable AABox aabb;
98 mutable bool aabb_valid;
100 // bounding sphere
101 mutable Sphere bsph;
102 mutable bool bsph_valid;
104 // keeps the last intersected face
105 mutable Triangle hitface;
106 // keeps the last intersected vertex position
107 mutable Vector3 hitvert;
109 void calc_aabb();
110 void calc_bsph();
112 static unsigned int intersect_mode;
113 static float vertex_sel_dist;
115 static float vis_vecsize;
117 /// update the VBOs after data has changed (invalid vbo/ibo)
118 void update_buffers();
119 /// construct/update the wireframe index buffer (called from draw_wire).
120 void update_wire_ibo();
123 public:
124 Mesh();
125 ~Mesh();
127 void set_name(const char *name);
128 const char *get_name() const;
130 bool has_attrib(int attr) const;
132 // clears everything about this mesh, and returns to the newly constructed state
133 void clear();
135 // access the vertex attribute data
136 // if vdata == 0, space is just allocated
137 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
138 float *get_attrib_data(int attrib); // invalidates vbo
139 const float *get_attrib_data(int attrib) const;
141 // simple access to any particular attribute
142 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
143 Vector4 get_attrib(int attrib, int idx) 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 void append(const Mesh &mesh);
152 // immediate-mode style mesh construction interface
153 void vertex(float x, float y, float z);
154 void normal(float nx, float ny, float nz);
155 void tangent(float tx, float ty, float tz);
156 void texcoord(float u, float v, float w);
157 void boneweights(float w1, float w2, float w3, float w4);
158 void boneidx(int idx1, int idx2, int idx3, int idx4);
160 /* apply a transformation to the vertices and its inverse-transpose
161 * to the normals and tangents.
162 */
163 void apply_xform(const Matrix4x4 &xform);
164 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
166 // adds a bone and returns its index
167 int add_bone(XFormNode *bone);
168 const XFormNode *get_bone(int idx) const;
169 int get_bones_count() const;
171 // access the shader attribute locations
172 static void set_attrib_location(int attr, int loc);
173 static int get_attrib_location(int attr);
174 static void clear_attrib_locations();
176 static void set_vis_vecsize(float sz);
177 static float get_vis_vecsize();
179 void draw() const;
180 void draw_wire() const;
181 void draw_vertices() const;
182 void draw_normals() const;
183 void draw_tangents() const;
185 /** get the bounding box in local space. The result will be cached, and subsequent
186 * calls will return the same box. The cache gets invalidated by any functions that can affect
187 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
188 * @{ */
189 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
190 const AABox &get_aabbox() const;
191 /// @}
193 /** get the bounding sphere in local space. The result will be cached, and subsequent
194 * calls will return the same box. The cache gets invalidated by any functions that can affect
195 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
196 * @{ */
197 float get_bsphere(Vector3 *center, float *rad) const;
198 const Sphere &get_bsphere() const;
200 static void set_intersect_mode(unsigned int mode);
201 static unsigned int get_intersect_mode();
202 static void set_vertex_select_distance(float dist);
203 static float get_vertex_select_distance();
205 /** Find the intersection between the mesh and a ray.
206 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
207 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
208 */
209 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
210 };
212 } // namespace goatgfx
214 #endif // MESH_H_