]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
Merge branch 'next' of git@gitorious.org:fg/simgear into next
[simgear.git] / simgear / io / sg_binobj.hxx
1 /**
2  * \file sg_binobj.hxx
3  * Routines to read and write the low level (binary) simgear 3d object format.
4  */
5
6 // Written by Curtis Olson, started January 2000.
7 //
8 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #ifndef _SG_BINOBJ_HXX
28 #define _SG_BINOBJ_HXX
29
30
31 #include <plib/sg.h>
32
33 #include <simgear/compiler.h>
34 #include <simgear/constants.h>
35 #include <simgear/math/sg_types.hxx>
36 #include <simgear/math/point3d.hxx>
37 #include <simgear/bucket/newbucket.hxx>
38
39 #include <stdio.h>
40 #include <time.h>
41
42 #include <list>
43 #include <string>
44
45
46
47 /** STL Structure used to store object information */
48 typedef vector < int_list > group_list;
49 typedef group_list::iterator group_list_iterator;
50 typedef group_list::const_iterator const_group_list_iterator;
51
52
53 /** Magic Number for our file format */
54 #define SG_FILE_MAGIC_NUMBER  ( ('S'<<24) + ('G'<<16) + SG_BINOBJ_VERSION )
55
56
57 /**
58  * A class to manipulate the simgear 3d object format.
59  * This class provides functionality to both read and write the binary format.
60  *
61  * Here is a really quick overview of the file syntax:
62  *
63  * - scenery-file: magic, nobjects, object+
64  *
65  * - magic: "TG" + version
66  *
67  * - object: obj_typecode, nproperties, nelements, property+, element+
68  *
69  * - element: nbytes, BYTE+
70  *
71  * - property: prop_typecode, nbytes, BYTE+
72  *
73  * - obj_typecode: bounding sphere | vertices | normals | texcoords |
74  *                 points | triangles | fans | strips 
75  *
76  * - prop_typecode: material_name | ???
77  *
78  * - nelements: USHORT (Gives us 65536 which ought to be enough, right?)
79  *
80  * - nproperties: USHORT
81  *
82  * - *_typecode: CHAR
83  *
84  * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
85  *                    floats = 5461 vertices which is not enough for future
86  *                    growth)
87  *
88  * - vertex: FLOAT, FLOAT, FLOAT
89 */
90 class SGBinObject {
91     unsigned short version;
92
93     SGVec3d gbs_center;
94     float gbs_radius;
95
96     std::vector<SGVec3d> wgs84_nodes;   // vertex list
97     std::vector<SGVec4f> colors;        // color list
98     std::vector<SGVec3f> normals;       // normal list
99     std::vector<SGVec2f> texcoords;     // texture coordinate list
100
101     group_list pts_v;           // points vertex index
102     group_list pts_n;           // points normal index
103     group_list pts_c;           // points color index
104     group_list pts_tc;          // points texture coordinate index
105     string_list pt_materials;   // points materials
106
107     group_list tris_v;          // triangles vertex index
108     group_list tris_n;          // triangles normal index
109     group_list tris_c;          // triangles color index
110     group_list tris_tc;         // triangles texture coordinate index
111     string_list tri_materials;  // triangles materials
112
113     group_list strips_v;        // tristrips vertex index
114     group_list strips_n;        // tristrips normal index
115     group_list strips_c;        // tristrips color index
116     group_list strips_tc;       // tristrips texture coordinate index
117     string_list strip_materials;// tristrips materials
118
119     group_list fans_v;          // fans vertex index
120     group_list fans_n;          // fans normal index
121     group_list fans_c;          // fans color index
122     group_list fans_tc;         // fans texture coordinate index
123     string_list fan_materials;  // fans materials
124
125 public:
126
127     inline unsigned short get_version() const { return version; }
128
129     inline Point3D get_gbs_center() const { return Point3D::fromSGVec3(gbs_center); }
130     inline void set_gbs_center( const Point3D& p ) { gbs_center = p.toSGVec3d(); }
131     inline const SGVec3d& get_gbs_center2() const { return gbs_center; }
132     inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; }
133
134     inline float get_gbs_radius() const { return gbs_radius; }
135     inline void set_gbs_radius( float r ) { gbs_radius = r; }
136
137     inline const std::vector<SGVec3d>& get_wgs84_nodes() const
138     { return wgs84_nodes; }
139     inline void set_wgs84_nodes( const std::vector<SGVec3d>& n )
140     { wgs84_nodes = n; }
141     inline void set_wgs84_nodes( const point_list& n )
142     {
143       wgs84_nodes.resize(n.size());
144       for (unsigned i = 0; i < wgs84_nodes.size(); ++i)
145         wgs84_nodes[i] = n[i].toSGVec3d();
146     }
147
148     inline const std::vector<SGVec4f>& get_colors() const { return colors; }
149     inline void set_colors( const std::vector<SGVec4f>& c ) { colors = c; }
150     inline void set_colors( const point_list& c )
151     {
152       colors.resize(c.size());
153       for (unsigned i = 0; i < colors.size(); ++i)
154         colors[i] = SGVec4f(c[i].toSGVec3f(), 1);
155     }
156
157     inline const std::vector<SGVec3f>& get_normals() const { return normals; }
158     inline void set_normals( const std::vector<SGVec3f>& n ) { normals = n; }
159     inline void set_normals( const point_list& n )
160     {
161       normals.resize(n.size());
162       for (unsigned i = 0; i < normals.size(); ++i)
163         normals[i] = n[i].toSGVec3f();
164     }
165
166     inline const std::vector<SGVec2f>& get_texcoords() const { return texcoords; }
167     inline void set_texcoords( const std::vector<SGVec2f>& t ) { texcoords = t; }
168     inline void set_texcoords( const point_list& t )
169     {
170       texcoords.resize(t.size());
171       for (unsigned i = 0; i < texcoords.size(); ++i)
172         texcoords[i] = t[i].toSGVec2f();
173     }
174
175     inline const group_list& get_pts_v() const { return pts_v; }
176     inline void set_pts_v( const group_list& g ) { pts_v = g; }
177     inline const group_list& get_pts_n() const { return pts_n; }
178     inline void set_pts_n( const group_list& g ) { pts_n = g; }
179     inline const group_list& get_pts_c() const { return pts_c; }
180     inline void set_pts_c( const group_list& g ) { pts_c = g; }
181     inline const group_list& get_pts_tc() const { return pts_tc; }
182     inline void set_pts_tc( const group_list& g ) { pts_tc = g; }
183     inline const string_list& get_pt_materials() const { return pt_materials; }
184     inline void set_pt_materials( const string_list& s ) { pt_materials = s; }
185
186     inline const group_list& get_tris_v() const { return tris_v; }
187     inline void set_tris_v( const group_list& g ) { tris_v = g; }
188     inline const group_list& get_tris_n() const { return tris_n; }
189     inline void set_tris_n( const group_list& g ) { tris_n = g; }
190     inline const group_list& get_tris_c() const { return tris_c; }
191     inline void set_tris_c( const group_list& g ) { tris_c = g; }
192     inline const group_list& get_tris_tc() const { return tris_tc; }
193     inline void set_tris_tc( const group_list& g ) { tris_tc = g; }
194     inline const string_list& get_tri_materials() const { return tri_materials; }
195     inline void set_tri_materials( const string_list& s ) { tri_materials = s; }
196     
197     inline const group_list& get_strips_v() const { return strips_v; }
198     inline void set_strips_v( const group_list& g ) { strips_v = g; }
199     inline const group_list& get_strips_n() const { return strips_n; }
200     inline void set_strips_n( const group_list& g ) { strips_n = g; }
201     inline const group_list& get_strips_c() const { return strips_c; }
202     inline void set_strips_c( const group_list& g ) { strips_c = g; }
203
204     inline const group_list& get_strips_tc() const { return strips_tc; }
205     inline void set_strips_tc( const group_list& g ) { strips_tc = g; }
206     inline const string_list& get_strip_materials() const { return strip_materials; }
207     inline void set_strip_materials( const string_list& s ) { strip_materials = s; }
208     
209     inline const group_list& get_fans_v() const { return fans_v; }
210     inline void set_fans_v( const group_list& g ) { fans_v = g; }
211     inline const group_list& get_fans_n() const { return fans_n; }
212     inline void set_fans_n( const group_list& g ) { fans_n = g; }
213     inline const group_list& get_fans_c() const { return fans_c; }
214     inline void set_fans_c( const group_list& g ) { fans_c = g; }
215
216     inline const group_list& get_fans_tc() const { return fans_tc; }
217     inline void set_fans_tc( const group_list& g ) { fans_tc = g; }
218     inline const string_list& get_fan_materials() const { return fan_materials; }
219     inline void set_fan_materials( const string_list& s ) { fan_materials = s; }
220
221     /**
222      * Read a binary file object and populate the provided structures.
223      * @param file input file name
224      * @return result of read
225      */
226     bool read_bin( const string& file );
227
228     /** 
229      * Write out the structures to a binary file.  We assume that the
230      * groups come to us sorted by material property.  If not, things
231      * don't break, but the result won't be as optimal.
232      * @param base name of output path
233      * @param name name of output file
234      * @param b bucket for object location
235      * @return result of write
236      */
237     bool write_bin( const string& base, const string& name, const SGBucket& b );
238
239     /**
240      * Write out the structures to an ASCII file.  We assume that the
241      * groups come to us sorted by material property.  If not, things
242      * don't break, but the result won't be as optimal.
243      * @param base name of output path
244      * @param name name of output file
245      * @param b bucket for object location
246      * @return result of write
247      */
248     bool write_ascii( const string& base, const string& name,
249                       const SGBucket& b );
250 };
251
252
253 /**
254  * \relates SGBinObject
255  * Calculate the center of a list of points, by taking the halfway
256  * point between the min and max points.
257  * @param wgs84_nodes list of points in wgs84 coordinates
258  * @return center point
259  */
260 Point3D sgCalcCenter( point_list& wgs84_nodes );
261
262
263 /**
264  * \relates SGBinObject
265  * Calculate the bounding sphere of a set of nodes.
266  * Center is the center of the tile and zero elevation.
267  * @param center center of our bounding radius
268  * @param wgs84_nodes list of points in wgs84 coordinates
269  * @return radius
270  */
271 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
272
273
274 #endif // _SG_BINOBJ_HXX