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,
55 SG_TRIANGLE_FACES = 10,
56 SG_TRIANGLE_STRIPS = 11,
65 class sgSimpleBuffer {
74 inline sgSimpleBuffer( unsigned int s )
80 cout << "Creating a new buffer of size = " << size << endl;
84 inline ~sgSimpleBuffer() {
88 inline unsigned int get_size() const { return size; }
89 inline char *get_ptr() const { return ptr; }
90 inline void resize( unsigned int s ) {
98 cout << "resizing buffer to size = " << size << endl;
105 // calculate the center of a list of points, by taking the halfway
106 // point between the min and max points.
107 static Point3D calc_center( point_list& wgs84_nodes ) {
110 if ( wgs84_nodes.size() ) {
111 min = max = wgs84_nodes[0];
113 min = max = Point3D( 0 );
116 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
119 if ( p.x() < min.x() ) { min.setx( p.x() ); }
120 if ( p.y() < min.y() ) { min.sety( p.y() ); }
121 if ( p.z() < min.z() ) { min.setz( p.z() ); }
123 if ( p.x() > max.x() ) { max.setx( p.x() ); }
124 if ( p.y() > max.y() ) { max.sety( p.y() ); }
125 if ( p.z() > max.z() ) { max.setz( p.z() ); }
128 return ( min + max ) / 2.0;
131 // calculate the bounding sphere. Center is the center of the
132 // tile and zero elevation
133 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
135 double radius_squared = 0;
137 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
138 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
139 if ( dist_squared > radius_squared ) {
140 radius_squared = dist_squared;
144 return sqrt(radius_squared);
148 // read a binary file and populate the provided structures.
149 bool SGBinObject::read_bin( const string& file ) {
154 static sgSimpleBuffer buf( 32768 ); // 32 Kb
156 // zero out structures
157 gbs_center = Point3D( 0 );
166 tri_materials.clear();
170 strip_materials.clear();
174 fan_materials.clear();
177 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
178 string filegz = file + ".gz";
179 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
180 // cout << "ERROR: opening " << file << " or " << filegz
181 // << "for reading!" << endl;
191 sgReadUInt( fp, &header );
192 if ( ((header & 0xFF000000) >> 24) == 'S' &&
193 ((header & 0x00FF0000) >> 16) == 'G' ) {
194 // cout << "Good header" << endl;
196 version = (header & 0x0000FFFF);
197 // cout << "File version = " << version << endl;
199 // close the file before we return
205 // read creation time
206 time_t calendar_time;
207 sgReadLong( fp, &calendar_time );
209 local_tm = localtime( &calendar_time );
211 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
212 // cout << "File created on " << time_str << endl;
214 // read number of top level objects
216 sgReadShort( fp, &nobjects );
217 // cout << "Total objects to read = " << nobjects << endl;
220 for ( i = 0; i < nobjects; ++i ) {
221 // read object header
223 short nproperties, nelements;
224 sgReadChar( fp, &obj_type );
225 sgReadShort( fp, &nproperties );
226 sgReadShort( fp, &nelements );
228 // cout << "object " << i << " = " << (int)obj_type << " props = "
229 // << nproperties << " elements = " << nelements << endl;
231 if ( obj_type == SG_BOUNDING_SPHERE ) {
232 // read bounding sphere properties
233 for ( j = 0; j < nproperties; ++j ) {
235 sgReadChar( fp, &prop_type );
237 sgReadUInt( fp, &nbytes );
238 // cout << "property size = " << nbytes << endl;
239 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
240 char *ptr = buf.get_ptr();
241 sgReadBytes( fp, nbytes, ptr );
244 // read bounding sphere elements
245 for ( j = 0; j < nelements; ++j ) {
246 sgReadUInt( fp, &nbytes );
247 // cout << "element size = " << nbytes << endl;
248 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
249 char *ptr = buf.get_ptr();
250 sgReadBytes( fp, nbytes, ptr );
252 double *dptr = (double *)ptr;
253 if ( sgIsBigEndian() ) {
254 sgEndianSwap( (uint64 *)&(dptr[0]) );
255 sgEndianSwap( (uint64 *)&(dptr[1]) );
256 sgEndianSwap( (uint64 *)&(dptr[2]) );
258 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
259 // cout << "Center = " << gbs_center << endl;
260 ptr += sizeof(double) * 3;
262 float *fptr = (float *)ptr;
263 if ( sgIsBigEndian() ) {
264 sgEndianSwap( (unsigned int *)fptr );
266 gbs_radius = fptr[0];
267 // cout << "Bounding radius = " << gbs_radius << endl;
269 } else if ( obj_type == SG_VERTEX_LIST ) {
270 // read vertex list properties
271 for ( j = 0; j < nproperties; ++j ) {
273 sgReadChar( fp, &prop_type );
275 sgReadUInt( fp, &nbytes );
276 // cout << "property size = " << nbytes << endl;
277 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
278 char *ptr = buf.get_ptr();
279 sgReadBytes( fp, nbytes, ptr );
282 // read vertex list elements
283 for ( j = 0; j < nelements; ++j ) {
284 sgReadUInt( fp, &nbytes );
285 // cout << "element size = " << nbytes << endl;
286 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
287 char *ptr = buf.get_ptr();
288 sgReadBytes( fp, nbytes, ptr );
289 int count = nbytes / (sizeof(float) * 3);
290 float *fptr = (float *)ptr;
291 for ( k = 0; k < count; ++k ) {
292 if ( sgIsBigEndian() ) {
293 sgEndianSwap( (unsigned int *)&(fptr[0]) );
294 sgEndianSwap( (unsigned int *)&(fptr[1]) );
295 sgEndianSwap( (unsigned int *)&(fptr[2]) );
297 p = Point3D( fptr[0], fptr[1], fptr[2] );
298 // cout << "node = " << p << endl;
299 wgs84_nodes.push_back( p );
303 } else if ( obj_type == SG_NORMAL_LIST ) {
304 // read normal list properties
305 for ( j = 0; j < nproperties; ++j ) {
307 sgReadChar( fp, &prop_type );
309 sgReadUInt( fp, &nbytes );
310 // cout << "property size = " << nbytes << endl;
311 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
312 char *ptr = buf.get_ptr();
313 sgReadBytes( fp, nbytes, ptr );
316 // read normal list elements
317 for ( j = 0; j < nelements; ++j ) {
318 sgReadUInt( fp, &nbytes );
319 // cout << "element size = " << nbytes << endl;
320 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
321 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
322 sgReadBytes( fp, nbytes, ptr );
323 int count = nbytes / 3;
324 for ( k = 0; k < count; ++k ) {
327 (ptr[0]) / 127.5 - 1.0,
328 (ptr[1]) / 127.5 - 1.0,
329 (ptr[2]) / 127.5 - 1.0 );
330 sgdNormalizeVec3( normal );
332 p = Point3D( normal[0], normal[1], normal[2] );
333 // cout << "normal = " << p << endl;
334 normals.push_back( p );
338 } else if ( obj_type == SG_TEXCOORD_LIST ) {
339 // read texcoord list properties
340 for ( j = 0; j < nproperties; ++j ) {
342 sgReadChar( fp, &prop_type );
344 sgReadUInt( fp, &nbytes );
345 // cout << "property size = " << nbytes << endl;
346 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
347 char *ptr = buf.get_ptr();
348 sgReadBytes( fp, nbytes, ptr );
351 // read texcoord list elements
352 for ( j = 0; j < nelements; ++j ) {
353 sgReadUInt( fp, &nbytes );
354 // cout << "element size = " << nbytes << endl;
355 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
356 char *ptr = buf.get_ptr();
357 sgReadBytes( fp, nbytes, ptr );
358 int count = nbytes / (sizeof(float) * 2);
359 float *fptr = (float *)ptr;
360 for ( k = 0; k < count; ++k ) {
361 if ( sgIsBigEndian() ) {
362 sgEndianSwap( (unsigned int *)&(fptr[0]) );
363 sgEndianSwap( (unsigned int *)&(fptr[1]) );
365 p = Point3D( fptr[0], fptr[1], 0 );
366 // cout << "texcoord = " << p << endl;
367 texcoords.push_back( p );
371 } else if ( obj_type == SG_TRIANGLE_FACES ) {
372 // read triangle face properties
373 for ( j = 0; j < nproperties; ++j ) {
375 sgReadChar( fp, &prop_type );
377 sgReadUInt( fp, &nbytes );
378 // cout << "property size = " << nbytes << endl;
379 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
380 char *ptr = buf.get_ptr();
381 sgReadBytes( fp, nbytes, ptr );
382 if ( prop_type == SG_MATERIAL ) {
383 strncpy( material, ptr, nbytes );
384 material[nbytes] = '\0';
385 // cout << "material type = " << material << endl;
389 // read triangle face elements
390 for ( j = 0; j < nelements; ++j ) {
391 sgReadUInt( fp, &nbytes );
392 // cout << "element size = " << nbytes << endl;
393 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
394 char *ptr = buf.get_ptr();
395 sgReadBytes( fp, nbytes, ptr );
396 int count = nbytes / (sizeof(short) * 2);
397 short *sptr = (short *)ptr;
399 vs.clear(); tcs.clear();
400 for ( k = 0; k < count; ++k ) {
401 if ( sgIsBigEndian() ) {
402 sgEndianSwap( (unsigned short *)&(sptr[0]) );
403 sgEndianSwap( (unsigned short *)&(sptr[1]) );
405 vs.push_back( sptr[0] );
406 tcs.push_back( sptr[1] );
407 // cout << sptr[0] << "/" << sptr[1] << " ";
411 tris_v.push_back( vs );
412 tris_tc.push_back( tcs );
413 tri_materials.push_back( material );
415 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
416 // read triangle strip properties
417 for ( j = 0; j < nproperties; ++j ) {
419 sgReadChar( fp, &prop_type );
421 sgReadUInt( fp, &nbytes );
422 // cout << "property size = " << nbytes << endl;
423 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
424 char *ptr = buf.get_ptr();
425 sgReadBytes( fp, nbytes, ptr );
426 if ( prop_type == SG_MATERIAL ) {
427 strncpy( material, ptr, nbytes );
428 material[nbytes] = '\0';
429 // cout << "material type = " << material << endl;
433 // read triangle strip elements
434 for ( j = 0; j < nelements; ++j ) {
435 sgReadUInt( fp, &nbytes );
436 // cout << "element size = " << nbytes << endl;
437 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
438 char *ptr = buf.get_ptr();
439 sgReadBytes( fp, nbytes, ptr );
440 int count = nbytes / (sizeof(short) * 2);
441 short *sptr = (short *)ptr;
443 vs.clear(); tcs.clear();
444 for ( k = 0; k < count; ++k ) {
445 if ( sgIsBigEndian() ) {
446 sgEndianSwap( (unsigned short *)&(sptr[0]) );
447 sgEndianSwap( (unsigned short *)&(sptr[1]) );
449 vs.push_back( sptr[0] );
450 tcs.push_back( sptr[1] );
451 // cout << sptr[0] << "/" << sptr[1] << " ";
455 strips_v.push_back( vs );
456 strips_tc.push_back( tcs );
457 strip_materials.push_back( material );
459 } else if ( obj_type == SG_TRIANGLE_FANS ) {
460 // read triangle fan properties
461 for ( j = 0; j < nproperties; ++j ) {
463 sgReadChar( fp, &prop_type );
465 sgReadUInt( fp, &nbytes );
466 // cout << "property size = " << nbytes << endl;
467 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
468 char *ptr = buf.get_ptr();
469 sgReadBytes( fp, nbytes, ptr );
470 if ( prop_type == SG_MATERIAL ) {
471 strncpy( material, ptr, nbytes );
472 material[nbytes] = '\0';
473 // cout << "material type = " << material << endl;
477 // read triangle fan elements
478 for ( j = 0; j < nelements; ++j ) {
479 sgReadUInt( fp, &nbytes );
480 // cout << "element size = " << nbytes << endl;
481 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
482 char *ptr = buf.get_ptr();
483 sgReadBytes( fp, nbytes, ptr );
484 int count = nbytes / (sizeof(short) * 2);
485 short *sptr = (short *)ptr;
487 vs.clear(); tcs.clear();
488 for ( k = 0; k < count; ++k ) {
489 if ( sgIsBigEndian() ) {
490 sgEndianSwap( (unsigned short *)&(sptr[0]) );
491 sgEndianSwap( (unsigned short *)&(sptr[1]) );
493 vs.push_back( sptr[0] );
494 tcs.push_back( sptr[1] );
495 // cout << sptr[0] << "/" << sptr[1] << " ";
499 fans_v.push_back( vs );
500 fans_tc.push_back( tcs );
501 fan_materials.push_back( material );
504 // unknown object type, just skip
507 for ( j = 0; j < nproperties; ++j ) {
509 sgReadChar( fp, &prop_type );
511 sgReadUInt( fp, &nbytes );
512 // cout << "property size = " << nbytes << endl;
513 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
514 char *ptr = buf.get_ptr();
515 sgReadBytes( fp, nbytes, ptr );
519 for ( j = 0; j < nelements; ++j ) {
520 sgReadUInt( fp, &nbytes );
521 // cout << "element size = " << nbytes << endl;
522 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
523 char *ptr = buf.get_ptr();
524 sgReadBytes( fp, nbytes, ptr );
532 if ( sgReadError() ) {
533 cout << "We detected an error while reading the file." << endl;
541 // write out the structures to a binary file. We assume that the
542 // groups come to us sorted by material property. If not, things
543 // don't break, but the result won't be as optimal.
544 bool SGBinObject::write_bin( const string& base, const string& name,
552 string dir = base + "/" + b.gen_base_path();
553 string command = "mkdir -p " + dir;
555 system( (string("mkdir ") + dir).c_str() );
557 system(command.c_str());
560 string file = dir + "/" + name + ".gz";
561 cout << "Output file = " << file << endl;
564 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
565 cout << "ERROR: opening " << file << " for writing!" << endl;
571 cout << "triangles size = " << tris_v.size() << " tri_materials = "
572 << tri_materials.size() << endl;
573 cout << "strips size = " << strips_v.size() << " strip_materials = "
574 << strip_materials.size() << endl;
575 cout << "fans size = " << fans_v.size() << " fan_materials = "
576 << fan_materials.size() << endl;
578 cout << "points = " << wgs84_nodes.size() << endl;
579 cout << "tex coords = " << texcoords.size() << endl;
581 // write header magic
582 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
583 time_t calendar_time = time(NULL);
584 sgWriteLong( fp, (long int)calendar_time );
586 // calculate and write number of top level objects
591 nobjects++; // for gbs
592 nobjects++; // for vertices
593 nobjects++; // for normals
594 nobjects++; // for texcoords
599 while ( start < (int)tri_materials.size() ) {
600 material = tri_materials[start];
601 while ( (end < (int)tri_materials.size()) &&
602 (material == tri_materials[end]) ) {
606 start = end; end = start + 1;
613 while ( start < (int)strip_materials.size() ) {
614 material = strip_materials[start];
615 while ( (end < (int)strip_materials.size()) &&
616 (material == strip_materials[end]) ) {
620 start = end; end = start + 1;
627 while ( start < (int)fan_materials.size() ) {
628 material = fan_materials[start];
629 while ( (end < (int)fan_materials.size()) &&
630 (material == fan_materials[end]) ) {
634 start = end; end = start + 1;
638 cout << "total top level objects = " << nobjects << endl;
639 sgWriteShort( fp, nobjects );
641 // write bounding sphere
642 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
643 sgWriteShort( fp, 0 ); // nproperties
644 sgWriteShort( fp, 1 ); // nelements
646 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
648 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
649 sgWritedVec3( fp, center );
650 sgWriteFloat( fp, gbs_radius );
653 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
654 sgWriteShort( fp, 0 ); // nproperties
655 sgWriteShort( fp, 1 ); // nelements
656 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
657 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
658 p = wgs84_nodes[i] - gbs_center;
659 sgSetVec3( pt, p.x(), p.y(), p.z() );
660 sgWriteVec3( fp, pt );
663 // dump vertex normal list
664 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
665 sgWriteShort( fp, 0 ); // nproperties
666 sgWriteShort( fp, 1 ); // nelements
667 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
669 for ( i = 0; i < (int)normals.size(); ++i ) {
671 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
672 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
673 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
674 sgWriteBytes( fp, 3, normal );
677 // dump texture coordinates
678 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
679 sgWriteShort( fp, 0 ); // nproperties
680 sgWriteShort( fp, 1 ); // nelements
681 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
682 for ( i = 0; i < (int)texcoords.size(); ++i ) {
684 sgSetVec2( t, p.x(), p.y() );
685 sgWriteVec2( fp, t );
688 // dump individual triangles if they exist
689 if ( tris_v.size() > 0 ) {
693 while ( start < (int)tri_materials.size() ) {
695 material = tri_materials[start];
696 while ( (end < (int)tri_materials.size()) &&
697 (material == tri_materials[end]) )
699 // cout << "end = " << end << endl;
702 // cout << "group = " << start << " to " << end - 1 << endl;
704 // write group headers
705 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
706 sgWriteShort( fp, 1 ); // nproperties
707 sgWriteShort( fp, 1 ); // nelements
709 sgWriteChar( fp, (char)SG_MATERIAL ); // property
710 sgWriteUInt( fp, material.length() ); // nbytes
711 sgWriteBytes( fp, material.length(), material.c_str() );
713 sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
716 for ( i = start; i < end; ++i ) {
717 for ( j = 0; j < 3; ++j ) {
718 sgWriteShort( fp, (short)tris_v[i][j] );
719 sgWriteShort( fp, (short)tris_tc[i][j] );
728 // dump triangle strips
729 if ( strips_v.size() > 0 ) {
733 while ( start < (int)strip_materials.size() ) {
735 material = strip_materials[start];
736 while ( (end < (int)strip_materials.size()) &&
737 (material == strip_materials[end]) )
739 // cout << "end = " << end << endl;
742 // cout << "group = " << start << " to " << end - 1 << endl;
744 // write group headers
745 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
746 sgWriteShort( fp, 1 ); // nproperties
747 sgWriteShort( fp, end - start ); // nelements
749 sgWriteChar( fp, (char)SG_MATERIAL ); // property
750 sgWriteUInt( fp, material.length() ); // nbytes
751 sgWriteBytes( fp, material.length(), material.c_str() );
754 for ( i = start; i < end; ++i ) {
756 sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
757 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
758 sgWriteShort( fp, (short)strips_v[i][j] );
759 sgWriteShort( fp, (short)strips_tc[i][j] );
768 // dump triangle fans
769 if ( fans_v.size() > 0 ) {
773 while ( start < (int)fan_materials.size() ) {
775 material = fan_materials[start];
776 while ( (end < (int)fan_materials.size()) &&
777 (material == fan_materials[end]) )
779 // cout << "end = " << end << endl;
782 // cout << "group = " << start << " to " << end - 1 << endl;
784 // write group headers
785 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
786 sgWriteShort( fp, 1 ); // nproperties
787 sgWriteShort( fp, end - start ); // nelements
789 sgWriteChar( fp, (char)SG_MATERIAL ); // property
790 sgWriteUInt( fp, material.length() ); // nbytes
791 sgWriteBytes( fp, material.length(), material.c_str() );
794 for ( i = start; i < end; ++i ) {
796 sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
797 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
798 sgWriteShort( fp, (short)fans_v[i][j] );
799 sgWriteShort( fp, (short)fans_tc[i][j] );
811 if ( sgWriteError() ) {
812 cout << "We detected an error while writing the file." << endl;
820 // write out the structures to an ASCII file. We assume that the
821 // groups come to us sorted by material property. If not, things
822 // don't break, but the result won't be as optimal.
823 bool SGBinObject::write_ascii( const string& base, const string& name,
829 string dir = base + "/" + b.gen_base_path();
830 string command = "mkdir -p " + dir;
832 system( (string("mkdir ") + dir).c_str() );
834 system(command.c_str());
837 // string file = dir + "/" + b.gen_index_str();
838 string file = dir + "/" + name;
839 cout << "Output file = " << file << endl;
842 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
843 cout << "ERROR: opening " << file << " for writing!" << endl;
847 cout << "triangles size = " << tris_v.size() << " tri_materials = "
848 << tri_materials.size() << endl;
849 cout << "strips size = " << strips_v.size() << " strip_materials = "
850 << strip_materials.size() << endl;
851 cout << "fans size = " << fans_v.size() << " fan_materials = "
852 << fan_materials.size() << endl;
854 cout << "points = " << wgs84_nodes.size() << endl;
855 cout << "tex coords = " << texcoords.size() << endl;
857 fprintf(fp, "# FGFS Scenery\n");
858 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
860 time_t calendar_time = time(NULL);
862 local_tm = localtime( &calendar_time );
864 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
865 fprintf(fp, "# Created %s\n", time_str );
868 // write bounding sphere
869 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
870 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
874 fprintf(fp, "# vertex list\n");
875 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
876 p = wgs84_nodes[i] - gbs_center;
878 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
882 fprintf(fp, "# vertex normal list\n");
883 for ( i = 0; i < (int)normals.size(); ++i ) {
885 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
889 // dump texture coordinates
890 fprintf(fp, "# texture coordinate list\n");
891 for ( i = 0; i < (int)texcoords.size(); ++i ) {
893 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
897 // dump individual triangles if they exist
898 if ( tris_v.size() > 0 ) {
899 fprintf(fp, "# triangle groups\n");
904 while ( start < (int)tri_materials.size() ) {
906 material = tri_materials[start];
907 while ( (end < (int)tri_materials.size()) &&
908 (material == tri_materials[end]) )
910 // cout << "end = " << end << endl;
913 // cout << "group = " << start << " to " << end - 1 << endl;
915 // make a list of points for the group
916 point_list group_nodes;
919 double bs_radius = 0;
920 for ( i = start; i < end; ++i ) {
921 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
922 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
923 bs_center = calc_center( group_nodes );
924 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
928 // write group headers
930 fprintf(fp, "# usemtl %s\n", material.c_str());
931 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
932 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
935 for ( i = start; i < end; ++i ) {
937 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
938 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
948 // dump triangle groups
949 if ( strips_v.size() > 0 ) {
950 fprintf(fp, "# triangle strips\n");
955 while ( start < (int)strip_materials.size() ) {
957 material = strip_materials[start];
958 while ( (end < (int)strip_materials.size()) &&
959 (material == strip_materials[end]) )
961 // cout << "end = " << end << endl;
964 // cout << "group = " << start << " to " << end - 1 << endl;
966 // make a list of points for the group
967 point_list group_nodes;
970 double bs_radius = 0;
971 for ( i = start; i < end; ++i ) {
972 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
973 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
974 bs_center = calc_center( group_nodes );
975 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
979 // write group headers
981 fprintf(fp, "# usemtl %s\n", material.c_str());
982 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
983 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
986 for ( i = start; i < end; ++i ) {
988 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
989 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1002 command = "gzip --force --best " + file;
1003 system(command.c_str());