goat3dgfx

annotate src/mesh.h @ 0:1873dfd13f2d

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