+ // 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(unsigned short));
+ unsigned short *sptr = (unsigned 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( (uint16_t *)&(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 ) {
+ SGVec3d p;
+ int i, j, k;
+ unsigned int nbytes;
+ sgSimpleBuffer buf( 32768 ); // 32 Kb