goat3d

annotate src/mesh.h @ 1:e46529a5d057

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