cubemapper

annotate src/mesh.h @ 0:8fc9e1d3aad2

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