]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
More libCurl version guards.
[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 #include <boost/array.hpp>
40
41 #define MAX_TC_SETS     (4)
42 #define MAX_VAS         (8)
43
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;
49
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;
54
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;
59
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;
64
65
66 // forward decls
67 class SGBucket;
68 class SGPath;
69
70 class SGBinObjectPoint {
71 public:
72     std::string material;
73     int_list    v_list;
74     int_list    n_list;
75     int_list    c_list;
76     
77     void clear( void ) {
78         material = "";
79         v_list.clear();
80         n_list.clear();
81         c_list.clear();
82     };    
83 };
84
85 class SGBinObjectTriangle {
86 public:
87     std::string material;
88     int_list    v_list;
89     int_list    n_list;
90     int_list    c_list;
91     
92     tci_list    tc_list;
93     vai_list    va_list;
94
95     void clear( void ) {
96         material = "";
97         v_list.clear();
98         n_list.clear();
99         c_list.clear();
100         for ( unsigned int i=0; i<MAX_TC_SETS; i++ ) {
101             tc_list[i].clear();
102         }
103         for ( unsigned int i=0; i<MAX_VAS; i++ ) {
104             va_list[i].clear();
105         }
106     };
107     
108 };
109
110
111
112 /**
113  * A class to manipulate the simgear 3d object format.
114  * This class provides functionality to both read and write the binary format.
115  *
116  * Here is a really quick overview of the file syntax:
117  *
118  * - scenery-file: magic, nobjects, object+
119  *
120  * - magic: "TG" + version
121  *
122  * - object: obj_typecode, nproperties, nelements, property+, element+
123  *
124  * - element: nbytes, BYTE+
125  *
126  * - property: prop_typecode, nbytes, BYTE+
127  *
128  * - obj_typecode: bounding sphere | vertices | normals | texcoords |
129  *                 points | triangles | fans | strips 
130  *
131  * - prop_typecode: material_name | ???
132  *
133  * - nelements: USHORT (Gives us 65536 which ought to be enough, right?)
134  *
135  * - nproperties: USHORT
136  *
137  * - *_typecode: CHAR
138  *
139  * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
140  *                    floats = 5461 vertices which is not enough for future
141  *                    growth)
142  *
143  * - vertex: FLOAT, FLOAT, FLOAT
144 */
145 class SGBinObject {
146 private:
147     unsigned short version;
148
149     SGVec3d gbs_center;
150     float gbs_radius;
151
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) 
158     
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
165
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
172
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
179
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
186
187     void read_properties(gzFile fp, int nproperties);
188     
189     void read_object( gzFile fp,
190                              int obj_type,
191                              int nproperties,
192                              int nelements,
193                              group_list& vertices, 
194                              group_list& normals,
195                              group_list& colors,
196                              group_tci_list& texCoords,
197                              group_vai_list& vertexAttribs,
198                              string_list& materials);
199                              
200     void write_header(gzFile fp, int type, int nProps, int nElements);
201     void write_objects(gzFile fp, 
202                        int type, 
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);
209         
210     unsigned int count_objects(const string_list& materials);
211     
212 public:    
213     inline unsigned short get_version() const { return version; }
214
215     inline const SGVec3d& get_gbs_center() const { return gbs_center; }
216     inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; }
217
218     inline float get_gbs_radius() const { return gbs_radius; }
219     inline void set_gbs_radius( float r ) { gbs_radius = r; }
220
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; }
223
224     inline const std::vector<SGVec4f>& get_colors() const { return colors; }
225     inline void set_colors( const std::vector<SGVec4f>& c ) { colors = c; }
226     
227     inline const std::vector<SGVec3f>& get_normals() const { return normals; }
228     inline void set_normals( const std::vector<SGVec3f>& n ) { normals = n; }
229     
230     inline const std::vector<SGVec2f>& get_texcoords() const { return texcoords; }
231     inline void set_texcoords( const std::vector<SGVec2f>& t ) { texcoords = t; }
232     
233     // Points API
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; }
240
241     // Triangles API
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; }
249     
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; }
257
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; }
265
266     /**
267      * Read a binary file object and populate the provided structures.
268      * @param file input file name
269      * @return result of read
270      */
271     bool read_bin( const std::string& file );
272
273     /** 
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
281      */
282     bool write_bin( const std::string& base, const std::string& name, const SGBucket& b );
283
284
285     bool write_bin_file(const SGPath& file);
286
287     /**
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
295      */
296     bool write_ascii( const std::string& base, const std::string& name,
297                       const SGBucket& b );
298 };
299
300 #endif // _SG_BINOBJ_HXX