cubemapper

view src/mesh.h @ 4:2bfafdced01a

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