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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
39 #include <simgear/bucket/newbucket.hxx>
40 #include <simgear/misc/sg_path.hxx>
42 #include "lowlevel.hxx"
43 #include "sg_binobj.hxx"
46 SG_USING_STD( string );
47 SG_USING_STD( vector );
53 SG_BOUNDING_SPHERE = 0,
62 SG_TRIANGLE_FACES = 10,
63 SG_TRIANGLE_STRIPS = 11,
68 SG_IDX_VERTICES = 0x01,
69 SG_IDX_NORMALS = 0x02,
71 SG_IDX_TEXCOORDS = 0x08
74 enum sgPropertyTypes {
80 class sgSimpleBuffer {
89 inline sgSimpleBuffer( unsigned int s )
95 SG_LOG(SG_EVENT, SG_DEBUG, "Creating a new buffer of size = " << size);
99 inline ~sgSimpleBuffer() {
103 inline unsigned int get_size() const { return size; }
104 inline char *get_ptr() const { return ptr; }
105 inline void resize( unsigned int s ) {
113 SG_LOG(SG_EVENT, SG_DEBUG, "resizing buffer to size = " << size);
114 ptr = new char[size];
120 // calculate the center of a list of points, by taking the halfway
121 // point between the min and max points.
122 Point3D sgCalcCenter( point_list& wgs84_nodes ) {
125 if ( wgs84_nodes.size() ) {
126 min = max = wgs84_nodes[0];
128 min = max = Point3D( 0 );
131 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
134 if ( p.x() < min.x() ) { min.setx( p.x() ); }
135 if ( p.y() < min.y() ) { min.sety( p.y() ); }
136 if ( p.z() < min.z() ) { min.setz( p.z() ); }
138 if ( p.x() > max.x() ) { max.setx( p.x() ); }
139 if ( p.y() > max.y() ) { max.sety( p.y() ); }
140 if ( p.z() > max.z() ) { max.setz( p.z() ); }
143 return ( min + max ) / 2.0;
146 // calculate the bounding sphere. Center is the center of the
147 // tile and zero elevation
148 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
150 double radius_squared = 0;
152 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
153 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
154 if ( dist_squared > radius_squared ) {
155 radius_squared = dist_squared;
159 return sqrt(radius_squared);
164 // read object properties
165 static void read_object( gzFile fp,
169 group_list *vertices,
172 group_list *texcoords,
173 string_list *materials )
176 unsigned char idx_mask;
178 bool do_vertices, do_normals, do_colors, do_texcoords;
180 sgSimpleBuffer buf( 32768 ); // 32 Kb
184 if ( obj_type == SG_POINTS ) {
186 idx_mask = SG_IDX_VERTICES;
190 do_texcoords = false;
193 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
200 for ( j = 0; j < nproperties; ++j ) {
202 sgReadChar( fp, &prop_type );
204 sgReadUInt( fp, &nbytes );
205 // cout << "property size = " << nbytes << endl;
206 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
207 char *ptr = buf.get_ptr();
208 sgReadBytes( fp, nbytes, ptr );
209 if ( prop_type == SG_MATERIAL ) {
210 strncpy( material, ptr, nbytes );
211 material[nbytes] = '\0';
212 // cout << "material type = " << material << endl;
213 } else if ( prop_type == SG_INDEX_TYPES ) {
215 // cout << "idx_mask = " << (int)idx_mask << endl;
220 do_texcoords = false;
221 if ( idx_mask & SG_IDX_VERTICES ) {
225 if ( idx_mask & SG_IDX_NORMALS ) {
229 if ( idx_mask & SG_IDX_COLORS ) {
233 if ( idx_mask & SG_IDX_TEXCOORDS ) {
240 for ( j = 0; j < nelements; ++j ) {
241 sgReadUInt( fp, &nbytes );
242 // cout << "element size = " << nbytes << endl;
243 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
244 char *ptr = buf.get_ptr();
245 sgReadBytes( fp, nbytes, ptr );
246 int count = nbytes / (idx_size * sizeof(unsigned short));
247 unsigned short *sptr = (unsigned short *)ptr;
248 int_list vs; vs.clear();
249 int_list ns; ns.clear();
250 int_list cs; cs.clear();
251 int_list tcs; tcs.clear();
252 for ( k = 0; k < count; ++k ) {
253 if ( sgIsBigEndian() ) {
254 for ( idx = 0; idx < idx_size; ++idx ) {
255 sgEndianSwap( (uint16_t *)&(sptr[idx]) );
260 vs.push_back( sptr[idx++] );
263 ns.push_back( sptr[idx++] );
266 cs.push_back( sptr[idx++] );
268 if ( do_texcoords ) {
269 tcs.push_back( sptr[idx++] );
271 // cout << sptr[0] << " ";
275 vertices->push_back( vs );
276 normals->push_back( ns );
277 colors->push_back( cs );
278 texcoords->push_back( tcs );
279 materials->push_back( material );
284 // read a binary file and populate the provided structures.
285 bool SGBinObject::read_bin( const string& file ) {
289 sgSimpleBuffer buf( 32768 ); // 32 Kb
291 // zero out structures
292 gbs_center = SGVec3d(0, 0, 0);
303 pt_materials.clear();
309 tri_materials.clear();
315 strip_materials.clear();
321 fan_materials.clear();
324 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
325 string filegz = file + ".gz";
326 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
327 SG_LOG( SG_EVENT, SG_ALERT,
328 "ERROR: opening " << file << " or " << filegz << " for reading!");
338 sgReadUInt( fp, &header );
339 if ( ((header & 0xFF000000) >> 24) == 'S' &&
340 ((header & 0x00FF0000) >> 16) == 'G' ) {
341 // cout << "Good header" << endl;
343 version = (header & 0x0000FFFF);
344 // cout << "File version = " << version << endl;
346 // close the file before we return
352 // read creation time
353 unsigned int foo_calendar_time;
354 sgReadUInt( fp, &foo_calendar_time );
357 time_t calendar_time = foo_calendar_time;
358 // The following code has a global effect on the host application
359 // and can screws up the time elsewhere. It should be avoided
360 // unless you need this for debugging in which case you should
361 // disable it again once the debugging task is finished.
363 local_tm = localtime( &calendar_time );
365 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
366 SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
369 // read number of top level objects
370 unsigned short nobjects;
371 if ( version >= 7 ) {
372 sgReadUShort( fp, &nobjects );
375 sgReadShort( fp, &tmp );
378 // cout << "Total objects to read = " << nobjects << endl;
381 for ( i = 0; i < nobjects; ++i ) {
382 // read object header
384 unsigned short nproperties, nelements;
385 sgReadChar( fp, &obj_type );
386 if ( version >= 7 ) {
387 sgReadUShort( fp, &nproperties );
388 sgReadUShort( fp, &nelements );
391 sgReadShort( fp, &tmp );
393 sgReadShort( fp, &tmp );
397 // cout << "object " << i << " = " << (int)obj_type << " props = "
398 // << nproperties << " elements = " << nelements << endl;
400 if ( obj_type == SG_BOUNDING_SPHERE ) {
401 // read bounding sphere properties
402 for ( j = 0; j < nproperties; ++j ) {
404 sgReadChar( fp, &prop_type );
406 sgReadUInt( fp, &nbytes );
407 // cout << "property size = " << nbytes << endl;
408 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
409 char *ptr = buf.get_ptr();
410 sgReadBytes( fp, nbytes, ptr );
413 // read bounding sphere elements
414 for ( j = 0; j < nelements; ++j ) {
415 sgReadUInt( fp, &nbytes );
416 // cout << "element size = " << nbytes << endl;
417 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
418 char *ptr = buf.get_ptr();
419 sgReadBytes( fp, nbytes, ptr );
421 double *dptr = (double *)ptr;
422 if ( sgIsBigEndian() ) {
423 sgEndianSwap( (uint64_t *)&(dptr[0]) );
424 sgEndianSwap( (uint64_t *)&(dptr[1]) );
425 sgEndianSwap( (uint64_t *)&(dptr[2]) );
427 gbs_center = SGVec3d( dptr[0], dptr[1], dptr[2] );
428 // cout << "Center = " << gbs_center << endl;
429 ptr += sizeof(double) * 3;
431 float *fptr = (float *)ptr;
432 if ( sgIsBigEndian() ) {
433 sgEndianSwap( (uint32_t *)fptr );
435 gbs_radius = fptr[0];
436 // cout << "Bounding radius = " << gbs_radius << endl;
438 } else if ( obj_type == SG_VERTEX_LIST ) {
439 // read vertex list properties
440 for ( j = 0; j < nproperties; ++j ) {
442 sgReadChar( fp, &prop_type );
444 sgReadUInt( fp, &nbytes );
445 // cout << "property size = " << nbytes << endl;
446 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
447 char *ptr = buf.get_ptr();
448 sgReadBytes( fp, nbytes, ptr );
451 // read vertex list elements
452 for ( j = 0; j < nelements; ++j ) {
453 sgReadUInt( fp, &nbytes );
454 // cout << "element size = " << nbytes << endl;
455 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
456 char *ptr = buf.get_ptr();
457 sgReadBytes( fp, nbytes, ptr );
458 int count = nbytes / (sizeof(float) * 3);
459 float *fptr = (float *)ptr;
460 wgs84_nodes.reserve( count );
461 for ( k = 0; k < count; ++k ) {
462 if ( sgIsBigEndian() ) {
463 sgEndianSwap( (uint32_t *)&(fptr[0]) );
464 sgEndianSwap( (uint32_t *)&(fptr[1]) );
465 sgEndianSwap( (uint32_t *)&(fptr[2]) );
467 wgs84_nodes.push_back( SGVec3d(fptr[0], fptr[1], fptr[2]) );
471 } else if ( obj_type == SG_COLOR_LIST ) {
472 // read color list properties
473 for ( j = 0; j < nproperties; ++j ) {
475 sgReadChar( fp, &prop_type );
477 sgReadUInt( fp, &nbytes );
478 // cout << "property size = " << nbytes << endl;
479 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
480 char *ptr = buf.get_ptr();
481 sgReadBytes( fp, nbytes, ptr );
484 // read color list elements
485 for ( j = 0; j < nelements; ++j ) {
486 sgReadUInt( fp, &nbytes );
487 // cout << "element size = " << nbytes << endl;
488 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
489 char *ptr = buf.get_ptr();
490 sgReadBytes( fp, nbytes, ptr );
491 int count = nbytes / (sizeof(float) * 4);
492 float *fptr = (float *)ptr;
493 colors.reserve(count);
494 for ( k = 0; k < count; ++k ) {
495 if ( sgIsBigEndian() ) {
496 sgEndianSwap( (uint32_t *)&(fptr[0]) );
497 sgEndianSwap( (uint32_t *)&(fptr[1]) );
498 sgEndianSwap( (uint32_t *)&(fptr[2]) );
499 sgEndianSwap( (uint32_t *)&(fptr[3]) );
501 SGVec4f color( fptr[0], fptr[1], fptr[2], fptr[3] );
502 colors.push_back( color );
506 } else if ( obj_type == SG_NORMAL_LIST ) {
507 // read normal list properties
508 for ( j = 0; j < nproperties; ++j ) {
510 sgReadChar( fp, &prop_type );
512 sgReadUInt( fp, &nbytes );
513 // cout << "property size = " << nbytes << endl;
514 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
515 char *ptr = buf.get_ptr();
516 sgReadBytes( fp, nbytes, ptr );
519 // read normal list elements
520 for ( j = 0; j < nelements; ++j ) {
521 sgReadUInt( fp, &nbytes );
522 // cout << "element size = " << nbytes << endl;
523 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
524 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
525 sgReadBytes( fp, nbytes, ptr );
526 int count = nbytes / 3;
527 normals.reserve( count );
528 for ( k = 0; k < count; ++k ) {
529 SGVec3f normal((ptr[0]) / 127.5 - 1.0,
530 (ptr[1]) / 127.5 - 1.0,
531 (ptr[2]) / 127.5 - 1.0);
533 normals.push_back(normalize(normal));
537 } else if ( obj_type == SG_TEXCOORD_LIST ) {
538 // read texcoord list properties
539 for ( j = 0; j < nproperties; ++j ) {
541 sgReadChar( fp, &prop_type );
543 sgReadUInt( fp, &nbytes );
544 // cout << "property size = " << nbytes << endl;
545 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
546 char *ptr = buf.get_ptr();
547 sgReadBytes( fp, nbytes, ptr );
550 // read texcoord list elements
551 for ( j = 0; j < nelements; ++j ) {
552 sgReadUInt( fp, &nbytes );
553 // cout << "element size = " << nbytes << endl;
554 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
555 char *ptr = buf.get_ptr();
556 sgReadBytes( fp, nbytes, ptr );
557 int count = nbytes / (sizeof(float) * 2);
558 float *fptr = (float *)ptr;
559 texcoords.reserve(count);
560 for ( k = 0; k < count; ++k ) {
561 if ( sgIsBigEndian() ) {
562 sgEndianSwap( (uint32_t *)&(fptr[0]) );
563 sgEndianSwap( (uint32_t *)&(fptr[1]) );
565 texcoords.push_back( SGVec2f( fptr[0], fptr[1] ) );
569 } else if ( obj_type == SG_POINTS ) {
570 // read point elements
571 read_object( fp, SG_POINTS, nproperties, nelements,
572 &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
573 } else if ( obj_type == SG_TRIANGLE_FACES ) {
574 // read triangle face properties
575 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
576 &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
577 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
578 // read triangle strip properties
579 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
580 &strips_v, &strips_n, &strips_c, &strips_tc,
582 } else if ( obj_type == SG_TRIANGLE_FANS ) {
583 // read triangle fan properties
584 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
585 &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
587 // unknown object type, just skip
590 for ( j = 0; j < nproperties; ++j ) {
592 sgReadChar( fp, &prop_type );
594 sgReadUInt( fp, &nbytes );
595 // cout << "property size = " << nbytes << endl;
596 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
597 char *ptr = buf.get_ptr();
598 sgReadBytes( fp, nbytes, ptr );
602 for ( j = 0; j < nelements; ++j ) {
603 sgReadUInt( fp, &nbytes );
604 // cout << "element size = " << nbytes << endl;
605 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
606 char *ptr = buf.get_ptr();
607 sgReadBytes( fp, nbytes, ptr );
615 if ( sgReadError() ) {
616 cout << "We detected an error while reading the file." << endl;
624 // write out the structures to a binary file. We assume that the
625 // groups come to us sorted by material property. If not, things
626 // don't break, but the result won't be as optimal.
627 bool SGBinObject::write_bin( const string& base, const string& name,
631 unsigned char idx_mask;
634 SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
635 file.create_dir( 0755 );
636 cout << "Output file = " << file.str() << endl;
639 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
640 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
646 cout << "points size = " << pts_v.size() << " pt_materials = "
647 << pt_materials.size() << endl;
648 cout << "triangles size = " << tris_v.size() << " tri_materials = "
649 << tri_materials.size() << endl;
650 cout << "strips size = " << strips_v.size() << " strip_materials = "
651 << strip_materials.size() << endl;
652 cout << "fans size = " << fans_v.size() << " fan_materials = "
653 << fan_materials.size() << endl;
655 cout << "nodes = " << wgs84_nodes.size() << endl;
656 cout << "colors = " << colors.size() << endl;
657 cout << "normals = " << normals.size() << endl;
658 cout << "tex coords = " << texcoords.size() << endl;
660 // write header magic
661 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
662 time_t calendar_time = time(NULL);
663 sgWriteLong( fp, (int32_t)calendar_time );
665 // calculate and write number of top level objects
669 unsigned short nobjects = 0;
670 nobjects++; // for gbs
671 nobjects++; // for vertices
672 nobjects++; // for colors
673 nobjects++; // for normals
674 nobjects++; // for texcoords
677 unsigned short npts = 0;
679 while ( start < (int)pt_materials.size() ) {
680 material = pt_materials[start];
681 while ( (end < (int)pt_materials.size()) &&
682 (material == pt_materials[end]) ) {
686 start = end; end = start + 1;
691 unsigned short ntris = 0;
693 while ( start < (int)tri_materials.size() ) {
694 material = tri_materials[start];
695 while ( (end < (int)tri_materials.size()) &&
696 (material == tri_materials[end]) ) {
700 start = end; end = start + 1;
705 unsigned short nstrips = 0;
707 while ( start < (int)strip_materials.size() ) {
708 material = strip_materials[start];
709 while ( (end < (int)strip_materials.size()) &&
710 (material == strip_materials[end]) ) {
714 start = end; end = start + 1;
719 unsigned short nfans = 0;
721 while ( start < (int)fan_materials.size() ) {
722 material = fan_materials[start];
723 while ( (end < (int)fan_materials.size()) &&
724 (material == fan_materials[end]) ) {
728 start = end; end = start + 1;
732 cout << "total top level objects = " << nobjects << endl;
733 sgWriteUShort( fp, nobjects );
735 // write bounding sphere
736 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
737 sgWriteUShort( fp, 0 ); // nproperties
738 sgWriteUShort( fp, 1 ); // nelements
740 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
742 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
743 sgWritedVec3( fp, center );
744 sgWriteFloat( fp, gbs_radius );
747 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
748 sgWriteUShort( fp, 0 ); // nproperties
749 sgWriteUShort( fp, 1 ); // nelements
750 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
751 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
752 SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center);
753 sgWriteVec3( fp, p.data() );
756 // dump vertex color list
757 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
758 sgWriteUShort( fp, 0 ); // nproperties
759 sgWriteUShort( fp, 1 ); // nelements
760 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
761 for ( i = 0; i < (int)colors.size(); ++i ) {
762 sgWriteVec4( fp, colors[i].data() );
765 // dump vertex normal list
766 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
767 sgWriteUShort( fp, 0 ); // nproperties
768 sgWriteUShort( fp, 1 ); // nelements
769 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
771 for ( i = 0; i < (int)normals.size(); ++i ) {
772 SGVec3f p = normals[i];
773 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
774 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
775 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
776 sgWriteBytes( fp, 3, normal );
779 // dump texture coordinates
780 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
781 sgWriteUShort( fp, 0 ); // nproperties
782 sgWriteUShort( fp, 1 ); // nelements
783 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
784 for ( i = 0; i < (int)texcoords.size(); ++i ) {
785 sgWriteVec2( fp, texcoords[i].data() );
788 // dump point groups if they exist
789 if ( pts_v.size() > 0 ) {
793 while ( start < (int)pt_materials.size() ) {
795 material = pt_materials[start];
796 while ( (end < (int)pt_materials.size()) &&
797 (material == pt_materials[end]) )
799 // cout << "end = " << end << endl;
802 // cout << "group = " << start << " to " << end - 1 << endl;
804 // write group headers
805 sgWriteChar( fp, (char)SG_POINTS ); // type
806 sgWriteUShort( fp, 2 ); // nproperties
807 sgWriteUShort( fp, end - start ); // nelements
809 sgWriteChar( fp, (char)SG_MATERIAL ); // property
810 sgWriteUInt( fp, material.length() ); // nbytes
811 sgWriteBytes( fp, material.length(), material.c_str() );
815 if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
816 if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
817 if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
818 if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
819 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
820 sgWriteUInt( fp, 1 ); // nbytes
821 sgWriteChar( fp, idx_mask );
824 for ( i = start; i < end; ++i ) {
826 sgWriteUInt( fp, pts_v[i].size() * idx_size
827 * sizeof(unsigned short) );
828 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
829 if ( pts_v.size() ) {
830 sgWriteUShort( fp, (unsigned short)pts_v[i][j] );
832 if ( pts_n.size() ) {
833 sgWriteUShort( fp, (unsigned short)pts_n[i][j] );
835 if ( pts_c.size() ) {
836 sgWriteUShort( fp, (unsigned short)pts_c[i][j] );
838 if ( pts_tc.size() ) {
839 sgWriteUShort( fp, (unsigned short)pts_tc[i][j] );
849 // dump individual triangles if they exist
850 if ( tris_v.size() > 0 ) {
854 while ( start < (int)tri_materials.size() ) {
856 material = tri_materials[start];
857 while ( (end < (int)tri_materials.size()) &&
858 (material == tri_materials[end]) &&
859 3*(end-start) < 32760 )
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 sgWriteUShort( fp, 2 ); // nproperties
869 sgWriteUShort( fp, 1 ); // nelements
871 sgWriteChar( fp, (char)SG_MATERIAL ); // property
872 sgWriteUInt( fp, material.length() ); // nbytes
873 sgWriteBytes( fp, material.length(), material.c_str() );
877 if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
878 if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
879 if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
880 if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
881 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
882 sgWriteUInt( fp, 1 ); // nbytes
883 sgWriteChar( fp, idx_mask );
886 sgWriteUInt( fp, (end - start) * 3 * idx_size
887 * sizeof(unsigned short) );
890 for ( i = start; i < end; ++i ) {
891 for ( j = 0; j < 3; ++j ) {
892 if ( tris_v.size() ) {
893 sgWriteUShort( fp, (unsigned short)tris_v[i][j] );
895 if ( tris_n.size() ) {
896 sgWriteUShort( fp, (unsigned short)tris_n[i][j] );
898 if ( tris_c.size() ) {
899 sgWriteUShort( fp, (unsigned short)tris_c[i][j] );
901 if ( tris_tc.size() ) {
902 sgWriteUShort( fp, (unsigned short)tris_tc[i][j] );
912 // dump triangle strips
913 if ( strips_v.size() > 0 ) {
917 while ( start < (int)strip_materials.size() ) {
919 material = strip_materials[start];
920 while ( (end < (int)strip_materials.size()) &&
921 (material == strip_materials[end]) )
923 // cout << "end = " << end << endl;
926 // cout << "group = " << start << " to " << end - 1 << endl;
928 // write group headers
929 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
930 sgWriteUShort( fp, 2 ); // nproperties
931 sgWriteUShort( fp, end - start ); // nelements
933 sgWriteChar( fp, (char)SG_MATERIAL ); // property
934 sgWriteUInt( fp, material.length() ); // nbytes
935 sgWriteBytes( fp, material.length(), material.c_str() );
939 if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
940 if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
941 if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
942 if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
943 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
944 sgWriteUInt( fp, 1 ); // nbytes
945 sgWriteChar( fp, idx_mask );
948 for ( i = start; i < end; ++i ) {
950 sgWriteUInt( fp, strips_v[i].size() * idx_size
951 * sizeof(unsigned short));
952 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
953 if ( strips_v.size() ) {
954 sgWriteUShort( fp, (unsigned short)strips_v[i][j] );
956 if ( strips_n.size() ) {
957 sgWriteUShort( fp, (unsigned short)strips_n[i][j] );
959 if ( strips_c.size() ) {
960 sgWriteUShort( fp, (unsigned short)strips_c[i][j] );
962 if ( strips_tc.size() ) {
963 sgWriteUShort( fp, (unsigned short)strips_tc[i][j] );
973 // dump triangle fans
974 if ( fans_v.size() > 0 ) {
978 while ( start < (int)fan_materials.size() ) {
980 material = fan_materials[start];
981 while ( (end < (int)fan_materials.size()) &&
982 (material == fan_materials[end]) )
984 // cout << "end = " << end << endl;
987 // cout << "group = " << start << " to " << end - 1 << endl;
989 // write group headers
990 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
991 sgWriteUShort( fp, 2 ); // nproperties
992 sgWriteUShort( fp, end - start ); // nelements
994 sgWriteChar( fp, (char)SG_MATERIAL ); // property
995 sgWriteUInt( fp, material.length() ); // nbytes
996 sgWriteBytes( fp, material.length(), material.c_str() );
1000 if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
1001 if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
1002 if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
1003 if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
1004 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
1005 sgWriteUInt( fp, 1 ); // nbytes
1006 sgWriteChar( fp, idx_mask );
1009 for ( i = start; i < end; ++i ) {
1011 sgWriteUInt( fp, fans_v[i].size() * idx_size
1012 * sizeof(unsigned short) );
1013 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
1014 if ( fans_v.size() ) {
1015 sgWriteUShort( fp, (unsigned short)fans_v[i][j] );
1017 if ( fans_n.size() ) {
1018 sgWriteUShort( fp, (unsigned short)fans_n[i][j] );
1020 if ( fans_c.size() ) {
1021 sgWriteUShort( fp, (unsigned short)fans_c[i][j] );
1023 if ( fans_tc.size() ) {
1024 sgWriteUShort( fp, (unsigned short)fans_tc[i][j] );
1037 if ( sgWriteError() ) {
1038 cout << "We detected an error while writing the file." << endl;
1046 // write out the structures to an ASCII file. We assume that the
1047 // groups come to us sorted by material property. If not, things
1048 // don't break, but the result won't be as optimal.
1049 bool SGBinObject::write_ascii( const string& base, const string& name,
1054 SGPath file = base + "/" + b.gen_base_path() + "/" + name;
1055 file.create_dir( 0755 );
1056 cout << "Output file = " << file.str() << endl;
1059 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1060 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
1064 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1065 << tri_materials.size() << endl;
1066 cout << "strips size = " << strips_v.size() << " strip_materials = "
1067 << strip_materials.size() << endl;
1068 cout << "fans size = " << fans_v.size() << " fan_materials = "
1069 << fan_materials.size() << endl;
1071 cout << "points = " << wgs84_nodes.size() << endl;
1072 cout << "tex coords = " << texcoords.size() << endl;
1074 fprintf(fp, "# FGFS Scenery\n");
1075 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1077 time_t calendar_time = time(NULL);
1078 struct tm *local_tm;
1079 local_tm = localtime( &calendar_time );
1081 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1082 fprintf(fp, "# Created %s\n", time_str );
1085 // write bounding sphere
1086 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1087 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1091 fprintf(fp, "# vertex list\n");
1092 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1093 SGVec3d p = wgs84_nodes[i] - gbs_center;
1095 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1099 fprintf(fp, "# vertex normal list\n");
1100 for ( i = 0; i < (int)normals.size(); ++i ) {
1101 SGVec3f p = normals[i];
1102 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1106 // dump texture coordinates
1107 fprintf(fp, "# texture coordinate list\n");
1108 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1109 SGVec2f p = texcoords[i];
1110 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1114 // dump individual triangles if they exist
1115 if ( tris_v.size() > 0 ) {
1116 fprintf(fp, "# triangle groups\n");
1121 while ( start < (int)tri_materials.size() ) {
1123 material = tri_materials[start];
1124 while ( (end < (int)tri_materials.size()) &&
1125 (material == tri_materials[end]) )
1127 // cout << "end = " << end << endl;
1130 // cout << "group = " << start << " to " << end - 1 << endl;
1132 // make a list of points for the group
1133 point_list group_nodes;
1134 group_nodes.clear();
1136 double bs_radius = 0;
1137 for ( i = start; i < end; ++i ) {
1138 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1139 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ tris_v[i][j] ]) );
1140 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1141 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1145 // write group headers
1147 fprintf(fp, "# usemtl %s\n", material.c_str());
1148 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1149 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1152 for ( i = start; i < end; ++i ) {
1154 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1155 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1165 // dump triangle groups
1166 if ( strips_v.size() > 0 ) {
1167 fprintf(fp, "# triangle strips\n");
1172 while ( start < (int)strip_materials.size() ) {
1174 material = strip_materials[start];
1175 while ( (end < (int)strip_materials.size()) &&
1176 (material == strip_materials[end]) )
1178 // cout << "end = " << end << endl;
1181 // cout << "group = " << start << " to " << end - 1 << endl;
1183 // make a list of points for the group
1184 point_list group_nodes;
1185 group_nodes.clear();
1187 double bs_radius = 0;
1188 for ( i = start; i < end; ++i ) {
1189 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1190 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ strips_v[i][j] ]) );
1191 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1192 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1196 // write group headers
1198 fprintf(fp, "# usemtl %s\n", material.c_str());
1199 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1200 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1203 for ( i = start; i < end; ++i ) {
1205 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1206 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1219 string command = "gzip --force --best " + file.str();
1220 system(command.c_str());