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
33 #include <simgear/compiler.h>
34 #include <simgear/constants.h>
35 #include <simgear/math/sg_types.hxx>
36 #include <simgear/bucket/newbucket.hxx>
46 /** STL Structure used to store object information */
47 typedef vector < int_list > group_list;
48 typedef group_list::iterator group_list_iterator;
49 typedef group_list::const_iterator const_group_list_iterator;
52 /** Magic Number for our file format */
53 #define SG_FILE_MAGIC_NUMBER ( ('S'<<24) + ('G'<<16) + SG_BINOBJ_VERSION )
57 * A class to manipulate the simgear 3d object format.
58 * This class provides functionality to both read and write the binary format.
60 * Here is a really quick overview of the file syntax:
62 * - scenery-file: magic, nobjects, object+
64 * - magic: "TG" + version
66 * - object: obj_typecode, nproperties, nelements, property+, element+
68 * - element: nbytes, BYTE+
70 * - property: prop_typecode, nbytes, BYTE+
72 * - obj_typecode: bounding sphere | vertices | normals | texcoords |
73 * points | triangles | fans | strips
75 * - prop_typecode: material_name | ???
77 * - nelements: USHORT (Gives us 65536 which ought to be enough, right?)
79 * - nproperties: USHORT
83 * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
84 * floats = 5461 vertices which is not enough for future
87 * - vertex: FLOAT, FLOAT, FLOAT
90 unsigned short version;
95 std::vector<SGVec3d> wgs84_nodes; // vertex list
96 std::vector<SGVec4f> colors; // color list
97 std::vector<SGVec3f> normals; // normal list
98 std::vector<SGVec2f> texcoords; // texture coordinate list
100 group_list pts_v; // points vertex index
101 group_list pts_n; // points normal index
102 group_list pts_c; // points color index
103 group_list pts_tc; // points texture coordinate index
104 string_list pt_materials; // points materials
106 group_list tris_v; // triangles vertex index
107 group_list tris_n; // triangles normal index
108 group_list tris_c; // triangles color index
109 group_list tris_tc; // triangles texture coordinate index
110 string_list tri_materials; // triangles materials
112 group_list strips_v; // tristrips vertex index
113 group_list strips_n; // tristrips normal index
114 group_list strips_c; // tristrips color index
115 group_list strips_tc; // tristrips texture coordinate index
116 string_list strip_materials;// tristrips materials
118 group_list fans_v; // fans vertex index
119 group_list fans_n; // fans normal index
120 group_list fans_c; // fans color index
121 group_list fans_tc; // fans texture coordinate index
122 string_list fan_materials; // fans materials
126 inline unsigned short get_version() const { return version; }
128 inline Point3D get_gbs_center() const { return Point3D::fromSGVec3(gbs_center); }
129 inline void set_gbs_center( const Point3D& p ) { gbs_center = p.toSGVec3d(); }
130 inline const SGVec3d& get_gbs_center2() const { return gbs_center; }
131 inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; }
133 inline float get_gbs_radius() const { return gbs_radius; }
134 inline void set_gbs_radius( float r ) { gbs_radius = r; }
136 inline const std::vector<SGVec3d>& get_wgs84_nodes() const
137 { return wgs84_nodes; }
138 inline void set_wgs84_nodes( const std::vector<SGVec3d>& n )
140 inline void set_wgs84_nodes( const point_list& n )
142 wgs84_nodes.resize(n.size());
143 for (unsigned i = 0; i < wgs84_nodes.size(); ++i)
144 wgs84_nodes[i] = n[i].toSGVec3d();
147 inline const std::vector<SGVec4f>& get_colors() const { return colors; }
148 inline void set_colors( const std::vector<SGVec4f>& c ) { colors = c; }
149 inline void set_colors( const point_list& c )
151 colors.resize(c.size());
152 for (unsigned i = 0; i < colors.size(); ++i)
153 colors[i] = SGVec4f(c[i].toSGVec3f(), 1);
156 inline const std::vector<SGVec3f>& get_normals() const { return normals; }
157 inline void set_normals( const std::vector<SGVec3f>& n ) { normals = n; }
158 inline void set_normals( const point_list& n )
160 normals.resize(n.size());
161 for (unsigned i = 0; i < normals.size(); ++i)
162 normals[i] = n[i].toSGVec3f();
165 inline const std::vector<SGVec2f>& get_texcoords() const { return texcoords; }
166 inline void set_texcoords( const std::vector<SGVec2f>& t ) { texcoords = t; }
167 inline void set_texcoords( const point_list& t )
169 texcoords.resize(t.size());
170 for (unsigned i = 0; i < texcoords.size(); ++i)
171 texcoords[i] = t[i].toSGVec2f();
174 inline const group_list& get_pts_v() const { return pts_v; }
175 inline void set_pts_v( const group_list& g ) { pts_v = g; }
176 inline const group_list& get_pts_n() const { return pts_n; }
177 inline void set_pts_n( const group_list& g ) { pts_n = g; }
178 inline const group_list& get_pts_c() const { return pts_c; }
179 inline void set_pts_c( const group_list& g ) { pts_c = g; }
180 inline const group_list& get_pts_tc() const { return pts_tc; }
181 inline void set_pts_tc( const group_list& g ) { pts_tc = g; }
182 inline const string_list& get_pt_materials() const { return pt_materials; }
183 inline void set_pt_materials( const string_list& s ) { pt_materials = s; }
185 inline const group_list& get_tris_v() const { return tris_v; }
186 inline void set_tris_v( const group_list& g ) { tris_v = g; }
187 inline const group_list& get_tris_n() const { return tris_n; }
188 inline void set_tris_n( const group_list& g ) { tris_n = g; }
189 inline const group_list& get_tris_c() const { return tris_c; }
190 inline void set_tris_c( const group_list& g ) { tris_c = g; }
191 inline const group_list& get_tris_tc() const { return tris_tc; }
192 inline void set_tris_tc( const group_list& g ) { tris_tc = g; }
193 inline const string_list& get_tri_materials() const { return tri_materials; }
194 inline void set_tri_materials( const string_list& s ) { tri_materials = s; }
196 inline const group_list& get_strips_v() const { return strips_v; }
197 inline void set_strips_v( const group_list& g ) { strips_v = g; }
198 inline const group_list& get_strips_n() const { return strips_n; }
199 inline void set_strips_n( const group_list& g ) { strips_n = g; }
200 inline const group_list& get_strips_c() const { return strips_c; }
201 inline void set_strips_c( const group_list& g ) { strips_c = g; }
203 inline const group_list& get_strips_tc() const { return strips_tc; }
204 inline void set_strips_tc( const group_list& g ) { strips_tc = g; }
205 inline const string_list& get_strip_materials() const { return strip_materials; }
206 inline void set_strip_materials( const string_list& s ) { strip_materials = s; }
208 inline const group_list& get_fans_v() const { return fans_v; }
209 inline void set_fans_v( const group_list& g ) { fans_v = g; }
210 inline const group_list& get_fans_n() const { return fans_n; }
211 inline void set_fans_n( const group_list& g ) { fans_n = g; }
212 inline const group_list& get_fans_c() const { return fans_c; }
213 inline void set_fans_c( const group_list& g ) { fans_c = g; }
215 inline const group_list& get_fans_tc() const { return fans_tc; }
216 inline void set_fans_tc( const group_list& g ) { fans_tc = g; }
217 inline const string_list& get_fan_materials() const { return fan_materials; }
218 inline void set_fan_materials( const string_list& s ) { fan_materials = s; }
221 * Read a binary file object and populate the provided structures.
222 * @param file input file name
223 * @return result of read
225 bool read_bin( const string& file );
228 * Write out the structures to a binary file. We assume that the
229 * groups come to us sorted by material property. If not, things
230 * don't break, but the result won't be as optimal.
231 * @param base name of output path
232 * @param name name of output file
233 * @param b bucket for object location
234 * @return result of write
236 bool write_bin( const string& base, const string& name, const SGBucket& b );
239 * Write out the structures to an ASCII file. We assume that the
240 * groups come to us sorted by material property. If not, things
241 * don't break, but the result won't be as optimal.
242 * @param base name of output path
243 * @param name name of output file
244 * @param b bucket for object location
245 * @return result of write
247 bool write_ascii( const string& base, const string& name,
253 * \relates SGBinObject
254 * Calculate the center of a list of points, by taking the halfway
255 * point between the min and max points.
256 * @param wgs84_nodes list of points in wgs84 coordinates
257 * @return center point
259 Point3D sgCalcCenter( point_list& wgs84_nodes );
263 * \relates SGBinObject
264 * Calculate the bounding sphere of a set of nodes.
265 * Center is the center of the tile and zero elevation.
266 * @param center center of our bounding radius
267 * @param wgs84_nodes list of points in wgs84 coordinates
270 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
273 #endif // _SG_BINOBJ_HXX