]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_binobj.cxx
Tweaks.
[simgear.git] / simgear / io / sg_binobj.cxx
index 41c4707243a66d2b610f362463934a6e60f26eab..237f24d9da98140386510900b62fb48089bc8e1a 100644 (file)
@@ -30,9 +30,8 @@
 
 #include <stdio.h>
 #include <time.h>
-#include <zlib.h>
 
-#include <list>
+#include <vector>
 #include STL_STRING
 
 #include <simgear/bucket/newbucket.hxx>
 #include "lowlevel.hxx"
 #include "sg_binobj.hxx"
 
-#ifdef _MSC_VER
-#  include <Win32/mkdir.hpp>
+
+SG_USING_STD( string );
+SG_USING_STD( vector );
+
+#if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
+SG_USING_STD( cout );
+SG_USING_STD( endl );
 #endif
 
-FG_USING_STD( cout );
-FG_USING_STD( endl );
 
 enum {
     SG_BOUNDING_SPHERE = 0,
@@ -64,6 +66,46 @@ enum {
 } tgPropertyTypes;
 
 
+class sgSimpleBuffer {
+
+private:
+
+    char *ptr;
+    unsigned int size;
+
+public:
+
+    inline sgSimpleBuffer( unsigned int s )
+    {
+       size = 1;
+       while ( size < s ) {
+           size *= 2;
+       }
+       cout << "Creating a new buffer of size = " << size << endl;
+       ptr = new char[size];
+    }
+
+    inline ~sgSimpleBuffer() {
+       delete [] ptr;
+    }
+
+    inline unsigned int get_size() const { return size; }
+    inline char *get_ptr() const { return ptr; }
+    inline void resize( unsigned int s ) {
+       if ( s > size ) {
+           if ( ptr != NULL ) {
+               delete [] ptr;
+           }
+           while ( size < s ) {
+               size *= 2;
+           }
+           cout << "resizing buffer to size = " << size << endl;
+           ptr = new char[size];
+       }
+    }
+};
+
+
 // calculate the center of a list of points, by taking the halfway
 // point between the min and max points.
 static Point3D calc_center( point_list& wgs84_nodes ) {
@@ -107,220 +149,18 @@ 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.
-bool 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 )
-{
-    Point3D p;
-    int i, j;
-
-    string dir = base + "/" + b.gen_base_path();
-    string command = "mkdir -p " + dir;
-#ifdef _MSC_VER
-    fg_mkdir( dir.c_str() );
-#else
-    system(command.c_str());
-#endif
-
-    // string file = dir + "/" + b.gen_index_str();
-    string file = dir + "/" + name;
-    cout << "Output file = " << file << endl;
-
-    FILE *fp;
-    if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
-       cout << "ERROR: opening " << file << " for writing!" << endl;
-       return false;
-    }
-
-    cout << "triangles size = " << tris_v.size() << "  tri_materials = " 
-        << tri_materials.size() << endl;
-    cout << "strips size = " << strips_v.size() << "  strip_materials = " 
-        << strip_materials.size() << endl;
-    cout << "fans size = " << fans_v.size() << "  fan_materials = " 
-        << fan_materials.size() << endl;
-
-    cout << "points = " << wgs84_nodes.size() << endl;
-    cout << "tex coords = " << texcoords.size() << endl;
-    // write headers
-    fprintf(fp, "# FGFS Scenery\n");
-    fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
-
-    time_t calendar_time = time(NULL);
-    struct tm *local_tm;
-    local_tm = localtime( &calendar_time );
-    char time_str[256];
-    strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
-    fprintf(fp, "# Created %s\n", time_str );
-    fprintf(fp, "\n");
-
-    // write bounding sphere
-    fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
-           gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
-    fprintf(fp, "\n");
-
-    // dump vertex list
-    fprintf(fp, "# vertex list\n");
-    for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
-       p = wgs84_nodes[i] - gbs_center;
-       
-       fprintf(fp,  "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
-    }
-    fprintf(fp, "\n");
-
-    fprintf(fp, "# vertex normal list\n");
-    for ( i = 0; i < (int)normals.size(); ++i ) {
-       p = normals[i];
-       fprintf(fp,  "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
-    }
-    fprintf(fp, "\n");
-
-    // dump texture coordinates
-    fprintf(fp, "# texture coordinate list\n");
-    for ( i = 0; i < (int)texcoords.size(); ++i ) {
-       p = texcoords[i];
-       fprintf(fp,  "vt %.5f %.5f\n", p.x(), p.y() );
-    }
-    fprintf(fp, "\n");
-
-    // dump individual triangles if they exist
-    if ( tris_v.size() > 0 ) {
-       fprintf(fp, "# triangle groups\n");
-
-       int start = 0;
-       int end = 1;
-       string material;
-       while ( start < (int)tri_materials.size() ) {
-           // find next group
-           material = tri_materials[start];
-           while ( (end < (int)tri_materials.size()) && 
-                   (material == tri_materials[end]) )
-           {
-               // cout << "end = " << end << endl;
-               end++;
-           }
-           // cout << "group = " << start << " to " << end - 1 << endl;
-
-           // make a list of points for the group
-           point_list group_nodes;
-           group_nodes.clear();
-           Point3D 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( wgs84_nodes[ tris_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
-                   bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
-               }
-           }
-
-           // write group headers
-           fprintf(fp, "\n");
-           fprintf(fp, "# usemtl %s\n", material.c_str());
-           fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
-                   bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
-
-           // write groups
-           for ( i = start; i < end; ++i ) {
-               fprintf(fp, "f");
-               for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
-                   fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
-               }
-               fprintf(fp, "\n");
-           }
-
-           start = end;
-           end = start + 1;
-       }
-    }
-
-    // dump triangle groups
-    if ( strips_v.size() > 0 ) {
-       fprintf(fp, "# triangle strips\n");
-
-       int start = 0;
-       int end = 1;
-       string material;
-       while ( start < (int)strip_materials.size() ) {
-           // find next group
-           material = strip_materials[start];
-           while ( (end < (int)strip_materials.size()) && 
-                   (material == strip_materials[end]) )
-               {
-                   // cout << "end = " << end << endl;
-                   end++;
-               }
-           // cout << "group = " << start << " to " << end - 1 << endl;
-
-           // make a list of points for the group
-           point_list group_nodes;
-           group_nodes.clear();
-           Point3D 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( wgs84_nodes[ strips_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
-                   bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
-               }
-           }
-
-           // write group headers
-           fprintf(fp, "\n");
-           fprintf(fp, "# usemtl %s\n", material.c_str());
-           fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
-                   bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
-
-           // write groups
-           for ( i = start; i < end; ++i ) {
-               fprintf(fp, "ts");
-               for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
-                   fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
-               }
-               fprintf(fp, "\n");
-           }
-           
-           start = end;
-           end = start + 1;
-       }
-    }
-
-    // close the file
-    fclose(fp);
-
-    command = "gzip --force --best " + file;
-    system(command.c_str());
-
-    return true;
-}
-
-
 // read a binary file and populate the provided structures.
