]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_binobj.cxx
Tidy up the autoconf/automake configuration a bit.
[simgear.git] / simgear / io / sg_binobj.cxx
index f68fb5d39ba542905578a875bc4435c479efdc64..3b4bdf7971e1500ea9dc3e9f30808ba17ddff9a4 100644 (file)
 //
 
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <simgear/compiler.h>
 
 #include <stdio.h>
 #include <time.h>
-#include <zlib.h>
 
 #include <vector>
 #include STL_STRING
 #include "sg_binobj.hxx"
 
 
-FG_USING_STD( string );
-FG_USING_STD( vector );
-FG_USING_STD( cout );
-FG_USING_STD( endl );
+SG_USING_STD( string );
+SG_USING_STD( vector );
+
+#if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
+SG_USING_STD( cout );
+SG_USING_STD( endl );
+#endif
 
 
 enum {
     SG_BOUNDING_SPHERE = 0,
 
     SG_VERTEX_LIST = 1,
+    SG_COLOR_LIST = 4,
     SG_NORMAL_LIST = 2,
     SG_TEXCOORD_LIST = 3,
 
+    SG_POINTS = 9,
+
     SG_TRIANGLE_FACES = 10,
     SG_TRIANGLE_STRIPS = 11,
     SG_TRIANGLE_FANS = 12
-} tgObjectTypes;
+} sgObjectTypes;
 
 enum {
-    SG_MATERIAL = 0
-} tgPropertyTypes;
+    SG_IDX_VERTICES =  0x01,
+    SG_IDX_NORMALS =   0x02,
+    SG_IDX_COLORS =    0x04,
+    SG_IDX_TEXCOORDS = 0x08
+} sgIndexTypes;
+
+enum {
+    SG_MATERIAL = 0,
+    SG_INDEX_TYPES = 1
+} sgPropertyTypes;
+
 
 
 class sgSimpleBuffer {
@@ -147,11 +157,131 @@ double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
 }
 
 
+
+// read object properties
+static 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 )
+{
+    unsigned int nbytes;
+    unsigned char idx_mask;
+    int idx_size;
+    bool do_vertices, do_normals, do_colors, do_texcoords;
+    int j, k, idx;
+    static sgSimpleBuffer buf( 32768 );  // 32 Kb
+    char material[256];
+
+    // default values
+    if ( obj_type == SG_POINTS ) {
+       idx_size = 1;
+       idx_mask = SG_IDX_VERTICES;
+       do_vertices = true;
+       do_normals = false;
+       do_colors = false;
+       do_texcoords = false;
+    } else {
+       idx_size = 2;
+       idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
+       do_vertices = true;
+       do_normals = false;
+       do_colors = false;
+       do_texcoords = true;
+    }
+
+    for ( j = 0; j < nproperties; ++j ) {
+       char prop_type;
+       sgReadChar( fp, &prop_type );
+
+       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;
+       } else if ( prop_type == SG_INDEX_TYPES ) {
+           idx_mask = ptr[0];
+           // cout << "idx_mask = " << (int)idx_mask << endl;
+           idx_size = 0;
+           do_vertices = false;
+           do_normals = false;
+           do_colors = false;
+           do_texcoords = false;
+           if ( idx_mask & SG_IDX_VERTICES ) {
+               do_vertices = true;
+               ++idx_size;
+           }
+           if ( idx_mask & SG_IDX_NORMALS ) {
+               do_normals = true;
+               ++idx_size;
+           }
+           if ( idx_mask & SG_IDX_COLORS ) {
+               do_colors = true;
+               ++idx_size;
+           }
+           if ( idx_mask & SG_IDX_TEXCOORDS ) {
+               do_texcoords = true;
+               ++idx_size;
+           }
+       }
+    }
+
+    for ( j = 0; j < nelements; ++j ) {
+       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 / (idx_size * sizeof(short));
+       short *sptr = (short *)ptr;
+       int_list vs; vs.clear();
+       int_list ns; ns.clear();
+       int_list cs; cs.clear();
+       int_list tcs; tcs.clear();
+       for ( k = 0; k < count; ++k ) {
+           if ( sgIsBigEndian() ) {
+               for ( idx = 0; idx < idx_size; ++idx ) {
+                   sgEndianSwap( (unsigned short *)&(sptr[idx]) );
+               }
+           }
+           idx = 0;
+           if ( do_vertices ) {
+               vs.push_back( sptr[idx++] );
+           }
+           if ( do_normals ) {
+               ns.push_back( sptr[idx++] );
+                   }
+           if ( do_colors ) {
+               cs.push_back( sptr[idx++] );
+           }
+           if ( do_texcoords ) {
+               tcs.push_back( sptr[idx++] );
+           }
+           // cout << sptr[0] << " ";
+           sptr += idx_size;
+       }
+       // cout << endl;
+       vertices->push_back( vs );
+       normals->push_back( ns );
+       colors->push_back( cs );
+       texcoords->push_back( tcs );
+       materials->push_back( material );
+    }
+}
+
+
 // read a binary file and populate the provided structures.
 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
 
