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.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
37 #include <simgear/bucket/newbucket.hxx>
39 #include "lowlevel.hxx"
40 #include "sg_binobj.hxx"
43 SG_USING_STD( string );
44 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
78 class sgSimpleBuffer {
87 inline sgSimpleBuffer( unsigned int s )
93 cout << "Creating a new buffer of size = " << size << endl;
97 inline ~sgSimpleBuffer() {
101 inline unsigned int get_size() const { return size; }
102 inline char *get_ptr() const { return ptr; }
103 inline void resize( unsigned int s ) {
111 cout << "resizing buffer to size = " << size << endl;
112 ptr = new char[size];
118 // calculate the center of a list of points, by taking the halfway
119 // point between the min and max points.
120 static Point3D calc_center( point_list& wgs84_nodes ) {
123 if ( wgs84_nodes.size() ) {
124 min = max = wgs84_nodes[0];
126 min = max = Point3D( 0 );
129 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
132 if ( p.x() < min.x() ) { min.setx( p.x() ); }
133 if ( p.y() < min.y() ) { min.sety( p.y() ); }
134 if ( p.z() < min.z() ) { min.setz( p.z() ); }
136 if ( p.x() > max.x() ) { max.setx( p.x() ); }
137 if ( p.y() > max.y() ) { max.sety( p.y() ); }
138 if ( p.z() > max.z() ) { max.setz( p.z() ); }
141 return ( min + max ) / 2.0;
144 // calculate the bounding sphere. Center is the center of the
145 // tile and zero elevation
146 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
148 double radius_squared = 0;
150 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
151 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
152 if ( dist_squared > radius_squared ) {
153 radius_squared = dist_squared;
157 return sqrt(radius_squared);
162 // read object properties
163 static void read_object( gzFile fp,
167 group_list *vertices,
170 group_list *texcoords,
171 string_list *materials )
174 unsigned char idx_mask;
176 bool do_vertices, do_normals, do_colors, do_texcoords;
178 static sgSimpleBuffer buf( 32768 ); // 32 Kb
182 if ( obj_type == SG_POINTS ) {
184 idx_mask = SG_IDX_VERTICES;
188 do_texcoords = false;
191 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
198 for ( j = 0; j < nproperties; ++j ) {
200 sgReadChar( fp, &prop_type );
202 sgReadUInt( fp, &nbytes );
203 // cout << "property size = " << nbytes << endl;
204 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
205 char *ptr = buf.get_ptr();
206 sgReadBytes( fp, nbytes, ptr );
207 if ( prop_type == SG_MATERIAL ) {
208 strncpy( material, ptr, nbytes );
209 material[nbytes] = '\0';
210 // cout << "material type = " << material << endl;
211 } else if ( prop_type == SG_INDEX_TYPES ) {
213 // cout << "idx_mask = " << (int)idx_mask << endl;
218 do_texcoords = false;
219 if ( idx_mask & SG_IDX_VERTICES ) {
223 if ( idx_mask & SG_IDX_NORMALS ) {
227 if ( idx_mask & SG_IDX_COLORS ) {
231 if ( idx_mask & SG_IDX_TEXCOORDS ) {
238 for ( j = 0; j < nelements; ++j ) {
239 sgReadUInt( fp, &nbytes );
240 // cout << "element size = " << nbytes << endl;
241 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
242 char *ptr = buf.get_ptr();
243 sgReadBytes( fp, nbytes, ptr );
244 int count = nbytes / (idx_size * sizeof(short));
245 short *sptr = (short *)ptr;
246 int_list vs; vs.clear();
247 int_list ns; ns.clear();
248 int_list cs; cs.clear();
249 int_list tcs; tcs.clear();
250 for ( k = 0; k < count; ++k ) {
251 if ( sgIsBigEndian() ) {
252 for ( idx = 0; idx < idx_size; ++idx ) {
253 sgEndianSwap( (unsigned short *)&(sptr[idx]) );
258 vs.push_back( sptr[idx++] );
261 ns.push_back( sptr[idx++] );
264 cs.push_back( sptr[idx++] );
266 if ( do_texcoords ) {
267 tcs.push_back( sptr[idx++] );
269 // cout << sptr[0] << " ";
273 vertices->push_back( vs );
274 normals->push_back( ns );
275 colors->push_back( cs );
276 texcoords->push_back( tcs );
277 materials->push_back( material );
282 // read a binary file and populate the provided structures.
283 bool SGBinObject::read_bin( const string& file ) {
287 static sgSimpleBuffer buf( 32768 ); // 32 Kb
289 // zero out structures
290 gbs_center = Point3D( 0 );
301 pt_materials.clear();
307 tri_materials.clear();
313 strip_materials.clear();
319 fan_materials.clear();
322 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
323 string filegz = file + ".gz";
324 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
325 cout << "ERROR: opening " << file << " or " << filegz
326 << "for reading!" << endl;
336 sgReadUInt( fp, &header );
337 if ( ((header & 0xFF000000) >> 24) == 'S' &&
338 ((header & 0x00FF0000) >> 16) == 'G' ) {
339 // cout << "Good header" << endl;
341 version = (header & 0x0000FFFF);
342 // cout << "File version = " << version << endl;
344 // close the file before we return
350 // read creation time
351 unsigned int foo_calendar_time;
352 sgReadUInt( fp, &foo_calendar_time );
355 time_t calendar_time = foo_calendar_time;
356 // The following code has a global effect on the host application
357 // and can screws up the time elsewhere. It should be avoided
358 // unless you need this for debugging in which case you should
359 // disable it again once the debugging task is finished.
361 local_tm = localtime( &calendar_time );
363 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
364 cout << "File created on " << time_str << endl;
367 // read number of top level objects
369 sgReadShort( fp, &nobjects );
370 // cout << "Total objects to read = " << nobjects << endl;
373 for ( i = 0; i < nobjects; ++i ) {
374 // read object header
376 short nproperties, nelements;
377 sgReadChar( fp, &obj_type );
378 sgReadShort( fp, &nproperties );
379 sgReadShort( fp, &nelements );
381 // cout << "object " << i << " = " << (int)obj_type << " props = "
382 // << nproperties << " elements = " << nelements << endl;
384 if ( obj_type == SG_BOUNDING_SPHERE ) {
385 // read bounding sphere properties
386 for ( j = 0; j < nproperties; ++j ) {
388 sgReadChar( fp, &prop_type );
390 sgReadUInt( fp, &nbytes );
391 // cout << "property size = " << nbytes << endl;
392 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
393 char *ptr = buf.get_ptr();
394 sgReadBytes( fp, nbytes, ptr );
397 // read bounding sphere elements
398 for ( j = 0; j < nelements; ++j ) {
399 sgReadUInt( fp, &nbytes );
400 // cout << "element size = " << nbytes << endl;
401 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
402 char *ptr = buf.get_ptr();
403 sgReadBytes( fp, nbytes, ptr );
405 double *dptr = (double *)ptr;
406 if ( sgIsBigEndian() ) {
407 sgEndianSwap( (uint64 *)&(dptr[0]) );
408 sgEndianSwap( (uint64 *)&(dptr[1]) );
409 sgEndianSwap( (uint64 *)&(dptr[2]) );
411 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
412 // cout << "Center = " << gbs_center << endl;
413 ptr += sizeof(double) * 3;
415 float *fptr = (float *)ptr;
416 if ( sgIsBigEndian() ) {
417 sgEndianSwap( (unsigned int *)fptr );
419 gbs_radius = fptr[0];
420 // cout << "Bounding radius = " << gbs_radius << endl;
422 } else if ( obj_type == SG_VERTEX_LIST ) {
423 // read vertex list properties
424 for ( j = 0; j < nproperties; ++j ) {
426 sgReadChar( fp, &prop_type );
428 sgReadUInt( fp, &nbytes );
429 // cout << "property size = " << nbytes << endl;
430 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
431 char *ptr = buf.get_ptr();
432 sgReadBytes( fp, nbytes, ptr );
435 // read vertex list elements
436 for ( j = 0; j < nelements; ++j ) {
437 sgReadUInt( fp, &nbytes );
438 // cout << "element size = " << nbytes << endl;
439 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
440 char *ptr = buf.get_ptr();
441 sgReadBytes( fp, nbytes, ptr );
442 int count = nbytes / (sizeof(float) * 3);
443 float *fptr = (float *)ptr;
444 wgs84_nodes.reserve( count );
445 for ( k = 0; k < count; ++k ) {
446 if ( sgIsBigEndian() ) {
447 sgEndianSwap( (unsigned int *)&(fptr[0]) );
448 sgEndianSwap( (unsigned int *)&(fptr[1]) );
449 sgEndianSwap( (unsigned int *)&(fptr[2]) );
451 wgs84_nodes.push_back( Point3D(fptr[0], fptr[1], fptr[2]) );
455 } else if ( obj_type == SG_COLOR_LIST ) {
456 // read color list properties
457 for ( j = 0; j < nproperties; ++j ) {
459 sgReadChar( fp, &prop_type );
461 sgReadUInt( fp, &nbytes );
462 // cout << "property size = " << nbytes << endl;
463 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
464 char *ptr = buf.get_ptr();
465 sgReadBytes( fp, nbytes, ptr );
468 // read color list elements
469 for ( j = 0; j < nelements; ++j ) {
470 sgReadUInt( fp, &nbytes );
471 // cout << "element size = " << nbytes << endl;
472 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
473 char *ptr = buf.get_ptr();
474 sgReadBytes( fp, nbytes, ptr );
475 int count = nbytes / (sizeof(float) * 4);
476 float *fptr = (float *)ptr;
477 colors.reserve(count);
478 for ( k = 0; k < count; ++k ) {
479 if ( sgIsBigEndian() ) {
480 sgEndianSwap( (unsigned int *)&(fptr[0]) );
481 sgEndianSwap( (unsigned int *)&(fptr[1]) );
482 sgEndianSwap( (unsigned int *)&(fptr[2]) );
483 sgEndianSwap( (unsigned int *)&(fptr[3]) );
485 colors.push_back( Point3D( fptr[0], fptr[1], fptr[2] ) );
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 ) {
514 (ptr[0]) / 127.5 - 1.0,
515 (ptr[1]) / 127.5 - 1.0,
516 (ptr[2]) / 127.5 - 1.0 );
517 sgdNormalizeVec3( normal );
519 normals.push_back(Point3D(normal[0], normal[1], normal[2]));
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 texcoords.reserve(count);
546 for ( k = 0; k < count; ++k ) {
547 if ( sgIsBigEndian() ) {
548 sgEndianSwap( (unsigned int *)&(fptr[0]) );
549 sgEndianSwap( (unsigned int *)&(fptr[1]) );
551 texcoords.push_back( Point3D( fptr[0], fptr[1], 0 ) );
555 } else if ( obj_type == SG_POINTS ) {
556 // read point elements
557 read_object( fp, SG_POINTS, nproperties, nelements,
558 &pts_v, &pts_n, &pts_c, &pts_tc, &pt_materials );
559 } else if ( obj_type == SG_TRIANGLE_FACES ) {
560 // read triangle face properties
561 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
562 &tris_v, &tris_n, &tris_c, &tris_tc, &tri_materials );
563 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
564 // read triangle strip properties
565 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
566 &strips_v, &strips_n, &strips_c, &strips_tc,
568 } else if ( obj_type == SG_TRIANGLE_FANS ) {
569 // read triangle fan properties
570 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
571 &fans_v, &fans_n, &fans_c, &fans_tc, &fan_materials );
573 // unknown object type, just skip
576 for ( j = 0; j < nproperties; ++j ) {
578 sgReadChar( fp, &prop_type );
580 sgReadUInt( fp, &nbytes );
581 // cout << "property size = " << nbytes << endl;
582 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
583 char *ptr = buf.get_ptr();
584 sgReadBytes( fp, nbytes, ptr );
588 for ( j = 0; j < nelements; ++j ) {
589 sgReadUInt( fp, &nbytes );
590 // cout << "element size = " << nbytes << endl;
591 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
592 char *ptr = buf.get_ptr();
593 sgReadBytes( fp, nbytes, ptr );
601 if ( sgReadError() ) {
602 cout << "We detected an error while reading the file." << endl;
610 // write out the structures to a binary file. We assume that the
611 // groups come to us sorted by material property. If not, things
612 // don't break, but the result won't be as optimal.
613 bool SGBinObject::write_bin( const string& base, const string& name,
621 unsigned char idx_mask;
624 string dir = base + "/" + b.gen_base_path();
625 string command = "mkdir -p " + dir;
626 #if defined(_MSC_VER) || defined(__MINGW32__)
627 system( (string("mkdir ") + dir).c_str() );
629 system(command.c_str());
632 string file = dir + "/" + name + ".gz";
633 cout << "Output file = " << file << endl;
636 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
637 cout << "ERROR: opening " << file << " for writing!" << endl;
643 cout << "points size = " << pts_v.size() << " pt_materials = "
644 << pt_materials.size() << endl;
645 cout << "triangles size = " << tris_v.size() << " tri_materials = "
646 << tri_materials.size() << endl;
647 cout << "strips size = " << strips_v.size() << " strip_materials = "
648 << strip_materials.size() << endl;
649 cout << "fans size = " << fans_v.size() << " fan_materials = "
650 << fan_materials.size() << endl;
652 cout << "nodes = " << wgs84_nodes.size() << endl;
653 cout << "colors = " << colors.size() << endl;
654 cout << "normals = " << normals.size() << endl;
655 cout << "tex coords = " << texcoords.size() << endl;
657 // write header magic
658 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
659 time_t calendar_time = time(NULL);
660 sgWriteLong( fp, (long int)calendar_time );
662 // calculate and write number of top level objects
667 nobjects++; // for gbs
668 nobjects++; // for vertices
669 nobjects++; // for colors
670 nobjects++; // for normals
671 nobjects++; // for texcoords
676 while ( start < (int)pt_materials.size() ) {
677 material = pt_materials[start];
678 while ( (end < (int)pt_materials.size()) &&
679 (material == pt_materials[end]) ) {
683 start = end; end = start + 1;
690 while ( start < (int)tri_materials.size() ) {
691 material = tri_materials[start];
692 while ( (end < (int)tri_materials.size()) &&
693 (material == tri_materials[end]) ) {
697 start = end; end = start + 1;
704 while ( start < (int)strip_materials.size() ) {
705 material = strip_materials[start];
706 while ( (end < (int)strip_materials.size()) &&
707 (material == strip_materials[end]) ) {
711 start = end; end = start + 1;
718 while ( start < (int)fan_materials.size() ) {
719 material = fan_materials[start];
720 while ( (end < (int)fan_materials.size()) &&
721 (material == fan_materials[end]) ) {
725 start = end; end = start + 1;
729 cout << "total top level objects = " << nobjects << endl;
730 sgWriteShort( fp, nobjects );
732 // write bounding sphere
733 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
734 sgWriteShort( fp, 0 ); // nproperties
735 sgWriteShort( fp, 1 ); // nelements
737 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
739 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
740 sgWritedVec3( fp, center );
741 sgWriteFloat( fp, gbs_radius );
744 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
745 sgWriteShort( fp, 0 ); // nproperties
746 sgWriteShort( fp, 1 ); // nelements
747 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
748 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
749 p = wgs84_nodes[i] - gbs_center;
750 sgSetVec3( pt, p.x(), p.y(), p.z() );
751 sgWriteVec3( fp, pt );
754 // dump vertex color list
755 sgWriteChar( fp, (char)SG_COLOR_LIST ); // type
756 sgWriteShort( fp, 0 ); // nproperties
757 sgWriteShort( fp, 1 ); // nelements
758 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
759 for ( i = 0; i < (int)colors.size(); ++i ) {
761 // Right now we have a place holder for color alpha but we
762 // need to update the interface so the calling program can
764 sgSetVec4( color, p.x(), p.y(), p.z(), 1.0 );
765 sgWriteVec4( fp, color );
768 // dump vertex normal list
769 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
770 sgWriteShort( fp, 0 ); // nproperties
771 sgWriteShort( fp, 1 ); // nelements
772 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
774 for ( i = 0; i < (int)normals.size(); ++i ) {
776 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
777 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
778 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
779 sgWriteBytes( fp, 3, normal );
782 // dump texture coordinates
783 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
784 sgWriteShort( fp, 0 ); // nproperties
785 sgWriteShort( fp, 1 ); // nelements
786 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
787 for ( i = 0; i < (int)texcoords.size(); ++i ) {
789 sgSetVec2( t, p.x(), p.y() );
790 sgWriteVec2( fp, t );
793 // dump point groups if they exist
794 if ( pts_v.size() > 0 ) {
798 while ( start < (int)pt_materials.size() ) {
800 material = pt_materials[start];
801 while ( (end < (int)pt_materials.size()) &&
802 (material == pt_materials[end]) )
804 // cout << "end = " << end << endl;
807 // cout << "group = " << start << " to " << end - 1 << endl;
809 // write group headers
810 sgWriteChar( fp, (char)SG_POINTS ); // type
811 sgWriteShort( fp, 2 ); // nproperties
812 sgWriteShort( fp, end - start ); // nelements
814 sgWriteChar( fp, (char)SG_MATERIAL ); // property
815 sgWriteUInt( fp, material.length() ); // nbytes
816 sgWriteBytes( fp, material.length(), material.c_str() );
820 if ( pts_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
821 if ( pts_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
822 if ( pts_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
823 if ( pts_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
824 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
825 sgWriteUInt( fp, 1 ); // nbytes
826 sgWriteChar( fp, idx_mask );
829 for ( i = start; i < end; ++i ) {
831 sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) );
832 for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
833 if ( pts_v.size() ) {
834 sgWriteShort( fp, (short)pts_v[i][j] );
836 if ( pts_n.size() ) {
837 sgWriteShort( fp, (short)pts_n[i][j] );
839 if ( pts_c.size() ) {
840 sgWriteShort( fp, (short)pts_c[i][j] );
842 if ( pts_tc.size() ) {
843 sgWriteShort( fp, (short)pts_tc[i][j] );
853 // dump individual triangles if they exist
854 if ( tris_v.size() > 0 ) {
858 while ( start < (int)tri_materials.size() ) {
860 material = tri_materials[start];
861 while ( (end < (int)tri_materials.size()) &&
862 (material == tri_materials[end]) )
864 // cout << "end = " << end << endl;
867 // cout << "group = " << start << " to " << end - 1 << endl;
869 // write group headers
870 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
871 sgWriteShort( fp, 2 ); // nproperties
872 sgWriteShort( fp, 1 ); // nelements
874 sgWriteChar( fp, (char)SG_MATERIAL ); // property
875 sgWriteUInt( fp, material.length() ); // nbytes
876 sgWriteBytes( fp, material.length(), material.c_str() );
880 if ( tris_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
881 if ( tris_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
882 if ( tris_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
883 if ( tris_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
884 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
885 sgWriteUInt( fp, 1 ); // nbytes
886 sgWriteChar( fp, idx_mask );
889 sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) );
892 for ( i = start; i < end; ++i ) {
893 for ( j = 0; j < 3; ++j ) {
894 if ( tris_v.size() ) {
895 sgWriteShort( fp, (short)tris_v[i][j] );
897 if ( tris_n.size() ) {
898 sgWriteShort( fp, (short)tris_n[i][j] );
900 if ( tris_c.size() ) {
901 sgWriteShort( fp, (short)tris_c[i][j] );
903 if ( tris_tc.size() ) {
904 sgWriteShort( fp, (short)tris_tc[i][j] );
914 // dump triangle strips
915 if ( strips_v.size() > 0 ) {
919 while ( start < (int)strip_materials.size() ) {
921 material = strip_materials[start];
922 while ( (end < (int)strip_materials.size()) &&
923 (material == strip_materials[end]) )
925 // cout << "end = " << end << endl;
928 // cout << "group = " << start << " to " << end - 1 << endl;
930 // write group headers
931 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
932 sgWriteShort( fp, 2 ); // nproperties
933 sgWriteShort( fp, end - start ); // nelements
935 sgWriteChar( fp, (char)SG_MATERIAL ); // property
936 sgWriteUInt( fp, material.length() ); // nbytes
937 sgWriteBytes( fp, material.length(), material.c_str() );
941 if ( strips_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
942 if ( strips_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
943 if ( strips_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
944 if ( strips_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size;}
945 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
946 sgWriteUInt( fp, 1 ); // nbytes
947 sgWriteChar( fp, idx_mask );
950 for ( i = start; i < end; ++i ) {
952 sgWriteUInt( fp, strips_v[i].size() * idx_size * sizeof(short));
953 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
954 if ( strips_v.size() ) {
955 sgWriteShort( fp, (short)strips_v[i][j] );
957 if ( strips_n.size() ) {
958 sgWriteShort( fp, (short)strips_n[i][j] );
960 if ( strips_c.size() ) {
961 sgWriteShort( fp, (short)strips_c[i][j] );
963 if ( strips_tc.size() ) {
964 sgWriteShort( fp, (short)strips_tc[i][j] );
974 // dump triangle fans
975 if ( fans_v.size() > 0 ) {
979 while ( start < (int)fan_materials.size() ) {
981 material = fan_materials[start];
982 while ( (end < (int)fan_materials.size()) &&
983 (material == fan_materials[end]) )
985 // cout << "end = " << end << endl;
988 // cout << "group = " << start << " to " << end - 1 << endl;
990 // write group headers
991 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
992 sgWriteShort( fp, 2 ); // nproperties
993 sgWriteShort( fp, end - start ); // nelements
995 sgWriteChar( fp, (char)SG_MATERIAL ); // property
996 sgWriteUInt( fp, material.length() ); // nbytes
997 sgWriteBytes( fp, material.length(), material.c_str() );
1001 if ( fans_v.size() ) { idx_mask |= SG_IDX_VERTICES; ++idx_size; }
1002 if ( fans_n.size() ) { idx_mask |= SG_IDX_NORMALS; ++idx_size; }
1003 if ( fans_c.size() ) { idx_mask |= SG_IDX_COLORS; ++idx_size; }
1004 if ( fans_tc.size() ) { idx_mask |= SG_IDX_TEXCOORDS; ++idx_size; }
1005 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
1006 sgWriteUInt( fp, 1 ); // nbytes
1007 sgWriteChar( fp, idx_mask );
1010 for ( i = start; i < end; ++i ) {
1012 sgWriteUInt( fp, fans_v[i].size() * idx_size * sizeof(short) );
1013 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
1014 if ( fans_v.size() ) {
1015 sgWriteShort( fp, (short)fans_v[i][j] );
1017 if ( fans_n.size() ) {
1018 sgWriteShort( fp, (short)fans_n[i][j] );
1020 if ( fans_c.size() ) {
1021 sgWriteShort( fp, (short)fans_c[i][j] );
1023 if ( fans_tc.size() ) {
1024 sgWriteShort( fp, (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,
1055 string dir = base + "/" + b.gen_base_path();
1056 string command = "mkdir -p " + dir;
1057 #if defined(_MSC_VER) || defined(__MINGW32__)
1058 system( (string("mkdir ") + dir).c_str() );
1060 system(command.c_str());
1063 // string file = dir + "/" + b.gen_index_str();
1064 string file = dir + "/" + name;
1065 cout << "Output file = " << file << endl;
1068 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
1069 cout << "ERROR: opening " << file << " for writing!" << endl;
1073 cout << "triangles size = " << tris_v.size() << " tri_materials = "
1074 << tri_materials.size() << endl;
1075 cout << "strips size = " << strips_v.size() << " strip_materials = "
1076 << strip_materials.size() << endl;
1077 cout << "fans size = " << fans_v.size() << " fan_materials = "
1078 << fan_materials.size() << endl;
1080 cout << "points = " << wgs84_nodes.size() << endl;
1081 cout << "tex coords = " << texcoords.size() << endl;
1083 fprintf(fp, "# FGFS Scenery\n");
1084 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
1086 time_t calendar_time = time(NULL);
1087 struct tm *local_tm;
1088 local_tm = localtime( &calendar_time );
1090 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
1091 fprintf(fp, "# Created %s\n", time_str );
1094 // write bounding sphere
1095 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
1096 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
1100 fprintf(fp, "# vertex list\n");
1101 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
1102 p = wgs84_nodes[i] - gbs_center;
1104 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1108 fprintf(fp, "# vertex normal list\n");
1109 for ( i = 0; i < (int)normals.size(); ++i ) {
1111 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
1115 // dump texture coordinates
1116 fprintf(fp, "# texture coordinate list\n");
1117 for ( i = 0; i < (int)texcoords.size(); ++i ) {
1119 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
1123 // dump individual triangles if they exist
1124 if ( tris_v.size() > 0 ) {
1125 fprintf(fp, "# triangle groups\n");
1130 while ( start < (int)tri_materials.size() ) {
1132 material = tri_materials[start];
1133 while ( (end < (int)tri_materials.size()) &&
1134 (material == tri_materials[end]) )
1136 // cout << "end = " << end << endl;
1139 // cout << "group = " << start << " to " << end - 1 << endl;
1141 // make a list of points for the group
1142 point_list group_nodes;
1143 group_nodes.clear();
1145 double bs_radius = 0;
1146 for ( i = start; i < end; ++i ) {
1147 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1148 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
1149 bs_center = calc_center( group_nodes );
1150 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1154 // write group headers
1156 fprintf(fp, "# usemtl %s\n", material.c_str());
1157 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1158 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1161 for ( i = start; i < end; ++i ) {
1163 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
1164 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
1174 // dump triangle groups
1175 if ( strips_v.size() > 0 ) {
1176 fprintf(fp, "# triangle strips\n");
1181 while ( start < (int)strip_materials.size() ) {
1183 material = strip_materials[start];
1184 while ( (end < (int)strip_materials.size()) &&
1185 (material == strip_materials[end]) )
1187 // cout << "end = " << end << endl;
1190 // cout << "group = " << start << " to " << end - 1 << endl;
1192 // make a list of points for the group
1193 point_list group_nodes;
1194 group_nodes.clear();
1196 double bs_radius = 0;
1197 for ( i = start; i < end; ++i ) {
1198 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1199 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
1200 bs_center = calc_center( group_nodes );
1201 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
1205 // write group headers
1207 fprintf(fp, "# usemtl %s\n", material.c_str());
1208 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
1209 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
1212 for ( i = start; i < end; ++i ) {
1214 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
1215 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1228 command = "gzip --force --best " + file;
1229 system(command.c_str());