goat3dgfx

annotate src/mesh.h @ 34:3eb6c8f89fe1

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