@@ -163,24 +293,37 @@ bool SGBinObject::read_bin( const string& file ) {
     normals.clear();
     texcoords.clear();
 
+    pts_v.clear();
+    pts_n.clear();
+    pts_c.clear();
+    pts_tc.clear();
+    pt_materials.clear();
+
     tris_v.clear();
+    tris_n.clear();
+    tris_c.clear();
     tris_tc.clear();
     tri_materials.clear();
 
     strips_v.clear();
+    strips_n.clear();
+    strips_c.clear();
     strips_tc.clear();
     strip_materials.clear();
 
     fans_v.clear();
+    fans_n.clear();
+    fans_c.clear();
     fans_tc.clear();
     fan_materials.clear();
-   
+
     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;
        }
     }
@@ -189,7 +332,6 @@ bool SGBinObject::read_bin( const string& file ) {
 
     // read headers
     unsigned int header;
-    unsigned short version;
     sgReadUInt( fp, &header );
     if ( ((header & 0xFF000000) >> 24) == 'S' &&
         ((header & 0x00FF0000) >> 16) == 'G' ) {
@@ -198,17 +340,28 @@ bool SGBinObject::read_bin( const string& file ) {
        version = (header & 0x0000FFFF);
        // cout << "File version = " << version << endl;
     } else {
+       // close the file before we return
+       gzclose(fp);
+
        return false;
     }
 
     // read creation time
-    time_t calendar_time;
-    sgReadLong( fp, &calendar_time );
+    unsigned int foo_calendar_time;
+    sgReadUInt( fp, &foo_calendar_time );
+
+#if 0
+    time_t calendar_time = foo_calendar_time;
+    // The following code has a global effect on the host application
+    // and can screws up the time elsewhere.  It should be avoided
+    // unless you need this for debugging in which case you should
+    // disable it again once the debugging task is finished.
     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);
-    // cout << "File created on " << time_str << endl;
+    cout << "File created on " << time_str << endl;
+#endif
 
     // read number of top level objects
     short nobjects;
@@ -249,11 +402,19 @@ bool SGBinObject::read_bin( const string& file ) {
                sgReadBytes( fp, nbytes, ptr );
 
                double *dptr = (double *)ptr;
+               if ( sgIsBigEndian() ) {
+                   sgEndianSwap( (uint64 *)&(dptr[0]) );
+                   sgEndianSwap( (uint64 *)&(dptr[1]) );
+                   sgEndianSwap( (uint64 *)&(dptr[2]) );
+               }
                gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
                // cout << "Center = " << gbs_center << endl;
                ptr += sizeof(double) * 3;
                
                float *fptr = (float *)ptr;
+               if ( sgIsBigEndian() ) {
+                   sgEndianSwap( (unsigned int *)fptr );
+               }
                gbs_radius = fptr[0];
                // cout << "Bounding radius = " << gbs_radius << endl;
            }
@@ -279,45 +440,19 @@ bool SGBinObject::read_bin( const string& file ) {
                sgReadBytes( fp, nbytes, ptr );
                int count = nbytes / (sizeof(float) * 3);
                float *fptr = (float *)ptr;
+               wgs84_nodes.reserve( count );
                for ( k = 0; k < count; ++k ) {
-                   p = Point3D( fptr[0], fptr[1], fptr[2] );
-                   // cout << "node = " << p << endl;
-                   wgs84_nodes.push_back( p );
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
+                   }
+                   wgs84_nodes.push_back( Point3D(fptr[0], fptr[1], fptr[2]) );
                    fptr += 3;
                }
            }
