dbf-halloween2015
diff src/mesh.h @ 0:50683c78264e
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:09:12 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.h Sun Nov 01 00:09:12 2015 +0200 1.3 @@ -0,0 +1,236 @@ 1.4 +#ifndef MESH_H_ 1.5 +#define MESH_H_ 1.6 + 1.7 +#include <string> 1.8 +#include <vector> 1.9 +#include "vmath/vmath.h" 1.10 +#include "geom.h" 1.11 + 1.12 +enum { 1.13 + MESH_ATTR_VERTEX, 1.14 + MESH_ATTR_NORMAL, 1.15 + MESH_ATTR_TANGENT, 1.16 + MESH_ATTR_TEXCOORD, 1.17 + MESH_ATTR_COLOR, 1.18 + MESH_ATTR_BONEWEIGHTS, 1.19 + MESH_ATTR_BONEIDX, 1.20 + 1.21 + NUM_MESH_ATTR 1.22 +}; 1.23 + 1.24 +// intersection mode flags 1.25 +enum { 1.26 + ISECT_DEFAULT = 0, // default (whole mesh, all intersections) 1.27 + ISECT_FRONT = 1, // front-faces only 1.28 + ISECT_FACE = 2, // return intersected face pointer instead of mesh 1.29 + ISECT_VERTICES = 4 // return (?) TODO 1.30 +}; 1.31 + 1.32 +//class XFormNode; 1.33 + 1.34 + 1.35 +class Triangle { 1.36 +public: 1.37 + Vector3 v[3]; 1.38 + Vector3 normal; 1.39 + bool normal_valid; 1.40 + int id; 1.41 + 1.42 + Triangle(); 1.43 + Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2); 1.44 + Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0); 1.45 + 1.46 + /// calculate normal (quite expensive) 1.47 + void calc_normal(); 1.48 + const Vector3 &get_normal() const; 1.49 + 1.50 + void transform(const Matrix4x4 &xform); 1.51 + 1.52 + void draw() const; 1.53 + void draw_wire() const; 1.54 + 1.55 + /// calculate barycentric coordinates of a point 1.56 + Vector3 calc_barycentric(const Vector3 &pos) const; 1.57 + 1.58 + bool intersect(const Ray &ray, HitPoint *hit = 0) const; 1.59 +}; 1.60 + 1.61 + 1.62 +class Mesh { 1.63 +private: 1.64 + std::string name; 1.65 + unsigned int nverts, nfaces; 1.66 + 1.67 + // current value for each attribute for the immedate mode 1.68 + // interface. 1.69 + Vector4 cur_val[NUM_MESH_ATTR]; 1.70 + 1.71 + unsigned int buffer_objects[NUM_MESH_ATTR + 1]; 1.72 + 1.73 + // vertex attribute data and buffer objects 1.74 + struct { 1.75 + int nelem; // number of elements per attribute range: [1, 4] 1.76 + std::vector<float> data; 1.77 + unsigned int vbo; 1.78 + mutable bool vbo_valid; // if this is false, the vbo needs updating from the data 1.79 + mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo 1.80 + //int sdr_loc; 1.81 + } vattr[NUM_MESH_ATTR]; 1.82 + 1.83 + static int global_sdr_loc[NUM_MESH_ATTR]; 1.84 + 1.85 + //std::vector<XFormNode*> bones; // bones affecting this mesh 1.86 + 1.87 + // index data and buffer object 1.88 + std::vector<unsigned int> idata; 1.89 + unsigned int ibo; 1.90 + mutable bool ibo_valid; 1.91 + mutable bool idata_valid; 1.92 + 1.93 + // index buffer object for wireframe rendering (constructed on demand) 1.94 + unsigned int wire_ibo; 1.95 + mutable bool wire_ibo_valid; 1.96 + 1.97 + // axis-aligned bounding box 1.98 + mutable AABox aabb; 1.99 + mutable bool aabb_valid; 1.100 + 1.101 + // bounding sphere 1.102 + mutable Sphere bsph; 1.103 + mutable bool bsph_valid; 1.104 + 1.105 + // keeps the last intersected face 1.106 + mutable Triangle hitface; 1.107 + // keeps the last intersected vertex position 1.108 + mutable Vector3 hitvert; 1.109 + 1.110 + void calc_aabb(); 1.111 + void calc_bsph(); 1.112 + 1.113 + static unsigned int intersect_mode; 1.114 + static float vertex_sel_dist; 1.115 + 1.116 + static float vis_vecsize; 1.117 + 1.118 + /// update the VBOs after data has changed (invalid vbo/ibo) 1.119 + void update_buffers(); 1.120 + /// construct/update the wireframe index buffer (called from draw_wire). 1.121 + void update_wire_ibo(); 1.122 + 1.123 + mutable int cur_sdr; 1.124 + bool pre_draw() const; 1.125 + void post_draw() const; 1.126 + 1.127 + 1.128 +public: 1.129 + static bool use_custom_sdr_attr; 1.130 + 1.131 + Mesh(); 1.132 + ~Mesh(); 1.133 + 1.134 + Mesh(const Mesh &rhs); 1.135 + Mesh &operator =(const Mesh &rhs); 1.136 + bool clone(const Mesh &m); 1.137 + 1.138 + void set_name(const char *name); 1.139 + const char *get_name() const; 1.140 + 1.141 + bool has_attrib(int attr) const; 1.142 + bool is_indexed() const; 1.143 + 1.144 + // clears everything about this mesh, and returns to the newly constructed state 1.145 + void clear(); 1.146 + 1.147 + // access the vertex attribute data 1.148 + // if vdata == 0, space is just allocated 1.149 + float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo 1.150 + float *get_attrib_data(int attrib); // invalidates vbo 1.151 + const float *get_attrib_data(int attrib) const; 1.152 + 1.153 + // simple access to any particular attribute 1.154 + void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo 1.155 + Vector4 get_attrib(int attrib, int idx) const; 1.156 + 1.157 + int get_attrib_count(int attrib) const; 1.158 + 1.159 + // ... same for index data 1.160 + unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo 1.161 + unsigned int *get_index_data(); // invalidates ibo 1.162 + const unsigned int *get_index_data() const; 1.163 + 1.164 + int get_index_count() const; 1.165 + 1.166 + void append(const Mesh &mesh); 1.167 + 1.168 + // immediate-mode style mesh construction interface 1.169 + void vertex(float x, float y, float z); 1.170 + void normal(float nx, float ny, float nz); 1.171 + void tangent(float tx, float ty, float tz); 1.172 + void texcoord(float u, float v, float w); 1.173 + void boneweights(float w1, float w2, float w3, float w4); 1.174 + void boneidx(int idx1, int idx2, int idx3, int idx4); 1.175 + 1.176 + int get_poly_count() const; 1.177 + 1.178 + /* apply a transformation to the vertices and its inverse-transpose 1.179 + * to the normals and tangents. 1.180 + */ 1.181 + void apply_xform(const Matrix4x4 &xform); 1.182 + void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform); 1.183 + 1.184 + void flip(); // both faces and normals 1.185 + void flip_faces(); 1.186 + void flip_normals(); 1.187 + 1.188 + // adds a bone and returns its index 1.189 + /*int add_bone(XFormNode *bone); 1.190 + const XFormNode *get_bone(int idx) const; 1.191 + int get_bones_count() const;*/ 1.192 + 1.193 + // access the shader attribute locations 1.194 + static void set_attrib_location(int attr, int loc); 1.195 + static int get_attrib_location(int attr); 1.196 + static void clear_attrib_locations(); 1.197 + 1.198 + static void set_vis_vecsize(float sz); 1.199 + static float get_vis_vecsize(); 1.200 + 1.201 + void draw() const; 1.202 + void draw_wire() const; 1.203 + void draw_vertices() const; 1.204 + void draw_normals() const; 1.205 + void draw_tangents() const; 1.206 + 1.207 + /** get the bounding box in local space. The result will be cached, and subsequent 1.208 + * calls will return the same box. The cache gets invalidated by any functions that can affect 1.209 + * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included). 1.210 + * @{ */ 1.211 + void get_aabbox(Vector3 *vmin, Vector3 *vmax) const; 1.212 + const AABox &get_aabbox() const; 1.213 + /// @} 1.214 + 1.215 + /** get the bounding sphere in local space. The result will be cached, and subsequent 1.216 + * calls will return the same box. The cache gets invalidated by any functions that can affect 1.217 + * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included). 1.218 + * @{ */ 1.219 + float get_bsphere(Vector3 *center, float *rad) const; 1.220 + const Sphere &get_bsphere() const; 1.221 + 1.222 + static void set_intersect_mode(unsigned int mode); 1.223 + static unsigned int get_intersect_mode(); 1.224 + static void set_vertex_select_distance(float dist); 1.225 + static float get_vertex_select_distance(); 1.226 + 1.227 + /** Find the intersection between the mesh and a ray. 1.228 + * XXX Brute force at the moment, not intended to be used for anything other than picking in tools. 1.229 + * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it! 1.230 + */ 1.231 + bool intersect(const Ray &ray, HitPoint *hit = 0) const; 1.232 + 1.233 + // texture coordinate manipulation 1.234 + void texcoord_apply_xform(const Matrix4x4 &xform); 1.235 + void texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang); 1.236 + void texcoord_gen_box(); 1.237 +}; 1.238 + 1.239 +#endif // MESH_H_