1 // sg_binobj.cxx -- routines to read and write low level flightgear 3d objects
3 // Written by Curtis Olson, started January 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
33 #include <simgear/bucket/newbucket.hxx>
35 #include "lowlevel.hxx"
36 #include "sg_binobj.hxx"
39 SG_USING_STD( string );
40 SG_USING_STD( vector );
42 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
49 SG_BOUNDING_SPHERE = 0,
58 SG_TRIANGLE_FACES = 10,
59 SG_TRIANGLE_STRIPS = 11,
64 SG_IDX_VERTICES = 0x0001,
65 SG_IDX_NORMALS = 0x0010,
66 SG_IDX_COLORS = 0x0100,
67 SG_IDX_TEXCOORDS = 0x1000
77 class sgSimpleBuffer {
86 inline sgSimpleBuffer( unsigned int s )
92 cout << "Creating a new buffer of size = " << size << endl;
96 inline ~sgSimpleBuffer() {
100 inline unsigned int get_size() const { return size; }
101 inline char *get_ptr() const { return ptr; }
102 inline void resize( unsigned int s ) {
110 cout << "resizing buffer to size = " << size << endl;
111 ptr = new char[size];
117 // calculate the center of a list of points, by taking the halfway
118 // point between the min and max points.
119 static Point3D calc_center( point_list& wgs84_nodes ) {
122 if ( wgs84_nodes.size() ) {
123 min = max = wgs84_nodes[0];
125 min = max = Point3D( 0 );
128 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
131 if ( p.x() < min.x() ) { min.setx( p.x() ); }
132 if ( p.y() < min.y() ) { min.sety( p.y() ); }
133 if ( p.z() < min.z() ) { min.setz( p.z() ); }
135 if ( p.x() > max.x() ) { max.setx( p.x() ); }
136 if ( p.y() > max.y() ) { max.sety( p.y() ); }
137 if ( p.z() > max.z() ) { max.setz( p.z() ); }
140 return ( min + max ) / 2.0;
143 // calculate the bounding sphere. Center is the center of the
144 // tile and zero elevation
145 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
147 double radius_squared = 0;
149 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
150 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
151 if ( dist_squared > radius_squared ) {
152 radius_squared = dist_squared;
156 return sqrt(radius_squared);
161 // read object properties
162 static void read_object( gzFile fp,
166 group_list *vertices,
169 group_list *texcoords,
170 string_list *materials )
175 bool do_vertices, do_normals, do_colors, do_texcoords;
177 static sgSimpleBuffer buf( 32768 ); // 32 Kb
181 if ( obj_type == SG_POINTS ) {
183 idx_mask = SG_IDX_VERTICES;
187 do_texcoords = false;
190 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
197 for ( j = 0; j < nproperties; ++j ) {
199 sgReadChar( fp, &prop_type );
201 sgReadUInt( fp, &nbytes );
202 // cout << "property size = " << nbytes << endl;
203 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
204 char *ptr = buf.get_ptr();
205 sgReadBytes( fp, nbytes, ptr );
206 if ( prop_type == SG_MATERIAL ) {
207 strncpy( material, ptr, nbytes );
208 material[nbytes] = '\0';
209 // cout << "material type = " << material << endl;
210 } else if ( prop_type == SG_INDEX_TYPES ) {
216 do_texcoords = false;
217 if ( idx_mask & SG_IDX_VERTICES ) {
221 if ( idx_mask & SG_IDX_NORMALS ) {
225 if ( idx_mask & SG_IDX_COLORS ) {
229 if ( idx_mask & SG_IDX_TEXCOORDS ) {
236 for ( j = 0; j < nelements; ++j ) {
237 sgReadUInt( fp, &nbytes );
238 // cout << "element size = " << nbytes << endl;
239 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
240 char *ptr = buf.get_ptr();
241 sgReadBytes( fp, nbytes, ptr );
242 int count = nbytes / (idx_size * sizeof(short));
243 short *sptr = (short *)ptr;
244 int_list vs; vs.clear();
245 int_list ns; ns.clear();
246 int_list cs; cs.clear();
247 int_list tcs; tcs.clear();
248 for ( k = 0; k < count; ++k ) {
249 if ( sgIsBigEndian() ) {
250 for ( idx = 0; idx < idx_size; ++idx ) {
251 sgEndianSwap( (unsigned short *)&(sptr[idx]) );
256 vs.push_back( sptr[idx++] );
259 ns.push_back( sptr[idx++] );
262 cs.push_back( sptr[idx++] );
264 if ( do_texcoords ) {
265 tcs.push_back( sptr[idx++] );
267 // cout << sptr[0] << " ";
271 vertices->push_back( vs );
272 normals->push_back( ns );
273 colors->push_back( cs );
274 texcoords->push_back( tcs );
275 materials->push_back( material );
280 // read a binary file and populate the provided structures.
281 bool SGBinObject::read_bin( const string& file ) {
285 static sgSimpleBuffer buf( 32768 ); // 32 Kb
287 // zero out structures
288 gbs_center = Point3D( 0 );
299 pt_materials.clear();
305 tri_materials.clear();
311 strip_materials.clear();
317 fan_materials.clear();
320 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
321 string filegz = file + ".gz";
322 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
323 // cout << "ERROR: opening " << file << " or " << filegz
324 // << "for reading!" << endl;
334 sgReadUInt( fp, &header );
335 if ( ((header & 0xFF000000) >> 24) == 'S' &&
336 ((header & 0x00FF0000) >> 16) == 'G' ) {
337 // cout << "Good header" << endl;
339 version = (header & 0x0000FFFF);
340 // cout << "File version = " << version << endl;
342 // close the file before we return
348 // read creation time
349 time_t calendar_time;
350 sgReadLong( fp, &calendar_time );
353 // The following code has a global effect on the host application
354 // and can screws up the time elsewhere. It should be avoided
355 // unless you need this for debugging in which case you should
356 // disable it again once the debugging task is finished.
358 local_tm = localtime( &calendar_time );
360 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
361 cout << "File created on " << time_str << endl;
364 // read number of top level objects
366 sgReadShort( fp, &nobjects );
367 // cout << "Total objects to read = " << nobjects << endl;
370 for ( i = 0; i < nobjects; ++i ) {
371 // read object header
373 short nproperties, nelements;
374 sgReadChar( fp, &obj_type );
375 sgReadShort( fp, &nproperties );
376 sgReadShort( fp, &nelements );
378 // cout << "object " << i << " = " << (int)obj_type << " props = "
379 // << nproperties << " elements = " << nelements << endl;
381 if ( obj_type == SG_BOUNDING_SPHERE ) {
382 // read bounding sphere properties
383 for ( j = 0; j < nproperties; ++j ) {
385 sgReadChar( fp, &prop_type );
387 sgReadUInt( fp, &nbytes );
388 // cout << "property size = " << nbytes << endl;
389 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
390 char *ptr = buf.get_ptr();
391 sgReadBytes( fp, nbytes, ptr );
394 // read bounding sphere elements
395 for ( j = 0; j < nelements; ++j ) {
396 sgReadUInt( fp, &nbytes );
397 // cout << "element size = " << nbytes << endl;
398 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
399 char *ptr = buf.get_ptr();
400 sgReadBytes( fp, nbytes, ptr );
402 double *dptr = (double *)ptr;
403 if ( sgIsBigEndian() ) {
404 sgEndianSwap( (uint64 *)&(dptr[0]) );
405 sgEndianSwap( (uint64 *)&(dptr[1]) );
406 sgEndianSwap( (uint64 *)&(dptr[2]) );
408 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
409 // cout << "Center = " << gbs_center << endl;
410 ptr += sizeof(double) * 3;
412 float *fptr = (float *)ptr;
413 if ( sgIsBigEndian() ) {
414 sgEndianSwap( (unsigned int *)fptr );
416 gbs_radius = fptr[0];
417 // cout << "Bounding radius = " << gbs_radius << endl;
419 } else if ( obj_type == SG_VERTEX_LIST ) {
420 // read vertex list 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 );
432 // read vertex list elements
433 for ( j = 0; j < nelements; ++j ) {
434 sgReadUInt( fp, &nbytes );
435 // cout << "element size = " << nbytes << endl;
436 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
437 char *ptr = buf.get_ptr();
438 sgReadBytes( fp, nbytes, ptr );
439 int count = nbytes / (sizeof(float) * 3);
440 float *fptr = (float *)ptr;
441 for ( k = 0; k < count; ++k ) {
442 if ( sgIsBigEndian() ) {
443 sgEndianSwap( (unsigned int *)&(fptr[0]) );
444 sgEndianSwap( (unsigned int *)&(fptr[1]) );
445 sgEndianSwap( (unsigned int *)&(fptr[2]) );
447 p = Point3D( fptr[0], fptr[1], fptr[2] );
448 // cout << "node = " << p << endl;
449 wgs84_nodes.push_back( p );
453 } else if ( obj_type == SG_COLOR_LIST ) {
454 // read color list properties
455 for ( j = 0; j < nproperties; ++j ) {
457 sgReadChar( fp, &prop_type );
459 sgReadUInt( fp, &nbytes );
460 // cout << "property size = " << nbytes << endl;
461 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
462 char *ptr = buf.get_ptr();
463 sgReadBytes( fp, nbytes, ptr );
466 // read color list elements
467 for ( j = 0; j < nelements; ++j ) {
468 sgReadUInt( fp, &nbytes );
469 // cout << "element size = " << nbytes << endl;
470 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
471 char *ptr = buf.get_ptr();
472 sgReadBytes( fp, nbytes, ptr );
473 int count = nbytes / (sizeof(float) * 4);
474 float *fptr = (float *)ptr;
475 for ( k = 0; k < count; ++k ) {
476 if ( sgIsBigEndian() ) {
477 sgEndianSwap( (unsigned int *)&(fptr[0]) );
478 sgEndianSwap( (unsigned int *)&(fptr[1]) );
479 sgEndianSwap( (unsigned int *)&(fptr[2]) );
480 sgEndianSwap( (unsigned int *)&(fptr[3]) );
482 p = Point3D( fptr[0], fptr[1], fptr[2] );
483 // cout << "node = " << p << endl;
484 colors.push_back( p );
488 } else if ( obj_type == SG_NORMAL_LIST ) {
489 // read normal list properties
490 for ( j = 0; j < nproperties; ++j ) {
492 sgReadChar( fp, &prop_type );
494 sgReadUInt( fp, &nbytes );
495 // cout << "property size = " << nbytes << endl;
496 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
497 char *ptr = buf.get_ptr();
498 sgReadBytes( fp, nbytes, ptr );
501 // read normal list elements
502 for ( j = 0; j < nelements; ++j ) {
503 sgReadUInt( fp, &nbytes );
504 // cout << "element size = " << nbytes << endl;
505 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
506 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
507 sgReadBytes( fp, nbytes, ptr );
508 int count = nbytes / 3;
509 for ( k = 0; k < count; ++k ) {
512 (ptr[0]) / 127.5 - 1.0,
513 (ptr[1]) / 127.5 - 1.0,
514 (ptr[2]) / 127.5 - 1.0 );
515 sgdNormalizeVec3( normal );
517 p = Point3D( normal[0], normal[1], normal[2] );
518 // cout << "normal = " << p << endl;
519 normals.push_back( p );
523 } else if ( obj_type == SG_TEXCOORD_LIST ) {
524 // read texcoord list properties
525 for ( j = 0; j < nproperties; ++j ) {
527 sgReadChar( fp, &prop_type );
529 sgReadUInt( fp, &nbytes );
530 // cout << "property size = " << nbytes << endl;
531 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
532 char *ptr = buf.get_ptr();
533 sgReadBytes( fp, nbytes, ptr );
536 // read texcoord list elements
537 for ( j = 0; j < nelements; ++j ) {
538 sgReadUInt( fp, &nbytes );
539 // cout << "element size = " << nbytes << endl;
540 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
541 char *ptr = buf.get_ptr();
542 sgReadBytes( fp, nbytes, ptr );
543 int count = nbytes / (sizeof(float) * 2);
544 float *fptr = (float *)ptr;
545 for ( k = 0; k < count; ++k ) {
546 if ( sgIsBigEndian() ) {
547 sgEndianSwap( (unsigned int *)&(fptr[0]) );
548 sgEndianSwap( (unsigned int *)&(fptr[1]) );
550 p = Point3D( fptr[0], fptr[1], 0 );
551 // cout << "texcoord = " << p << endl;
552 texcoords.push_back( p );
556 } else if ( obj_type == SG_POINTS ) {
557 // read point elements
558 read_object( fp, SG_POINTS, nproperties, nelements,
559 &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
560 } else if ( obj_type == SG_TRIANGLE_FACES ) {
561 // read triangle face properties
562 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
563 &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
564 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
565 // read triangle strip properties
566 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
567 &strips_v, &strips_n, &strips_c, &strips_tc,
572 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
578 for ( j = 0; j < nproperties; ++j ) {
580 sgReadChar( fp, &prop_type );
582 sgReadUInt( fp, &nbytes );
583 // cout << "property size = " << nbytes << endl;
584 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
585 char *ptr = buf.get_ptr();
586 sgReadBytes( fp, nbytes, ptr );
587 if ( prop_type == SG_MATERIAL ) {
588 strncpy( material, ptr, nbytes );
589 material[nbytes] = '\0';
590 // cout << "material type = " << material << endl;
594 // read triangle strip elements
595 for ( j = 0; j < nelements; ++j ) {
596 sgReadUInt( fp, &nbytes );
597 // cout << "element size = " << nbytes << endl;
598 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
599 char *ptr = buf.get_ptr();
600 sgReadBytes( fp, nbytes, ptr );
601 int count = nbytes / (sizeof(short) * 2);
602 short *sptr = (short *)ptr;
604 vs.clear(); tcs.clear();
605 for ( k = 0; k < count; ++k ) {
606 if ( sgIsBigEndian() ) {
607 sgEndianSwap( (unsigned short *)&(sptr[0]) );
608 sgEndianSwap( (unsigned short *)&(sptr[1]) );
610 vs.push_back( sptr[0] );
611 tcs.push_back( sptr[1] );
612 // cout << sptr[0] << "/" << sptr[1] << " ";
616 strips_v.push_back( vs );
617 strips_tc.push_back( tcs );
618 strip_materials.push_back( material );
621 } else if ( obj_type == SG_TRIANGLE_FANS ) {
622 // read triangle fan properties
623 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
624 &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
628 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
634 for ( j = 0; j < nproperties; ++j ) {
636 sgReadChar( fp, &prop_type );
638 sgReadUInt( fp, &nbytes );
639 // cout << "property size = " << nbytes << endl;
640 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
641 char *ptr = buf.get_ptr();
642 sgReadBytes( fp, nbytes, ptr );
643 if ( prop_type == SG_MATERIAL ) {
644 strncpy( material, ptr, nbytes );
645 material[nbytes] = '\0';
646 // cout << "material type = " << material << endl;
650 // read triangle fan elements
651 for ( j = 0; j < nelements; ++j ) {
652 sgReadUInt( fp, &nbytes );
653 // cout << "element size = " << nbytes << endl;
654 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
655 char *ptr = buf.get_ptr();
656 sgReadBytes( fp, nbytes, ptr );
657 int count = nbytes / (sizeof(short) * 2);
658 short *sptr = (short *)ptr;
660 vs.clear(); tcs.clear();
661 for ( k = 0; k < count; ++k ) {
662 if ( sgIsBigEndian() ) {
663 sgEndianSwap( (unsigned short *)&(sptr[0]) );
664 sgEndianSwap( (unsigned short *)&(sptr[1]) );
666 vs.push_back( sptr[0] );
667 tcs.push_back( sptr[1] );
668 // cout << sptr[0] << "/" << sptr[1] << " ";
672 fans_v.push_back( vs );
673 fans_tc.push_back( tcs );
674 fan_materials.push_back( material );
678 // unknown object type, just skip
681 for ( j = 0; j < nproperties; ++j ) {
683 sgReadChar( fp, &prop_type );
685 sgReadUInt( fp, &nbytes );
686 // cout << "property size = " << nbytes << endl;
687 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
688 char *ptr = buf.get_ptr();
689 sgReadBytes( fp, nbytes, ptr );
693 for ( j = 0; j < nelements; ++j ) {
694 sgReadUInt( fp, &nbytes );
695 // cout << "element size = " << nbytes << endl;
696 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
697 char *ptr = buf.get_ptr();
698 sgReadBytes( fp, nbytes, ptr );
706 if ( sgReadError() ) {
707 cout << "We detected an error while reading the file." << endl;
715 // write out the structures to a binary file. We assume that the
716 // groups come to us sorted by material property. If not, things
717 // don't break, but the result won't be as optimal.
718 bool SGBinObject::write_bin( const string& base, const string& name,
729 string dir = base + "/" + b.gen_base_path();
730 string command = "mkdir -p " + dir;
731 #if defined(_MSC_VER) || defined(__MINGW32__)
732 system( (string("mkdir ") + dir).c_str() );
734 system(command.c_str());
737 string file = dir + "/" + name + ".gz";
738 cout << "Output file = " << file << endl;
741 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
742 cout << "ERROR: opening " << file << " for writing!" << endl;
748 cout << "points size = " << pts_v.size() << " pt_materials = "
749 << pt_materials.size() << endl;
750 cout << "triangles size = " << tris_v.size() << " tri_materials = "
751 << tri_materials.size() << endl;
752 cout << "strips size = " << strips_v.size() << " strip_materials = "
753 << strip_materials.size() << endl;
754 cout << "fans size = " << fans_v.size() << " fan_materials = "
755 << fan_materials.size() << endl;
757 cout << "nodes = " << wgs84_nodes.size() << endl;
758 cout << "colors = " << colors.size() << endl;
759 cout << "normals = " << normals.size() << endl;
760 cout << "tex coords = " << texcoords.size() << endl;
762 // write header magic
763 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
764 time_t calendar_time = time(NULL);
765 sgWriteLong( fp, (long int)calendar_time );
767 // calculate and write number of top level objects
772 nobjects++; // for gbs
773 nobjects++; // for vertices
774 nobjects++; // for colors
775 nobjects++; // for normals
776 nobjects++; // for texcoords
781 while ( start < (int)pt_materials.size() ) {
782 material = pt_materials[start];
783 while ( (end < (int)pt_materials.size()) &&
784 (material == pt_materials[end]) ) {
788 start = end; end = start + 1;
795 while ( start < (int)tri_materials.size() ) {
796 material = tri_materials[start];
797 while ( (end < (int)tri_materials.size()) &&
798 (material == tri_materials[end]) ) {
802 start = end; end = start + 1;
809 while ( start < (int)strip_materials.size() ) {
810 material = strip_materials[start];
811 while ( (end < (int)strip_materials.size()) &&
812 (material == strip_materials[end]) ) {
816 start = end; end = start + 1;
823 while ( start < (int)fan_materials.size() ) {
824 material = fan_materials[start];
825 while ( (end < (int)fan_materials.size()) &&
826 (material == fan_materials[end]) ) {
830 start = end; end = start + 1;
834 cout << "total top level objects = " << nobjects << endl;
835 sgWriteShort( fp, nobjects );
837 // write bounding sphere
838 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
839 sgWriteShort( fp, 0 ); // nproperties
840 sgWriteShort( fp, 1 ); // nelements
842 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
844 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
845 sgWritedVec3( fp, center );
846 sgWriteFloat( fp, gbs_radius );
849 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
850 sgWriteShort( fp, 0 ); // nproperties
851 sgWriteShort( fp, 1 ); // nelements
852 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
853 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
854 p = wgs84_nodes[i] - gbs_center;
855 sgSetVec3( pt, p.x(), p.y(), p.z() );
856 sgWriteVec3( fp, pt );
859 // dump vertex color list
860 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
861 sgWriteShort( fp, 0 ); // nproperties
862 sgWriteShort( fp, 1 ); // nelements
863 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
864 for ( i = 0; i < (int)colors.size(); ++i ) {
866 // Right now we have a place holder for color alpha but we
867 // need to update the interface so the calling program can
869 sgSetVec4( color, p.x(), p.y(), p.z(), 1.0 );
870 sgWriteVec4( fp, color );
873 // dump vertex normal list
874 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
875 sgWriteShort( fp, 0 ); // nproperties
876 sgWriteShort( fp, 1 ); // nelements
877 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
879 for ( i = 0; i < (int)normals.size(); ++i ) {
881 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
882 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
883 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
884 sgWriteBytes( fp, 3, normal );
887 // dump texture coordinates
888 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
889 sgWriteShort( fp, 0 ); // nproperties
890 sgWriteShort( fp, 1 ); // nelements
891 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
892 for ( i = 0; i < (int)texcoords.size(); ++i ) {
894 sgSetVec2( t, p.x(), p.y() );
895 sgWriteVec2( fp, t );
898 // dump point groups if they exist
899 if ( pts_v.size() > 0 ) {
903 while ( start < (int)pt_materials.size() ) {
905 material = pt_materials[start];
906 while ( (end < (int)pt_materials.size()) &&
907 (material == pt_materials[end]) )
909 // cout << "end = " << end << endl;
912 // cout << "group = " << start << " to " << end - 1 << endl;
914 // write group headers
915 sgWriteChar( fp, (char)SG_POINTS ); // type
916 sgWriteShort( fp, 2 ); // nproperties
917 sgWriteShort( fp, end - start ); // nelements
919 sgWriteChar( fp, (char)SG_MATERIAL ); // property
920 sgWriteUInt( fp, material.length() ); // nbytes
921 sgWriteBytes( fp, material.length(), material.c_str() );
925 if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
926 if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
927 if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
928 if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
929 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
930 sgWriteUInt( fp, 1 ); // nbytes
931 sgWriteChar( fp, idx_mask );
934 for ( i = start; i < end; ++i ) {
936 sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) );
937 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
938 if ( pts_v.size() ) {
939 sgWriteShort( fp, (short)pts_v[i][j] );
941 if ( pts_n.size() ) {
942 sgWriteShort( fp, (short)pts_n[i][j] );
944 if ( pts_c.size() ) {
945 sgWriteShort( fp, (short)pts_c[i][j] );
947 if ( pts_tc.size() ) {
948 sgWriteShort( fp, (short)pts_tc[i][j] );
958 // dump individual triangles if they exist
959 if ( tris_v.size() > 0 ) {
963 while ( start < (int)tri_materials.size() ) {
965 material = tri_materials[start];
966 while ( (end < (int)tri_materials.size()) &&
967 (material == tri_materials[end]) )
969 // cout << "end = " << end << endl;
972 // cout << "group = " << start << " to " << end - 1 << endl;
974 // write group headers
975 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
976 sgWriteShort( fp, 2 ); // nproperties
977 sgWriteShort( fp, 1 ); // nelements
979 sgWriteChar( fp, (char)SG_MATERIAL ); // property
980 sgWriteUInt( fp, material.length() ); // nbytes
981 sgWriteBytes( fp, material.length(), material.c_str() );
985 if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
986 if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
987 if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
988 if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
989 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
990 sgWriteUInt( fp, 1 ); // nbytes
991 sgWriteChar( fp, idx_mask );
994 sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) );
997 for ( i = start; i < end; ++i ) {
998 for ( j = 0; j < 3; ++j ) {
999 if ( tris_v.size() ) {
1000 sgWriteShort( fp, (short)tris_v[i][j] );
1002 if ( tris_n.size() ) {
1003 sgWriteShort( fp, (short)tris_n[i][j] );
1005 if ( tris_c.size() ) {
1006 sgWriteShort( fp, (short)tris_c[i][j] );
1008 if ( tris_tc.size() ) {
1009 sgWriteShort( fp, (short)tris_tc[i][j] );
1019 // dump triangle strips
1020 if ( strips_v.size() > 0 ) {
1024 while ( start < (int)strip_materials.size() ) {
1026 material = strip_materials[start];
1027 while ( (end < (int)strip_materials.size()) &&
1028 (material == strip_materials[end]) )
1030 // cout << "end = " << end << endl;
1033 // cout << "group = " << start << " to " << end - 1 << endl;
1035 // write group headers
1036 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
1037 sgWriteShort( fp, 2 ); // nproperties
1038 sgWriteShort( fp, end - start ); // nelements
1040 sgWriteChar( fp, (char)SG_MATERIAL ); // property
1041 sgWriteUInt( fp, material.length() ); // nbytes
1042 sgWriteBytes( fp, material.length(), material.c_str() );
1046 if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
1047 if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
1048 if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
1049 if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
1050 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
1051 sgWriteUInt( fp, 1 ); // nbytes
1052 sgWriteChar( fp, idx_mask );
1055 for ( i = start; i < end; ++i ) {
1057 sgWriteUInt( fp, strips_v[i].size() * idx_size * sizeof(short));
1058 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1059 if ( strips_v.size() ) {
1060 sgWriteShort( fp, (short)strips_v[i][j] );
1062 if ( strips_n.size() ) {
1063 sgWriteShort( fp, (short)strips_n[i][j] );
1065 if ( strips_c.size() ) {
1066 sgWriteShort( fp, (short)strips_c[i][j] );
1068 if ( strips_tc.size() ) {
1069 sgWriteShort( fp, (short)strips_tc[i][j] );
1079 // dump triangle fans
1080 if ( fans_v.size() > 0 ) {
1084 while ( start < (int)fan_materials.size() ) {
1086 material = fan_materials[start];
1087 while ( (end < (int)fan_materials.size()) &&
1088 (material == fan_materials[end]) )
1090 // cout << "end = " << end << endl;
1093 // cout << "group = " << start << " to " << end - 1 << endl;
1095 // write group headers
1096 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
1097 sgWriteShort( fp, 2 ); // nproperties
1098 sgWriteShort( fp, end - start ); // nelements
1100 sgWriteChar( fp, (char)SG_MATERIAL ); // property
1101 sgWriteUInt( fp, material.length() ); // nbytes
1102 sgWriteBytes( fp, material.length(), material.c_str() );
1106 if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
1107 if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
1108 if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
1109 if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
1110 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
1111 sgWriteUInt( fp, 1 ); // nbytes
1112 sgWriteChar( fp, idx_mask );
1115 for ( i = start; i < end; ++i ) {
1117 sgWriteUInt( fp, fans_v[i].size() * idx_size * sizeof(short) );
1118 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
1119 if ( fans_v.size() ) {
1120 sgWriteShort( fp, (short)fans_v[i][j] );
1122 if ( fans_n.size() ) {
1123 sgWriteShort( fp, (short)fans_n[i][j] );
1125 if ( fans_c.size() ) {
1126 sgWriteShort( fp, (short)fans_c[i][j] );
1128 if ( fans_tc.size() ) {
1129 sgWriteShort( fp, (short)fans_tc[i][j] );
1142 if ( sgWriteError() ) {
1143 cout << "We detected an error while writing the file." << endl;
1151 // write out the structures to an ASCII file. We assume that the
1152 // groups come to us sorted by material property. If not, things
1153 // don't break, but the result won't be as optimal.
1154 bool SGBinObject::write_ascii( const string& base, const string& name,
1160 string dir = base + "/" + b.gen_base_path();
1161 string command = "mkdir -p " + dir;
1162 #if defined(_MSC_VER) || defined(__MINGW32__)
1163 system( (string("mkdir ") + dir).c_str() );
1165 system(command.c_str());
1168 // string file = dir + "/" + b.gen_index_str();
1169 string file = dir + "/" + name;
1170 cout << "Output file = " << file << endl;
1173 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1174 cout << "ERROR: opening " << file << " for writing!" << endl;
1178 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1179 << tri_materials.size() << endl;
1180 cout << "strips size = " << strips_v.size() << " strip_materials = "
1181 << strip_materials.size() << endl;
1182 cout << "fans size = " << fans_v.size() << " fan_materials = "
1183 << fan_materials.size() << endl;
1185 cout << "points = " << wgs84_nodes.size() << endl;
1186 cout << "tex coords = " << texcoords.size() << endl;
1188 fprintf(fp, "# FGFS Scenery\n");
1189 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1191 time_t calendar_time = time(NULL);
1192 struct tm *local_tm;
1193 local_tm = localtime( &calendar_time );
1195 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1196 fprintf(fp, "# Created %s\n", time_str );
1199 // write bounding sphere
1200 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1201 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1205 fprintf(fp, "# vertex list\n");
1206 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1207 p = wgs84_nodes[i] - gbs_center;
1209 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1213 fprintf(fp, "# vertex normal list\n");
1214 for ( i = 0; i < (int)normals.size(); ++i ) {
1216 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1220 // dump texture coordinates
1221 fprintf(fp, "# texture coordinate list\n");
1222 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1224 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1228 // dump individual triangles if they exist
1229 if ( tris_v.size() > 0 ) {
1230 fprintf(fp, "# triangle groups\n");
1235 while ( start < (int)tri_materials.size() ) {
1237 material = tri_materials[start];
1238 while ( (end < (int)tri_materials.size()) &&
1239 (material == tri_materials[end]) )
1241 // cout << "end = " << end << endl;
1244 // cout << "group = " << start << " to " << end - 1 << endl;
1246 // make a list of points for the group
1247 point_list group_nodes;
1248 group_nodes.clear();
1250 double bs_radius = 0;
1251 for ( i = start; i < end; ++i ) {
1252 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1253 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
1254 bs_center = calc_center( group_nodes );
1255 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1259 // write group headers
1261 fprintf(fp, "# usemtl %s\n", material.c_str());
1262 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1263 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1266 for ( i = start; i < end; ++i ) {
1268 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1269 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1279 // dump triangle groups
1280 if ( strips_v.size() > 0 ) {
1281 fprintf(fp, "# triangle strips\n");
1286 while ( start < (int)strip_materials.size() ) {
1288 material = strip_materials[start];
1289 while ( (end < (int)strip_materials.size()) &&
1290 (material == strip_materials[end]) )
1292 // cout << "end = " << end << endl;
1295 // cout << "group = " << start << " to " << end - 1 << endl;
1297 // make a list of points for the group
1298 point_list group_nodes;
1299 group_nodes.clear();
1301 double bs_radius = 0;
1302 for ( i = start; i < end; ++i ) {
1303 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1304 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
1305 bs_center = calc_center( group_nodes );
1306 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1310 // write group headers
1312 fprintf(fp, "# usemtl %s\n", material.c_str());
1313 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1314 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1317 for ( i = start; i < end; ++i ) {
1319 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1320 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1333 command = "gzip --force --best " + file;
1334 system(command.c_str());