-bool 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 )
-{
+bool SGBinObject::read_bin( const string& file ) {
     Point3D p;
     int i, j, k;
     char material[256];
+    unsigned int nbytes;
+    static sgSimpleBuffer buf( 32768 );  // 32 Kb
 
     // zero out structures
+    gbs_center = Point3D( 0 );
+    gbs_radius = 0.0;
+
     wgs84_nodes.clear();
     normals.clear();
     texcoords.clear();
@@ -337,14 +177,13 @@ bool sgReadBinObj( const string& file,
     fans_tc.clear();
     fan_materials.clear();
    
-    cout << "Loading binary input file = " << file << endl;
-
     gzFile fp;
     if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
        string filegz = file + ".gz";
        if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
-           cout << "ERROR: opening " << file << " or " << filegz
-                << "for reading!" << endl;
+           // cout << "ERROR: opening " << file << " or " << filegz
+           //      << "for reading!" << endl;
+
            return false;
        }
     }
@@ -352,15 +191,18 @@ bool sgReadBinObj( const string& file,
     sgClearReadError();
 
     // read headers
-    int header, version;
-    sgReadInt( fp, &header );
+    unsigned int header;
+    sgReadUInt( fp, &header );
     if ( ((header & 0xFF000000) >> 24) == 'S' &&
         ((header & 0x00FF0000) >> 16) == 'G' ) {
-       cout << "Good header" << endl;
+       // cout << "Good header" << endl;
        // read file version
        version = (header & 0x0000FFFF);
-       cout << "File version = " << version << endl;
+       // cout << "File version = " << version << endl;
     } else {
+       // close the file before we return
+       gzclose(fp);
+
        return false;
     }
 
@@ -371,12 +213,12 @@ bool sgReadBinObj( const string& file,
     local_tm = localtime( &calendar_time );
     char time_str[256];
     strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
-    cout << "File created on " << time_str << endl;
+    // cout << "File created on " << time_str << endl;
 
     // read number of top level objects
     short nobjects;
     sgReadShort( fp, &nobjects );
-    cout << "Total objects to read = " << nobjects << endl;
+    // cout << "Total objects to read = " << nobjects << endl;
 
     // read in objects
     for ( i = 0; i < nobjects; ++i ) {
@@ -387,8 +229,8 @@ bool sgReadBinObj( const string& file,
        sgReadShort( fp, &nproperties );
        sgReadShort( fp, &nelements );
 
-       cout << "object " << i << " = " << (int)obj_type << " props = "
-            << nproperties << " elements = " << nelements << endl;
+       // cout << "object " << i << " = " << (int)obj_type << " props = "
+       //      << nproperties << " elements = " << nelements << endl;
            
        if ( obj_type == SG_BOUNDING_SPHERE ) {
            // read bounding sphere properties
@@ -396,31 +238,37 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
 
            // read bounding sphere elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
 
                double *dptr = (double *)ptr;
+               if ( sgIsBigEndian() ) {
+                   sgEndianSwap( (unsigned long long *)&(dptr[0]) );
+                   sgEndianSwap( (unsigned long long *)&(dptr[1]) );
+                   sgEndianSwap( (unsigned long long *)&(dptr[2]) );
+               }
                gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
-               cout << "Center = " << gbs_center << endl;
+               // cout << "Center = " << gbs_center << endl;
                ptr += sizeof(double) * 3;
                
                float *fptr = (float *)ptr;
-               *gbs_radius = fptr[0];
-               cout << "Bounding radius = " << *gbs_radius << endl;
+               if ( sgIsBigEndian() ) {
+                   sgEndianSwap( (unsigned int *)fptr );
+               }
+               gbs_radius = fptr[0];
+               // cout << "Bounding radius = " << gbs_radius << endl;
            }
        } else if ( obj_type == SG_VERTEX_LIST ) {
            // read vertex list properties
@@ -428,27 +276,30 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
 
            // read vertex list elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(float) * 3);
                float *fptr = (float *)ptr;
                for ( k = 0; k < count; ++k ) {
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
+                   }
                    p = Point3D( fptr[0], fptr[1], fptr[2] );
-                   cout << "node = " << p << endl;
+                   // cout << "node = " << p << endl;
                    wgs84_nodes.push_back( p );
                    fptr += 3;
                }
@@ -459,28 +310,31 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
 
            // read normal list elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               unsigned char *ptr = (unsigned char *)(buf.get_ptr());
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / 3;
                for ( k = 0; k < count; ++k ) {
-                   p = Point3D( ptr[0] / 128.0 - 1.0,
-                                ptr[1] / 128.0 - 1.0,
-                                ptr[2] / 128.0 - 1.0 );
-                   cout << "normal = " << p << endl;
+                    sgdVec3 normal;
+                    sgdSetVec3( normal,
+                               (ptr[0]) / 127.5 - 1.0,
+                               (ptr[1]) / 127.5 - 1.0,
+                               (ptr[2]) / 127.5 - 1.0 );
+                    sgdNormalizeVec3( normal );
+
+                   p = Point3D( normal[0], normal[1], normal[2] );
+                   // cout << "normal = " << p << endl;
                    normals.push_back( p );
                    ptr += 3;
                }
@@ -491,27 +345,29 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
 
            // read texcoord list elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(float) * 2);
                float *fptr = (float *)ptr;
                for ( k = 0; k < count; ++k ) {
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                   }
                    p = Point3D( fptr[0], fptr[1], 0 );
-                   cout << "texcoord = " << p << endl;
+                   // cout << "texcoord = " << p << endl;
                    texcoords.push_back( p );
                    fptr += 2;
                }
@@ -522,38 +378,40 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                if ( prop_type == SG_MATERIAL ) {
                    strncpy( material, ptr, nbytes );
                    material[nbytes] = '\0';
-                   cout << "material type = " << material << endl;
+                   // cout << "material type = " << material << endl;
                }
            }
 
            // read triangle face elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(short) * 2);
                short *sptr = (short *)ptr;
                int_list vs, tcs;
                vs.clear(); tcs.clear();
                for ( k = 0; k < count; ++k ) {
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned short *)&(sptr[0]) );
+                       sgEndianSwap( (unsigned short *)&(sptr[1]) );
+                   }
                    vs.push_back( sptr[0] );
                    tcs.push_back( sptr[1] );
