1 // sg_binobj.cxx -- routines to read and write low level flightgear 3d objects
3 // Written by Curtis Olson, started January 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
33 #include <simgear/bucket/newbucket.hxx>
35 #include "lowlevel.hxx"
36 #include "sg_binobj.hxx"
39 SG_USING_STD( string );
40 SG_USING_STD( vector );
42 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
49 SG_BOUNDING_SPHERE = 0,
58 SG_TRIANGLE_FACES = 10,
59 SG_TRIANGLE_STRIPS = 11,
68 class sgSimpleBuffer {
77 inline sgSimpleBuffer( unsigned int s )
83 cout << "Creating a new buffer of size = " << size << endl;
87 inline ~sgSimpleBuffer() {
91 inline unsigned int get_size() const { return size; }
92 inline char *get_ptr() const { return ptr; }
93 inline void resize( unsigned int s ) {
101 cout << "resizing buffer to size = " << size << endl;
102 ptr = new char[size];
108 // calculate the center of a list of points, by taking the halfway
109 // point between the min and max points.
110 static Point3D calc_center( point_list& wgs84_nodes ) {
113 if ( wgs84_nodes.size() ) {
114 min = max = wgs84_nodes[0];
116 min = max = Point3D( 0 );
119 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
122 if ( p.x() < min.x() ) { min.setx( p.x() ); }
123 if ( p.y() < min.y() ) { min.sety( p.y() ); }
124 if ( p.z() < min.z() ) { min.setz( p.z() ); }
126 if ( p.x() > max.x() ) { max.setx( p.x() ); }
127 if ( p.y() > max.y() ) { max.sety( p.y() ); }
128 if ( p.z() > max.z() ) { max.setz( p.z() ); }
131 return ( min + max ) / 2.0;
134 // calculate the bounding sphere. Center is the center of the
135 // tile and zero elevation
136 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
138 double radius_squared = 0;
140 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
141 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
142 if ( dist_squared > radius_squared ) {
143 radius_squared = dist_squared;
147 return sqrt(radius_squared);
151 // read a binary file and populate the provided structures.
152 bool SGBinObject::read_bin( const string& file ) {
157 static sgSimpleBuffer buf( 32768 ); // 32 Kb
159 // zero out structures
160 gbs_center = Point3D( 0 );
168 pt_materials.clear();
172 tri_materials.clear();
176 strip_materials.clear();
180 fan_materials.clear();
183 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
184 string filegz = file + ".gz";
185 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
186 // cout << "ERROR: opening " << file << " or " << filegz
187 // << "for reading!" << endl;
197 sgReadUInt( fp, &header );
198 if ( ((header & 0xFF000000) >> 24) == 'S' &&
199 ((header & 0x00FF0000) >> 16) == 'G' ) {
200 // cout << "Good header" << endl;
202 version = (header & 0x0000FFFF);
203 // cout << "File version = " << version << endl;
205 // close the file before we return
211 // read creation time
212 time_t calendar_time;
213 sgReadLong( fp, &calendar_time );
216 // The following code has a global effect on the host application
217 // and can screws up the time elsewhere. It should be avoided
218 // unless you need this for debugging in which case you should
219 // disable it again once the debugging task is finished.
221 local_tm = localtime( &calendar_time );
223 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
224 cout << "File created on " << time_str << endl;
227 // read number of top level objects
229 sgReadShort( fp, &nobjects );
230 // cout << "Total objects to read = " << nobjects << endl;
233 for ( i = 0; i < nobjects; ++i ) {
234 // read object header
236 short nproperties, nelements;
237 sgReadChar( fp, &obj_type );
238 sgReadShort( fp, &nproperties );
239 sgReadShort( fp, &nelements );
241 // cout << "object " << i << " = " << (int)obj_type << " props = "
242 // << nproperties << " elements = " << nelements << endl;
244 if ( obj_type == SG_BOUNDING_SPHERE ) {
245 // read bounding sphere properties
246 for ( j = 0; j < nproperties; ++j ) {
248 sgReadChar( fp, &prop_type );
250 sgReadUInt( fp, &nbytes );
251 // cout << "property size = " << nbytes << endl;
252 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
253 char *ptr = buf.get_ptr();
254 sgReadBytes( fp, nbytes, ptr );
257 // read bounding sphere elements
258 for ( j = 0; j < nelements; ++j ) {
259 sgReadUInt( fp, &nbytes );
260 // cout << "element size = " << nbytes << endl;
261 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
262 char *ptr = buf.get_ptr();
263 sgReadBytes( fp, nbytes, ptr );
265 double *dptr = (double *)ptr;
266 if ( sgIsBigEndian() ) {
267 sgEndianSwap( (uint64 *)&(dptr[0]) );
268 sgEndianSwap( (uint64 *)&(dptr[1]) );
269 sgEndianSwap( (uint64 *)&(dptr[2]) );
271 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
272 // cout << "Center = " << gbs_center << endl;
273 ptr += sizeof(double) * 3;
275 float *fptr = (float *)ptr;
276 if ( sgIsBigEndian() ) {
277 sgEndianSwap( (unsigned int *)fptr );
279 gbs_radius = fptr[0];
280 // cout << "Bounding radius = " << gbs_radius << endl;
282 } else if ( obj_type == SG_VERTEX_LIST ) {
283 // read vertex list properties
284 for ( j = 0; j < nproperties; ++j ) {
286 sgReadChar( fp, &prop_type );
288 sgReadUInt( fp, &nbytes );
289 // cout << "property size = " << nbytes << endl;
290 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
291 char *ptr = buf.get_ptr();
292 sgReadBytes( fp, nbytes, ptr );
295 // read vertex list elements
296 for ( j = 0; j < nelements; ++j ) {
297 sgReadUInt( fp, &nbytes );
298 // cout << "element size = " << nbytes << endl;
299 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
300 char *ptr = buf.get_ptr();
301 sgReadBytes( fp, nbytes, ptr );
302 int count = nbytes / (sizeof(float) * 3);
303 float *fptr = (float *)ptr;
304 for ( k = 0; k < count; ++k ) {
305 if ( sgIsBigEndian() ) {
306 sgEndianSwap( (unsigned int *)&(fptr[0]) );
307 sgEndianSwap( (unsigned int *)&(fptr[1]) );
308 sgEndianSwap( (unsigned int *)&(fptr[2]) );
310 p = Point3D( fptr[0], fptr[1], fptr[2] );
311 // cout << "node = " << p << endl;
312 wgs84_nodes.push_back( p );
316 } else if ( obj_type == SG_COLOR_LIST ) {
317 // read color list properties
318 for ( j = 0; j < nproperties; ++j ) {
320 sgReadChar( fp, &prop_type );
322 sgReadUInt( fp, &nbytes );
323 // cout << "property size = " << nbytes << endl;
324 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
325 char *ptr = buf.get_ptr();
326 sgReadBytes( fp, nbytes, ptr );
329 // read color list elements
330 for ( j = 0; j < nelements; ++j ) {
331 sgReadUInt( fp, &nbytes );
332 // cout << "element size = " << nbytes << endl;
333 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
334 char *ptr = buf.get_ptr();
335 sgReadBytes( fp, nbytes, ptr );
336 int count = nbytes / (sizeof(float) * 4);
337 float *fptr = (float *)ptr;
338 for ( k = 0; k < count; ++k ) {
339 if ( sgIsBigEndian() ) {
340 sgEndianSwap( (unsigned int *)&(fptr[0]) );
341 sgEndianSwap( (unsigned int *)&(fptr[1]) );
342 sgEndianSwap( (unsigned int *)&(fptr[2]) );
343 sgEndianSwap( (unsigned int *)&(fptr[3]) );
345 p = Point3D( fptr[0], fptr[1], fptr[2] );
346 // cout << "node = " << p << endl;
347 colors.push_back( p );
351 } else if ( obj_type == SG_NORMAL_LIST ) {
352 // read normal list properties
353 for ( j = 0; j < nproperties; ++j ) {
355 sgReadChar( fp, &prop_type );
357 sgReadUInt( fp, &nbytes );
358 // cout << "property size = " << nbytes << endl;
359 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
360 char *ptr = buf.get_ptr();
361 sgReadBytes( fp, nbytes, ptr );
364 // read normal list elements
365 for ( j = 0; j < nelements; ++j ) {
366 sgReadUInt( fp, &nbytes );
367 // cout << "element size = " << nbytes << endl;
368 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
369 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
370 sgReadBytes( fp, nbytes, ptr );
371 int count = nbytes / 3;
372 for ( k = 0; k < count; ++k ) {
375 (ptr[0]) / 127.5 - 1.0,
376 (ptr[1]) / 127.5 - 1.0,
377 (ptr[2]) / 127.5 - 1.0 );
378 sgdNormalizeVec3( normal );
380 p = Point3D( normal[0], normal[1], normal[2] );
381 // cout << "normal = " << p << endl;
382 normals.push_back( p );
386 } else if ( obj_type == SG_TEXCOORD_LIST ) {
387 // read texcoord list properties
388 for ( j = 0; j < nproperties; ++j ) {
390 sgReadChar( fp, &prop_type );
392 sgReadUInt( fp, &nbytes );
393 // cout << "property size = " << nbytes << endl;
394 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
395 char *ptr = buf.get_ptr();
396 sgReadBytes( fp, nbytes, ptr );
399 // read texcoord list elements
400 for ( j = 0; j < nelements; ++j ) {
401 sgReadUInt( fp, &nbytes );
402 // cout << "element size = " << nbytes << endl;
403 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
404 char *ptr = buf.get_ptr();
405 sgReadBytes( fp, nbytes, ptr );
406 int count = nbytes / (sizeof(float) * 2);
407 float *fptr = (float *)ptr;
408 for ( k = 0; k < count; ++k ) {
409 if ( sgIsBigEndian() ) {
410 sgEndianSwap( (unsigned int *)&(fptr[0]) );
411 sgEndianSwap( (unsigned int *)&(fptr[1]) );
413 p = Point3D( fptr[0], fptr[1], 0 );
414 // cout << "texcoord = " << p << endl;
415 texcoords.push_back( p );
419 } else if ( obj_type == SG_POINTS ) {
420 // read points properties
421 for ( j = 0; j < nproperties; ++j ) {
423 sgReadChar( fp, &prop_type );
425 sgReadUInt( fp, &nbytes );
426 // cout << "property size = " << nbytes << endl;
427 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
428 char *ptr = buf.get_ptr();
429 sgReadBytes( fp, nbytes, ptr );
430 if ( prop_type == SG_MATERIAL ) {
431 strncpy( material, ptr, nbytes );
432 material[nbytes] = '\0';
433 // cout << "material type = " << material << endl;
437 // read point elements
438 for ( j = 0; j < nelements; ++j ) {
439 sgReadUInt( fp, &nbytes );
440 // cout << "element size = " << nbytes << endl;
441 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
442 char *ptr = buf.get_ptr();
443 sgReadBytes( fp, nbytes, ptr );
444 int count = nbytes / sizeof(short);
445 short *sptr = (short *)ptr;
448 for ( k = 0; k < count; ++k ) {
449 if ( sgIsBigEndian() ) {
450 sgEndianSwap( (unsigned short *)&(sptr[0]) );
452 vs.push_back( sptr[0] );
453 // cout << sptr[0] << " ";
457 pts_v.push_back( vs );
458 pt_materials.push_back( material );
460 } else if ( obj_type == SG_TRIANGLE_FACES ) {
461 // read triangle face properties
462 for ( j = 0; j < nproperties; ++j ) {
464 sgReadChar( fp, &prop_type );
466 sgReadUInt( fp, &nbytes );
467 // cout << "property size = " << nbytes << endl;
468 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
469 char *ptr = buf.get_ptr();
470 sgReadBytes( fp, nbytes, ptr );
471 if ( prop_type == SG_MATERIAL ) {
472 strncpy( material, ptr, nbytes );
473 material[nbytes] = '\0';
474 // cout << "material type = " << material << endl;
478 // read triangle face elements
479 for ( j = 0; j < nelements; ++j ) {
480 sgReadUInt( fp, &nbytes );
481 // cout << "element size = " << nbytes << endl;
482 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
483 char *ptr = buf.get_ptr();
484 sgReadBytes( fp, nbytes, ptr );
485 int count = nbytes / (sizeof(short) * 2);
486 short *sptr = (short *)ptr;
488 vs.clear(); tcs.clear();
489 for ( k = 0; k < count; ++k ) {
490 if ( sgIsBigEndian() ) {
491 sgEndianSwap( (unsigned short *)&(sptr[0]) );
492 sgEndianSwap( (unsigned short *)&(sptr[1]) );
494 vs.push_back( sptr[0] );
495 tcs.push_back( sptr[1] );
496 // cout << sptr[0] << "/" << sptr[1] << " ";
500 tris_v.push_back( vs );
501 tris_tc.push_back( tcs );
502 tri_materials.push_back( material );
504 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
505 // read triangle strip properties
506 for ( j = 0; j < nproperties; ++j ) {
508 sgReadChar( fp, &prop_type );
510 sgReadUInt( fp, &nbytes );
511 // cout << "property size = " << nbytes << endl;
512 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
513 char *ptr = buf.get_ptr();
514 sgReadBytes( fp, nbytes, ptr );
515 if ( prop_type == SG_MATERIAL ) {
516 strncpy( material, ptr, nbytes );
517 material[nbytes] = '\0';
518 // cout << "material type = " << material << endl;
522 // read triangle strip elements
523 for ( j = 0; j < nelements; ++j ) {
524 sgReadUInt( fp, &nbytes );
525 // cout << "element size = " << nbytes << endl;
526 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
527 char *ptr = buf.get_ptr();
528 sgReadBytes( fp, nbytes, ptr );
529 int count = nbytes / (sizeof(short) * 2);
530 short *sptr = (short *)ptr;
532 vs.clear(); tcs.clear();
533 for ( k = 0; k < count; ++k ) {
534 if ( sgIsBigEndian() ) {
535 sgEndianSwap( (unsigned short *)&(sptr[0]) );
536 sgEndianSwap( (unsigned short *)&(sptr[1]) );
538 vs.push_back( sptr[0] );
539 tcs.push_back( sptr[1] );
540 // cout << sptr[0] << "/" << sptr[1] << " ";
544 strips_v.push_back( vs );
545 strips_tc.push_back( tcs );
546 strip_materials.push_back( material );
548 } else if ( obj_type == SG_TRIANGLE_FANS ) {
549 // read triangle fan properties
550 for ( j = 0; j < nproperties; ++j ) {
552 sgReadChar( fp, &prop_type );
554 sgReadUInt( fp, &nbytes );
555 // cout << "property size = " << nbytes << endl;
556 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
557 char *ptr = buf.get_ptr();
558 sgReadBytes( fp, nbytes, ptr );
559 if ( prop_type == SG_MATERIAL ) {
560 strncpy( material, ptr, nbytes );
561 material[nbytes] = '\0';
562 // cout << "material type = " << material << endl;
566 // read triangle fan elements
567 for ( j = 0; j < nelements; ++j ) {
568 sgReadUInt( fp, &nbytes );
569 // cout << "element size = " << nbytes << endl;
570 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
571 char *ptr = buf.get_ptr();
572 sgReadBytes( fp, nbytes, ptr );
573 int count = nbytes / (sizeof(short) * 2);
574 short *sptr = (short *)ptr;
576 vs.clear(); tcs.clear();
577 for ( k = 0; k < count; ++k ) {
578 if ( sgIsBigEndian() ) {
579 sgEndianSwap( (unsigned short *)&(sptr[0]) );
580 sgEndianSwap( (unsigned short *)&(sptr[1]) );
582 vs.push_back( sptr[0] );
583 tcs.push_back( sptr[1] );
584 // cout << sptr[0] << "/" << sptr[1] << " ";
588 fans_v.push_back( vs );
589 fans_tc.push_back( tcs );
590 fan_materials.push_back( material );
593 // unknown object type, just skip
596 for ( j = 0; j < nproperties; ++j ) {
598 sgReadChar( fp, &prop_type );
600 sgReadUInt( fp, &nbytes );
601 // cout << "property size = " << nbytes << endl;
602 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
603 char *ptr = buf.get_ptr();
604 sgReadBytes( fp, nbytes, ptr );
608 for ( j = 0; j < nelements; ++j ) {
609 sgReadUInt( fp, &nbytes );
610 // cout << "element size = " << nbytes << endl;
611 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
612 char *ptr = buf.get_ptr();
613 sgReadBytes( fp, nbytes, ptr );
621 if ( sgReadError() ) {
622 cout << "We detected an error while reading the file." << endl;
630 // write out the structures to a binary file. We assume that the
631 // groups come to us sorted by material property. If not, things
632 // don't break, but the result won't be as optimal.
633 bool SGBinObject::write_bin( const string& base, const string& name,
642 string dir = base + "/" + b.gen_base_path();
643 string command = "mkdir -p " + dir;
644 #if defined(_MSC_VER) || defined(__MINGW32__)
645 system( (string("mkdir ") + dir).c_str() );
647 system(command.c_str());
650 string file = dir + "/" + name + ".gz";
651 cout << "Output file = " << file << endl;
654 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
655 cout << "ERROR: opening " << file << " for writing!" << endl;
661 cout << "points size = " << pts_v.size() << " pt_materials = "
662 << pt_materials.size() << endl;
663 cout << "triangles size = " << tris_v.size() << " tri_materials = "
664 << tri_materials.size() << endl;
665 cout << "strips size = " << strips_v.size() << " strip_materials = "
666 << strip_materials.size() << endl;
667 cout << "fans size = " << fans_v.size() << " fan_materials = "
668 << fan_materials.size() << endl;
670 cout << "nodes = " << wgs84_nodes.size() << endl;
671 cout << "colors = " << colors.size() << endl;
672 cout << "normals = " << normals.size() << endl;
673 cout << "tex coords = " << texcoords.size() << endl;
675 // write header magic
676 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
677 time_t calendar_time = time(NULL);
678 sgWriteLong( fp, (long int)calendar_time );
680 // calculate and write number of top level objects
685 nobjects++; // for gbs
686 nobjects++; // for vertices
687 nobjects++; // for colors
688 nobjects++; // for normals
689 nobjects++; // for texcoords
694 while ( start < (int)pt_materials.size() ) {
695 material = pt_materials[start];
696 while ( (end < (int)pt_materials.size()) &&
697 (material == pt_materials[end]) ) {
701 start = end; end = start + 1;
708 while ( start < (int)tri_materials.size() ) {
709 material = tri_materials[start];
710 while ( (end < (int)tri_materials.size()) &&
711 (material == tri_materials[end]) ) {
715 start = end; end = start + 1;
722 while ( start < (int)strip_materials.size() ) {
723 material = strip_materials[start];
724 while ( (end < (int)strip_materials.size()) &&
725 (material == strip_materials[end]) ) {
729 start = end; end = start + 1;
736 while ( start < (int)fan_materials.size() ) {
737 material = fan_materials[start];
738 while ( (end < (int)fan_materials.size()) &&
739 (material == fan_materials[end]) ) {
743 start = end; end = start + 1;
747 cout << "total top level objects = " << nobjects << endl;
748 sgWriteShort( fp, nobjects );
750 // write bounding sphere
751 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
752 sgWriteShort( fp, 0 ); // nproperties
753 sgWriteShort( fp, 1 ); // nelements
755 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
757 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
758 sgWritedVec3( fp, center );
759 sgWriteFloat( fp, gbs_radius );
762 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
763 sgWriteShort( fp, 0 ); // nproperties
764 sgWriteShort( fp, 1 ); // nelements
765 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
766 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
767 p = wgs84_nodes[i] - gbs_center;
768 sgSetVec3( pt, p.x(), p.y(), p.z() );
769 sgWriteVec3( fp, pt );
772 // dump vertex color list
773 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
774 sgWriteShort( fp, 0 ); // nproperties
775 sgWriteShort( fp, 1 ); // nelements
776 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
777 for ( i = 0; i < (int)colors.size(); ++i ) {
779 // Right now we have a place holder for color alpha but we
780 // need to update the interface so the calling program can
782 sgSetVec4( color, p.x(), p.y(), p.z(), 1.0 );
783 sgWriteVec4( fp, color );
786 // dump vertex normal list
787 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
788 sgWriteShort( fp, 0 ); // nproperties
789 sgWriteShort( fp, 1 ); // nelements
790 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
792 for ( i = 0; i < (int)normals.size(); ++i ) {
794 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
795 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
796 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
797 sgWriteBytes( fp, 3, normal );
800 // dump texture coordinates
801 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
802 sgWriteShort( fp, 0 ); // nproperties
803 sgWriteShort( fp, 1 ); // nelements
804 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
805 for ( i = 0; i < (int)texcoords.size(); ++i ) {
807 sgSetVec2( t, p.x(), p.y() );
808 sgWriteVec2( fp, t );
811 // dump point groups if they exist
812 if ( pts_v.size() > 0 ) {
816 while ( start < (int)pt_materials.size() ) {
818 material = pt_materials[start];
819 while ( (end < (int)pt_materials.size()) &&
820 (material == pt_materials[end]) )
822 // cout << "end = " << end << endl;
825 // cout << "group = " << start << " to " << end - 1 << endl;
827 // write group headers
828 sgWriteChar( fp, (char)SG_POINTS ); // type
829 sgWriteShort( fp, 1 ); // nproperties
830 sgWriteShort( fp, end - start ); // nelements
832 sgWriteChar( fp, (char)SG_MATERIAL ); // property
833 sgWriteUInt( fp, material.length() ); // nbytes
834 sgWriteBytes( fp, material.length(), material.c_str() );
837 for ( i = start; i < end; ++i ) {
839 sgWriteUInt( fp, pts_v[i].size() * sizeof(short) );
840 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
841 sgWriteShort( fp, (short)pts_v[i][j] );
850 // dump individual triangles if they exist
851 if ( tris_v.size() > 0 ) {
855 while ( start < (int)tri_materials.size() ) {
857 material = tri_materials[start];
858 while ( (end < (int)tri_materials.size()) &&
859 (material == tri_materials[end]) )
861 // cout << "end = " << end << endl;
864 // cout << "group = " << start << " to " << end - 1 << endl;
866 // write group headers
867 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
868 sgWriteShort( fp, 1 ); // nproperties
869 sgWriteShort( fp, 1 ); // nelements
871 sgWriteChar( fp, (char)SG_MATERIAL ); // property
872 sgWriteUInt( fp, material.length() ); // nbytes
873 sgWriteBytes( fp, material.length(), material.c_str() );
875 sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
878 for ( i = start; i < end; ++i ) {
879 for ( j = 0; j < 3; ++j ) {
880 sgWriteShort( fp, (short)tris_v[i][j] );
881 sgWriteShort( fp, (short)tris_tc[i][j] );
890 // dump triangle strips
891 if ( strips_v.size() > 0 ) {
895 while ( start < (int)strip_materials.size() ) {
897 material = strip_materials[start];
898 while ( (end < (int)strip_materials.size()) &&
899 (material == strip_materials[end]) )
901 // cout << "end = " << end << endl;
904 // cout << "group = " << start << " to " << end - 1 << endl;
906 // write group headers
907 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
908 sgWriteShort( fp, 1 ); // nproperties
909 sgWriteShort( fp, end - start ); // nelements
911 sgWriteChar( fp, (char)SG_MATERIAL ); // property
912 sgWriteUInt( fp, material.length() ); // nbytes
913 sgWriteBytes( fp, material.length(), material.c_str() );
916 for ( i = start; i < end; ++i ) {
918 sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
919 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
920 sgWriteShort( fp, (short)strips_v[i][j] );
921 sgWriteShort( fp, (short)strips_tc[i][j] );
930 // dump triangle fans
931 if ( fans_v.size() > 0 ) {
935 while ( start < (int)fan_materials.size() ) {
937 material = fan_materials[start];
938 while ( (end < (int)fan_materials.size()) &&
939 (material == fan_materials[end]) )
941 // cout << "end = " << end << endl;
944 // cout << "group = " << start << " to " << end - 1 << endl;
946 // write group headers
947 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
948 sgWriteShort( fp, 1 ); // nproperties
949 sgWriteShort( fp, end - start ); // nelements
951 sgWriteChar( fp, (char)SG_MATERIAL ); // property
952 sgWriteUInt( fp, material.length() ); // nbytes
953 sgWriteBytes( fp, material.length(), material.c_str() );
956 for ( i = start; i < end; ++i ) {
958 sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
959 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
960 sgWriteShort( fp, (short)fans_v[i][j] );
961 sgWriteShort( fp, (short)fans_tc[i][j] );
973 if ( sgWriteError() ) {
974 cout << "We detected an error while writing the file." << endl;
982 // write out the structures to an ASCII file. We assume that the
983 // groups come to us sorted by material property. If not, things
984 // don't break, but the result won't be as optimal.
985 bool SGBinObject::write_ascii( const string& base, const string& name,
991 string dir = base + "/" + b.gen_base_path();
992 string command = "mkdir -p " + dir;
993 #if defined(_MSC_VER) || defined(__MINGW32__)
994 system( (string("mkdir ") + dir).c_str() );
996 system(command.c_str());
999 // string file = dir + "/" + b.gen_index_str();
1000 string file = dir + "/" + name;
1001 cout << "Output file = " << file << endl;
1004 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1005 cout << "ERROR: opening " << file << " for writing!" << endl;
1009 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1010 << tri_materials.size() << endl;
1011 cout << "strips size = " << strips_v.size() << " strip_materials = "
1012 << strip_materials.size() << endl;
1013 cout << "fans size = " << fans_v.size() << " fan_materials = "
1014 << fan_materials.size() << endl;
1016 cout << "points = " << wgs84_nodes.size() << endl;
1017 cout << "tex coords = " << texcoords.size() << endl;
1019 fprintf(fp, "# FGFS Scenery\n");
1020 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1022 time_t calendar_time = time(NULL);
1023 struct tm *local_tm;
1024 local_tm = localtime( &calendar_time );
1026 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1027 fprintf(fp, "# Created %s\n", time_str );
1030 // write bounding sphere
1031 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1032 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1036 fprintf(fp, "# vertex list\n");
1037 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1038 p = wgs84_nodes[i] - gbs_center;
1040 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1044 fprintf(fp, "# vertex normal list\n");
1045 for ( i = 0; i < (int)normals.size(); ++i ) {
1047 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1051 // dump texture coordinates
1052 fprintf(fp, "# texture coordinate list\n");
1053 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1055 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1059 // dump individual triangles if they exist
1060 if ( tris_v.size() > 0 ) {
1061 fprintf(fp, "# triangle groups\n");
1066 while ( start < (int)tri_materials.size() ) {
1068 material = tri_materials[start];
1069 while ( (end < (int)tri_materials.size()) &&
1070 (material == tri_materials[end]) )
1072 // cout << "end = " << end << endl;
1075 // cout << "group = " << start << " to " << end - 1 << endl;
1077 // make a list of points for the group
1078 point_list group_nodes;
1079 group_nodes.clear();
1081 double bs_radius = 0;
1082 for ( i = start; i < end; ++i ) {
1083 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1084 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
1085 bs_center = calc_center( group_nodes );
1086 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1090 // write group headers
1092 fprintf(fp, "# usemtl %s\n", material.c_str());
1093 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1094 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1097 for ( i = start; i < end; ++i ) {
1099 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1100 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1110 // dump triangle groups
1111 if ( strips_v.size() > 0 ) {
1112 fprintf(fp, "# triangle strips\n");
1117 while ( start < (int)strip_materials.size() ) {
1119 material = strip_materials[start];
1120 while ( (end < (int)strip_materials.size()) &&
1121 (material == strip_materials[end]) )
1123 // cout << "end = " << end << endl;
1126 // cout << "group = " << start << " to " << end - 1 << endl;
1128 // make a list of points for the group
1129 point_list group_nodes;
1130 group_nodes.clear();
1132 double bs_radius = 0;
1133 for ( i = start; i < end; ++i ) {
1134 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1135 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
1136 bs_center = calc_center( group_nodes );
1137 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1141 // write group headers
1143 fprintf(fp, "# usemtl %s\n", material.c_str());
1144 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1145 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1148 for ( i = start; i < end; ++i ) {
1150 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1151 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1164 command = "gzip --force --best " + file;
1165 system(command.c_str());