X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_binobj.cxx;h=5f7a34d27c8639f376e0247bdff32213fee27b02;hb=427d6c3316e59aa86bbf4d3e51f522e0aba9c4fd;hp=ee976d6832569cb5e2f6905b5654cf5a4bfea78c;hpb=560c100484e3678770c9589e6067d627ec5554e5;p=simgear.git diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index ee976d68..5f7a34d2 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -31,20 +31,25 @@ #include #include +#include +#include // for system() #include -#include STL_STRING +#include +#include #include #include +#include #include "lowlevel.hxx" #include "sg_binobj.hxx" -SG_USING_STD( string ); -SG_USING_STD( vector ); - +using std::string; +using std::vector; +using std::cout; +using std::endl; enum sgObjectTypes { SG_BOUNDING_SPHERE = 0, @@ -113,51 +118,6 @@ public: } }; - -// calculate the center of a list of points, by taking the halfway -// point between the min and max points. -Point3D sgCalcCenter( point_list& wgs84_nodes ) { - Point3D p, min, max; - - if ( wgs84_nodes.size() ) { - min = max = wgs84_nodes[0]; - } else { - min = max = Point3D( 0 ); - } - - for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) { - p = wgs84_nodes[i]; - - if ( p.x() < min.x() ) { min.setx( p.x() ); } - if ( p.y() < min.y() ) { min.sety( p.y() ); } - if ( p.z() < min.z() ) { min.setz( p.z() ); } - - if ( p.x() > max.x() ) { max.setx( p.x() ); } - if ( p.y() > max.y() ) { max.sety( p.y() ); } - if ( p.z() > max.z() ) { max.setz( p.z() ); } - } - - return ( min + max ) / 2.0; -} - -// calculate the bounding sphere. Center is the center of the -// tile and zero elevation -double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) { - double dist_squared; - double radius_squared = 0; - - for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) { - dist_squared = center.distance3Dsquared( wgs84_nodes[i] ); - if ( dist_squared > radius_squared ) { - radius_squared = dist_squared; - } - } - - return sqrt(radius_squared); -} - - - // read object properties static void read_object( gzFile fp, int obj_type, @@ -364,18 +324,32 @@ bool SGBinObject::read_bin( const string& file ) { #endif // read number of top level objects - short nobjects; - sgReadShort( fp, &nobjects ); + unsigned short nobjects; + if ( version >= 7 ) { + sgReadUShort( fp, &nobjects ); + } else { + short tmp; + sgReadShort( fp, &tmp ); + nobjects = tmp; + } // cout << "Total objects to read = " << nobjects << endl; // read in objects for ( i = 0; i < nobjects; ++i ) { // read object header char obj_type; - short nproperties, nelements; + unsigned short nproperties, nelements; sgReadChar( fp, &obj_type ); - sgReadShort( fp, &nproperties ); - sgReadShort( fp, &nelements ); + if ( version >= 7 ) { + sgReadUShort( fp, &nproperties ); + sgReadUShort( fp, &nelements ); + } else { + short tmp; + sgReadShort( fp, &tmp ); + nproperties = tmp; + sgReadShort( fp, &tmp ); + nelements = tmp; + } // cout << "object " << i << " = " << (int)obj_type << " props = " // << nproperties << " elements = " << nelements << endl; @@ -649,7 +623,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, string material; int start; int end; - short nobjects = 0; + unsigned short nobjects = 0; nobjects++; // for gbs nobjects++; // for vertices nobjects++; // for colors @@ -657,7 +631,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, nobjects++; // for texcoords // points - short npts = 0; + unsigned short npts = 0; start = 0; end = 1; while ( start < (int)pt_materials.size() ) { material = pt_materials[start]; @@ -671,7 +645,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, nobjects += npts; // tris - short ntris = 0; + unsigned short ntris = 0; start = 0; end = 1; while ( start < (int)tri_materials.size() ) { material = tri_materials[start]; @@ -685,7 +659,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, nobjects += ntris; // strips - short nstrips = 0; + unsigned short nstrips = 0; start = 0; end = 1; while ( start < (int)strip_materials.size() ) { material = strip_materials[start]; @@ -699,7 +673,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, nobjects += nstrips; // fans - short nfans = 0; + unsigned short nfans = 0; start = 0; end = 1; while ( start < (int)fan_materials.size() ) { material = fan_materials[start]; @@ -713,42 +687,39 @@ bool SGBinObject::write_bin( const string& base, const string& name, nobjects += nfans; cout << "total top level objects = " << nobjects << endl; - sgWriteShort( fp, nobjects ); + sgWriteUShort( fp, nobjects ); // write bounding sphere sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type - sgWriteShort( fp, 0 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 0 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes - sgdVec3 center; - sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() ); - sgWritedVec3( fp, center ); + sgWritedVec3( fp, gbs_center ); sgWriteFloat( fp, gbs_radius ); // dump vertex list sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type - sgWriteShort( fp, 0 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 0 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) { - SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center); - sgWriteVec3( fp, p.data() ); + sgWriteVec3( fp, toVec3f(wgs84_nodes[i] - gbs_center)); } // dump vertex color list sgWriteChar( fp, (char)SG_COLOR_LIST ); // type - sgWriteShort( fp, 0 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 0 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes for ( i = 0; i < (int)colors.size(); ++i ) { - sgWriteVec4( fp, colors[i].data() ); + sgWriteVec4( fp, colors[i]); } // dump vertex normal list sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type - sgWriteShort( fp, 0 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 0 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteUInt( fp, normals.size() * 3 ); // nbytes char normal[3]; for ( i = 0; i < (int)normals.size(); ++i ) { @@ -761,11 +732,11 @@ bool SGBinObject::write_bin( const string& base, const string& name, // dump texture coordinates sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type - sgWriteShort( fp, 0 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 0 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes for ( i = 0; i < (int)texcoords.size(); ++i ) { - sgWriteVec2( fp, texcoords[i].data() ); + sgWriteVec2( fp, texcoords[i]); } // dump point groups if they exist @@ -786,8 +757,8 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write group headers sgWriteChar( fp, (char)SG_POINTS ); // type - sgWriteShort( fp, 2 ); // nproperties - sgWriteShort( fp, end - start ); // nelements + sgWriteUShort( fp, 2 ); // nproperties + sgWriteUShort( fp, end - start ); // nelements sgWriteChar( fp, (char)SG_MATERIAL ); // property sgWriteUInt( fp, material.length() ); // nbytes @@ -806,19 +777,20 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write strips for ( i = start; i < end; ++i ) { // nbytes - sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) ); + sgWriteUInt( fp, pts_v[i].size() * idx_size + * sizeof(unsigned short) ); for ( j = 0; j < (int)pts_v[i].size(); ++j ) { if ( pts_v.size() ) { - sgWriteShort( fp, (short)pts_v[i][j] ); + sgWriteUShort( fp, (unsigned short)pts_v[i][j] ); } if ( pts_n.size() ) { - sgWriteShort( fp, (short)pts_n[i][j] ); + sgWriteUShort( fp, (unsigned short)pts_n[i][j] ); } if ( pts_c.size() ) { - sgWriteShort( fp, (short)pts_c[i][j] ); + sgWriteUShort( fp, (unsigned short)pts_c[i][j] ); } if ( pts_tc.size() ) { - sgWriteShort( fp, (short)pts_tc[i][j] ); + sgWriteUShort( fp, (unsigned short)pts_tc[i][j] ); } } } @@ -847,8 +819,8 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write group headers sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type - sgWriteShort( fp, 2 ); // nproperties - sgWriteShort( fp, 1 ); // nelements + sgWriteUShort( fp, 2 ); // nproperties + sgWriteUShort( fp, 1 ); // nelements sgWriteChar( fp, (char)SG_MATERIAL ); // property sgWriteUInt( fp, material.length() ); // nbytes @@ -865,22 +837,23 @@ bool SGBinObject::write_bin( const string& base, const string& name, sgWriteChar( fp, idx_mask ); // nbytes - sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) ); + sgWriteUInt( fp, (end - start) * 3 * idx_size + * sizeof(unsigned short) ); // write group for ( i = start; i < end; ++i ) { for ( j = 0; j < 3; ++j ) { if ( tris_v.size() ) { - sgWriteShort( fp, (short)tris_v[i][j] ); + sgWriteUShort( fp, (unsigned short)tris_v[i][j] ); } if ( tris_n.size() ) { - sgWriteShort( fp, (short)tris_n[i][j] ); + sgWriteUShort( fp, (unsigned short)tris_n[i][j] ); } if ( tris_c.size() ) { - sgWriteShort( fp, (short)tris_c[i][j] ); + sgWriteUShort( fp, (unsigned short)tris_c[i][j] ); } if ( tris_tc.size() ) { - sgWriteShort( fp, (short)tris_tc[i][j] ); + sgWriteUShort( fp, (unsigned short)tris_tc[i][j] ); } } } @@ -908,8 +881,8 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write group headers sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type - sgWriteShort( fp, 2 ); // nproperties - sgWriteShort( fp, end - start ); // nelements + sgWriteUShort( fp, 2 ); // nproperties + sgWriteUShort( fp, end - start ); // nelements sgWriteChar( fp, (char)SG_MATERIAL ); // property sgWriteUInt( fp, material.length() ); // nbytes @@ -928,19 +901,20 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write strips for ( i = start; i < end; ++i ) { // nbytes - sgWriteUInt( fp, strips_v[i].size() * idx_size * sizeof(short)); + sgWriteUInt( fp, strips_v[i].size() * idx_size + * sizeof(unsigned short)); for ( j = 0; j < (int)strips_v[i].size(); ++j ) { if ( strips_v.size() ) { - sgWriteShort( fp, (short)strips_v[i][j] ); + sgWriteUShort( fp, (unsigned short)strips_v[i][j] ); } if ( strips_n.size() ) { - sgWriteShort( fp, (short)strips_n[i][j] ); + sgWriteUShort( fp, (unsigned short)strips_n[i][j] ); } if ( strips_c.size() ) { - sgWriteShort( fp, (short)strips_c[i][j] ); + sgWriteUShort( fp, (unsigned short)strips_c[i][j] ); } if ( strips_tc.size() ) { - sgWriteShort( fp, (short)strips_tc[i][j] ); + sgWriteUShort( fp, (unsigned short)strips_tc[i][j] ); } } } @@ -968,8 +942,8 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write group headers sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type - sgWriteShort( fp, 2 ); // nproperties - sgWriteShort( fp, end - start ); // nelements + sgWriteUShort( fp, 2 ); // nproperties + sgWriteUShort( fp, end - start ); // nelements sgWriteChar( fp, (char)SG_MATERIAL ); // property sgWriteUInt( fp, material.length() ); // nbytes @@ -988,19 +962,20 @@ bool SGBinObject::write_bin( const string& base, const string& name, // write fans for ( i = start; i < end; ++i ) { // nbytes - sgWriteUInt( fp, fans_v[i].size() * idx_size * sizeof(short) ); + sgWriteUInt( fp, fans_v[i].size() * idx_size + * sizeof(unsigned short) ); for ( j = 0; j < (int)fans_v[i].size(); ++j ) { if ( fans_v.size() ) { - sgWriteShort( fp, (short)fans_v[i][j] ); + sgWriteUShort( fp, (unsigned short)fans_v[i][j] ); } if ( fans_n.size() ) { - sgWriteShort( fp, (short)fans_n[i][j] ); + sgWriteUShort( fp, (unsigned short)fans_n[i][j] ); } if ( fans_c.size() ) { - sgWriteShort( fp, (short)fans_c[i][j] ); + sgWriteUShort( fp, (unsigned short)fans_c[i][j] ); } if ( fans_tc.size() ) { - sgWriteShort( fp, (short)fans_tc[i][j] ); + sgWriteUShort( fp, (unsigned short)fans_tc[i][j] ); } } } @@ -1108,19 +1083,16 @@ bool SGBinObject::write_ascii( const string& base, const string& name, } // cout << "group = " << start << " to " << end - 1 << endl; - // make a list of points for the group - point_list group_nodes; - group_nodes.clear(); - SGVec3d bs_center; - double bs_radius = 0; - for ( i = start; i < end; ++i ) { - for ( j = 0; j < (int)tris_v[i].size(); ++j ) { - group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ tris_v[i][j] ]) ); - bs_center = sgCalcCenter( group_nodes ).toSGVec3d(); - bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes ); - } - } - + SGSphered d; + for ( i = start; i < end; ++i ) { + for ( j = 0; j < (int)tris_v[i].size(); ++j ) { + d.expandBy(wgs84_nodes[ tris_v[i][j] ]); + } + } + + SGVec3d bs_center = d.getCenter(); + double bs_radius = d.getRadius(); + // write group headers fprintf(fp, "\n"); fprintf(fp, "# usemtl %s\n", material.c_str()); @@ -1159,18 +1131,16 @@ bool SGBinObject::write_ascii( const string& base, const string& name, } // cout << "group = " << start << " to " << end - 1 << endl; - // make a list of points for the group - point_list group_nodes; - group_nodes.clear(); - SGVec3d bs_center; - double bs_radius = 0; - for ( i = start; i < end; ++i ) { - for ( j = 0; j < (int)strips_v[i].size(); ++j ) { - group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ strips_v[i][j] ]) ); - bs_center = sgCalcCenter( group_nodes ).toSGVec3d(); - bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes ); - } - } + + SGSphered d; + for ( i = start; i < end; ++i ) { + for ( j = 0; j < (int)tris_v[i].size(); ++j ) { + d.expandBy(wgs84_nodes[ tris_v[i][j] ]); + } + } + + SGVec3d bs_center = d.getCenter(); + double bs_radius = d.getRadius(); // write group headers fprintf(fp, "\n");