3 * Routines to read and write the low level (binary) simgear 3d object format.
6 // Written by Curtis Olson, started January 2000.
8 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #ifndef _SG_BINOBJ_HXX
28 #define _SG_BINOBJ_HXX
30 #include <zlib.h> // for gzFile
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/math/sg_types.hxx>
35 #include <simgear/math/SGMath.hxx>
39 #include <boost/array.hpp>
41 #define MAX_TC_SETS (4)
44 // I really want to pass around fixed length arrays, as the size
45 // has to be hardcoded
46 // but it's a C++0x feature use boost in its absence
47 typedef boost::array<int_list, MAX_TC_SETS> tci_list;
48 typedef boost::array<int_list, MAX_VAS> vai_list;
50 /** STL Structure used to store (integer index) object information */
51 typedef std::vector < int_list > group_list;
52 typedef group_list::iterator group_list_iterator;
53 typedef group_list::const_iterator const_group_list_iterator;
55 /** STL Structure used to store (tc index) object information */
56 typedef std::vector < tci_list > group_tci_list;
57 typedef group_tci_list::iterator group_tci_list_iterator;
58 typedef group_tci_list::const_iterator const_group_tci_list_iterator;
60 /** STL Structure used to store (va index) object information */
61 typedef std::vector < vai_list > group_vai_list;
62 typedef group_vai_list::iterator group_vai_list_iterator;
63 typedef group_vai_list::const_iterator const_group_vai_list_iterator;
70 class SGBinObjectPoint {
85 class SGBinObjectTriangle {
100 for ( unsigned int i=0; i<MAX_TC_SETS; i++ ) {
103 for ( unsigned int i=0; i<MAX_VAS; i++ ) {
113 * A class to manipulate the simgear 3d object format.
114 * This class provides functionality to both read and write the binary format.
116 * Here is a really quick overview of the file syntax:
118 * - scenery-file: magic, nobjects, object+
120 * - magic: "TG" + version
122 * - object: obj_typecode, nproperties, nelements, property+, element+
124 * - element: nbytes, BYTE+
126 * - property: prop_typecode, nbytes, BYTE+
128 * - obj_typecode: bounding sphere | vertices | normals | texcoords |
129 * points | triangles | fans | strips
131 * - prop_typecode: material_name | ???
133 * - nelements: USHORT (Gives us 65536 which ought to be enough, right?)
135 * - nproperties: USHORT
139 * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
140 * floats = 5461 vertices which is not enough for future
143 * - vertex: FLOAT, FLOAT, FLOAT
147 unsigned short version;
152 std::vector<SGVec3d> wgs84_nodes; // vertex list
153 std::vector<SGVec4f> colors; // color list
154 std::vector<SGVec3f> normals; // normal list
155 std::vector<SGVec2f> texcoords; // texture coordinate list
156 std::vector<float> va_flt; // vertex attribute list (floats)
157 std::vector<int> va_int; // vertex attribute list (ints)
159 group_list pts_v; // points vertex index
160 group_list pts_n; // points normal index
161 group_list pts_c; // points color index
162 group_tci_list pts_tcs; // points texture coordinates ( up to 4 sets )
163 group_vai_list pts_vas; // points vertex attributes ( up to 8 sets )
164 string_list pt_materials; // points materials
166 group_list tris_v; // triangles vertex index
167 group_list tris_n; // triangles normal index
168 group_list tris_c; // triangles color index
169 group_tci_list tris_tcs; // triangles texture coordinates ( up to 4 sets )
170 group_vai_list tris_vas; // triangles vertex attributes ( up to 8 sets )
171 string_list tri_materials; // triangles materials
173 group_list strips_v; // tristrips vertex index
174 group_list strips_n; // tristrips normal index
175 group_list strips_c; // tristrips color index
176 group_tci_list strips_tcs; // tristrips texture coordinates ( up to 4 sets )
177 group_vai_list strips_vas; // tristrips vertex attributes ( up to 8 sets )
178 string_list strip_materials; // tristrips materials
180 group_list fans_v; // fans vertex index
181 group_list fans_n; // fans normal index
182 group_list fans_c; // fans color index
183 group_tci_list fans_tcs; // fanss texture coordinates ( up to 4 sets )
184 group_vai_list fans_vas; // fans vertex attributes ( up to 8 sets )
185 string_list fan_materials; // fans materials
187 void read_properties(gzFile fp, int nproperties);
189 void read_object( gzFile fp,
193 group_list& vertices,
196 group_tci_list& texCoords,
197 group_vai_list& vertexAttribs,
198 string_list& materials);
200 void write_header(gzFile fp, int type, int nProps, int nElements);
201 void write_objects(gzFile fp,
203 const group_list& verts,
204 const group_list& normals,
205 const group_list& colors,
206 const group_tci_list& texCoords,
207 const group_vai_list& vertexAttribs,
208 const string_list& materials);
210 unsigned int count_objects(const string_list& materials);
213 inline unsigned short get_version() const { return version; }
215 inline const SGVec3d& get_gbs_center() const { return gbs_center; }
216 inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; }
218 inline float get_gbs_radius() const { return gbs_radius; }
219 inline void set_gbs_radius( float r ) { gbs_radius = r; }
221 inline const std::vector<SGVec3d>& get_wgs84_nodes() const { return wgs84_nodes; }
222 inline void set_wgs84_nodes( const std::vector<SGVec3d>& n ) { wgs84_nodes = n; }
224 inline const std::vector<SGVec4f>& get_colors() const { return colors; }
225 inline void set_colors( const std::vector<SGVec4f>& c ) { colors = c; }
227 inline const std::vector<SGVec3f>& get_normals() const { return normals; }
228 inline void set_normals( const std::vector<SGVec3f>& n ) { normals = n; }
230 inline const std::vector<SGVec2f>& get_texcoords() const { return texcoords; }
231 inline void set_texcoords( const std::vector<SGVec2f>& t ) { texcoords = t; }
234 bool add_point( const SGBinObjectPoint& pt );
235 inline const group_list& get_pts_v() const { return pts_v; }
236 inline const group_list& get_pts_n() const { return pts_n; }
237 inline const group_tci_list& get_pts_tcs() const { return pts_tcs; }
238 inline const group_vai_list& get_pts_vas() const { return pts_vas; }
239 inline const string_list& get_pt_materials() const { return pt_materials; }
242 bool add_triangle( const SGBinObjectTriangle& tri );
243 inline const group_list& get_tris_v() const { return tris_v; }
244 inline const group_list& get_tris_n() const { return tris_n; }
245 inline const group_list& get_tris_c() const { return tris_c; }
246 inline const group_tci_list& get_tris_tcs() const { return tris_tcs; }
247 inline const group_vai_list& get_tris_vas() const { return tris_vas; }
248 inline const string_list& get_tri_materials() const { return tri_materials; }
250 // Strips API (deprecated - read only)
251 inline const group_list& get_strips_v() const { return strips_v; }
252 inline const group_list& get_strips_n() const { return strips_n; }
253 inline const group_list& get_strips_c() const { return strips_c; }
254 inline const group_tci_list& get_strips_tcs() const { return strips_tcs; }
255 inline const group_vai_list& get_strips_vas() const { return strips_vas; }
256 inline const string_list& get_strip_materials() const { return strip_materials; }
258 // Fans API (deprecated - read only )
259 inline const group_list& get_fans_v() const { return fans_v; }
260 inline const group_list& get_fans_n() const { return fans_n; }
261 inline const group_list& get_fans_c() const { return fans_c; }
262 inline const group_tci_list& get_fans_tcs() const { return fans_tcs; }
263 inline const group_vai_list& get_fans_vas() const { return fans_vas; }
264 inline const string_list& get_fan_materials() const { return fan_materials; }
267 * Read a binary file object and populate the provided structures.
268 * @param file input file name
269 * @return result of read
271 bool read_bin( const std::string& file );
274 * Write out the structures to a binary file. We assume that the
275 * groups come to us sorted by material property. If not, things
276 * don't break, but the result won't be as optimal.
277 * @param base name of output path
278 * @param name name of output file
279 * @param b bucket for object location
280 * @return result of write
282 bool write_bin( const std::string& base, const std::string& name, const SGBucket& b );
285 bool write_bin_file(const SGPath& file);
288 * Write out the structures to an ASCII file. We assume that the
289 * groups come to us sorted by material property. If not, things
290 * don't break, but the result won't be as optimal.
291 * @param base name of output path
292 * @param name name of output file
293 * @param b bucket for object location
294 * @return result of write
296 bool write_ascii( const std::string& base, const std::string& name,
300 #endif // _SG_BINOBJ_HXX