X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_binobj.hxx;h=d594113de0ef3b9e8f3b622df2e2441520f0d66e;hb=201cb61f842ef50a19438e3872ba22e588fa1afc;hp=fc9a3de9d615e1878cae697755d66f0b2ebddfd0;hpb=70f495b4e156dfa076b61f0afadbd288afbbba54;p=simgear.git diff --git a/simgear/io/sg_binobj.hxx b/simgear/io/sg_binobj.hxx index fc9a3de9..d594113d 100644 --- a/simgear/io/sg_binobj.hxx +++ b/simgear/io/sg_binobj.hxx @@ -1,8 +1,11 @@ -// sg_binobj.hxx -- routines to read and write low level flightgear 3d objects -// +/** + * \file sg_binobj.hxx + * Routines to read and write the low level (binary) simgear 3d object format. + */ + // Written by Curtis Olson, started January 2000. // -// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org +// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -16,106 +19,222 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ -// #ifndef _SG_BINOBJ_HXX #define _SG_BINOBJ_HXX - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include +#include // for gzFile #include #include - -#include -#include - -#include -#include STL_STRING - #include +#include +#include +#include -typedef vector < int_list > group_list; +/** STL Structure used to store object information */ +typedef std::vector < int_list > group_list; typedef group_list::iterator group_list_iterator; typedef group_list::const_iterator const_group_list_iterator; - -#define SG_FILE_MAGIC_NUMBER ( ('S'<<24) + ('G'<<16) + SG_BINOBJ_VERSION ) - - -/* - scenery-file: magic, nobjects, object+ - magic: "TG" + version - object: obj_typecode, nproperties, nelements, property+, element+ - element: nbytes, BYTE+ - property: prop_typecode, nbytes, BYTE+ - obj_typecode: bounding sphere | vertices | normals | texcoords | triangles | - fans | strips - prop_typecode: material_name | ??? - nelements: SHORT (Gives us 65536 which ought to be enough, right?) - nproperties: SHORT - *_typecode: CHAR - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384 - floats = 5461 vertices which is not enough for future - growth) - vertex: FLOAT, FLOAT, FLOAT +// forward decls +class SGBucket; +class SGPath; + +/** + * A class to manipulate the simgear 3d object format. + * This class provides functionality to both read and write the binary format. + * + * Here is a really quick overview of the file syntax: + * + * - scenery-file: magic, nobjects, object+ + * + * - magic: "TG" + version + * + * - object: obj_typecode, nproperties, nelements, property+, element+ + * + * - element: nbytes, BYTE+ + * + * - property: prop_typecode, nbytes, BYTE+ + * + * - obj_typecode: bounding sphere | vertices | normals | texcoords | + * points | triangles | fans | strips + * + * - prop_typecode: material_name | ??? + * + * - nelements: USHORT (Gives us 65536 which ought to be enough, right?) + * + * - nproperties: USHORT + * + * - *_typecode: CHAR + * + * - nbytes: INTEGER (If we used short here that would mean 65536 bytes = 16384 + * floats = 5461 vertices which is not enough for future + * growth) + * + * - vertex: FLOAT, FLOAT, FLOAT */ - - -// calculate the bounding sphere. Center is the center of the -// tile and zero elevation -double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ); - - -// write out the structures to an ASCII file. We assume that the -// groups come to us sorted by material property. If not, things -// don't break, but the result won't be as optimal. -void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, - Point3D gbs_center, float gbs_radius, - const point_list& wgs84_nodes, const point_list& normals, - const point_list& texcoords, - const group_list& tris_v, const group_list& tris_tc, - const string_list& tri_materials, - const group_list& strips_v, const group_list& strips_tc, - const string_list& strip_materials, - const group_list& fans_v, const group_list& fans_tc, - const string_list& fan_materials ); - - -// read a binary file object and populate the provided structures. -void sgReadBinObj( const string& file, - Point3D &gbs_center, float *gbs_radius, - point_list& wgs84_nodes, point_list& normals, - point_list& texcoords, - group_list& tris_v, group_list& tris_tc, - string_list& tri_materials, - group_list& strips_v, group_list& strips_tc, - string_list& strip_materials, - group_list& fans_v, group_list& fans_tc, - string_list& fan_materials ); - -// write out the structures to a binary file. We assume that the -// groups come to us sorted by material property. If not, things -// don't break, but the result won't be as optimal. -void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, - Point3D gbs_center, float gbs_radius, - const point_list& wgs84_nodes, const point_list& normals, - const point_list& texcoords, - const group_list& tris_v, const group_list& tris_tc, - const string_list& tri_materials, - const group_list& strips_v, const group_list& strips_tc, - const string_list& strip_materials, - const group_list& fans_v, const group_list& fans_tc, - const string_list& fan_materials ); - +class SGBinObject { +private: + unsigned short version; + + SGVec3d gbs_center; + float gbs_radius; + + std::vector wgs84_nodes; // vertex list + std::vector colors; // color list + std::vector normals; // normal list + std::vector texcoords; // texture coordinate list + + group_list pts_v; // points vertex index + group_list pts_n; // points normal index + group_list pts_c; // points color index + group_list pts_tc; // points texture coordinate index + string_list pt_materials; // points materials + + group_list tris_v; // triangles vertex index + group_list tris_n; // triangles normal index + group_list tris_c; // triangles color index + group_list tris_tc; // triangles texture coordinate index + string_list tri_materials; // triangles materials + + group_list strips_v; // tristrips vertex index + group_list strips_n; // tristrips normal index + group_list strips_c; // tristrips color index + group_list strips_tc; // tristrips texture coordinate index + string_list strip_materials;// tristrips materials + + group_list fans_v; // fans vertex index + group_list fans_n; // fans normal index + group_list fans_c; // fans color index + group_list fans_tc; // fans texture coordinate index + string_list fan_materials; // fans materials + + void read_properties(gzFile fp, int nproperties); + + void read_object( gzFile fp, + int obj_type, + int nproperties, + int nelements, + group_list& vertices, + group_list& normals, + group_list& colors, + group_list& texCoords, + string_list& materials); + + void write_header(gzFile fp, int type, int nProps, int nElements); + void write_objects(gzFile fp, int type, const group_list& verts, + const group_list& normals, const group_list& colors, + const group_list& texCoords, const string_list& materials); + + unsigned int count_objects(const string_list& materials); +public: + + inline unsigned short get_version() const { return version; } + + inline const SGVec3d& get_gbs_center() const { return gbs_center; } + inline void set_gbs_center( const SGVec3d& p ) { gbs_center = p; } + + inline float get_gbs_radius() const { return gbs_radius; } + inline void set_gbs_radius( float r ) { gbs_radius = r; } + + inline const std::vector& get_wgs84_nodes() const + { return wgs84_nodes; } + inline void set_wgs84_nodes( const std::vector& n ) + { wgs84_nodes = n; } + + inline const std::vector& get_colors() const { return colors; } + inline void set_colors( const std::vector& c ) { colors = c; } + + inline const std::vector& get_normals() const { return normals; } + inline void set_normals( const std::vector& n ) { normals = n; } + + inline const std::vector& get_texcoords() const { return texcoords; } + inline void set_texcoords( const std::vector& t ) { texcoords = t; } + + inline const group_list& get_pts_v() const { return pts_v; } + inline void set_pts_v( const group_list& g ) { pts_v = g; } + inline const group_list& get_pts_n() const { return pts_n; } + inline void set_pts_n( const group_list& g ) { pts_n = g; } + inline const group_list& get_pts_c() const { return pts_c; } + inline void set_pts_c( const group_list& g ) { pts_c = g; } + inline const group_list& get_pts_tc() const { return pts_tc; } + inline void set_pts_tc( const group_list& g ) { pts_tc = g; } + inline const string_list& get_pt_materials() const { return pt_materials; } + inline void set_pt_materials( const string_list& s ) { pt_materials = s; } + + inline const group_list& get_tris_v() const { return tris_v; } + inline void set_tris_v( const group_list& g ) { tris_v = g; } + inline const group_list& get_tris_n() const { return tris_n; } + inline void set_tris_n( const group_list& g ) { tris_n = g; } + inline const group_list& get_tris_c() const { return tris_c; } + inline void set_tris_c( const group_list& g ) { tris_c = g; } + inline const group_list& get_tris_tc() const { return tris_tc; } + inline void set_tris_tc( const group_list& g ) { tris_tc = g; } + inline const string_list& get_tri_materials() const { return tri_materials; } + inline void set_tri_materials( const string_list& s ) { tri_materials = s; } + + inline const group_list& get_strips_v() const { return strips_v; } + inline void set_strips_v( const group_list& g ) { strips_v = g; } + inline const group_list& get_strips_n() const { return strips_n; } + inline void set_strips_n( const group_list& g ) { strips_n = g; } + inline const group_list& get_strips_c() const { return strips_c; } + inline void set_strips_c( const group_list& g ) { strips_c = g; } + + inline const group_list& get_strips_tc() const { return strips_tc; } + inline void set_strips_tc( const group_list& g ) { strips_tc = g; } + inline const string_list& get_strip_materials() const { return strip_materials; } + inline void set_strip_materials( const string_list& s ) { strip_materials = s; } + + inline const group_list& get_fans_v() const { return fans_v; } + inline void set_fans_v( const group_list& g ) { fans_v = g; } + inline const group_list& get_fans_n() const { return fans_n; } + inline void set_fans_n( const group_list& g ) { fans_n = g; } + inline const group_list& get_fans_c() const { return fans_c; } + inline void set_fans_c( const group_list& g ) { fans_c = g; } + + inline const group_list& get_fans_tc() const { return fans_tc; } + inline void set_fans_tc( const group_list& g ) { fans_tc = g; } + inline const string_list& get_fan_materials() const { return fan_materials; } + inline void set_fan_materials( const string_list& s ) { fan_materials = s; } + + /** + * Read a binary file object and populate the provided structures. + * @param file input file name + * @return result of read + */ + bool read_bin( const std::string& file ); + + /** + * Write out the structures to a binary file. We assume that the + * groups come to us sorted by material property. If not, things + * don't break, but the result won't be as optimal. + * @param base name of output path + * @param name name of output file + * @param b bucket for object location + * @return result of write + */ + bool write_bin( const std::string& base, const std::string& name, const SGBucket& b ); + + + bool write_bin_file(const SGPath& file); + + /** + * Write out the structures to an ASCII file. We assume that the + * groups come to us sorted by material property. If not, things + * don't break, but the result won't be as optimal. + * @param base name of output path + * @param name name of output file + * @param b bucket for object location + * @return result of write + */ + bool write_ascii( const std::string& base, const std::string& name, + const SGBucket& b ); +}; #endif // _SG_BINOBJ_HXX