-                   cout << sptr[0] << "/" << sptr[1] << " ";
+                   // cout << sptr[0] << "/" << sptr[1] << " ";
                    sptr += 2;
                }
-               cout << endl;
+               // cout << endl;
                tris_v.push_back( vs );
                tris_tc.push_back( tcs );
                tri_materials.push_back( material );
@@ -564,38 +422,40 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                if ( prop_type == SG_MATERIAL ) {
                    strncpy( material, ptr, nbytes );
                    material[nbytes] = '\0';
-                   cout << "material type = " << material << endl;
+                   // cout << "material type = " << material << endl;
                }
            }
 
            // read triangle strip elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(short) * 2);
                short *sptr = (short *)ptr;
                int_list vs, tcs;
                vs.clear(); tcs.clear();
                for ( k = 0; k < count; ++k ) {
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned short *)&(sptr[0]) );
+                       sgEndianSwap( (unsigned short *)&(sptr[1]) );
+                   }
                    vs.push_back( sptr[0] );
                    tcs.push_back( sptr[1] );
-                   cout << sptr[0] << "/" << sptr[1] << " ";
+                   // cout << sptr[0] << "/" << sptr[1] << " ";
                    sptr += 2;
                }
-               cout << endl;
+               // cout << endl;
                strips_v.push_back( vs );
                strips_tc.push_back( tcs );
                strip_materials.push_back( material );
