]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
Add initial support for per vertex or per object colors.
[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 colors;
96     point_list normals;
97     point_list texcoords;
98     group_list pts_v;
99     string_list pt_materials;
100     group_list tris_v;
101     group_list tris_tc; 
102     string_list tri_materials;
103     group_list strips_v;
104     group_list strips_tc; 
105     string_list strip_materials;
106     group_list fans_v;
107     group_list fans_tc;
108     string_list fan_materials;
109
110 public:
111
112     inline unsigned short get_version() const { return version; }
113
114     inline Point3D get_gbs_center() const { return gbs_center; }
115     inline void set_gbs_center( Point3D p ) { gbs_center = p; }
116
117     inline float get_gbs_radius() const { return gbs_radius; }
118     inline void set_gbs_radius( float r ) { gbs_radius = r; }
119
120     inline point_list get_wgs84_nodes() const { return wgs84_nodes; }
121     inline void set_wgs84_nodes( point_list n ) { wgs84_nodes = n; }
122
123     inline point_list get_colors() const { return colors; }
124     inline void set_colors( point_list c ) { colors = c; }
125
126     inline point_list get_normals() const { return normals; }
127     inline void set_normals( point_list n ) { normals = n; }
128
129     inline point_list get_texcoords() const { return texcoords; }
130     inline void set_texcoords( point_list t ) { texcoords = t; }
131
132     inline group_list get_pts_v() const { return pts_v; }
133     inline void set_pts_v( group_list g ) { pts_v = g; }
134     inline string_list get_pt_materials() const { return pt_materials; }
135     inline void set_pt_materials( string_list s ) { pt_materials = s; }
136
137     inline group_list get_tris_v() const { return tris_v; }
138     inline void set_tris_v( group_list g ) { tris_v = g; }
139     inline group_list get_tris_tc() const { return tris_tc; }
140     inline void set_tris_tc( group_list g ) { tris_tc = g; }
141     inline string_list get_tri_materials() const { return tri_materials; }
142     inline void set_tri_materials( string_list s ) { tri_materials = s; }
143     
144     inline group_list get_strips_v() const { return strips_v; }
145     inline void set_strips_v( group_list g ) { strips_v = g; }
146     inline group_list get_strips_tc() const { return strips_tc; }
147     inline void set_strips_tc( group_list g ) { strips_tc = g; }
148     inline string_list get_strip_materials() const { return strip_materials; }
149     inline void set_strip_materials( string_list s ) { strip_materials = s; }
150     
151     inline group_list get_fans_v() const { return fans_v; }
152     inline void set_fans_v( group_list g ) { fans_v = g; }
153     inline group_list get_fans_tc() const { return fans_tc; }
154     inline void set_fans_tc( group_list g ) { fans_tc = g; }
155     inline string_list get_fan_materials() const { return fan_materials; }
156     inline void set_fan_materials( string_list s ) { fan_materials = s; }
157
158     /**
159      * Read a binary file object and populate the provided structures.
160      * @param file input file name
161      * @return result of read
162      */
163     bool read_bin( const string& file );
164
165     /** 
166      * Write out the structures to a binary file.  We assume that the
167      * groups come to us sorted by material property.  If not, things
168      * don't break, but the result won't be as optimal.
169      * @param base name of output path
170      * @param name name of output file
171      * @param b bucket for object location
172      * @return result of write
173      */
174     bool write_bin( const string& base, const string& name, const SGBucket& b );
175
176     /**
177      * Write out the structures to an ASCII file.  We assume that the
178      * groups come to us sorted by material property.  If not, things
179      * don't break, but the result won't be as optimal.
180      * @param base name of output path
181      * @param name name of output file
182      * @param b bucket for object location
183      * @return result of write
184      */
185     bool write_ascii( const string& base, const string& name,
186                       const SGBucket& b );
187 };
188
189
190 /**
191  * \relates SGBinObject
192  * Calculate the bounding sphere of a set of nodes.
193  * Center is the center of the tile and zero elevation.
194  * @param center center of our bounding radius
195  * @param wgs84_nodes list of points in wgs84 coordinates
196  * @return radius
197  */
198 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
199
200
201 #endif // _SG_BINOBJ_HXX