-       } else if ( obj_type == SG_NORMAL_LIST ) {
-           // read normal list properties
-           for ( j = 0; j < nproperties; ++j ) {
-               char prop_type;
-               sgReadChar( fp, &prop_type );
-
-               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 ) {
-               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;
-                   normals.push_back( p );
-                   ptr += 3;
-               }
-           }
-       } else if ( obj_type == SG_TEXCOORD_LIST ) {
-           // read texcoord list properties
+       } else if ( obj_type == SG_COLOR_LIST ) {
+           // read color list properties
            for ( j = 0; j < nproperties; ++j ) {
                char prop_type;
                sgReadChar( fp, &prop_type );
@@ -329,24 +464,29 @@ bool SGBinObject::read_bin( const string& file ) {
                sgReadBytes( fp, nbytes, ptr );
            }
 
-           // read texcoord list elements
+           // read color list elements
            for ( j = 0; j < nelements; ++j ) {
                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);
+               int count = nbytes / (sizeof(float) * 4);
                float *fptr = (float *)ptr;
+               colors.reserve(count);
                for ( k = 0; k < count; ++k ) {
-                   p = Point3D( fptr[0], fptr[1], 0 );
-                   // cout << "texcoord = " << p << endl;
-                   texcoords.push_back( p );
-                   fptr += 2;
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[3]) );
+                   }
+                   colors.push_back( Point3D( fptr[0], fptr[1], fptr[2] ) );
+                   fptr += 4;
                }
            }