@@ -606,38 +466,40 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                if ( prop_type == SG_MATERIAL ) {
                    strncpy( material, ptr, nbytes );
                    material[nbytes] = '\0';
-                   cout << "material type = " << material << endl;
+                   // cout << "material type = " << material << endl;
                }
            }
 
            // read triangle fan elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(short) * 2);
                short *sptr = (short *)ptr;
                int_list vs, tcs;
                vs.clear(); tcs.clear();
                for ( k = 0; k < count; ++k ) {
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned short *)&(sptr[0]) );
+                       sgEndianSwap( (unsigned short *)&(sptr[1]) );
+                   }
                    vs.push_back( sptr[0] );
                    tcs.push_back( sptr[1] );
-                   cout << sptr[0] << "/" << sptr[1] << " ";
+                   // cout << sptr[0] << "/" << sptr[1] << " ";
                    sptr += 2;
                }
-               cout << endl;
+               // cout << endl;
                fans_v.push_back( vs );
                fans_tc.push_back( tcs );
                fan_materials.push_back( material );
@@ -650,21 +512,19 @@ bool sgReadBinObj( const string& file,
                char prop_type;
                sgReadChar( fp, &prop_type );
 
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "property size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "property size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
 
            // read elements
            for ( j = 0; j < nelements; ++j ) {
-               int nbytes;
-               sgReadInt( fp, &nbytes );
-               cout << "element size = " << nbytes << endl;
-               char buf[nbytes];
-               char *ptr = buf;
+               sgReadUInt( fp, &nbytes );
+               // cout << "element size = " << nbytes << endl;
+               if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
+               char *ptr = buf.get_ptr();
                sgReadBytes( fp, nbytes, ptr );
            }
        }
