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.
29 #include <simgear/compiler.h>
37 #include <simgear/bucket/newbucket.hxx>
39 #include "lowlevel.hxx"
40 #include "sg_binobj.hxx"
43 SG_USING_STD( string );
44 SG_USING_STD( vector );
46 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
53 SG_BOUNDING_SPHERE = 0,
59 SG_TRIANGLE_FACES = 10,
60 SG_TRIANGLE_STRIPS = 11,
69 class sgSimpleBuffer {
78 inline sgSimpleBuffer( unsigned int s )
84 cout << "Creating a new buffer of size = " << size << endl;
88 inline ~sgSimpleBuffer() {
92 inline unsigned int get_size() const { return size; }
93 inline char *get_ptr() const { return ptr; }
94 inline void resize( unsigned int s ) {
102 cout << "resizing buffer to size = " << size << endl;
103 ptr = new char[size];
109 // calculate the center of a list of points, by taking the halfway
110 // point between the min and max points.
111 static Point3D calc_center( point_list& wgs84_nodes ) {
114 if ( wgs84_nodes.size() ) {
115 min = max = wgs84_nodes[0];
117 min = max = Point3D( 0 );
120 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
123 if ( p.x() < min.x() ) { min.setx( p.x() ); }
124 if ( p.y() < min.y() ) { min.sety( p.y() ); }
125 if ( p.z() < min.z() ) { min.setz( p.z() ); }
127 if ( p.x() > max.x() ) { max.setx( p.x() ); }
128 if ( p.y() > max.y() ) { max.sety( p.y() ); }
129 if ( p.z() > max.z() ) { max.setz( p.z() ); }
132 return ( min + max ) / 2.0;
135 // calculate the bounding sphere. Center is the center of the
136 // tile and zero elevation
137 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
139 double radius_squared = 0;
141 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
142 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
143 if ( dist_squared > radius_squared ) {
144 radius_squared = dist_squared;
148 return sqrt(radius_squared);
152 // read a binary file and populate the provided structures.
153 bool SGBinObject::read_bin( const string& file ) {
158 static sgSimpleBuffer buf( 32768 ); // 32 Kb
160 // zero out structures
161 gbs_center = Point3D( 0 );
170 tri_materials.clear();
174 strip_materials.clear();
178 fan_materials.clear();
181 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
182 string filegz = file + ".gz";
183 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
184 // cout << "ERROR: opening " << file << " or " << filegz
185 // << "for reading!" << endl;
195 sgReadUInt( fp, &header );
196 if ( ((header & 0xFF000000) >> 24) == 'S' &&
197 ((header & 0x00FF0000) >> 16) == 'G' ) {
198 // cout << "Good header" << endl;
200 version = (header & 0x0000FFFF);
201 // cout << "File version = " << version << endl;
203 // close the file before we return
209 // read creation time
210 time_t calendar_time;
211 sgReadLong( fp, &calendar_time );
213 local_tm = localtime( &calendar_time );
215 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
216 // cout << "File created on " << time_str << endl;
218 // read number of top level objects
220 sgReadShort( fp, &nobjects );
221 // cout << "Total objects to read = " << nobjects << endl;
224 for ( i = 0; i < nobjects; ++i ) {
225 // read object header
227 short nproperties, nelements;
228 sgReadChar( fp, &obj_type );
229 sgReadShort( fp, &nproperties );
230 sgReadShort( fp, &nelements );
232 // cout << "object " << i << " = " << (int)obj_type << " props = "
233 // << nproperties << " elements = " << nelements << endl;
235 if ( obj_type == SG_BOUNDING_SPHERE ) {
236 // read bounding sphere properties
237 for ( j = 0; j < nproperties; ++j ) {
239 sgReadChar( fp, &prop_type );
241 sgReadUInt( fp, &nbytes );
242 // cout << "property size = " << nbytes << endl;
243 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
244 char *ptr = buf.get_ptr();
245 sgReadBytes( fp, nbytes, ptr );
248 // read bounding sphere elements
249 for ( j = 0; j < nelements; ++j ) {
250 sgReadUInt( fp, &nbytes );
251 // cout << "element size = " << nbytes << endl;
252 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
253 char *ptr = buf.get_ptr();
254 sgReadBytes( fp, nbytes, ptr );
256 double *dptr = (double *)ptr;
257 if ( sgIsBigEndian() ) {
258 sgEndianSwap( (uint64 *)&(dptr[0]) );
259 sgEndianSwap( (uint64 *)&(dptr[1]) );
260 sgEndianSwap( (uint64 *)&(dptr[2]) );
262 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
263 // cout << "Center = " << gbs_center << endl;
264 ptr += sizeof(double) * 3;
266 float *fptr = (float *)ptr;
267 if ( sgIsBigEndian() ) {
268 sgEndianSwap( (unsigned int *)fptr );
270 gbs_radius = fptr[0];
271 // cout << "Bounding radius = " << gbs_radius << endl;
273 } else if ( obj_type == SG_VERTEX_LIST ) {
274 // read vertex list properties
275 for ( j = 0; j < nproperties; ++j ) {
277 sgReadChar( fp, &prop_type );
279 sgReadUInt( fp, &nbytes );
280 // cout << "property size = " << nbytes << endl;
281 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
282 char *ptr = buf.get_ptr();
283 sgReadBytes( fp, nbytes, ptr );
286 // read vertex list elements
287 for ( j = 0; j < nelements; ++j ) {
288 sgReadUInt( fp, &nbytes );
289 // cout << "element size = " << nbytes << endl;
290 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
291 char *ptr = buf.get_ptr();
292 sgReadBytes( fp, nbytes, ptr );
293 int count = nbytes / (sizeof(float) * 3);
294 float *fptr = (float *)ptr;
295 for ( k = 0; k < count; ++k ) {
296 if ( sgIsBigEndian() ) {
297 sgEndianSwap( (unsigned int *)&(fptr[0]) );
298 sgEndianSwap( (unsigned int *)&(fptr[1]) );
299 sgEndianSwap( (unsigned int *)&(fptr[2]) );
301 p = Point3D( fptr[0], fptr[1], fptr[2] );
302 // cout << "node = " << p << endl;
303 wgs84_nodes.push_back( p );
307 } else if ( obj_type == SG_NORMAL_LIST ) {
308 // read normal list properties
309 for ( j = 0; j < nproperties; ++j ) {
311 sgReadChar( fp, &prop_type );
313 sgReadUInt( fp, &nbytes );
314 // cout << "property size = " << nbytes << endl;
315 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
316 char *ptr = buf.get_ptr();
317 sgReadBytes( fp, nbytes, ptr );
320 // read normal list elements
321 for ( j = 0; j < nelements; ++j ) {
322 sgReadUInt( fp, &nbytes );
323 // cout << "element size = " << nbytes << endl;
324 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
325 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
326 sgReadBytes( fp, nbytes, ptr );
327 int count = nbytes / 3;
328 for ( k = 0; k < count; ++k ) {
331 (ptr[0]) / 127.5 - 1.0,
332 (ptr[1]) / 127.5 - 1.0,
333 (ptr[2]) / 127.5 - 1.0 );
334 sgdNormalizeVec3( normal );
336 p = Point3D( normal[0], normal[1], normal[2] );
337 // cout << "normal = " << p << endl;
338 normals.push_back( p );
342 } else if ( obj_type == SG_TEXCOORD_LIST ) {
343 // read texcoord list properties
344 for ( j = 0; j < nproperties; ++j ) {
346 sgReadChar( fp, &prop_type );
348 sgReadUInt( fp, &nbytes );
349 // cout << "property size = " << nbytes << endl;
350 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
351 char *ptr = buf.get_ptr();
352 sgReadBytes( fp, nbytes, ptr );
355 // read texcoord list elements
356 for ( j = 0; j < nelements; ++j ) {
357 sgReadUInt( fp, &nbytes );
358 // cout << "element size = " << nbytes << endl;
359 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
360 char *ptr = buf.get_ptr();
361 sgReadBytes( fp, nbytes, ptr );
362 int count = nbytes / (sizeof(float) * 2);
363 float *fptr = (float *)ptr;
364 for ( k = 0; k < count; ++k ) {
365 if ( sgIsBigEndian() ) {
366 sgEndianSwap( (unsigned int *)&(fptr[0]) );
367 sgEndianSwap( (unsigned int *)&(fptr[1]) );
369 p = Point3D( fptr[0], fptr[1], 0 );
370 // cout << "texcoord = " << p << endl;
371 texcoords.push_back( p );
375 } else if ( obj_type == SG_TRIANGLE_FACES ) {
376 // read triangle face properties
377 for ( j = 0; j < nproperties; ++j ) {
379 sgReadChar( fp, &prop_type );
381 sgReadUInt( fp, &nbytes );
382 // cout << "property size = " << nbytes << endl;
383 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
384 char *ptr = buf.get_ptr();
385 sgReadBytes( fp, nbytes, ptr );
386 if ( prop_type == SG_MATERIAL ) {
387 strncpy( material, ptr, nbytes );
388 material[nbytes] = '\0';
389 // cout << "material type = " << material << endl;
393 // read triangle face elements
394 for ( j = 0; j < nelements; ++j ) {
395 sgReadUInt( fp, &nbytes );
396 // cout << "element size = " << nbytes << endl;
397 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
398 char *ptr = buf.get_ptr();
399 sgReadBytes( fp, nbytes, ptr );
400 int count = nbytes / (sizeof(short) * 2);
401 short *sptr = (short *)ptr;
403 vs.clear(); tcs.clear();
404 for ( k = 0; k < count; ++k ) {
405 if ( sgIsBigEndian() ) {
406 sgEndianSwap( (unsigned short *)&(sptr[0]) );
407 sgEndianSwap( (unsigned short *)&(sptr[1]) );
409 vs.push_back( sptr[0] );
410 tcs.push_back( sptr[1] );
411 // cout << sptr[0] << "/" << sptr[1] << " ";
415 tris_v.push_back( vs );
416 tris_tc.push_back( tcs );
417 tri_materials.push_back( material );
419 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
420 // read triangle strip 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 triangle strip 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) * 2);
445 short *sptr = (short *)ptr;
447 vs.clear(); tcs.clear();
448 for ( k = 0; k < count; ++k ) {
449 if ( sgIsBigEndian() ) {
450 sgEndianSwap( (unsigned short *)&(sptr[0]) );
451 sgEndianSwap( (unsigned short *)&(sptr[1]) );
453 vs.push_back( sptr[0] );
454 tcs.push_back( sptr[1] );
455 // cout << sptr[0] << "/" << sptr[1] << " ";
459 strips_v.push_back( vs );
460 strips_tc.push_back( tcs );
461 strip_materials.push_back( material );
463 } else if ( obj_type == SG_TRIANGLE_FANS ) {
464 // read triangle fan properties
465 for ( j = 0; j < nproperties; ++j ) {
467 sgReadChar( fp, &prop_type );
469 sgReadUInt( fp, &nbytes );
470 // cout << "property size = " << nbytes << endl;
471 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
472 char *ptr = buf.get_ptr();
473 sgReadBytes( fp, nbytes, ptr );
474 if ( prop_type == SG_MATERIAL ) {
475 strncpy( material, ptr, nbytes );
476 material[nbytes] = '\0';
477 // cout << "material type = " << material << endl;
481 // read triangle fan elements
482 for ( j = 0; j < nelements; ++j ) {
483 sgReadUInt( fp, &nbytes );
484 // cout << "element size = " << nbytes << endl;
485 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
486 char *ptr = buf.get_ptr();
487 sgReadBytes( fp, nbytes, ptr );
488 int count = nbytes / (sizeof(short) * 2);
489 short *sptr = (short *)ptr;
491 vs.clear(); tcs.clear();
492 for ( k = 0; k < count; ++k ) {
493 if ( sgIsBigEndian() ) {
494 sgEndianSwap( (unsigned short *)&(sptr[0]) );
495 sgEndianSwap( (unsigned short *)&(sptr[1]) );
497 vs.push_back( sptr[0] );
498 tcs.push_back( sptr[1] );
499 // cout << sptr[0] << "/" << sptr[1] << " ";
503 fans_v.push_back( vs );
504 fans_tc.push_back( tcs );
505 fan_materials.push_back( material );
508 // unknown object type, just skip
511 for ( j = 0; j < nproperties; ++j ) {
513 sgReadChar( fp, &prop_type );
515 sgReadUInt( fp, &nbytes );
516 // cout << "property size = " << nbytes << endl;
517 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
518 char *ptr = buf.get_ptr();
519 sgReadBytes( fp, nbytes, ptr );
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 );
536 if ( sgReadError() ) {
537 cout << "We detected an error while reading the file." << endl;
545 // write out the structures to a binary file. We assume that the
546 // groups come to us sorted by material property. If not, things
547 // don't break, but the result won't be as optimal.
548 bool SGBinObject::write_bin( const string& base, const string& name,
556 string dir = base + "/" + b.gen_base_path();
557 string command = "mkdir -p " + dir;
559 system( (string("mkdir ") + dir).c_str() );
561 system(command.c_str());
564 string file = dir + "/" + name + ".gz";
565 cout << "Output file = " << file << endl;
568 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
569 cout << "ERROR: opening " << file << " for writing!" << endl;
575 cout << "triangles size = " << tris_v.size() << " tri_materials = "
576 << tri_materials.size() << endl;
577 cout << "strips size = " << strips_v.size() << " strip_materials = "
578 << strip_materials.size() << endl;
579 cout << "fans size = " << fans_v.size() << " fan_materials = "
580 << fan_materials.size() << endl;
582 cout << "points = " << wgs84_nodes.size() << endl;
583 cout << "tex coords = " << texcoords.size() << endl;
585 // write header magic
586 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
587 time_t calendar_time = time(NULL);
588 sgWriteLong( fp, (long int)calendar_time );
590 // calculate and write number of top level objects
595 nobjects++; // for gbs
596 nobjects++; // for vertices
597 nobjects++; // for normals
598 nobjects++; // for texcoords
603 while ( start < (int)tri_materials.size() ) {
604 material = tri_materials[start];
605 while ( (end < (int)tri_materials.size()) &&
606 (material == tri_materials[end]) ) {
610 start = end; end = start + 1;
617 while ( start < (int)strip_materials.size() ) {
618 material = strip_materials[start];
619 while ( (end < (int)strip_materials.size()) &&
620 (material == strip_materials[end]) ) {
624 start = end; end = start + 1;
631 while ( start < (int)fan_materials.size() ) {
632 material = fan_materials[start];
633 while ( (end < (int)fan_materials.size()) &&
634 (material == fan_materials[end]) ) {
638 start = end; end = start + 1;
642 cout << "total top level objects = " << nobjects << endl;
643 sgWriteShort( fp, nobjects );
645 // write bounding sphere
646 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
647 sgWriteShort( fp, 0 ); // nproperties
648 sgWriteShort( fp, 1 ); // nelements
650 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
652 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
653 sgWritedVec3( fp, center );
654 sgWriteFloat( fp, gbs_radius );
657 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
658 sgWriteShort( fp, 0 ); // nproperties
659 sgWriteShort( fp, 1 ); // nelements
660 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
661 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
662 p = wgs84_nodes[i] - gbs_center;
663 sgSetVec3( pt, p.x(), p.y(), p.z() );
664 sgWriteVec3( fp, pt );
667 // dump vertex normal list
668 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
669 sgWriteShort( fp, 0 ); // nproperties
670 sgWriteShort( fp, 1 ); // nelements
671 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
673 for ( i = 0; i < (int)normals.size(); ++i ) {
675 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
676 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
677 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
678 sgWriteBytes( fp, 3, normal );
681 // dump texture coordinates
682 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
683 sgWriteShort( fp, 0 ); // nproperties
684 sgWriteShort( fp, 1 ); // nelements
685 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
686 for ( i = 0; i < (int)texcoords.size(); ++i ) {
688 sgSetVec2( t, p.x(), p.y() );
689 sgWriteVec2( fp, t );
692 // dump individual triangles if they exist
693 if ( tris_v.size() > 0 ) {
697 while ( start < (int)tri_materials.size() ) {
699 material = tri_materials[start];
700 while ( (end < (int)tri_materials.size()) &&
701 (material == tri_materials[end]) )
703 // cout << "end = " << end << endl;
706 // cout << "group = " << start << " to " << end - 1 << endl;
708 // write group headers
709 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
710 sgWriteShort( fp, 1 ); // nproperties
711 sgWriteShort( fp, 1 ); // nelements
713 sgWriteChar( fp, (char)SG_MATERIAL ); // property
714 sgWriteUInt( fp, material.length() ); // nbytes
715 sgWriteBytes( fp, material.length(), material.c_str() );
717 sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
720 for ( i = start; i < end; ++i ) {
721 for ( j = 0; j < 3; ++j ) {
722 sgWriteShort( fp, (short)tris_v[i][j] );
723 sgWriteShort( fp, (short)tris_tc[i][j] );
732 // dump triangle strips
733 if ( strips_v.size() > 0 ) {
737 while ( start < (int)strip_materials.size() ) {
739 material = strip_materials[start];
740 while ( (end < (int)strip_materials.size()) &&
741 (material == strip_materials[end]) )
743 // cout << "end = " << end << endl;
746 // cout << "group = " << start << " to " << end - 1 << endl;
748 // write group headers
749 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
750 sgWriteShort( fp, 1 ); // nproperties
751 sgWriteShort( fp, end - start ); // nelements
753 sgWriteChar( fp, (char)SG_MATERIAL ); // property
754 sgWriteUInt( fp, material.length() ); // nbytes
755 sgWriteBytes( fp, material.length(), material.c_str() );
758 for ( i = start; i < end; ++i ) {
760 sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
761 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
762 sgWriteShort( fp, (short)strips_v[i][j] );
763 sgWriteShort( fp, (short)strips_tc[i][j] );
772 // dump triangle fans
773 if ( fans_v.size() > 0 ) {
777 while ( start < (int)fan_materials.size() ) {
779 material = fan_materials[start];
780 while ( (end < (int)fan_materials.size()) &&
781 (material == fan_materials[end]) )
783 // cout << "end = " << end << endl;
786 // cout << "group = " << start << " to " << end - 1 << endl;
788 // write group headers
789 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
790 sgWriteShort( fp, 1 ); // nproperties
791 sgWriteShort( fp, end - start ); // nelements
793 sgWriteChar( fp, (char)SG_MATERIAL ); // property
794 sgWriteUInt( fp, material.length() ); // nbytes
795 sgWriteBytes( fp, material.length(), material.c_str() );
798 for ( i = start; i < end; ++i ) {
800 sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
801 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
802 sgWriteShort( fp, (short)fans_v[i][j] );
803 sgWriteShort( fp, (short)fans_tc[i][j] );
815 if ( sgWriteError() ) {
816 cout << "We detected an error while writing the file." << endl;
824 // write out the structures to an ASCII file. We assume that the
825 // groups come to us sorted by material property. If not, things
826 // don't break, but the result won't be as optimal.
827 bool SGBinObject::write_ascii( const string& base, const string& name,
833 string dir = base + "/" + b.gen_base_path();
834 string command = "mkdir -p " + dir;
836 system( (string("mkdir ") + dir).c_str() );
838 system(command.c_str());
841 // string file = dir + "/" + b.gen_index_str();
842 string file = dir + "/" + name;
843 cout << "Output file = " << file << endl;
846 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
847 cout << "ERROR: opening " << file << " for writing!" << endl;
851 cout << "triangles size = " << tris_v.size() << " tri_materials = "
852 << tri_materials.size() << endl;
853 cout << "strips size = " << strips_v.size() << " strip_materials = "
854 << strip_materials.size() << endl;
855 cout << "fans size = " << fans_v.size() << " fan_materials = "
856 << fan_materials.size() << endl;
858 cout << "points = " << wgs84_nodes.size() << endl;
859 cout << "tex coords = " << texcoords.size() << endl;
861 fprintf(fp, "# FGFS Scenery\n");
862 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
864 time_t calendar_time = time(NULL);
866 local_tm = localtime( &calendar_time );
868 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
869 fprintf(fp, "# Created %s\n", time_str );
872 // write bounding sphere
873 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
874 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
878 fprintf(fp, "# vertex list\n");
879 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
880 p = wgs84_nodes[i] - gbs_center;
882 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
886 fprintf(fp, "# vertex normal list\n");
887 for ( i = 0; i < (int)normals.size(); ++i ) {
889 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
893 // dump texture coordinates
894 fprintf(fp, "# texture coordinate list\n");
895 for ( i = 0; i < (int)texcoords.size(); ++i ) {
897 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
901 // dump individual triangles if they exist
902 if ( tris_v.size() > 0 ) {
903 fprintf(fp, "# triangle groups\n");
908 while ( start < (int)tri_materials.size() ) {
910 material = tri_materials[start];
911 while ( (end < (int)tri_materials.size()) &&
912 (material == tri_materials[end]) )
914 // cout << "end = " << end << endl;
917 // cout << "group = " << start << " to " << end - 1 << endl;
919 // make a list of points for the group
920 point_list group_nodes;
923 double bs_radius = 0;
924 for ( i = start; i < end; ++i ) {
925 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
926 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
927 bs_center = calc_center( group_nodes );
928 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
932 // write group headers
934 fprintf(fp, "# usemtl %s\n", material.c_str());
935 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
936 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
939 for ( i = start; i < end; ++i ) {
941 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
942 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
952 // dump triangle groups
953 if ( strips_v.size() > 0 ) {
954 fprintf(fp, "# triangle strips\n");
959 while ( start < (int)strip_materials.size() ) {
961 material = strip_materials[start];
962 while ( (end < (int)strip_materials.size()) &&
963 (material == strip_materials[end]) )
965 // cout << "end = " << end << endl;
968 // cout << "group = " << start << " to " << end - 1 << endl;
970 // make a list of points for the group
971 point_list group_nodes;
974 double bs_radius = 0;
975 for ( i = start; i < end; ++i ) {
976 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
977 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
978 bs_center = calc_center( group_nodes );
979 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
983 // write group headers
985 fprintf(fp, "# usemtl %s\n", material.c_str());
986 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
987 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
990 for ( i = start; i < end; ++i ) {
992 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
993 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1006 command = "gzip --force --best " + file;
1007 system(command.c_str());