-       } else if ( obj_type == SG_TRIANGLE_FACES ) {
-           // read triangle face properties
+       } else if ( obj_type == SG_NORMAL_LIST ) {
+           // read normal list properties
            for ( j = 0; j < nproperties; ++j ) {
                char prop_type;
                sgReadChar( fp, &prop_type );
@@ -356,37 +496,31 @@ bool SGBinObject::read_bin( const string& file ) {
                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;
-               }
            }
 
-           // read triangle face elements
+           // read normal list elements
            for ( j = 0; j < nelements; ++j ) {
                sgReadUInt( fp, &nbytes );
                // cout << "element size = " << nbytes << endl;
                if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
-               char *ptr = buf.get_ptr();
+               unsigned char *ptr = (unsigned char *)(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();
+               int count = nbytes / 3;
+               normals.reserve( count );
                for ( k = 0; k < count; ++k ) {
-                   vs.push_back( sptr[0] );
-                   tcs.push_back( sptr[1] );
-                   // cout << sptr[0] << "/" << sptr[1] << " ";
-                   sptr += 2;
+                    sgdVec3 normal;
+                    sgdSetVec3( normal,
+                               (ptr[0]) / 127.5 - 1.0,
+                               (ptr[1]) / 127.5 - 1.0,
+                               (ptr[2]) / 127.5 - 1.0 );
+                    sgdNormalizeVec3( normal );
+
+                   normals.push_back(Point3D(normal[0], normal[1], normal[2]));
+                   ptr += 3;
                }
-               // cout << endl;
-               tris_v.push_back( vs );
-               tris_tc.push_back( tcs );
-               tri_materials.push_back( material );
            }
-       } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
-           // read triangle strip properties
+       } else if ( obj_type == SG_TEXCOORD_LIST ) {
+           // read texcoord list properties
            for ( j = 0; j < nproperties; ++j ) {
                char prop_type;
                sgReadChar( fp, &prop_type );
@@ -396,75 +530,44 @@ bool SGBinObject::read_bin( const string& file ) {
                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;
-               }
            }
 
-           // read triangle strip elements
+           // read texcoord list elements
            for ( j = 0; j < nelements; ++j ) {
                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();
+               int count = nbytes / (sizeof(float) * 2);
+               float *fptr = (float *)ptr;
+               texcoords.reserve(count);
                for ( k = 0; k < count; ++k ) {
-                   vs.push_back( sptr[0] );
-                   tcs.push_back( sptr[1] );
-                   // cout << sptr[0] << "/" << sptr[1] << " ";
-                   sptr += 2;
+                   if ( sgIsBigEndian() ) {
+                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
+                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                   }
+                   texcoords.push_back( Point3D( fptr[0], fptr[1], 0 ) );
+                   fptr += 2;
                }
-               // cout << endl;
-               strips_v.push_back( vs );
-               strips_tc.push_back( tcs );
-               strip_materials.push_back( material );
            }
+       } else if ( obj_type == SG_POINTS ) {
+           // read point elements
+           read_object( fp, SG_POINTS, nproperties, nelements,
+                        &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
+       } else if ( obj_type == SG_TRIANGLE_FACES ) {
+           // read triangle face properties
+           read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
+                        &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
+       } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
+           // read triangle strip properties
+           read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
+                        &strips_v, &strips_n, &strips_c, &strips_tc,
+                        &strip_materials );
        } else if ( obj_type == SG_TRIANGLE_FANS ) {
            // read triangle fan properties
-           for ( j = 0; j < nproperties; ++j ) {
-               char prop_type;
-               sgReadChar( fp, &prop_type );
-
-               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;
-               }
-           }
-
-           // read triangle fan elements
-           for ( j = 0; j < nelements; ++j ) {
-               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 ) {
-                   vs.push_back( sptr[0] );
-                   tcs.push_back( sptr[1] );
-                   // cout << sptr[0] << "/" << sptr[1] << " ";
-                   sptr += 2;
-               }
-               // cout << endl;
-               fans_v.push_back( vs );
-               fans_tc.push_back( tcs );
-               fan_materials.push_back( material );
-           }
+           read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
+                        &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
        } else {
            // unknown object type, just skip
 
@@ -512,11 +615,14 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     Point3D p;
     sgVec2 t;
     sgVec3 pt;
+    sgVec4 color;
     int i, j;
+    unsigned char idx_mask;
+    int idx_size;
 
     string dir = base + "/" + b.gen_base_path();
     string command = "mkdir -p " + dir;
-#ifdef _MSC_VER
+#if defined(_MSC_VER) || defined(__MINGW32__)
     system( (string("mkdir ") + dir).c_str() );
 #else
     system(command.c_str());
@@ -533,6 +639,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
     sgClearWriteError();
 
+    cout << "points size = " << pts_v.size() << "  pt_materials = " 
+        << pt_materials.size() << endl;
     cout << "triangles size = " << tris_v.size() << "  tri_materials = " 
         << tri_materials.size() << endl;
     cout << "strips size = " << strips_v.size() << "  strip_materials = " 
@@ -540,7 +648,9 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     cout << "fans size = " << fans_v.size() << "  fan_materials = " 
         << fan_materials.size() << endl;
 
-    cout << "points = " << wgs84_nodes.size() << endl;
+    cout << "nodes = " << wgs84_nodes.size() << endl;
+    cout << "colors = " << colors.size() << endl;
+    cout << "normals = " << normals.size() << endl;
     cout << "tex coords = " << texcoords.size() << endl;
 
     // write header magic
@@ -555,9 +665,24 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     short nobjects = 0;
     nobjects++;                        // for gbs
     nobjects++;                        // for vertices
+    nobjects++;                        // for colors
     nobjects++;                        // for normals
     nobjects++;                        // for texcoords
 
+    // points
+    short npts = 0;
+    start = 0; end = 1;
+    while ( start < (int)pt_materials.size() ) {
+       material = pt_materials[start];
+       while ( (end < (int)pt_materials.size()) &&
+               (material == pt_materials[end]) ) {
+           end++;
+       }
+       npts++;
+       start = end; end = start + 1;
+    }
+    nobjects += npts;
+
     // tris
     short ntris = 0;
     start = 0; end = 1;
@@ -625,6 +750,20 @@ bool SGBinObject::write_bin( const string& base, const string& name,
        sgWriteVec3( fp, pt );
     }
 
+    // dump vertex color list
+    sgWriteChar( fp, (char)SG_COLOR_LIST );             // type
+    sgWriteShort( fp, 0 );                              // nproperties
+    sgWriteShort( fp, 1 );                              // nelements
+    sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
+    for ( i = 0; i < (int)colors.size(); ++i ) {
+       p = colors[i];
+        // Right now we have a place holder for color alpha but we
+        // need to update the interface so the calling program can
+        // provide the info.
+       sgSetVec4( color, p.x(), p.y(), p.z(), 1.0 );
+       sgWriteVec4( fp, color );
+    }
+
     // dump vertex normal list
     sgWriteChar( fp, (char)SG_NORMAL_LIST );            // type
     sgWriteShort( fp, 0 );                             // nproperties
@@ -633,9 +772,9 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     char normal[3];
     for ( i = 0; i < (int)normals.size(); ++i ) {
        p = normals[i];
-       normal[0] = (unsigned char)((p.x() + 1.0) * 128);
-       normal[1] = (unsigned char)((p.y() + 1.0) * 128);
-       normal[2] = (unsigned 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 );
     }
 
@@ -650,6 +789,66 @@ bool SGBinObject::write_bin( const string& base, const string& name,
        sgWriteVec2( fp, t );
     }
 
+    // dump point groups if they exist
+    if ( pts_v.size() > 0 ) {
+       int start = 0;
+       int end = 1;
+       string material;
+       while ( start < (int)pt_materials.size() ) {
+           // find next group
+           material = pt_materials[start];
+           while ( (end < (int)pt_materials.size()) && 
+                   (material == pt_materials[end]) )
+               {
+                   // cout << "end = " << end << endl;
+                   end++;
+               }
+           // cout << "group = " << start << " to " << end - 1 << endl;
+
+           // write group headers
+           sgWriteChar( fp, (char)SG_POINTS );          // type
+           sgWriteShort( fp, 2 );                       // nproperties
+           sgWriteShort( fp, end - start );             // nelements
+
+           sgWriteChar( fp, (char)SG_MATERIAL );        // property
+           sgWriteUInt( fp, material.length() );        // nbytes
+           sgWriteBytes( fp, material.length(), material.c_str() );
+
+           idx_mask = 0;
+           idx_size = 0;
+           if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
+           if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
+           if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
+           if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
+           sgWriteChar( fp, (char)SG_INDEX_TYPES );     // property
+           sgWriteUInt( fp, 1 );                        // nbytes
+           sgWriteChar( fp, idx_mask );
+
+           // write strips
+           for ( i = start; i < end; ++i ) {
+               // nbytes
+               sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) );
+               for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
+                   if ( pts_v.size() ) { 
+                       sgWriteShort( fp, (short)pts_v[i][j] );
+                   }
+                   if ( pts_n.size() ) { 
+                       sgWriteShort( fp, (short)pts_n[i][j] );
+                   }
+                   if ( pts_c.size() ) { 
+                       sgWriteShort( fp, (short)pts_c[i][j] );
+                   }
+                   if ( pts_tc.size() ) { 
+                       sgWriteShort( fp, (short)pts_tc[i][j] );
+                   }
+               }
+           }
+           
+           start = end;
+           end = start + 1;
+       }
+    }
+
     // dump individual triangles if they exist
     if ( tris_v.size() > 0 ) {
        int start = 0;
@@ -668,20 +867,41 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
-           sgWriteShort( fp, 1 );                      // nproperties
+           sgWriteShort( fp, 2 );                      // nproperties
            sgWriteShort( fp, 1 );                      // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
            sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
-           sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
+           idx_mask = 0;
+           idx_size = 0;
+           if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
+           if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
+           if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
+           if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
+           sgWriteChar( fp, (char)SG_INDEX_TYPES );     // property
+           sgWriteUInt( fp, 1 );                        // nbytes
+           sgWriteChar( fp, idx_mask );
+
+           // nbytes
+           sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) );
 
            // write group
            for ( i = start; i < end; ++i ) {
                for ( j = 0; j < 3; ++j ) {
-                   sgWriteShort( fp, (short)tris_v[i][j] );
-                   sgWriteShort( fp, (short)tris_tc[i][j] );
+                   if ( tris_v.size() ) {
+                       sgWriteShort( fp, (short)tris_v[i][j] );
+                   }
+                   if ( tris_n.size() ) {
+                       sgWriteShort( fp, (short)tris_n[i][j] );
+                   }
+                   if ( tris_c.size() ) {
+                       sgWriteShort( fp, (short)tris_c[i][j] );
+                   }
+                   if ( tris_tc.size() ) {
+                       sgWriteShort( fp, (short)tris_tc[i][j] );
+                   }
                }
            }
 