@@ -685,16 +545,8 @@ bool sgReadBinObj( const 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.
-bool 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 )
+bool SGBinObject::write_bin( const string& base, const string& name,
+                            const SGBucket& b )
 {
     Point3D p;
     sgVec2 t;
@@ -704,7 +556,7 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     string dir = base + "/" + b.gen_base_path();
     string command = "mkdir -p " + dir;
 #ifdef _MSC_VER
-    fg_mkdir( dir.c_str() );
+    system( (string("mkdir ") + dir).c_str() );
 #else
     system(command.c_str());
 #endif
@@ -731,7 +583,7 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     cout << "tex coords = " << texcoords.size() << endl;
 
     // write header magic
-    sgWriteInt( fp, SG_FILE_MAGIC_NUMBER );
+    sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
     time_t calendar_time = time(NULL);
     sgWriteLong( fp, (long int)calendar_time );
 
@@ -795,7 +647,7 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     sgWriteShort( fp, 0 );                             // nproperties
     sgWriteShort( fp, 1 );                             // nelements
 
-    sgWriteInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
+    sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
     sgdVec3 center;
     sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
     sgWritedVec3( fp, center );
@@ -805,7 +657,7 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     sgWriteChar( fp, (char)SG_VERTEX_LIST );             // type
     sgWriteShort( fp, 0 );                              // nproperties
     sgWriteShort( fp, 1 );                              // nelements
-    sgWriteInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
+    sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
     for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
        p = wgs84_nodes[i] - gbs_center;
        sgSetVec3( pt, p.x(), p.y(), p.z() );
@@ -816,13 +668,13 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     sgWriteChar( fp, (char)SG_NORMAL_LIST );            // type
     sgWriteShort( fp, 0 );                             // nproperties
     sgWriteShort( fp, 1 );                             // nelements
-    sgWriteInt( fp, normals.size() * 3 );               // nbytes
+    sgWriteUInt( fp, normals.size() * 3 );              // nbytes
     char normal[3];
     for ( i = 0; i < (int)normals.size(); ++i ) {
        p = normals[i];
-       normal[0] = (char)((p.x() + 1.0) * 128);
-       normal[1] = (char)((p.y() + 1.0) * 128);
-       normal[2] = (char)((p.z() + 1.0) * 128);
+       normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
+       normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
+       normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
        sgWriteBytes( fp, 3, normal );
     }
 
@@ -830,7 +682,7 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
     sgWriteChar( fp, (char)SG_TEXCOORD_LIST );          // type
     sgWriteShort( fp, 0 );                             // nproperties
     sgWriteShort( fp, 1 );                             // nelements
-    sgWriteInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
+    sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
     for ( i = 0; i < (int)texcoords.size(); ++i ) {
        p = texcoords[i];
        sgSetVec2( t, p.x(), p.y() );
@@ -859,10 +711,10 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
            sgWriteShort( fp, 1 );                      // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
-           sgWriteInt( fp, material.length() );        // nbytes
+           sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
-           sgWriteInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
+           sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
 
            // write group
            for ( i = start; i < end; ++i ) {
@@ -898,14 +750,14 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
            sgWriteShort( fp, 1 );                       // nproperties
            sgWriteShort( fp, end - start );             // nelements
 
-           sgWriteChar( fp, (char)SG_MATERIAL );       // property
-           sgWriteInt( fp, material.length() );        // nbytes
+           sgWriteChar( fp, (char)SG_MATERIAL );        // property
+           sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
            // write strips
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteInt( fp, strips_v[i].size() * 2 * sizeof(short) );
+               sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
                for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
                    sgWriteShort( fp, (short)strips_v[i][j] );
                    sgWriteShort( fp, (short)strips_tc[i][j] );
@@ -939,13 +791,13 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
            sgWriteShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
-           sgWriteInt( fp, material.length() );        // nbytes
+           sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
            // write fans
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteInt( fp, fans_v[i].size() * 2 * sizeof(short) );
+               sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
                for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
                    sgWriteShort( fp, (short)fans_v[i][j] );
                    sgWriteShort( fp, (short)fans_tc[i][j] );
@@ -967,3 +819,192 @@ bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
 
     return true;
 }
+
+
+// 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.
+bool SGBinObject::write_ascii( const string& base, const string& name,
+                              const SGBucket& b )
+{
+    Point3D p;
+    int i, j;
+
+    string dir = base + "/" + b.gen_base_path();
+    string command = "mkdir -p " + dir;
+#ifdef _MSC_VER
+    system( (string("mkdir ") + dir).c_str() );
+#else
+    system(command.c_str());
+#endif
+
+    // string file = dir + "/" + b.gen_index_str();
+    string file = dir + "/" + name;
+    cout << "Output file = " << file << endl;
+
+    FILE *fp;
+    if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
+       cout << "ERROR: opening " << file << " for writing!" << endl;
+       return false;
+    }
+
+    cout << "triangles size = " << tris_v.size() << "  tri_materials = " 
+        << tri_materials.size() << endl;
+    cout << "strips size = " << strips_v.size() << "  strip_materials = " 
+        << strip_materials.size() << endl;
+    cout << "fans size = " << fans_v.size() << "  fan_materials = " 
+        << fan_materials.size() << endl;
+
+    cout << "points = " << wgs84_nodes.size() << endl;
+    cout << "tex coords = " << texcoords.size() << endl;
+    // write headers
+    fprintf(fp, "# FGFS Scenery\n");
+    fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
+
+    time_t calendar_time = time(NULL);
+    struct tm *local_tm;
+    local_tm = localtime( &calendar_time );
+    char time_str[256];
+    strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
+    fprintf(fp, "# Created %s\n", time_str );
+    fprintf(fp, "\n");
+
+    // write bounding sphere
+    fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
+           gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
+    fprintf(fp, "\n");
+
+    // dump vertex list
+    fprintf(fp, "# vertex list\n");
+    for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
+       p = wgs84_nodes[i] - gbs_center;
+       
+       fprintf(fp,  "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
+    }
+    fprintf(fp, "\n");
+
+    fprintf(fp, "# vertex normal list\n");
+    for ( i = 0; i < (int)normals.size(); ++i ) {
+       p = normals[i];
+       fprintf(fp,  "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
+    }
+    fprintf(fp, "\n");
+
+    // dump texture coordinates
+    fprintf(fp, "# texture coordinate list\n");
+    for ( i = 0; i < (int)texcoords.size(); ++i ) {
+       p = texcoords[i];
+       fprintf(fp,  "vt %.5f %.5f\n", p.x(), p.y() );
+    }
+    fprintf(fp, "\n");
+
+    // dump individual triangles if they exist
+    if ( tris_v.size() > 0 ) {
+       fprintf(fp, "# triangle groups\n");
+
+       int start = 0;
+       int end = 1;
+       string material;
+       while ( start < (int)tri_materials.size() ) {
+           // find next group
+           material = tri_materials[start];
+           while ( (end < (int)tri_materials.size()) && 
+                   (material == tri_materials[end]) )
+           {
+               // cout << "end = " << end << endl;
+               end++;
+           }
+           // cout << "group = " << start << " to " << end - 1 << endl;
+
+           // make a list of points for the group
+           point_list group_nodes;
+           group_nodes.clear();
+           Point3D 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( wgs84_nodes[ tris_v[i][j] ] );
+                   bs_center = calc_center( group_nodes );
+                   bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
+               }
+           }
+
+           // write group headers
+           fprintf(fp, "\n");
+           fprintf(fp, "# usemtl %s\n", material.c_str());
+           fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
+                   bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
+
+           // write groups
+           for ( i = start; i < end; ++i ) {
+               fprintf(fp, "f");
+               for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
+                   fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
+               }
+               fprintf(fp, "\n");
+           }
+
+           start = end;
+           end = start + 1;
+       }
+    }
+
+    // dump triangle groups
+    if ( strips_v.size() > 0 ) {
+       fprintf(fp, "# triangle strips\n");
+
+       int start = 0;
+       int end = 1;
+       string material;
+       while ( start < (int)strip_materials.size() ) {
+           // find next group
+           material = strip_materials[start];
+           while ( (end < (int)strip_materials.size()) && 
+                   (material == strip_materials[end]) )
+               {
+                   // cout << "end = " << end << endl;
+                   end++;
+               }
+           // cout << "group = " << start << " to " << end - 1 << endl;
+
+           // make a list of points for the group
+           point_list group_nodes;
+           group_nodes.clear();
+           Point3D 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( wgs84_nodes[ strips_v[i][j] ] );
+                   bs_center = calc_center( group_nodes );
+                   bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
+               }
+           }
+
+           // write group headers
+           fprintf(fp, "\n");
+           fprintf(fp, "# usemtl %s\n", material.c_str());
+           fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
+                   bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
+
+           // write groups
+           for ( i = start; i < end; ++i ) {
+               fprintf(fp, "ts");
+               for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
+                   fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
+               }
+               fprintf(fp, "\n");
+           }
+           
+           start = end;
+           end = start + 1;
+       }
+    }
+
+    // close the file
+    fclose(fp);
+
+    command = "gzip --force --best " + file;
+    system(command.c_str());
+
+    return true;
+}