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"
52 SG_BOUNDING_SPHERE = 0,
61 SG_TRIANGLE_FACES = 10,
62 SG_TRIANGLE_STRIPS = 11,
67 SG_IDX_VERTICES = 0x01,
68 SG_IDX_NORMALS = 0x02,
70 SG_IDX_TEXCOORDS = 0x08
73 enum sgPropertyTypes {
79 class sgSimpleBuffer {
88 inline sgSimpleBuffer( unsigned int s )
94 SG_LOG(SG_EVENT, SG_DEBUG, "Creating a new buffer of size = " << size);
98 inline ~sgSimpleBuffer() {
102 inline unsigned int get_size() const { return size; }
103 inline char *get_ptr() const { return ptr; }
104 inline void resize( unsigned int s ) {
112 SG_LOG(SG_EVENT, SG_DEBUG, "resizing buffer to size = " << size);
113 ptr = new char[size];
119 // calculate the center of a list of points, by taking the halfway
120 // point between the min and max points.
121 Point3D sgCalcCenter( point_list& wgs84_nodes ) {
124 if ( wgs84_nodes.size() ) {
125 min = max = wgs84_nodes[0];
127 min = max = Point3D( 0 );
130 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
133 if ( p.x() < min.x() ) { min.setx( p.x() ); }
134 if ( p.y() < min.y() ) { min.sety( p.y() ); }
135 if ( p.z() < min.z() ) { min.setz( p.z() ); }
137 if ( p.x() > max.x() ) { max.setx( p.x() ); }
138 if ( p.y() > max.y() ) { max.sety( p.y() ); }
139 if ( p.z() > max.z() ) { max.setz( p.z() ); }
142 return ( min + max ) / 2.0;
145 // calculate the bounding sphere. Center is the center of the
146 // tile and zero elevation
147 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
149 double radius_squared = 0;
151 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
152 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
153 if ( dist_squared > radius_squared ) {
154 radius_squared = dist_squared;
158 return sqrt(radius_squared);
163 // read object properties
164 static void read_object( gzFile fp,
168 group_list *vertices,
171 group_list *texcoords,
172 string_list *materials )
175 unsigned char idx_mask;
177 bool do_vertices, do_normals, do_colors, do_texcoords;
179 sgSimpleBuffer buf( 32768 ); // 32 Kb
183 if ( obj_type == SG_POINTS ) {
185 idx_mask = SG_IDX_VERTICES;
189 do_texcoords = false;
192 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
199 for ( j = 0; j < nproperties; ++j ) {
201 sgReadChar( fp, &prop_type );
203 sgReadUInt( fp, &nbytes );
204 // cout << "property size = " << nbytes << endl;
205 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
206 char *ptr = buf.get_ptr();
207 sgReadBytes( fp, nbytes, ptr );
208 if ( prop_type == SG_MATERIAL ) {
209 strncpy( material, ptr, nbytes );
210 material[nbytes] = '\0';
211 // cout << "material type = " << material << endl;
212 } else if ( prop_type == SG_INDEX_TYPES ) {
214 // cout << "idx_mask = " << (int)idx_mask << endl;
219 do_texcoords = false;
220 if ( idx_mask & SG_IDX_VERTICES ) {
224 if ( idx_mask & SG_IDX_NORMALS ) {
228 if ( idx_mask & SG_IDX_COLORS ) {
232 if ( idx_mask & SG_IDX_TEXCOORDS ) {
239 for ( j = 0; j < nelements; ++j ) {
240 sgReadUInt( fp, &nbytes );
241 // cout << "element size = " << nbytes << endl;
242 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
243 char *ptr = buf.get_ptr();
244 sgReadBytes( fp, nbytes, ptr );
245 int count = nbytes / (idx_size * sizeof(unsigned short));
246 unsigned short *sptr = (unsigned short *)ptr;
247 int_list vs; vs.clear();
248 int_list ns; ns.clear();
249 int_list cs; cs.clear();
250 int_list tcs; tcs.clear();
251 for ( k = 0; k < count; ++k ) {
252 if ( sgIsBigEndian() ) {
253 for ( idx = 0; idx < idx_size; ++idx ) {
254 sgEndianSwap( (uint16_t *)&(sptr[idx]) );
259 vs.push_back( sptr[idx++] );
262 ns.push_back( sptr[idx++] );
265 cs.push_back( sptr[idx++] );
267 if ( do_texcoords ) {
268 tcs.push_back( sptr[idx++] );
270 // cout << sptr[0] << " ";
274 vertices->push_back( vs );
275 normals->push_back( ns );
276 colors->push_back( cs );
277 texcoords->push_back( tcs );
278 materials->push_back( material );
283 // read a binary file and populate the provided structures.
284 bool SGBinObject::read_bin( const string& file ) {
288 sgSimpleBuffer buf( 32768 ); // 32 Kb
290 // zero out structures
291 gbs_center = SGVec3d(0, 0, 0);
302 pt_materials.clear();
308 tri_materials.clear();
314 strip_materials.clear();
320 fan_materials.clear();
323 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
324 string filegz = file + ".gz";
325 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
326 SG_LOG( SG_EVENT, SG_ALERT,
327 "ERROR: opening " << file << " or " << filegz << " for reading!");
337 sgReadUInt( fp, &header );
338 if ( ((header & 0xFF000000) >> 24) == 'S' &&
339 ((header & 0x00FF0000) >> 16) == 'G' ) {
340 // cout << "Good header" << endl;
342 version = (header & 0x0000FFFF);
343 // cout << "File version = " << version << endl;
345 // close the file before we return
351 // read creation time
352 unsigned int foo_calendar_time;
353 sgReadUInt( fp, &foo_calendar_time );
356 time_t calendar_time = foo_calendar_time;
357 // The following code has a global effect on the host application
358 // and can screws up the time elsewhere. It should be avoided
359 // unless you need this for debugging in which case you should
360 // disable it again once the debugging task is finished.
362 local_tm = localtime( &calendar_time );
364 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
365 SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
368 // read number of top level objects
369 unsigned short nobjects;
370 if ( version >= 7 ) {
371 sgReadUShort( fp, &nobjects );
374 sgReadShort( fp, &tmp );
377 // cout << "Total objects to read = " << nobjects << endl;
380 for ( i = 0; i < nobjects; ++i ) {
381 // read object header
383 unsigned short nproperties, nelements;
384 sgReadChar( fp, &obj_type );
385 if ( version >= 7 ) {
386 sgReadUShort( fp, &nproperties );
387 sgReadUShort( fp, &nelements );
390 sgReadShort( fp, &tmp );
392 sgReadShort( fp, &tmp );
396 // cout << "object " << i << " = " << (int)obj_type << " props = "
397 // << nproperties << " elements = " << nelements << endl;
399 if ( obj_type == SG_BOUNDING_SPHERE ) {
400 // read bounding sphere properties
401 for ( j = 0; j < nproperties; ++j ) {
403 sgReadChar( fp, &prop_type );
405 sgReadUInt( fp, &nbytes );
406 // cout << "property size = " << nbytes << endl;
407 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
408 char *ptr = buf.get_ptr();
409 sgReadBytes( fp, nbytes, ptr );
412 // read bounding sphere elements
413 for ( j = 0; j < nelements; ++j ) {
414 sgReadUInt( fp, &nbytes );
415 // cout << "element size = " << nbytes << endl;
416 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
417 char *ptr = buf.get_ptr();
418 sgReadBytes( fp, nbytes, ptr );
420 double *dptr = (double *)ptr;
421 if ( sgIsBigEndian() ) {
422 sgEndianSwap( (uint64_t *)&(dptr[0]) );
423 sgEndianSwap( (uint64_t *)&(dptr[1]) );
424 sgEndianSwap( (uint64_t *)&(dptr[2]) );
426 gbs_center = SGVec3d( dptr[0], dptr[1], dptr[2] );
427 // cout << "Center = " << gbs_center << endl;
428 ptr += sizeof(double) * 3;
430 float *fptr = (float *)ptr;
431 if ( sgIsBigEndian() ) {
432 sgEndianSwap( (uint32_t *)fptr );
434 gbs_radius = fptr[0];
435 // cout << "Bounding radius = " << gbs_radius << endl;
437 } else if ( obj_type == SG_VERTEX_LIST ) {
438 // read vertex list properties
439 for ( j = 0; j < nproperties; ++j ) {
441 sgReadChar( fp, &prop_type );
443 sgReadUInt( fp, &nbytes );
444 // cout << "property size = " << nbytes << endl;
445 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
446 char *ptr = buf.get_ptr();
447 sgReadBytes( fp, nbytes, ptr );
450 // read vertex list elements
451 for ( j = 0; j < nelements; ++j ) {
452 sgReadUInt( fp, &nbytes );
453 // cout << "element size = " << nbytes << endl;
454 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
455 char *ptr = buf.get_ptr();
456 sgReadBytes( fp, nbytes, ptr );
457 int count = nbytes / (sizeof(float) * 3);
458 float *fptr = (float *)ptr;
459 wgs84_nodes.reserve( count );
460 for ( k = 0; k < count; ++k ) {
461 if ( sgIsBigEndian() ) {
462 sgEndianSwap( (uint32_t *)&(fptr[0]) );
463 sgEndianSwap( (uint32_t *)&(fptr[1]) );
464 sgEndianSwap( (uint32_t *)&(fptr[2]) );
466 wgs84_nodes.push_back( SGVec3d(fptr[0], fptr[1], fptr[2]) );
470 } else if ( obj_type == SG_COLOR_LIST ) {
471 // read color list properties
472 for ( j = 0; j < nproperties; ++j ) {
474 sgReadChar( fp, &prop_type );
476 sgReadUInt( fp, &nbytes );
477 // cout << "property size = " << nbytes << endl;
478 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
479 char *ptr = buf.get_ptr();
480 sgReadBytes( fp, nbytes, ptr );
483 // read color list elements
484 for ( j = 0; j < nelements; ++j ) {
485 sgReadUInt( fp, &nbytes );
486 // cout << "element size = " << nbytes << endl;
487 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
488 char *ptr = buf.get_ptr();
489 sgReadBytes( fp, nbytes, ptr );
490 int count = nbytes / (sizeof(float) * 4);
491 float *fptr = (float *)ptr;
492 colors.reserve(count);
493 for ( k = 0; k < count; ++k ) {
494 if ( sgIsBigEndian() ) {
495 sgEndianSwap( (uint32_t *)&(fptr[0]) );
496 sgEndianSwap( (uint32_t *)&(fptr[1]) );
497 sgEndianSwap( (uint32_t *)&(fptr[2]) );
498 sgEndianSwap( (uint32_t *)&(fptr[3]) );
500 SGVec4f color( fptr[0], fptr[1], fptr[2], fptr[3] );
501 colors.push_back( color );
505 } else if ( obj_type == SG_NORMAL_LIST ) {
506 // read normal list properties
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 );
518 // read normal list elements
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 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
524 sgReadBytes( fp, nbytes, ptr );
525 int count = nbytes / 3;
526 normals.reserve( count );
527 for ( k = 0; k < count; ++k ) {
528 SGVec3f normal((ptr[0]) / 127.5 - 1.0,
529 (ptr[1]) / 127.5 - 1.0,
530 (ptr[2]) / 127.5 - 1.0);
532 normals.push_back(normalize(normal));
536 } else if ( obj_type == SG_TEXCOORD_LIST ) {
537 // read texcoord list properties
538 for ( j = 0; j < nproperties; ++j ) {
540 sgReadChar( fp, &prop_type );
542 sgReadUInt( fp, &nbytes );
543 // cout << "property size = " << nbytes << endl;
544 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
545 char *ptr = buf.get_ptr();
546 sgReadBytes( fp, nbytes, ptr );
549 // read texcoord list elements
550 for ( j = 0; j < nelements; ++j ) {
551 sgReadUInt( fp, &nbytes );
552 // cout << "element size = " << nbytes << endl;
553 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
554 char *ptr = buf.get_ptr();
555 sgReadBytes( fp, nbytes, ptr );
556 int count = nbytes / (sizeof(float) * 2);
557 float *fptr = (float *)ptr;
558 texcoords.reserve(count);
559 for ( k = 0; k < count; ++k ) {
560 if ( sgIsBigEndian() ) {
561 sgEndianSwap( (uint32_t *)&(fptr[0]) );
562 sgEndianSwap( (uint32_t *)&(fptr[1]) );
564 texcoords.push_back( SGVec2f( fptr[0], fptr[1] ) );
568 } else if ( obj_type == SG_POINTS ) {
569 // read point elements
570 read_object( fp, SG_POINTS, nproperties, nelements,
571 &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
572 } else if ( obj_type == SG_TRIANGLE_FACES ) {
573 // read triangle face properties
574 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
575 &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
576 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
577 // read triangle strip properties
578 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
579 &strips_v, &strips_n, &strips_c, &strips_tc,
581 } else if ( obj_type == SG_TRIANGLE_FANS ) {
582 // read triangle fan properties
583 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
584 &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
586 // unknown object type, just skip
589 for ( j = 0; j < nproperties; ++j ) {
591 sgReadChar( fp, &prop_type );
593 sgReadUInt( fp, &nbytes );
594 // cout << "property size = " << nbytes << endl;
595 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
596 char *ptr = buf.get_ptr();
597 sgReadBytes( fp, nbytes, ptr );
601 for ( j = 0; j < nelements; ++j ) {
602 sgReadUInt( fp, &nbytes );
603 // cout << "element size = " << nbytes << endl;
604 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
605 char *ptr = buf.get_ptr();
606 sgReadBytes( fp, nbytes, ptr );
614 if ( sgReadError() ) {
615 cout << "We detected an error while reading the file." << endl;
623 // write out the structures to a binary file. We assume that the
624 // groups come to us sorted by material property. If not, things
625 // don't break, but the result won't be as optimal.
626 bool SGBinObject::write_bin( const string& base, const string& name,
630 unsigned char idx_mask;
633 SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
634 file.create_dir( 0755 );
635 cout << "Output file = " << file.str() << endl;
638 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
639 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
645 cout << "points size = " << pts_v.size() << " pt_materials = "
646 << pt_materials.size() << endl;
647 cout << "triangles size = " << tris_v.size() << " tri_materials = "
648 << tri_materials.size() << endl;
649 cout << "strips size = " << strips_v.size() << " strip_materials = "
650 << strip_materials.size() << endl;
651 cout << "fans size = " << fans_v.size() << " fan_materials = "
652 << fan_materials.size() << endl;
654 cout << "nodes = " << wgs84_nodes.size() << endl;
655 cout << "colors = " << colors.size() << endl;
656 cout << "normals = " << normals.size() << endl;
657 cout << "tex coords = " << texcoords.size() << endl;
659 // write header magic
660 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
661 time_t calendar_time = time(NULL);
662 sgWriteLong( fp, (int32_t)calendar_time );
664 // calculate and write number of top level objects
668 unsigned short nobjects = 0;
669 nobjects++; // for gbs
670 nobjects++; // for vertices
671 nobjects++; // for colors
672 nobjects++; // for normals
673 nobjects++; // for texcoords
676 unsigned short npts = 0;
678 while ( start < (int)pt_materials.size() ) {
679 material = pt_materials[start];
680 while ( (end < (int)pt_materials.size()) &&
681 (material == pt_materials[end]) ) {
685 start = end; end = start + 1;
690 unsigned short ntris = 0;
692 while ( start < (int)tri_materials.size() ) {
693 material = tri_materials[start];
694 while ( (end < (int)tri_materials.size()) &&
695 (material == tri_materials[end]) ) {
699 start = end; end = start + 1;
704 unsigned short nstrips = 0;
706 while ( start < (int)strip_materials.size() ) {
707 material = strip_materials[start];
708 while ( (end < (int)strip_materials.size()) &&
709 (material == strip_materials[end]) ) {
713 start = end; end = start + 1;
718 unsigned short nfans = 0;
720 while ( start < (int)fan_materials.size() ) {
721 material = fan_materials[start];
722 while ( (end < (int)fan_materials.size()) &&
723 (material == fan_materials[end]) ) {
727 start = end; end = start + 1;
731 cout << "total top level objects = " << nobjects << endl;
732 sgWriteUShort( fp, nobjects );
734 // write bounding sphere
735 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
736 sgWriteUShort( fp, 0 ); // nproperties
737 sgWriteUShort( fp, 1 ); // nelements
739 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
741 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
742 sgWritedVec3( fp, center );
743 sgWriteFloat( fp, gbs_radius );
746 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
747 sgWriteUShort( fp, 0 ); // nproperties
748 sgWriteUShort( fp, 1 ); // nelements
749 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
750 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
751 SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center);
752 sgWriteVec3( fp, p.data() );
755 // dump vertex color list
756 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
757 sgWriteUShort( fp, 0 ); // nproperties
758 sgWriteUShort( fp, 1 ); // nelements
759 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
760 for ( i = 0; i < (int)colors.size(); ++i ) {
761 sgWriteVec4( fp, colors[i].data() );
764 // dump vertex normal list
765 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
766 sgWriteUShort( fp, 0 ); // nproperties
767 sgWriteUShort( fp, 1 ); // nelements
768 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
770 for ( i = 0; i < (int)normals.size(); ++i ) {
771 SGVec3f p = normals[i];
772 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
773 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
774 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
775 sgWriteBytes( fp, 3, normal );
778 // dump texture coordinates
779 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
780 sgWriteUShort( fp, 0 ); // nproperties
781 sgWriteUShort( fp, 1 ); // nelements
782 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
783 for ( i = 0; i < (int)texcoords.size(); ++i ) {
784 sgWriteVec2( fp, texcoords[i].data() );
787 // dump point groups if they exist
788 if ( pts_v.size() > 0 ) {
792 while ( start < (int)pt_materials.size() ) {
794 material = pt_materials[start];
795 while ( (end < (int)pt_materials.size()) &&
796 (material == pt_materials[end]) )
798 // cout << "end = " << end << endl;
801 // cout << "group = " << start << " to " << end - 1 << endl;
803 // write group headers
804 sgWriteChar( fp, (char)SG_POINTS ); // type
805 sgWriteUShort( fp, 2 ); // nproperties
806 sgWriteUShort( fp, end - start ); // nelements
808 sgWriteChar( fp, (char)SG_MATERIAL ); // property
809 sgWriteUInt( fp, material.length() ); // nbytes
810 sgWriteBytes( fp, material.length(), material.c_str() );
814 if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
815 if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
816 if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
817 if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
818 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
819 sgWriteUInt( fp, 1 ); // nbytes
820 sgWriteChar( fp, idx_mask );
823 for ( i = start; i < end; ++i ) {
825 sgWriteUInt( fp, pts_v[i].size() * idx_size
826 * sizeof(unsigned short) );
827 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
828 if ( pts_v.size() ) {
829 sgWriteUShort( fp, (unsigned short)pts_v[i][j] );
831 if ( pts_n.size() ) {
832 sgWriteUShort( fp, (unsigned short)pts_n[i][j] );
834 if ( pts_c.size() ) {
835 sgWriteUShort( fp, (unsigned short)pts_c[i][j] );
837 if ( pts_tc.size() ) {
838 sgWriteUShort( fp, (unsigned short)pts_tc[i][j] );
848 // dump individual triangles if they exist
849 if ( tris_v.size() > 0 ) {
853 while ( start < (int)tri_materials.size() ) {
855 material = tri_materials[start];
856 while ( (end < (int)tri_materials.size()) &&
857 (material == tri_materials[end]) &&
858 3*(end-start) < 32760 )
860 // cout << "end = " << end << endl;
863 // cout << "group = " << start << " to " << end - 1 << endl;
865 // write group headers
866 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
867 sgWriteUShort( fp, 2 ); // nproperties
868 sgWriteUShort( fp, 1 ); // nelements
870 sgWriteChar( fp, (char)SG_MATERIAL ); // property
871 sgWriteUInt( fp, material.length() ); // nbytes
872 sgWriteBytes( fp, material.length(), material.c_str() );
876 if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
877 if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
878 if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
879 if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
880 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
881 sgWriteUInt( fp, 1 ); // nbytes
882 sgWriteChar( fp, idx_mask );
885 sgWriteUInt( fp, (end - start) * 3 * idx_size
886 * sizeof(unsigned short) );
889 for ( i = start; i < end; ++i ) {
890 for ( j = 0; j < 3; ++j ) {
891 if ( tris_v.size() ) {
892 sgWriteUShort( fp, (unsigned short)tris_v[i][j] );
894 if ( tris_n.size() ) {
895 sgWriteUShort( fp, (unsigned short)tris_n[i][j] );
897 if ( tris_c.size() ) {
898 sgWriteUShort( fp, (unsigned short)tris_c[i][j] );
900 if ( tris_tc.size() ) {
901 sgWriteUShort( fp, (unsigned short)tris_tc[i][j] );
911 // dump triangle strips
912 if ( strips_v.size() > 0 ) {
916 while ( start < (int)strip_materials.size() ) {
918 material = strip_materials[start];
919 while ( (end < (int)strip_materials.size()) &&
920 (material == strip_materials[end]) )
922 // cout << "end = " << end << endl;
925 // cout << "group = " << start << " to " << end - 1 << endl;
927 // write group headers
928 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
929 sgWriteUShort( fp, 2 ); // nproperties
930 sgWriteUShort( fp, end - start ); // nelements
932 sgWriteChar( fp, (char)SG_MATERIAL ); // property
933 sgWriteUInt( fp, material.length() ); // nbytes
934 sgWriteBytes( fp, material.length(), material.c_str() );
938 if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
939 if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
940 if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
941 if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
942 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
943 sgWriteUInt( fp, 1 ); // nbytes
944 sgWriteChar( fp, idx_mask );
947 for ( i = start; i < end; ++i ) {
949 sgWriteUInt( fp, strips_v[i].size() * idx_size
950 * sizeof(unsigned short));
951 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
952 if ( strips_v.size() ) {
953 sgWriteUShort( fp, (unsigned short)strips_v[i][j] );
955 if ( strips_n.size() ) {
956 sgWriteUShort( fp, (unsigned short)strips_n[i][j] );
958 if ( strips_c.size() ) {
959 sgWriteUShort( fp, (unsigned short)strips_c[i][j] );
961 if ( strips_tc.size() ) {
962 sgWriteUShort( fp, (unsigned short)strips_tc[i][j] );
972 // dump triangle fans
973 if ( fans_v.size() > 0 ) {
977 while ( start < (int)fan_materials.size() ) {
979 material = fan_materials[start];
980 while ( (end < (int)fan_materials.size()) &&
981 (material == fan_materials[end]) )
983 // cout << "end = " << end << endl;
986 // cout << "group = " << start << " to " << end - 1 << endl;
988 // write group headers
989 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
990 sgWriteUShort( fp, 2 ); // nproperties
991 sgWriteUShort( fp, end - start ); // nelements
993 sgWriteChar( fp, (char)SG_MATERIAL ); // property
994 sgWriteUInt( fp, material.length() ); // nbytes
995 sgWriteBytes( fp, material.length(), material.c_str() );
999 if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
1000 if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
1001 if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
1002 if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
1003 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
1004 sgWriteUInt( fp, 1 ); // nbytes
1005 sgWriteChar( fp, idx_mask );
1008 for ( i = start; i < end; ++i ) {
1010 sgWriteUInt( fp, fans_v[i].size() * idx_size
1011 * sizeof(unsigned short) );
1012 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
1013 if ( fans_v.size() ) {
1014 sgWriteUShort( fp, (unsigned short)fans_v[i][j] );
1016 if ( fans_n.size() ) {
1017 sgWriteUShort( fp, (unsigned short)fans_n[i][j] );
1019 if ( fans_c.size() ) {
1020 sgWriteUShort( fp, (unsigned short)fans_c[i][j] );
1022 if ( fans_tc.size() ) {
1023 sgWriteUShort( fp, (unsigned short)fans_tc[i][j] );
1036 if ( sgWriteError() ) {
1037 cout << "We detected an error while writing the file." << endl;
1045 // write out the structures to an ASCII file. We assume that the
1046 // groups come to us sorted by material property. If not, things
1047 // don't break, but the result won't be as optimal.
1048 bool SGBinObject::write_ascii( const string& base, const string& name,
1053 SGPath file = base + "/" + b.gen_base_path() + "/" + name;
1054 file.create_dir( 0755 );
1055 cout << "Output file = " << file.str() << endl;
1058 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1059 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
1063 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1064 << tri_materials.size() << endl;
1065 cout << "strips size = " << strips_v.size() << " strip_materials = "
1066 << strip_materials.size() << endl;
1067 cout << "fans size = " << fans_v.size() << " fan_materials = "
1068 << fan_materials.size() << endl;
1070 cout << "points = " << wgs84_nodes.size() << endl;
1071 cout << "tex coords = " << texcoords.size() << endl;
1073 fprintf(fp, "# FGFS Scenery\n");
1074 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1076 time_t calendar_time = time(NULL);
1077 struct tm *local_tm;
1078 local_tm = localtime( &calendar_time );
1080 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1081 fprintf(fp, "# Created %s\n", time_str );
1084 // write bounding sphere
1085 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1086 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1090 fprintf(fp, "# vertex list\n");
1091 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1092 SGVec3d p = wgs84_nodes[i] - gbs_center;
1094 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1098 fprintf(fp, "# vertex normal list\n");
1099 for ( i = 0; i < (int)normals.size(); ++i ) {
1100 SGVec3f p = normals[i];
1101 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1105 // dump texture coordinates
1106 fprintf(fp, "# texture coordinate list\n");
1107 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1108 SGVec2f p = texcoords[i];
1109 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1113 // dump individual triangles if they exist
1114 if ( tris_v.size() > 0 ) {
1115 fprintf(fp, "# triangle groups\n");
1120 while ( start < (int)tri_materials.size() ) {
1122 material = tri_materials[start];
1123 while ( (end < (int)tri_materials.size()) &&
1124 (material == tri_materials[end]) )
1126 // cout << "end = " << end << endl;
1129 // cout << "group = " << start << " to " << end - 1 << endl;
1131 // make a list of points for the group
1132 point_list group_nodes;
1133 group_nodes.clear();
1135 double bs_radius = 0;
1136 for ( i = start; i < end; ++i ) {
1137 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1138 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ tris_v[i][j] ]) );
1139 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1140 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1144 // write group headers
1146 fprintf(fp, "# usemtl %s\n", material.c_str());
1147 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1148 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1151 for ( i = start; i < end; ++i ) {
1153 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1154 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1164 // dump triangle groups
1165 if ( strips_v.size() > 0 ) {
1166 fprintf(fp, "# triangle strips\n");
1171 while ( start < (int)strip_materials.size() ) {
1173 material = strip_materials[start];
1174 while ( (end < (int)strip_materials.size()) &&
1175 (material == strip_materials[end]) )
1177 // cout << "end = " << end << endl;
1180 // cout << "group = " << start << " to " << end - 1 << endl;
1182 // make a list of points for the group
1183 point_list group_nodes;
1184 group_nodes.clear();
1186 double bs_radius = 0;
1187 for ( i = start; i < end; ++i ) {
1188 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1189 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ strips_v[i][j] ]) );
1190 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1191 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1195 // write group headers
1197 fprintf(fp, "# usemtl %s\n", material.c_str());
1198 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1199 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1202 for ( i = start; i < end; ++i ) {
1204 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1205 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1218 string command = "gzip --force --best " + file.str();
1219 system(command.c_str());