cubemapper

annotate src/mesh.h @ 5:614295b72341

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