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