]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_binobj.hxx
Simplified calling interface to read/write obj functions.
[simgear.git] / simgear / io / sg_binobj.hxx
1 // sg_binobj.hxx -- routines to read and write low level flightgear 3d objects
2 //
3 // Written by Curtis Olson, started January 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 //
23
24
25 #ifndef _SG_BINOBJ_HXX
26 #define _SG_BINOBJ_HXX
27
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <plib/sg.h>
34
35 #include <simgear/compiler.h>
36 #include <simgear/constants.h>
37 #include <simgear/math/sg_types.hxx>
38 #include <simgear/bucket/newbucket.hxx>
39
40 #include <stdio.h>
41 #include <time.h>
42
43 #include <list>
44 #include STL_STRING
45
46
47
48 typedef vector < int_list > group_list;
49 typedef group_list::iterator group_list_iterator;
50 typedef group_list::const_iterator const_group_list_iterator;
51
52
53 #define SG_FILE_MAGIC_NUMBER  ( ('S'<<24) + ('G'<<16) + SG_BINOBJ_VERSION )
54
55
56 class SGBinObject {
57     Point3D gbs_center;
58     float gbs_radius;
59     point_list wgs84_nodes;
60     point_list normals;
61     point_list texcoords;
62     group_list tris_v;
63     group_list tris_tc; 
64     string_list tri_materials;
65     group_list strips_v;
66     group_list strips_tc; 
67     string_list strip_materials;
68     group_list fans_v;
69     group_list fans_tc;
70     string_list fan_materials;
71
72 public:
73
74     inline Point3D get_gbs_center() const { return gbs_center; }
75     inline void set_gbs_center( Point3D p ) { gbs_center = p; }
76
77     inline float get_gbs_radius() const { return gbs_radius; }
78     inline void set_gbs_radius( float r ) { gbs_radius = r; }
79
80     inline point_list get_wgs84_nodes() const { return wgs84_nodes; }
81     inline void set_wgs84_nodes( point_list n ) { wgs84_nodes = n; }
82
83     inline point_list get_normals() const { return normals; }
84     inline void set_normals( point_list n ) { normals = n; }
85
86     inline point_list get_texcoords() const { return texcoords; }
87     inline void set_texcoords( point_list t ) { texcoords = t; }
88
89     inline group_list get_tris_v() const { return tris_v; }
90     inline void set_tris_v( group_list g ) { tris_v = g; }
91     inline group_list get_tris_tc() const { return tris_tc; }
92     inline void set_tris_tc( group_list g ) { tris_tc = g; }
93     inline string_list get_tri_materials() const { return tri_materials; }
94     inline void set_tri_materials( string_list s ) { tri_materials = s; }
95     
96     inline group_list get_strips_v() const { return strips_v; }
97     inline void set_strips_v( group_list g ) { strips_v = g; }
98     inline group_list get_strips_tc() const { return strips_tc; }
99     inline void set_strips_tc( group_list g ) { strips_tc = g; }
100     inline string_list get_strip_materials() const { return strip_materials; }
101     inline void set_strip_materials( string_list s ) { strip_materials = s; }
102     
103     inline group_list get_fans_v() const { return fans_v; }
104     inline void set_fans_v( group_list g ) { fans_v = g; }
105     inline group_list get_fans_tc() const { return fans_tc; }
106     inline void set_fans_tc( group_list g ) { fans_tc = g; }
107     inline string_list get_fan_materials() const { return fan_materials; }
108     inline void set_fan_materials( string_list s ) { fan_materials = s; }
109 };
110
111
112 /*
113    scenery-file: magic, nobjects, object+
114    magic: "TG" + version
115    object: obj_typecode, nproperties, nelements, property+, element+
116    element: nbytes, BYTE+
117    property: prop_typecode, nbytes, BYTE+
118    obj_typecode: bounding sphere | vertices | normals | texcoords | triangles |
119                 fans | strips 
120    prop_typecode: material_name | ???
121    nelements: SHORT (Gives us 65536 which ought to be enough, right?)
122    nproperties: SHORT
123    *_typecode: CHAR
124    nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384
125                     floats = 5461 vertices which is not enough for future
126                     growth)
127    vertex: FLOAT, FLOAT, FLOAT
128 */
129
130
131 // calculate the bounding sphere.  Center is the center of the
132 // tile and zero elevation
133 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
134
135
136 // read a binary file object and populate the provided structures.
137 bool sgReadBinObj( const string& file, SGBinObject* obj );
138
139 // write out the structures to a binary file.  We assume that the
140 // groups come to us sorted by material property.  If not, things
141 // don't break, but the result won't be as optimal.
142 bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
143                     const SGBinObject* obj );
144
145 // write out the structures to an ASCII file.  We assume that the
146 // groups come to us sorted by material property.  If not, things
147 // don't break, but the result won't be as optimal.
148 bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
149                       SGBinObject* obj );
150
151
152 #endif // _SG_BINOBJ_HXX