@@ -708,20 +928,40 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
-           sgWriteShort( fp, 1 );                       // nproperties
+           sgWriteShort( fp, 2 );                       // nproperties
            sgWriteShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );        // property
            sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
+           idx_mask = 0;
+           idx_size = 0;
+           if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
+           if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
+           if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
+           if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
+           sgWriteChar( fp, (char)SG_INDEX_TYPES );     // property
+           sgWriteUInt( fp, 1 );                        // nbytes
+           sgWriteChar( fp, idx_mask );
+
            // write strips
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
+               sgWriteUInt( fp, strips_v[i].size() * idx_size * 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] );
+                   if ( strips_v.size() ) { 
+                       sgWriteShort( fp, (short)strips_v[i][j] );
+                   }
+                   if ( strips_n.size() ) { 
+                       sgWriteShort( fp, (short)strips_n[i][j] );
+                   }
+                   if ( strips_c.size() ) { 
+                       sgWriteShort( fp, (short)strips_c[i][j] );
+                   }
+                   if ( strips_tc.size() ) { 
+                       sgWriteShort( fp, (short)strips_tc[i][j] );
+                   }
                }
            }
            
@@ -748,20 +988,40 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_FANS );   // type
-           sgWriteShort( fp, 1 );                       // nproperties
+           sgWriteShort( fp, 2 );                       // nproperties
            sgWriteShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
            sgWriteUInt( fp, material.length() );        // nbytes
            sgWriteBytes( fp, material.length(), material.c_str() );
 
+           idx_mask = 0;
+           idx_size = 0;
+           if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
+           if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
+           if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
+           if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
+           sgWriteChar( fp, (char)SG_INDEX_TYPES );     // property
+           sgWriteUInt( fp, 1 );                        // nbytes
+           sgWriteChar( fp, idx_mask );
+
            // write fans
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
+               sgWriteUInt( fp, fans_v[i].size() * idx_size * 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] );
+                   if ( fans_v.size() ) {
+                       sgWriteShort( fp, (short)fans_v[i][j] );
+                   }
+                   if ( fans_n.size() ) {
+                       sgWriteShort( fp, (short)fans_n[i][j] );
+                   }
+                   if ( fans_c.size() ) {
+                       sgWriteShort( fp, (short)fans_c[i][j] );
+                   }
+                   if ( fans_tc.size() ) {
+                       sgWriteShort( fp, (short)fans_tc[i][j] );
+                   }
                }
            }
            
@@ -793,7 +1053,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
 
     string dir = base + "/" + b.gen_base_path();
     string command = "mkdir -p " + dir;
-#ifdef _MSC_VER
+#if defined(_MSC_VER) || defined(__MINGW32__)
     system( (string("mkdir ") + dir).c_str() );
 #else
     system(command.c_str());