]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
Unit test for SGBinObj, and fix a bug in large-indice handling the test revealed.
[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 #include <zlib.h> // for gzFile
31
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/math/sg_types.hxx>
35 #include <simgear/math/SGMath.hxx>
36
37 #include <vector>
38 #include <string>
39
40 /** STL Structure used to store object information */
41 typedef std::vector < int_list > group_list;
42 typedef group_list::iterator group_list_iterator;
43 typedef group_list::const_iterator const_group_list_iterator;
44
45 // forward decls
46 class SGBucket;
47 class SGPath;
48
49 /**
50  * A class to manipulate the simgear 3d object format.
51  * This class provides functionality to both read and write the binary format.
52  *
53  * Here is a really quick overview of the file syntax:
54  *
55  * - scenery-file: magic, nobjects, object+
56  *
57  * - magic: "TG" + version
58  *
59  * - object: obj_typecode, nproperties, nelements, property+, element+
60  *
61  * - element: nbytes, BYTE+
62  *
63  * - property: prop_typecode, nbytes, BYTE+
64  *
65  * - obj_typecode: bounding sphere | vertices | normals | texcoords |
66  *                 points | triangles | fans | strips 
67  *
68  * - prop_typecode: material_name | ???
69  *
70  * - nelements: USHORT (Gives us 65536 which ought to be enough, right?)
71  *
72  * - nproperties: USHORT
73  *
74  * - *_typecode: CHAR
75  *
76  * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
77  *                    floats = 5461 vertices which is not enough for future
78  *                    growth)
79  *
80  * - vertex: FLOAT, FLOAT, FLOAT
81 */
82 class SGBinObject {
83 private:
84     unsigned short version;
85
86     SGVec3d gbs_center;
87     float gbs_radius;
88
89     std::vector<SGVec3d> wgs84_nodes;   // vertex list
90     std::vector<SGVec4f> colors;        // color list
91     std::vector<SGVec3f> normals;       // normal list
92     std::vector<SGVec2f> texcoords;     // texture coordinate list
93
94     group_list pts_v;           // points vertex index
95     group_list pts_n;           // points normal index
96     group_list pts_c;           // points color index
97     group_list pts_tc;          // points texture coordinate index
98     string_list pt_materials;   // points materials
99
100     group_list tris_v;          // triangles vertex index
101     group_list tris_n;          // triangles normal index
102     group_list tris_c;          // triangles color index
103     group_list tris_tc;         // triangles texture coordinate index
104     string_list tri_materials;  // triangles materials
105
106     group_list strips_v;        // tristrips vertex index
107     group_list strips_n;        // tristrips normal index
108     group_list strips_c;        // tristrips color index
109     group_list strips_tc;       // tristrips texture coordinate index
110     string_list strip_materials;// tristrips materials
111
112     group_list fans_v;          // fans vertex index
113     group_list fans_n;          // fans normal index
114     group_list fans_c;          // fans color index
115     group_list fans_tc;         // fans texture coordinate index
116     string_list fan_materials;  // fans materials
117
118     void read_properties(gzFile fp, int nproperties);
119     
120     void read_object( gzFile fp,
121                              int obj_type,
122                              int nproperties,
123                              int nelements,
124                              group_list& vertices, 
125                              group_list& normals,
126                              group_list& colors,
127                              group_list& texCoords,
128                              string_list& materials);
129                              
130     void write_header(gzFile fp, int type, int nProps, int nElements);
131     void write_objects(gzFile fp, int type, const group_list& verts,
132         const group_list& normals, const group_list& colors, 
133         const group_list& texCoords, const string_list& materials);
134         
135     unsigned int count_objects(const string_list& materials);
136 public:
137
138     inline unsigned short get_version() const { return version; }
139
140     inline const SGVec3d& get_gbs_center() const { return gbs_center; }
141     inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; }
142
143     inline float get_gbs_radius() const { return gbs_radius; }
144     inline void set_gbs_radius( float r ) { gbs_radius = r; }
145
146     inline const std::vector<SGVec3d>& get_wgs84_nodes() const
147     { return wgs84_nodes; }
148     inline void set_wgs84_nodes( const std::vector<SGVec3d>& n )
149     { wgs84_nodes = n; }
150
151     inline const std::vector<SGVec4f>& get_colors() const { return colors; }
152     inline void set_colors( const std::vector<SGVec4f>& c ) { colors = c; }
153     
154     inline const std::vector<SGVec3f>& get_normals() const { return normals; }
155     inline void set_normals( const std::vector<SGVec3f>& n ) { normals = n; }
156     
157     inline const std::vector<SGVec2f>& get_texcoords() const { return texcoords; }
158     inline void set_texcoords( const std::vector<SGVec2f>& t ) { texcoords = t; }
159     
160     inline const group_list& get_pts_v() const { return pts_v; }
161     inline void set_pts_v( const group_list& g ) { pts_v = g; }
162     inline const group_list& get_pts_n() const { return pts_n; }
163     inline void set_pts_n( const group_list& g ) { pts_n = g; }
164     inline const group_list& get_pts_c() const { return pts_c; }
165     inline void set_pts_c( const group_list& g ) { pts_c = g; }
166     inline const group_list& get_pts_tc() const { return pts_tc; }
167     inline void set_pts_tc( const group_list& g ) { pts_tc = g; }
168     inline const string_list& get_pt_materials() const { return pt_materials; }
169     inline void set_pt_materials( const string_list& s ) { pt_materials = s; }
170
171     inline const group_list& get_tris_v() const { return tris_v; }
172     inline void set_tris_v( const group_list& g ) { tris_v = g; }
173     inline const group_list& get_tris_n() const { return tris_n; }
174     inline void set_tris_n( const group_list& g ) { tris_n = g; }
175     inline const group_list& get_tris_c() const { return tris_c; }
176     inline void set_tris_c( const group_list& g ) { tris_c = g; }
177     inline const group_list& get_tris_tc() const { return tris_tc; }
178     inline void set_tris_tc( const group_list& g ) { tris_tc = g; }
179     inline const string_list& get_tri_materials() const { return tri_materials; }
180     inline void set_tri_materials( const string_list& s ) { tri_materials = s; }
181     
182     inline const group_list& get_strips_v() const { return strips_v; }
183     inline void set_strips_v( const group_list& g ) { strips_v = g; }
184     inline const group_list& get_strips_n() const { return strips_n; }
185     inline void set_strips_n( const group_list& g ) { strips_n = g; }
186     inline const group_list& get_strips_c() const { return strips_c; }
187     inline void set_strips_c( const group_list& g ) { strips_c = g; }
188
189     inline const group_list& get_strips_tc() const { return strips_tc; }
190     inline void set_strips_tc( const group_list& g ) { strips_tc = g; }
191     inline const string_list& get_strip_materials() const { return strip_materials; }
192     inline void set_strip_materials( const string_list& s ) { strip_materials = s; }
193     
194     inline const group_list& get_fans_v() const { return fans_v; }
195     inline void set_fans_v( const group_list& g ) { fans_v = g; }
196     inline const group_list& get_fans_n() const { return fans_n; }
197     inline void set_fans_n( const group_list& g ) { fans_n = g; }
198     inline const group_list& get_fans_c() const { return fans_c; }
199     inline void set_fans_c( const group_list& g ) { fans_c = g; }
200
201     inline const group_list& get_fans_tc() const { return fans_tc; }
202     inline void set_fans_tc( const group_list& g ) { fans_tc = g; }
203     inline const string_list& get_fan_materials() const { return fan_materials; }
204     inline void set_fan_materials( const string_list& s ) { fan_materials = s; }
205
206     /**
207      * Read a binary file object and populate the provided structures.
208      * @param file input file name
209      * @return result of read
210      */
211     bool read_bin( const std::string& file );
212
213     /** 
214      * Write out the structures to a binary file.  We assume that the
215      * groups come to us sorted by material property.  If not, things
216      * don't break, but the result won't be as optimal.
217      * @param base name of output path
218      * @param name name of output file
219      * @param b bucket for object location
220      * @return result of write
221      */
222     bool write_bin( const std::string& base, const std::string& name, const SGBucket& b );
223
224
225     bool write_bin_file(const SGPath& file);
226
227     /**
228      * Write out the structures to an ASCII 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
235      */
236     bool write_ascii( const std::string& base, const std::string& name,
237                       const SGBucket& b );
238 };
239
240 #endif // _SG_BINOBJ_HXX