conworlds

annotate src/mesh.h @ 17:c814f77d177e

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