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>
38 #include <simgear/bucket/newbucket.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include "lowlevel.hxx"
42 #include "sg_binobj.hxx"
45 SG_USING_STD( string );
46 SG_USING_STD( vector );
50 SG_BOUNDING_SPHERE = 0,
59 SG_TRIANGLE_FACES = 10,
60 SG_TRIANGLE_STRIPS = 11,
65 SG_IDX_VERTICES = 0x01,
66 SG_IDX_NORMALS = 0x02,
68 SG_IDX_TEXCOORDS = 0x08
71 enum sgPropertyTypes {
77 class sgSimpleBuffer {
86 inline sgSimpleBuffer( unsigned int s )
92 SG_LOG(SG_EVENT, SG_DEBUG, "Creating a new buffer of size = " << size);
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 SG_LOG(SG_EVENT, SG_DEBUG, "resizing buffer to size = " << size);
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 Point3D sgCalcCenter( 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 )
173 unsigned char idx_mask;
175 bool do_vertices, do_normals, do_colors, do_texcoords;
177 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 ) {
212 // cout << "idx_mask = " << (int)idx_mask << endl;
217 do_texcoords = false;
218 if ( idx_mask & SG_IDX_VERTICES ) {
222 if ( idx_mask & SG_IDX_NORMALS ) {
226 if ( idx_mask & SG_IDX_COLORS ) {
230 if ( idx_mask & SG_IDX_TEXCOORDS ) {
237 for ( j = 0; j < nelements; ++j ) {
238 sgReadUInt( fp, &nbytes );
239 // cout << "element size = " << nbytes << endl;
240 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
241 char *ptr = buf.get_ptr();
242 sgReadBytes( fp, nbytes, ptr );
243 int count = nbytes / (idx_size * sizeof(unsigned short));
244 unsigned short *sptr = (unsigned short *)ptr;
245 int_list vs; vs.clear();
246 int_list ns; ns.clear();
247 int_list cs; cs.clear();
248 int_list tcs; tcs.clear();
249 for ( k = 0; k < count; ++k ) {
250 if ( sgIsBigEndian() ) {
251 for ( idx = 0; idx < idx_size; ++idx ) {
252 sgEndianSwap( (uint16_t *)&(sptr[idx]) );
257 vs.push_back( sptr[idx++] );
260 ns.push_back( sptr[idx++] );
263 cs.push_back( sptr[idx++] );
265 if ( do_texcoords ) {
266 tcs.push_back( sptr[idx++] );
268 // cout << sptr[0] << " ";
272 vertices->push_back( vs );
273 normals->push_back( ns );
274 colors->push_back( cs );
275 texcoords->push_back( tcs );
276 materials->push_back( material );
281 // read a binary file and populate the provided structures.
282 bool SGBinObject::read_bin( const string& file ) {
286 sgSimpleBuffer buf( 32768 ); // 32 Kb
288 // zero out structures
289 gbs_center = SGVec3d(0, 0, 0);
300 pt_materials.clear();
306 tri_materials.clear();
312 strip_materials.clear();
318 fan_materials.clear();
321 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
322 string filegz = file + ".gz";
323 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
324 SG_LOG( SG_EVENT, SG_ALERT,
325 "ERROR: opening " << file << " or " << filegz << " for reading!");
335 sgReadUInt( fp, &header );
336 if ( ((header & 0xFF000000) >> 24) == 'S' &&
337 ((header & 0x00FF0000) >> 16) == 'G' ) {
338 // cout << "Good header" << endl;
340 version = (header & 0x0000FFFF);
341 // cout << "File version = " << version << endl;
343 // close the file before we return
349 // read creation time
350 unsigned int foo_calendar_time;
351 sgReadUInt( fp, &foo_calendar_time );
354 time_t calendar_time = foo_calendar_time;
355 // The following code has a global effect on the host application
356 // and can screws up the time elsewhere. It should be avoided
357 // unless you need this for debugging in which case you should
358 // disable it again once the debugging task is finished.
360 local_tm = localtime( &calendar_time );
362 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
363 SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
366 // read number of top level objects
368 sgReadShort( fp, &nobjects );
369 // cout << "Total objects to read = " << nobjects << endl;
372 for ( i = 0; i < nobjects; ++i ) {
373 // read object header
375 short nproperties, nelements;
376 sgReadChar( fp, &obj_type );
377 sgReadShort( fp, &nproperties );
378 sgReadShort( fp, &nelements );
380 // cout << "object " << i << " = " << (int)obj_type << " props = "
381 // << nproperties << " elements = " << nelements << endl;
383 if ( obj_type == SG_BOUNDING_SPHERE ) {
384 // read bounding sphere properties
385 for ( j = 0; j < nproperties; ++j ) {
387 sgReadChar( fp, &prop_type );
389 sgReadUInt( fp, &nbytes );
390 // cout << "property size = " << nbytes << endl;
391 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
392 char *ptr = buf.get_ptr();
393 sgReadBytes( fp, nbytes, ptr );
396 // read bounding sphere elements
397 for ( j = 0; j < nelements; ++j ) {
398 sgReadUInt( fp, &nbytes );
399 // cout << "element size = " << nbytes << endl;
400 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
401 char *ptr = buf.get_ptr();
402 sgReadBytes( fp, nbytes, ptr );
404 double *dptr = (double *)ptr;
405 if ( sgIsBigEndian() ) {
406 sgEndianSwap( (uint64_t *)&(dptr[0]) );
407 sgEndianSwap( (uint64_t *)&(dptr[1]) );
408 sgEndianSwap( (uint64_t *)&(dptr[2]) );
410 gbs_center = SGVec3d( dptr[0], dptr[1], dptr[2] );
411 // cout << "Center = " << gbs_center << endl;
412 ptr += sizeof(double) * 3;
414 float *fptr = (float *)ptr;
415 if ( sgIsBigEndian() ) {
416 sgEndianSwap( (uint32_t *)fptr );
418 gbs_radius = fptr[0];
419 // cout << "Bounding radius = " << gbs_radius << endl;
421 } else if ( obj_type == SG_VERTEX_LIST ) {
422 // read vertex list properties
423 for ( j = 0; j < nproperties; ++j ) {
425 sgReadChar( fp, &prop_type );
427 sgReadUInt( fp, &nbytes );
428 // cout << "property size = " << nbytes << endl;
429 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
430 char *ptr = buf.get_ptr();
431 sgReadBytes( fp, nbytes, ptr );
434 // read vertex list elements
435 for ( j = 0; j < nelements; ++j ) {
436 sgReadUInt( fp, &nbytes );
437 // cout << "element size = " << nbytes << endl;
438 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
439 char *ptr = buf.get_ptr();
440 sgReadBytes( fp, nbytes, ptr );
441 int count = nbytes / (sizeof(float) * 3);
442 float *fptr = (float *)ptr;
443 wgs84_nodes.reserve( count );
444 for ( k = 0; k < count; ++k ) {
445 if ( sgIsBigEndian() ) {
446 sgEndianSwap( (uint32_t *)&(fptr[0]) );
447 sgEndianSwap( (uint32_t *)&(fptr[1]) );
448 sgEndianSwap( (uint32_t *)&(fptr[2]) );
450 wgs84_nodes.push_back( SGVec3d(fptr[0], fptr[1], fptr[2]) );
454 } else if ( obj_type == SG_COLOR_LIST ) {
455 // read color list properties
456 for ( j = 0; j < nproperties; ++j ) {
458 sgReadChar( fp, &prop_type );
460 sgReadUInt( fp, &nbytes );
461 // cout << "property size = " << nbytes << endl;
462 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
463 char *ptr = buf.get_ptr();
464 sgReadBytes( fp, nbytes, ptr );
467 // read color list elements
468 for ( j = 0; j < nelements; ++j ) {
469 sgReadUInt( fp, &nbytes );
470 // cout << "element size = " << nbytes << endl;
471 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
472 char *ptr = buf.get_ptr();
473 sgReadBytes( fp, nbytes, ptr );
474 int count = nbytes / (sizeof(float) * 4);
475 float *fptr = (float *)ptr;
476 colors.reserve(count);
477 for ( k = 0; k < count; ++k ) {
478 if ( sgIsBigEndian() ) {
479 sgEndianSwap( (uint32_t *)&(fptr[0]) );
480 sgEndianSwap( (uint32_t *)&(fptr[1]) );
481 sgEndianSwap( (uint32_t *)&(fptr[2]) );
482 sgEndianSwap( (uint32_t *)&(fptr[3]) );
484 SGVec4f color( fptr[0], fptr[1], fptr[2], fptr[3] );
485 colors.push_back( color );
489 } else if ( obj_type == SG_NORMAL_LIST ) {
490 // read normal list properties
491 for ( j = 0; j < nproperties; ++j ) {
493 sgReadChar( fp, &prop_type );
495 sgReadUInt( fp, &nbytes );
496 // cout << "property size = " << nbytes << endl;
497 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
498 char *ptr = buf.get_ptr();
499 sgReadBytes( fp, nbytes, ptr );
502 // read normal list elements
503 for ( j = 0; j < nelements; ++j ) {
504 sgReadUInt( fp, &nbytes );
505 // cout << "element size = " << nbytes << endl;
506 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
507 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
508 sgReadBytes( fp, nbytes, ptr );
509 int count = nbytes / 3;
510 normals.reserve( count );
511 for ( k = 0; k < count; ++k ) {
512 SGVec3f normal((ptr[0]) / 127.5 - 1.0,
513 (ptr[1]) / 127.5 - 1.0,
514 (ptr[2]) / 127.5 - 1.0);
516 normals.push_back(normalize(normal));
520 } else if ( obj_type == SG_TEXCOORD_LIST ) {
521 // read texcoord list properties
522 for ( j = 0; j < nproperties; ++j ) {
524 sgReadChar( fp, &prop_type );
526 sgReadUInt( fp, &nbytes );
527 // cout << "property size = " << nbytes << endl;
528 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
529 char *ptr = buf.get_ptr();
530 sgReadBytes( fp, nbytes, ptr );
533 // read texcoord list elements
534 for ( j = 0; j < nelements; ++j ) {
535 sgReadUInt( fp, &nbytes );
536 // cout << "element size = " << nbytes << endl;
537 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
538 char *ptr = buf.get_ptr();
539 sgReadBytes( fp, nbytes, ptr );
540 int count = nbytes / (sizeof(float) * 2);
541 float *fptr = (float *)ptr;
542 texcoords.reserve(count);
543 for ( k = 0; k < count; ++k ) {
544 if ( sgIsBigEndian() ) {
545 sgEndianSwap( (uint32_t *)&(fptr[0]) );
546 sgEndianSwap( (uint32_t *)&(fptr[1]) );
548 texcoords.push_back( SGVec2f( fptr[0], fptr[1] ) );
552 } else if ( obj_type == SG_POINTS ) {
553 // read point elements
554 read_object( fp, SG_POINTS, nproperties, nelements,
555 &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
556 } else if ( obj_type == SG_TRIANGLE_FACES ) {
557 // read triangle face properties
558 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
559 &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
560 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
561 // read triangle strip properties
562 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
563 &strips_v, &strips_n, &strips_c, &strips_tc,
565 } else if ( obj_type == SG_TRIANGLE_FANS ) {
566 // read triangle fan properties
567 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
568 &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
570 // unknown object type, just skip
573 for ( j = 0; j < nproperties; ++j ) {
575 sgReadChar( fp, &prop_type );
577 sgReadUInt( fp, &nbytes );
578 // cout << "property size = " << nbytes << endl;
579 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
580 char *ptr = buf.get_ptr();
581 sgReadBytes( fp, nbytes, ptr );
585 for ( j = 0; j < nelements; ++j ) {
586 sgReadUInt( fp, &nbytes );
587 // cout << "element size = " << nbytes << endl;
588 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
589 char *ptr = buf.get_ptr();
590 sgReadBytes( fp, nbytes, ptr );
598 if ( sgReadError() ) {
599 cout << "We detected an error while reading the file." << endl;
607 // write out the structures to a binary file. We assume that the
608 // groups come to us sorted by material property. If not, things
609 // don't break, but the result won't be as optimal.
610 bool SGBinObject::write_bin( const string& base, const string& name,
614 unsigned char idx_mask;
617 SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
618 file.create_dir( 0755 );
619 cout << "Output file = " << file.str() << endl;
622 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
623 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
629 cout << "points size = " << pts_v.size() << " pt_materials = "
630 << pt_materials.size() << endl;
631 cout << "triangles size = " << tris_v.size() << " tri_materials = "
632 << tri_materials.size() << endl;
633 cout << "strips size = " << strips_v.size() << " strip_materials = "
634 << strip_materials.size() << endl;
635 cout << "fans size = " << fans_v.size() << " fan_materials = "
636 << fan_materials.size() << endl;
638 cout << "nodes = " << wgs84_nodes.size() << endl;
639 cout << "colors = " << colors.size() << endl;
640 cout << "normals = " << normals.size() << endl;
641 cout << "tex coords = " << texcoords.size() << endl;
643 // write header magic
644 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
645 time_t calendar_time = time(NULL);
646 sgWriteLong( fp, (int32_t)calendar_time );
648 // calculate and write number of top level objects
653 nobjects++; // for gbs
654 nobjects++; // for vertices
655 nobjects++; // for colors
656 nobjects++; // for normals
657 nobjects++; // for texcoords
662 while ( start < (int)pt_materials.size() ) {
663 material = pt_materials[start];
664 while ( (end < (int)pt_materials.size()) &&
665 (material == pt_materials[end]) ) {
669 start = end; end = start + 1;
676 while ( start < (int)tri_materials.size() ) {
677 material = tri_materials[start];
678 while ( (end < (int)tri_materials.size()) &&
679 (material == tri_materials[end]) ) {
683 start = end; end = start + 1;
690 while ( start < (int)strip_materials.size() ) {
691 material = strip_materials[start];
692 while ( (end < (int)strip_materials.size()) &&
693 (material == strip_materials[end]) ) {
697 start = end; end = start + 1;
704 while ( start < (int)fan_materials.size() ) {
705 material = fan_materials[start];
706 while ( (end < (int)fan_materials.size()) &&
707 (material == fan_materials[end]) ) {
711 start = end; end = start + 1;
715 cout << "total top level objects = " << nobjects << endl;
716 sgWriteShort( fp, nobjects );
718 // write bounding sphere
719 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
720 sgWriteShort( fp, 0 ); // nproperties
721 sgWriteShort( fp, 1 ); // nelements
723 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
725 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
726 sgWritedVec3( fp, center );
727 sgWriteFloat( fp, gbs_radius );
730 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
731 sgWriteShort( fp, 0 ); // nproperties
732 sgWriteShort( fp, 1 ); // nelements
733 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
734 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
735 SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center);
736 sgWriteVec3( fp, p.data() );
739 // dump vertex color list
740 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
741 sgWriteShort( fp, 0 ); // nproperties
742 sgWriteShort( fp, 1 ); // nelements
743 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
744 for ( i = 0; i < (int)colors.size(); ++i ) {
745 sgWriteVec4( fp, colors[i].data() );
748 // dump vertex normal list
749 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
750 sgWriteShort( fp, 0 ); // nproperties
751 sgWriteShort( fp, 1 ); // nelements
752 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
754 for ( i = 0; i < (int)normals.size(); ++i ) {
755 SGVec3f p = normals[i];
756 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
757 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
758 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
759 sgWriteBytes( fp, 3, normal );
762 // dump texture coordinates
763 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
764 sgWriteShort( fp, 0 ); // nproperties
765 sgWriteShort( fp, 1 ); // nelements
766 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
767 for ( i = 0; i < (int)texcoords.size(); ++i ) {
768 sgWriteVec2( fp, texcoords[i].data() );
771 // dump point groups if they exist
772 if ( pts_v.size() > 0 ) {
776 while ( start < (int)pt_materials.size() ) {
778 material = pt_materials[start];
779 while ( (end < (int)pt_materials.size()) &&
780 (material == pt_materials[end]) )
782 // cout << "end = " << end << endl;
785 // cout << "group = " << start << " to " << end - 1 << endl;
787 // write group headers
788 sgWriteChar( fp, (char)SG_POINTS ); // type
789 sgWriteShort( fp, 2 ); // nproperties
790 sgWriteShort( fp, end - start ); // nelements
792 sgWriteChar( fp, (char)SG_MATERIAL ); // property
793 sgWriteUInt( fp, material.length() ); // nbytes
794 sgWriteBytes( fp, material.length(), material.c_str() );
798 if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
799 if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
800 if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
801 if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
802 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
803 sgWriteUInt( fp, 1 ); // nbytes
804 sgWriteChar( fp, idx_mask );
807 for ( i = start; i < end; ++i ) {
809 sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) );
810 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
811 if ( pts_v.size() ) {
812 sgWriteShort( fp, (short)pts_v[i][j] );
814 if ( pts_n.size() ) {
815 sgWriteShort( fp, (short)pts_n[i][j] );
817 if ( pts_c.size() ) {
818 sgWriteShort( fp, (short)pts_c[i][j] );
820 if ( pts_tc.size() ) {
821 sgWriteShort( fp, (short)pts_tc[i][j] );
831 // dump individual triangles if they exist
832 if ( tris_v.size() > 0 ) {
836 while ( start < (int)tri_materials.size() ) {
838 material = tri_materials[start];
839 while ( (end < (int)tri_materials.size()) &&
840 (material == tri_materials[end]) &&
841 3*(end-start) < 32760 )
843 // cout << "end = " << end << endl;
846 // cout << "group = " << start << " to " << end - 1 << endl;
848 // write group headers
849 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
850 sgWriteShort( fp, 2 ); // nproperties
851 sgWriteShort( fp, 1 ); // nelements
853 sgWriteChar( fp, (char)SG_MATERIAL ); // property
854 sgWriteUInt( fp, material.length() ); // nbytes
855 sgWriteBytes( fp, material.length(), material.c_str() );
859 if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
860 if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
861 if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
862 if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
863 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
864 sgWriteUInt( fp, 1 ); // nbytes
865 sgWriteChar( fp, idx_mask );
868 sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) );
871 for ( i = start; i < end; ++i ) {
872 for ( j = 0; j < 3; ++j ) {
873 if ( tris_v.size() ) {
874 sgWriteShort( fp, (short)tris_v[i][j] );
876 if ( tris_n.size() ) {
877 sgWriteShort( fp, (short)tris_n[i][j] );
879 if ( tris_c.size() ) {
880 sgWriteShort( fp, (short)tris_c[i][j] );
882 if ( tris_tc.size() ) {
883 sgWriteShort( fp, (short)tris_tc[i][j] );
893 // dump triangle strips
894 if ( strips_v.size() > 0 ) {
898 while ( start < (int)strip_materials.size() ) {
900 material = strip_materials[start];
901 while ( (end < (int)strip_materials.size()) &&
902 (material == strip_materials[end]) )
904 // cout << "end = " << end << endl;
907 // cout << "group = " << start << " to " << end - 1 << endl;
909 // write group headers
910 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
911 sgWriteShort( fp, 2 ); // nproperties
912 sgWriteShort( fp, end - start ); // nelements
914 sgWriteChar( fp, (char)SG_MATERIAL ); // property
915 sgWriteUInt( fp, material.length() ); // nbytes
916 sgWriteBytes( fp, material.length(), material.c_str() );
920 if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
921 if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
922 if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
923 if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
924 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
925 sgWriteUInt( fp, 1 ); // nbytes
926 sgWriteChar( fp, idx_mask );
929 for ( i = start; i < end; ++i ) {
931 sgWriteUInt( fp, strips_v[i].size() * idx_size * sizeof(short));
932 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
933 if ( strips_v.size() ) {
934 sgWriteShort( fp, (short)strips_v[i][j] );
936 if ( strips_n.size() ) {
937 sgWriteShort( fp, (short)strips_n[i][j] );
939 if ( strips_c.size() ) {
940 sgWriteShort( fp, (short)strips_c[i][j] );
942 if ( strips_tc.size() ) {
943 sgWriteShort( fp, (short)strips_tc[i][j] );
953 // dump triangle fans
954 if ( fans_v.size() > 0 ) {
958 while ( start < (int)fan_materials.size() ) {
960 material = fan_materials[start];
961 while ( (end < (int)fan_materials.size()) &&
962 (material == fan_materials[end]) )
964 // cout << "end = " << end << endl;
967 // cout << "group = " << start << " to " << end - 1 << endl;
969 // write group headers
970 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
971 sgWriteShort( fp, 2 ); // nproperties
972 sgWriteShort( fp, end - start ); // nelements
974 sgWriteChar( fp, (char)SG_MATERIAL ); // property
975 sgWriteUInt( fp, material.length() ); // nbytes
976 sgWriteBytes( fp, material.length(), material.c_str() );
980 if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
981 if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
982 if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
983 if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
984 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
985 sgWriteUInt( fp, 1 ); // nbytes
986 sgWriteChar( fp, idx_mask );
989 for ( i = start; i < end; ++i ) {
991 sgWriteUInt( fp, fans_v[i].size() * idx_size * sizeof(short) );
992 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
993 if ( fans_v.size() ) {
994 sgWriteShort( fp, (short)fans_v[i][j] );
996 if ( fans_n.size() ) {
997 sgWriteShort( fp, (short)fans_n[i][j] );
999 if ( fans_c.size() ) {
1000 sgWriteShort( fp, (short)fans_c[i][j] );
1002 if ( fans_tc.size() ) {
1003 sgWriteShort( fp, (short)fans_tc[i][j] );
1016 if ( sgWriteError() ) {
1017 cout << "We detected an error while writing the file." << endl;
1025 // write out the structures to an ASCII file. We assume that the
1026 // groups come to us sorted by material property. If not, things
1027 // don't break, but the result won't be as optimal.
1028 bool SGBinObject::write_ascii( const string& base, const string& name,
1033 SGPath file = base + "/" + b.gen_base_path() + "/" + name;
1034 file.create_dir( 0755 );
1035 cout << "Output file = " << file.str() << endl;
1038 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1039 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
1043 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1044 << tri_materials.size() << endl;
1045 cout << "strips size = " << strips_v.size() << " strip_materials = "
1046 << strip_materials.size() << endl;
1047 cout << "fans size = " << fans_v.size() << " fan_materials = "
1048 << fan_materials.size() << endl;
1050 cout << "points = " << wgs84_nodes.size() << endl;
1051 cout << "tex coords = " << texcoords.size() << endl;
1053 fprintf(fp, "# FGFS Scenery\n");
1054 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1056 time_t calendar_time = time(NULL);
1057 struct tm *local_tm;
1058 local_tm = localtime( &calendar_time );
1060 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1061 fprintf(fp, "# Created %s\n", time_str );
1064 // write bounding sphere
1065 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1066 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1070 fprintf(fp, "# vertex list\n");
1071 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1072 SGVec3d p = wgs84_nodes[i] - gbs_center;
1074 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1078 fprintf(fp, "# vertex normal list\n");
1079 for ( i = 0; i < (int)normals.size(); ++i ) {
1080 SGVec3f p = normals[i];
1081 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1085 // dump texture coordinates
1086 fprintf(fp, "# texture coordinate list\n");
1087 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1088 SGVec2f p = texcoords[i];
1089 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1093 // dump individual triangles if they exist
1094 if ( tris_v.size() > 0 ) {
1095 fprintf(fp, "# triangle groups\n");
1100 while ( start < (int)tri_materials.size() ) {
1102 material = tri_materials[start];
1103 while ( (end < (int)tri_materials.size()) &&
1104 (material == tri_materials[end]) )
1106 // cout << "end = " << end << endl;
1109 // cout << "group = " << start << " to " << end - 1 << endl;
1111 // make a list of points for the group
1112 point_list group_nodes;
1113 group_nodes.clear();
1115 double bs_radius = 0;
1116 for ( i = start; i < end; ++i ) {
1117 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1118 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ tris_v[i][j] ]) );
1119 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1120 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1124 // write group headers
1126 fprintf(fp, "# usemtl %s\n", material.c_str());
1127 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1128 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1131 for ( i = start; i < end; ++i ) {
1133 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1134 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1144 // dump triangle groups
1145 if ( strips_v.size() > 0 ) {
1146 fprintf(fp, "# triangle strips\n");
1151 while ( start < (int)strip_materials.size() ) {
1153 material = strip_materials[start];
1154 while ( (end < (int)strip_materials.size()) &&
1155 (material == strip_materials[end]) )
1157 // cout << "end = " << end << endl;
1160 // cout << "group = " << start << " to " << end - 1 << endl;
1162 // make a list of points for the group
1163 point_list group_nodes;
1164 group_nodes.clear();
1166 double bs_radius = 0;
1167 for ( i = start; i < end; ++i ) {
1168 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1169 group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ strips_v[i][j] ]) );
1170 bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
1171 bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
1175 // write group headers
1177 fprintf(fp, "# usemtl %s\n", material.c_str());
1178 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1179 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1182 for ( i = start; i < end; ++i ) {
1184 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1185 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1198 string command = "gzip --force --best " + file.str();
1199 system(command.c_str());