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