coeng
diff src/mesh.h @ 8:8cce82794f90
seems to work nicely
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Feb 2015 05:14:20 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.h Sun Feb 15 05:14:20 2015 +0200 1.3 @@ -0,0 +1,209 @@ 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 + 1.33 +class Triangle { 1.34 +public: 1.35 + Vector3 v[3]; 1.36 + Vector3 normal; 1.37 + bool normal_valid; 1.38 + int id; 1.39 + 1.40 + Triangle(); 1.41 + Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2); 1.42 + Triangle(int n, const Vector3 *varr, const unsigned int *idxarr = 0); 1.43 + 1.44 + /// calculate normal (quite expensive) 1.45 + void calc_normal(); 1.46 + const Vector3 &get_normal() const; 1.47 + 1.48 + void transform(const Matrix4x4 &xform); 1.49 + 1.50 + void draw() const; 1.51 + void draw_wire() const; 1.52 + 1.53 + /// calculate barycentric coordinates of a point 1.54 + Vector3 calc_barycentric(const Vector3 &pos) const; 1.55 + 1.56 + bool intersect(const Ray &ray, HitPoint *hit = 0) const; 1.57 +}; 1.58 + 1.59 + 1.60 +class Mesh { 1.61 +private: 1.62 + std::string name; 1.63 + unsigned int nverts, nfaces; 1.64 + 1.65 + // current value for each attribute for the immedate mode 1.66 + // interface. 1.67 + Vector4 cur_val[NUM_MESH_ATTR]; 1.68 + 1.69 + unsigned int buffer_objects[NUM_MESH_ATTR + 1]; 1.70 + 1.71 + // vertex attribute data and buffer objects 1.72 + struct { 1.73 + int nelem; // number of elements per attribute range: [1, 4] 1.74 + std::vector<float> data; 1.75 + unsigned int vbo; 1.76 + mutable bool vbo_valid; // if this is false, the vbo needs updating from the data 1.77 + mutable bool data_valid; // if this is false, the data needs to be pulled from the vbo 1.78 + //int sdr_loc; 1.79 + } vattr[NUM_MESH_ATTR]; 1.80 + 1.81 + static int global_sdr_loc[NUM_MESH_ATTR]; 1.82 + 1.83 + //std::vector<XFormNode*> bones; // bones affecting this mesh 1.84 + 1.85 + // index data and buffer object 1.86 + std::vector<unsigned int> idata; 1.87 + unsigned int ibo; 1.88 + mutable bool ibo_valid; 1.89 + mutable bool idata_valid; 1.90 + 1.91 + // index buffer object for wireframe rendering (constructed on demand) 1.92 + unsigned int wire_ibo; 1.93 + mutable bool wire_ibo_valid; 1.94 + 1.95 + // axis-aligned bounding box 1.96 + mutable AABox aabb; 1.97 + mutable bool aabb_valid; 1.98 + 1.99 + // bounding sphere 1.100 + mutable Sphere bsph; 1.101 + mutable bool bsph_valid; 1.102 + 1.103 + // keeps the last intersected face 1.104 + mutable Triangle hitface; 1.105 + // keeps the last intersected vertex position 1.106 + mutable Vector3 hitvert; 1.107 + 1.108 + void calc_aabb(); 1.109 + void calc_bsph(); 1.110 + 1.111 + static unsigned int intersect_mode; 1.112 + static float vertex_sel_dist; 1.113 + 1.114 + static float vis_vecsize; 1.115 + 1.116 + /// update the VBOs after data has changed (invalid vbo/ibo) 1.117 + void update_buffers(); 1.118 + /// construct/update the wireframe index buffer (called from draw_wire). 1.119 + void update_wire_ibo(); 1.120 + 1.121 + 1.122 +public: 1.123 + Mesh(); 1.124 + ~Mesh(); 1.125 + 1.126 + void set_name(const char *name); 1.127 + const char *get_name() const; 1.128 + 1.129 + bool has_attrib(int attr) const; 1.130 + 1.131 + // clears everything about this mesh, and returns to the newly constructed state 1.132 + void clear(); 1.133 + 1.134 + // access the vertex attribute data 1.135 + // if vdata == 0, space is just allocated 1.136 + float *set_attrib_data(int attrib, int nelem, unsigned int num, const float *vdata = 0); // invalidates vbo 1.137 + float *get_attrib_data(int attrib); // invalidates vbo 1.138 + const float *get_attrib_data(int attrib) const; 1.139 + 1.140 + // simple access to any particular attribute 1.141 + void set_attrib(int attrib, int idx, const Vector4 &v); // invalidates vbo 1.142 + Vector4 get_attrib(int attrib, int idx) const; 1.143 + 1.144 + // ... same for index data 1.145 + unsigned int *set_index_data(int num, const unsigned int *indices = 0); // invalidates ibo 1.146 + unsigned int *get_index_data(); // invalidates ibo 1.147 + const unsigned int *get_index_data() const; 1.148 + 1.149 + void append(const Mesh &mesh); 1.150 + 1.151 + // immediate-mode style mesh construction interface 1.152 + void vertex(float x, float y, float z); 1.153 + void normal(float nx, float ny, float nz); 1.154 + void tangent(float tx, float ty, float tz); 1.155 + void texcoord(float u, float v, float w); 1.156 + void boneweights(float w1, float w2, float w3, float w4); 1.157 + void boneidx(int idx1, int idx2, int idx3, int idx4); 1.158 + 1.159 + /* apply a transformation to the vertices and its inverse-transpose 1.160 + * to the normals and tangents. 1.161 + */ 1.162 + void apply_xform(const Matrix4x4 &xform); 1.163 + void apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform); 1.164 + 1.165 + // adds a bone and returns its index 1.166 + /*int add_bone(XFormNode *bone); 1.167 + const XFormNode *get_bone(int idx) const; 1.168 + int get_bones_count() const; 1.169 + */ 1.170 + 1.171 + // access the shader attribute locations 1.172 + static void set_attrib_location(int attr, int loc); 1.173 + static int get_attrib_location(int attr); 1.174 + static void clear_attrib_locations(); 1.175 + 1.176 + static void set_vis_vecsize(float sz); 1.177 + static float get_vis_vecsize(); 1.178 + 1.179 + void draw() const; 1.180 + void draw_wire() const; 1.181 + void draw_vertices() const; 1.182 + void draw_normals() const; 1.183 + void draw_tangents() const; 1.184 + 1.185 + /** get the bounding box in local space. The result will be cached, and subsequent 1.186 + * calls will return the same box. The cache gets invalidated by any functions that can affect 1.187 + * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included). 1.188 + * @{ */ 1.189 + void get_aabbox(Vector3 *vmin, Vector3 *vmax) const; 1.190 + const AABox &get_aabbox() const; 1.191 + /// @} 1.192 + 1.193 + /** get the bounding sphere in local space. The result will be cached, and subsequent 1.194 + * calls will return the same box. The cache gets invalidated by any functions that can affect 1.195 + * the vertex data (non-const variant of get_attrib_data(MESH_ATTR_VERTEX, ...) included). 1.196 + * @{ */ 1.197 + float get_bsphere(Vector3 *center, float *rad) const; 1.198 + const Sphere &get_bsphere() const; 1.199 + 1.200 + static void set_intersect_mode(unsigned int mode); 1.201 + static unsigned int get_intersect_mode(); 1.202 + static void set_vertex_select_distance(float dist); 1.203 + static float get_vertex_select_distance(); 1.204 + 1.205 + /** Find the intersection between the mesh and a ray. 1.206 + * XXX Brute force at the moment, not intended to be used for anything other than picking in tools. 1.207 + * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it! 1.208 + */ 1.209 + bool intersect(const Ray &ray, HitPoint *hit = 0) const; 1.210 +}; 1.211 + 1.212 +#endif // MESH_H_