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