2 This file is part of the 3dengfx, realtime visualization system.
3 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* fundamendal data structures for 3D graphics
22 * Author: John Tsiombikas 2004
24 * Mihalis Georgoulopoulos 2004
25 * John Tsiombikas 2005
28 // JT: seperated animation classes and placed them in animation.hpp/cpp - 2/2005
33 #include "3dengfx_config.h"
35 #include "n3dmath2/n3dmath2.hpp"
41 typedef uint32_t Index;
44 scalar_t u, v, w; // or s,t,v if you prefer... I like u,v,w more though.
46 TexCoord(scalar_t u = 0.0f, scalar_t v = 0.0f, scalar_t w = 0.0f);
49 class Triangle; // fwd declaration
60 Vertex(const Vector3 &position, scalar_t tu = 0.0f, scalar_t tv = 0.0f, const Color &color = Color(1.0f, 1.0f, 1.0f));
63 #define NO_ADJFACE 0xFFFFFFFF
71 Edge(Index v1, Index v2, Index af1 = NO_ADJFACE, Index af2 = NO_ADJFACE);
74 std::ostream &operator <<(std::ostream &o, const Edge &e);
82 unsigned int smoothing_group;
84 Triangle(Index v1 = 0, Index v2 = 0, Index v3 = 0);
86 void calculate_normal(const Vertex *vbuffer, bool normalize=false);
87 void calculate_tangent(const Vertex *vbuffer, bool normalize=false);
90 std::ostream &operator <<(std::ostream &o, const Triangle &t);
98 unsigned int smoothing_group;
100 Quad(Index v1 = 0, Index v2 = 0, Index v3 = 0, Index v4 = 0);
102 void calculate_normal(const Vertex *vbuffer, bool normalize=0);
106 //////////////// Geometry Arrays //////////////
107 template <class DataType>
108 class GeometryArray {
113 unsigned int buffer_object; // for OGL VBOs
116 void sync_buffer_object();
119 GeometryArray(bool dynamic = true);
120 GeometryArray(const DataType *data, unsigned long count, bool dynamic = true);
121 GeometryArray(const GeometryArray &ga);
124 GeometryArray &operator =(const GeometryArray &ga);
126 inline void set_data(const DataType *data, unsigned long count);
127 inline const DataType *get_data() const;
128 inline DataType *get_mod_data();
130 inline unsigned long get_count() const;
132 inline void set_dynamic(bool enable);
133 inline bool get_dynamic() const;
135 inline unsigned int get_buffer_object() const;
139 // specialization of template class GeometryArray for type Index
141 class GeometryArray<Index> {
146 unsigned int buffer_object;
149 void sync_buffer_object();
152 GeometryArray(bool dynamic = true);
153 GeometryArray(const Index *data, unsigned long count, bool dynamic = true);
154 GeometryArray(const GeometryArray<Triangle> &tarray); // conversion from triangle data
155 GeometryArray(const GeometryArray &ga);
158 GeometryArray &operator =(const GeometryArray &ga);
160 void set_data(const Index *data, unsigned long count);
161 inline const Index *get_data() const;
162 inline Index *get_mod_data();
164 inline unsigned long get_count() const;
166 inline void set_dynamic(bool enable);
167 inline bool get_dynamic() const;
169 inline unsigned int get_buffer_object() const;
171 friend void tri_to_index_array(GeometryArray<Index> *ia, const GeometryArray<Triangle> &ta);
174 typedef GeometryArray<Vertex> VertexArray;
175 typedef GeometryArray<Triangle> TriangleArray;
176 typedef GeometryArray<Index> IndexArray;
178 ////////////// triangle mesh class ////////////
179 struct VertexStatistics {
184 scalar_t xmin, xmax, ymin, ymax, zmin, zmax;
190 TriangleArray tarray;
192 IndexArray index_graph;
194 GeometryArray<Edge> earray;
196 mutable VertexStatistics vstats;
198 mutable bool vertex_stats_valid;
201 bool index_graph_valid;
202 bool triangle_normals_valid;
203 bool triangle_normals_normalized;
205 void calculate_edges();
206 void calculate_index_graph();
207 void calculate_triangle_normals(bool normalize);
211 TriMesh(const Vertex *vdata, unsigned long vcount, const Triangle *tdata, unsigned long tcount);
213 inline const VertexArray *get_vertex_array() const;
214 inline VertexArray *get_mod_vertex_array();
216 inline const TriangleArray *get_triangle_array() const;
217 inline TriangleArray *get_mod_triangle_array();
219 const IndexArray *get_index_array();
220 const GeometryArray<Edge> *get_edge_array() const;
222 void set_data(const Vertex *vdata, unsigned long vcount, const Triangle *tdata, unsigned long tcount);
224 void calculate_normals_by_index();
225 void calculate_normals();
226 void normalize_normals();
227 void invert_winding();
229 void calculate_tangents();
231 void apply_xform(const Matrix4x4 &xform);
233 void operator +=(const TriMesh *m2);
235 void sort_triangles(Vector3 point, bool hilo=true);
237 VertexStatistics get_vertex_stats() const;
240 std::vector<Edge> *get_contour_edges(const Vector3 &pov_or_dir, bool dir = false);
241 //TriMesh *get_uncapped_shadow_volume(const Vector3 &pov_or_dir, bool dir = false);
242 TriMesh *get_shadow_volume(const Vector3 &pov_or_dir, bool dir = false);
248 void join_tri_mesh(TriMesh *ret, const TriMesh *m1, const TriMesh *m2);
249 TriMesh *join_tri_mesh(const TriMesh *m1, const TriMesh *m2);
250 Vector3 extrude(const Vector3 &vec, scalar_t distance, const Vector3 &pov_or_dir, bool dir);
252 #include "3dgeom.inl"
254 #endif // _3DGEOM_HPP_