]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
Extended .btg format to support a 'points' primitive.
[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  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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/bucket/newbucket.hxx>
37
38 #include <stdio.h>
39 #include <time.h>
40
41 #include <list>
42 #include STL_STRING
43
44
45
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;
50
51
52 /** Magic Number for our file format */
53 #define SG_FILE_MAGIC_NUMBER  ( ('S'<<24) + ('G'<<16) + SG_BINOBJ_VERSION )
54
55
56 /**
57  * A class to manipulate the simgear 3d object format.
58  * This class provides functionality to both read and write the binary format.
59  *
60  * Here is a really quick overview of the file syntax:
61  *
62  * - scenery-file: magic, nobjects, object+
63  *
64  * - magic: "TG" + version
65  *
66  * - object: obj_typecode, nproperties, nelements, property+, element+
67  *
68  * - element: nbytes, BYTE+
69  *
70  * - property: prop_typecode, nbytes, BYTE+
71  *
72  * - obj_typecode: bounding sphere | vertices | normals | texcoords |
73  *                 points | triangles | fans | strips 
74  *
75  * - prop_typecode: material_name | ???
76  *
77  * - nelements: SHORT (Gives us 65536 which ought to be enough, right?)
78  *
79  * - nproperties: SHORT
80  *
81  * - *_typecode: CHAR
82  *
83  * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
84  *                    floats = 5461 vertices which is not enough for future
85  *                    growth)
86  *
87  * - vertex: FLOAT, FLOAT, FLOAT
88 */
89 class SGBinObject {
90     unsigned short version;
91
92     Point3D gbs_center;
93     float gbs_radius;
94     point_list wgs84_nodes;
95     point_list normals;
96     point_list texcoords;
97     group_list pts_v;
98     string_list pt_materials;
99     group_list tris_v;
100     group_list tris_tc; 
101     string_list tri_materials;
102     group_list strips_v;
103     group_list strips_tc; 
104     string_list strip_materials;
105     group_list fans_v;
106     group_list fans_tc;
107     string_list fan_materials;
108
109 public:
110
111     inline unsigned short get_version() const { return version; }
112
113     inline Point3D get_gbs_center() const { return gbs_center; }
114     inline void set_gbs_center( Point3D p ) { gbs_center = p; }
115
116     inline float get_gbs_radius() const { return gbs_radius; }
117     inline void set_gbs_radius( float r ) { gbs_radius = r; }
118
119     inline point_list get_wgs84_nodes() const { return wgs84_nodes; }
120     inline void set_wgs84_nodes( point_list n ) { wgs84_nodes = n; }
121
122     inline point_list get_normals() const { return normals; }
123     inline void set_normals( point_list n ) { normals = n; }
124
125     inline point_list get_texcoords() const { return texcoords; }
126     inline void set_texcoords( point_list t ) { texcoords = t; }
127
128     inline group_list get_pts_v() const { return pts_v; }
129     inline void set_pts_v( group_list g ) { pts_v = g; }
130     inline string_list get_pt_materials() const { return pt_materials; }
131     inline void set_pt_materials( string_list s ) { pt_materials = s; }
132
133     inline group_list get_tris_v() const { return tris_v; }
134     inline void set_tris_v( group_list g ) { tris_v = g; }
135     inline group_list get_tris_tc() const { return tris_tc; }
136     inline void set_tris_tc( group_list g ) { tris_tc = g; }
137     inline string_list get_tri_materials() const { return tri_materials; }
138     inline void set_tri_materials( string_list s ) { tri_materials = s; }
139     
140     inline group_list get_strips_v() const { return strips_v; }
141     inline void set_strips_v( group_list g ) { strips_v = g; }
142     inline group_list get_strips_tc() const { return strips_tc; }
143     inline void set_strips_tc( group_list g ) { strips_tc = g; }
144     inline string_list get_strip_materials() const { return strip_materials; }
145     inline void set_strip_materials( string_list s ) { strip_materials = s; }
146     
147     inline group_list get_fans_v() const { return fans_v; }
148     inline void set_fans_v( group_list g ) { fans_v = g; }
149     inline group_list get_fans_tc() const { return fans_tc; }
150     inline void set_fans_tc( group_list g ) { fans_tc = g; }
151     inline string_list get_fan_materials() const { return fan_materials; }
152     inline void set_fan_materials( string_list s ) { fan_materials = s; }
153
154     /**
155      * Read a binary file object and populate the provided structures.
156      * @param file input file name
157      * @return result of read
158      */
159     bool read_bin( const string& file );
160
161     /** 
162      * Write out the structures to a binary file.  We assume that the
163      * groups come to us sorted by material property.  If not, things
164      * don't break, but the result won't be as optimal.
165      * @param base name of output path
166      * @param name name of output file
167      * @param b bucket for object location
168      * @return result of write
169      */
170     bool write_bin( const string& base, const string& name, const SGBucket& b );
171
172     /**
173      * Write out the structures to an ASCII file.  We assume that the
174      * groups come to us sorted by material property.  If not, things
175      * don't break, but the result won't be as optimal.
176      * @param base name of output path
177      * @param name name of output file
178      * @param b bucket for object location
179      * @return result of write
180      */
181     bool write_ascii( const string& base, const string& name,
182                       const SGBucket& b );
183 };
184
185
186 /**
187  * \relates SGBinObject
188  * Calculate the bounding sphere of a set of nodes.
189  * Center is the center of the tile and zero elevation.
190  * @param center center of our bounding radius
191  * @param wgs84_nodes list of points in wgs84 coordinates
192  * @return radius
193  */
194 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
195
196
197 #endif // _SG_BINOBJ_HXX