goat3dgfx

view src/mesh.h @ 2:7bd5ebec3b6f

fixed visual studio project files, removed the dll crap
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 16 Nov 2013 13:26:53 +0200
parents
children 7d6b667821cf
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 void set_name(const char *name);
126 const char *get_name() const;
128 bool has_attrib(int attr) const;
130 // clears everything about this mesh, and returns to the newly constructed state
131 void clear();
133 // access the vertex attribute data
134 // if vdata == 0, space is just allocated
135 float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo
136 float *get_attrib_data(int attrib); // invalidates vbo
137 const float *get_attrib_data(int attrib) const;
139 // simple access to any particular attribute
140 void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo
141 Vector4 get_attrib(int attrib, int idx) const;
143 // ... same for index data
144 unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo
145 unsigned int *get_index_data(); // invalidates ibo
146 const unsigned int *get_index_data() const;
148 void append(const Mesh &mesh);
150 // immediate-mode style mesh construction interface
151 void vertex(float x, float y, float z);
152 void normal(float nx, float ny, float nz);
153 void tangent(float tx, float ty, float tz);
154 void texcoord(float u, float v, float w);
155 void boneweights(float w1, float w2, float w3, float w4);
156 void boneidx(int idx1, int idx2, int idx3, int idx4);
158 /* apply a transformation to the vertices and its inverse-transpose
159 * to the normals and tangents.
160 */
161 void apply_xform(const Matrix4x4 &xform);
162 void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform);
164 // adds a bone and returns its index
165 int add_bone(XFormNode *bone);
166 const XFormNode *get_bone(int idx) const;
167 int get_bones_count() const;
169 // access the shader attribute locations
170 static void set_attrib_location(int attr, int loc);
171 static int get_attrib_location(int attr);
172 static void clear_attrib_locations();
174 static void set_vis_vecsize(float sz);
175 static float get_vis_vecsize();
177 void draw() const;
178 void draw_wire() const;
179 void draw_vertices() const;
180 void draw_normals() const;
181 void draw_tangents() const;
183 /** get the bounding box in local space. The result will be cached, and subsequent
184 * calls will return the same box. The cache gets invalidated by any functions that can affect
185 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
186 * @{ */
187 void get_aabbox(Vector3 *vmin, Vector3 *vmax) const;
188 const AABox &get_aabbox() const;
189 /// @}
191 /** get the bounding sphere in local space. The result will be cached, and subsequent
192 * calls will return the same box. The cache gets invalidated by any functions that can affect
193 * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included).
194 * @{ */
195 float get_bsphere(Vector3 *center, float *rad) const;
196 const Sphere &get_bsphere() const;
198 static void set_intersect_mode(unsigned int mode);
199 static unsigned int get_intersect_mode();
200 static void set_vertex_select_distance(float dist);
201 static float get_vertex_select_distance();
203 /** Find the intersection between the mesh and a ray.
204 * XXX Brute force at the moment, not intended to be used for anything other than picking in tools.
205 * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it!
206 */
207 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
208 };
211 #endif // MESH_H_