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.
29 #include <simgear/compiler.h>
38 #include <simgear/bucket/newbucket.hxx>
40 #include "lowlevel.hxx"
41 #include "sg_binobj.hxx"
44 # include <Win32/mkdir.hpp>
51 SG_BOUNDING_SPHERE = 0,
57 SG_TRIANGLE_FACES = 10,
58 SG_TRIANGLE_STRIPS = 11,
67 // calculate the center of a list of points, by taking the halfway
68 // point between the min and max points.
69 static Point3D calc_center( point_list& wgs84_nodes ) {
72 if ( wgs84_nodes.size() ) {
73 min = max = wgs84_nodes[0];
75 min = max = Point3D( 0 );
78 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
81 if ( p.x() < min.x() ) { min.setx( p.x() ); }
82 if ( p.y() < min.y() ) { min.sety( p.y() ); }
83 if ( p.z() < min.z() ) { min.setz( p.z() ); }
85 if ( p.x() > max.x() ) { max.setx( p.x() ); }
86 if ( p.y() > max.y() ) { max.sety( p.y() ); }
87 if ( p.z() > max.z() ) { max.setz( p.z() ); }
90 return ( min + max ) / 2.0;
93 // calculate the bounding sphere. Center is the center of the
94 // tile and zero elevation
95 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
97 double radius_squared = 0;
99 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
100 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
101 if ( dist_squared > radius_squared ) {
102 radius_squared = dist_squared;
106 return sqrt(radius_squared);
110 // read a binary file and populate the provided structures.
111 bool sgReadBinObj( const string& file, SGBinObject* obj ) {
116 Point3D gbs_center = Point3D( 0 );
117 float gbs_radius = 0.0;
118 point_list wgs84_nodes;
120 point_list texcoords;
123 string_list tri_materials;
125 group_list strips_tc;
126 string_list strip_materials;
129 string_list fan_materials;
131 // zero out structures
138 tri_materials.clear();
142 strip_materials.clear();
146 fan_materials.clear();
148 cout << "Loading binary input file = " << file << endl;
151 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
152 string filegz = file + ".gz";
153 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
154 cout << "ERROR: opening " << file << " or " << filegz
155 << "for reading!" << endl;
164 sgReadInt( fp, &header );
165 if ( ((header & 0xFF000000) >> 24) == 'S' &&
166 ((header & 0x00FF0000) >> 16) == 'G' ) {
167 cout << "Good header" << endl;
169 version = (header & 0x0000FFFF);
170 cout << "File version = " << version << endl;
175 // read creation time
176 time_t calendar_time;
177 sgReadLong( fp, &calendar_time );
179 local_tm = localtime( &calendar_time );
181 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
182 cout << "File created on " << time_str << endl;
184 // read number of top level objects
186 sgReadShort( fp, &nobjects );
187 cout << "Total objects to read = " << nobjects << endl;
190 for ( i = 0; i < nobjects; ++i ) {
191 // read object header
193 short nproperties, nelements;
194 sgReadChar( fp, &obj_type );
195 sgReadShort( fp, &nproperties );
196 sgReadShort( fp, &nelements );
198 cout << "object " << i << " = " << (int)obj_type << " props = "
199 << nproperties << " elements = " << nelements << endl;
201 if ( obj_type == SG_BOUNDING_SPHERE ) {
202 // read bounding sphere properties
203 for ( j = 0; j < nproperties; ++j ) {
205 sgReadChar( fp, &prop_type );
208 sgReadInt( fp, &nbytes );
209 cout << "property size = " << nbytes << endl;
212 sgReadBytes( fp, nbytes, ptr );
215 // read bounding sphere elements
216 for ( j = 0; j < nelements; ++j ) {
218 sgReadInt( fp, &nbytes );
219 cout << "element size = " << nbytes << endl;
222 sgReadBytes( fp, nbytes, ptr );
224 double *dptr = (double *)ptr;
225 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
226 cout << "Center = " << gbs_center << endl;
227 ptr += sizeof(double) * 3;
229 float *fptr = (float *)ptr;
230 gbs_radius = fptr[0];
231 cout << "Bounding radius = " << gbs_radius << endl;
233 } else if ( obj_type == SG_VERTEX_LIST ) {
234 // read vertex list properties
235 for ( j = 0; j < nproperties; ++j ) {
237 sgReadChar( fp, &prop_type );
240 sgReadInt( fp, &nbytes );
241 cout << "property size = " << nbytes << endl;
244 sgReadBytes( fp, nbytes, ptr );
247 // read vertex list elements
248 for ( j = 0; j < nelements; ++j ) {
250 sgReadInt( fp, &nbytes );
251 cout << "element size = " << nbytes << endl;
254 sgReadBytes( fp, nbytes, ptr );
255 int count = nbytes / (sizeof(float) * 3);
256 float *fptr = (float *)ptr;
257 for ( k = 0; k < count; ++k ) {
258 p = Point3D( fptr[0], fptr[1], fptr[2] );
259 cout << "node = " << p << endl;
260 wgs84_nodes.push_back( p );
264 } else if ( obj_type == SG_NORMAL_LIST ) {
265 // read normal list properties
266 for ( j = 0; j < nproperties; ++j ) {
268 sgReadChar( fp, &prop_type );
271 sgReadInt( fp, &nbytes );
272 cout << "property size = " << nbytes << endl;
275 sgReadBytes( fp, nbytes, ptr );
278 // read normal list elements
279 for ( j = 0; j < nelements; ++j ) {
281 sgReadInt( fp, &nbytes );
282 cout << "element size = " << nbytes << endl;
284 unsigned char *ptr = (unsigned char *)buf;
285 sgReadBytes( fp, nbytes, ptr );
286 int count = nbytes / 3;
287 for ( k = 0; k < count; ++k ) {
288 p = Point3D( ptr[0] / 128.0 - 1.0,
289 ptr[1] / 128.0 - 1.0,
290 ptr[2] / 128.0 - 1.0 );
291 cout << "normal = " << p << endl;
292 normals.push_back( p );
296 } else if ( obj_type == SG_TEXCOORD_LIST ) {
297 // read texcoord list properties
298 for ( j = 0; j < nproperties; ++j ) {
300 sgReadChar( fp, &prop_type );
303 sgReadInt( fp, &nbytes );
304 cout << "property size = " << nbytes << endl;
307 sgReadBytes( fp, nbytes, ptr );
310 // read texcoord list elements
311 for ( j = 0; j < nelements; ++j ) {
313 sgReadInt( fp, &nbytes );
314 cout << "element size = " << nbytes << endl;
317 sgReadBytes( fp, nbytes, ptr );
318 int count = nbytes / (sizeof(float) * 2);
319 float *fptr = (float *)ptr;
320 for ( k = 0; k < count; ++k ) {
321 p = Point3D( fptr[0], fptr[1], 0 );
322 cout << "texcoord = " << p << endl;
323 texcoords.push_back( p );
327 } else if ( obj_type == SG_TRIANGLE_FACES ) {
328 // read triangle face properties
329 for ( j = 0; j < nproperties; ++j ) {
331 sgReadChar( fp, &prop_type );
334 sgReadInt( fp, &nbytes );
335 cout << "property size = " << nbytes << endl;
338 sgReadBytes( fp, nbytes, ptr );
339 if ( prop_type == SG_MATERIAL ) {
340 strncpy( material, ptr, nbytes );
341 material[nbytes] = '\0';
342 cout << "material type = " << material << endl;
346 // read triangle face elements
347 for ( j = 0; j < nelements; ++j ) {
349 sgReadInt( fp, &nbytes );
350 cout << "element size = " << nbytes << endl;
353 sgReadBytes( fp, nbytes, ptr );
354 int count = nbytes / (sizeof(short) * 2);
355 short *sptr = (short *)ptr;
357 vs.clear(); tcs.clear();
358 for ( k = 0; k < count; ++k ) {
359 vs.push_back( sptr[0] );
360 tcs.push_back( sptr[1] );
361 cout << sptr[0] << "/" << sptr[1] << " ";
365 tris_v.push_back( vs );
366 tris_tc.push_back( tcs );
367 tri_materials.push_back( material );
369 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
370 // read triangle strip properties
371 for ( j = 0; j < nproperties; ++j ) {
373 sgReadChar( fp, &prop_type );
376 sgReadInt( fp, &nbytes );
377 cout << "property size = " << nbytes << endl;
380 sgReadBytes( fp, nbytes, ptr );
381 if ( prop_type == SG_MATERIAL ) {
382 strncpy( material, ptr, nbytes );
383 material[nbytes] = '\0';
384 cout << "material type = " << material << endl;
388 // read triangle strip elements
389 for ( j = 0; j < nelements; ++j ) {
391 sgReadInt( fp, &nbytes );
392 cout << "element size = " << nbytes << endl;
395 sgReadBytes( fp, nbytes, ptr );
396 int count = nbytes / (sizeof(short) * 2);
397 short *sptr = (short *)ptr;
399 vs.clear(); tcs.clear();
400 for ( k = 0; k < count; ++k ) {
401 vs.push_back( sptr[0] );
402 tcs.push_back( sptr[1] );
403 cout << sptr[0] << "/" << sptr[1] << " ";
407 strips_v.push_back( vs );
408 strips_tc.push_back( tcs );
409 strip_materials.push_back( material );
411 } else if ( obj_type == SG_TRIANGLE_FANS ) {
412 // read triangle fan properties
413 for ( j = 0; j < nproperties; ++j ) {
415 sgReadChar( fp, &prop_type );
418 sgReadInt( fp, &nbytes );
419 cout << "property size = " << nbytes << endl;
422 sgReadBytes( fp, nbytes, ptr );
423 if ( prop_type == SG_MATERIAL ) {
424 strncpy( material, ptr, nbytes );
425 material[nbytes] = '\0';
426 cout << "material type = " << material << endl;
430 // read triangle fan elements
431 for ( j = 0; j < nelements; ++j ) {
433 sgReadInt( fp, &nbytes );
434 cout << "element size = " << nbytes << endl;
437 sgReadBytes( fp, nbytes, ptr );
438 int count = nbytes / (sizeof(short) * 2);
439 short *sptr = (short *)ptr;
441 vs.clear(); tcs.clear();
442 for ( k = 0; k < count; ++k ) {
443 vs.push_back( sptr[0] );
444 tcs.push_back( sptr[1] );
445 cout << sptr[0] << "/" << sptr[1] << " ";
449 fans_v.push_back( vs );
450 fans_tc.push_back( tcs );
451 fan_materials.push_back( material );
454 // unknown object type, just skip
457 for ( j = 0; j < nproperties; ++j ) {
459 sgReadChar( fp, &prop_type );
462 sgReadInt( fp, &nbytes );
463 cout << "property size = " << nbytes << endl;
466 sgReadBytes( fp, nbytes, ptr );
470 for ( j = 0; j < nelements; ++j ) {
472 sgReadInt( fp, &nbytes );
473 cout << "element size = " << nbytes << endl;
476 sgReadBytes( fp, nbytes, ptr );
484 obj->set_gbs_center( gbs_center );
485 obj->set_gbs_radius( gbs_radius );
486 obj->set_wgs84_nodes( wgs84_nodes );
487 obj->set_normals( normals );
488 obj->set_texcoords( texcoords );
489 obj->set_tris_v( tris_v );
490 obj->set_tris_tc( tris_tc );
491 obj->set_tri_materials( tri_materials );
492 obj->set_strips_v( strips_v );
493 obj->set_strips_tc( strips_tc );
494 obj->set_strip_materials( strip_materials );
495 obj->set_fans_v( fans_v );
496 obj->set_fans_tc( fans_tc );
497 obj->set_fan_materials( fan_materials );
499 if ( sgReadError() ) {
500 cout << "We detected an error while reading the file." << endl;
508 // write out the structures to a binary file. We assume that the
509 // groups come to us sorted by material property. If not, things
510 // don't break, but the result won't be as optimal.
511 bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
512 const SGBinObject* obj )
519 Point3D gbs_center = obj->get_gbs_center();
520 float gbs_radius = obj->get_gbs_radius();
521 point_list wgs84_nodes = obj->get_wgs84_nodes();
522 point_list normals = obj->get_normals();
523 point_list texcoords = obj->get_texcoords();
524 group_list tris_v = obj->get_tris_v();
525 group_list tris_tc = obj->get_tris_tc();
526 string_list tri_materials = obj->get_tri_materials();
527 group_list strips_v = obj->get_strips_v();
528 group_list strips_tc = obj->get_strips_tc();
529 string_list strip_materials = obj->get_strip_materials();
530 group_list fans_v = obj->get_fans_v();
531 group_list fans_tc = obj->get_fans_tc();
532 string_list fan_materials = obj->get_fan_materials();
534 string dir = base + "/" + b.gen_base_path();
535 string command = "mkdir -p " + dir;
537 fg_mkdir( dir.c_str() );
539 system(command.c_str());
542 string file = dir + "/" + name + ".gz";
543 cout << "Output file = " << file << endl;
546 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
547 cout << "ERROR: opening " << file << " for writing!" << endl;
553 cout << "triangles size = " << tris_v.size() << " tri_materials = "
554 << tri_materials.size() << endl;
555 cout << "strips size = " << strips_v.size() << " strip_materials = "
556 << strip_materials.size() << endl;
557 cout << "fans size = " << fans_v.size() << " fan_materials = "
558 << fan_materials.size() << endl;
560 cout << "points = " << wgs84_nodes.size() << endl;
561 cout << "tex coords = " << texcoords.size() << endl;
563 // write header magic
564 sgWriteInt( fp, SG_FILE_MAGIC_NUMBER );
565 time_t calendar_time = time(NULL);
566 sgWriteLong( fp, (long int)calendar_time );
568 // calculate and write number of top level objects
573 nobjects++; // for gbs
574 nobjects++; // for vertices
575 nobjects++; // for normals
576 nobjects++; // for texcoords
581 while ( start < (int)tri_materials.size() ) {
582 material = tri_materials[start];
583 while ( (end < (int)tri_materials.size()) &&
584 (material == tri_materials[end]) ) {
588 start = end; end = start + 1;
595 while ( start < (int)strip_materials.size() ) {
596 material = strip_materials[start];
597 while ( (end < (int)strip_materials.size()) &&
598 (material == strip_materials[end]) ) {
602 start = end; end = start + 1;
609 while ( start < (int)fan_materials.size() ) {
610 material = fan_materials[start];
611 while ( (end < (int)fan_materials.size()) &&
612 (material == fan_materials[end]) ) {
616 start = end; end = start + 1;
620 cout << "total top level objects = " << nobjects << endl;
621 sgWriteShort( fp, nobjects );
623 // write bounding sphere
624 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
625 sgWriteShort( fp, 0 ); // nproperties
626 sgWriteShort( fp, 1 ); // nelements
628 sgWriteInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
630 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
631 sgWritedVec3( fp, center );
632 sgWriteFloat( fp, gbs_radius );
635 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
636 sgWriteShort( fp, 0 ); // nproperties
637 sgWriteShort( fp, 1 ); // nelements
638 sgWriteInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
639 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
640 p = wgs84_nodes[i] - gbs_center;
641 sgSetVec3( pt, p.x(), p.y(), p.z() );
642 sgWriteVec3( fp, pt );
645 // dump vertex normal list
646 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
647 sgWriteShort( fp, 0 ); // nproperties
648 sgWriteShort( fp, 1 ); // nelements
649 sgWriteInt( fp, normals.size() * 3 ); // nbytes
651 for ( i = 0; i < (int)normals.size(); ++i ) {
653 normal[0] = (char)((p.x() + 1.0) * 128);
654 normal[1] = (char)((p.y() + 1.0) * 128);
655 normal[2] = (char)((p.z() + 1.0) * 128);
656 sgWriteBytes( fp, 3, normal );
659 // dump texture coordinates
660 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
661 sgWriteShort( fp, 0 ); // nproperties
662 sgWriteShort( fp, 1 ); // nelements
663 sgWriteInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
664 for ( i = 0; i < (int)texcoords.size(); ++i ) {
666 sgSetVec2( t, p.x(), p.y() );
667 sgWriteVec2( fp, t );
670 // dump individual triangles if they exist
671 if ( tris_v.size() > 0 ) {
675 while ( start < (int)tri_materials.size() ) {
677 material = tri_materials[start];
678 while ( (end < (int)tri_materials.size()) &&
679 (material == tri_materials[end]) )
681 // cout << "end = " << end << endl;
684 // cout << "group = " << start << " to " << end - 1 << endl;
686 // write group headers
687 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
688 sgWriteShort( fp, 1 ); // nproperties
689 sgWriteShort( fp, 1 ); // nelements
691 sgWriteChar( fp, (char)SG_MATERIAL ); // property
692 sgWriteInt( fp, material.length() ); // nbytes
693 sgWriteBytes( fp, material.length(), material.c_str() );
695 sgWriteInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
698 for ( i = start; i < end; ++i ) {
699 for ( j = 0; j < 3; ++j ) {
700 sgWriteShort( fp, (short)tris_v[i][j] );
701 sgWriteShort( fp, (short)tris_tc[i][j] );
710 // dump triangle strips
711 if ( strips_v.size() > 0 ) {
715 while ( start < (int)strip_materials.size() ) {
717 material = strip_materials[start];
718 while ( (end < (int)strip_materials.size()) &&
719 (material == strip_materials[end]) )
721 // cout << "end = " << end << endl;
724 // cout << "group = " << start << " to " << end - 1 << endl;
726 // write group headers
727 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
728 sgWriteShort( fp, 1 ); // nproperties
729 sgWriteShort( fp, end - start ); // nelements
731 sgWriteChar( fp, (char)SG_MATERIAL ); // property
732 sgWriteInt( fp, material.length() ); // nbytes
733 sgWriteBytes( fp, material.length(), material.c_str() );
736 for ( i = start; i < end; ++i ) {
738 sgWriteInt( fp, strips_v[i].size() * 2 * sizeof(short) );
739 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
740 sgWriteShort( fp, (short)strips_v[i][j] );
741 sgWriteShort( fp, (short)strips_tc[i][j] );
750 // dump triangle fans
751 if ( fans_v.size() > 0 ) {
755 while ( start < (int)fan_materials.size() ) {
757 material = fan_materials[start];
758 while ( (end < (int)fan_materials.size()) &&
759 (material == fan_materials[end]) )
761 // cout << "end = " << end << endl;
764 // cout << "group = " << start << " to " << end - 1 << endl;
766 // write group headers
767 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
768 sgWriteShort( fp, 1 ); // nproperties
769 sgWriteShort( fp, end - start ); // nelements
771 sgWriteChar( fp, (char)SG_MATERIAL ); // property
772 sgWriteInt( fp, material.length() ); // nbytes
773 sgWriteBytes( fp, material.length(), material.c_str() );
776 for ( i = start; i < end; ++i ) {
778 sgWriteInt( fp, fans_v[i].size() * 2 * sizeof(short) );
779 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
780 sgWriteShort( fp, (short)fans_v[i][j] );
781 sgWriteShort( fp, (short)fans_tc[i][j] );
793 if ( sgWriteError() ) {
794 cout << "We detected an error while writing the file." << endl;
802 // write out the structures to an ASCII file. We assume that the
803 // groups come to us sorted by material property. If not, things
804 // don't break, but the result won't be as optimal.
805 bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
811 Point3D gbs_center = obj->get_gbs_center();
812 float gbs_radius = obj->get_gbs_radius();
813 point_list wgs84_nodes = obj->get_wgs84_nodes();
814 point_list normals = obj->get_normals();
815 point_list texcoords = obj->get_texcoords();
816 group_list tris_v = obj->get_tris_v();
817 group_list tris_tc = obj->get_tris_tc();
818 string_list tri_materials = obj->get_tri_materials();
819 group_list strips_v = obj->get_strips_v();
820 group_list strips_tc = obj->get_strips_tc();
821 string_list strip_materials = obj->get_strip_materials();
822 group_list fans_v = obj->get_fans_v();
823 group_list fans_tc = obj->get_fans_tc();
824 string_list fan_materials = obj->get_fan_materials();
826 string dir = base + "/" + b.gen_base_path();
827 string command = "mkdir -p " + dir;
829 fg_mkdir( dir.c_str() );
831 system(command.c_str());
834 // string file = dir + "/" + b.gen_index_str();
835 string file = dir + "/" + name;
836 cout << "Output file = " << file << endl;
839 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
840 cout << "ERROR: opening " << file << " for writing!" << endl;
844 cout << "triangles size = " << tris_v.size() << " tri_materials = "
845 << tri_materials.size() << endl;
846 cout << "strips size = " << strips_v.size() << " strip_materials = "
847 << strip_materials.size() << endl;
848 cout << "fans size = " << fans_v.size() << " fan_materials = "
849 << fan_materials.size() << endl;
851 cout << "points = " << wgs84_nodes.size() << endl;
852 cout << "tex coords = " << texcoords.size() << endl;
854 fprintf(fp, "# FGFS Scenery\n");
855 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
857 time_t calendar_time = time(NULL);
859 local_tm = localtime( &calendar_time );
861 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
862 fprintf(fp, "# Created %s\n", time_str );
865 // write bounding sphere
866 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
867 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
871 fprintf(fp, "# vertex list\n");
872 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
873 p = wgs84_nodes[i] - gbs_center;
875 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
879 fprintf(fp, "# vertex normal list\n");
880 for ( i = 0; i < (int)normals.size(); ++i ) {
882 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
886 // dump texture coordinates
887 fprintf(fp, "# texture coordinate list\n");
888 for ( i = 0; i < (int)texcoords.size(); ++i ) {
890 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
894 // dump individual triangles if they exist
895 if ( tris_v.size() > 0 ) {
896 fprintf(fp, "# triangle groups\n");
901 while ( start < (int)tri_materials.size() ) {
903 material = tri_materials[start];
904 while ( (end < (int)tri_materials.size()) &&
905 (material == tri_materials[end]) )
907 // cout << "end = " << end << endl;
910 // cout << "group = " << start << " to " << end - 1 << endl;
912 // make a list of points for the group
913 point_list group_nodes;
916 double bs_radius = 0;
917 for ( i = start; i < end; ++i ) {
918 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
919 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
920 bs_center = calc_center( group_nodes );
921 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
925 // write group headers
927 fprintf(fp, "# usemtl %s\n", material.c_str());
928 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
929 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
932 for ( i = start; i < end; ++i ) {
934 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
935 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
945 // dump triangle groups
946 if ( strips_v.size() > 0 ) {
947 fprintf(fp, "# triangle strips\n");
952 while ( start < (int)strip_materials.size() ) {
954 material = strip_materials[start];
955 while ( (end < (int)strip_materials.size()) &&
956 (material == strip_materials[end]) )
958 // cout << "end = " << end << endl;
961 // cout << "group = " << start << " to " << end - 1 << endl;
963 // make a list of points for the group
964 point_list group_nodes;
967 double bs_radius = 0;
968 for ( i = start; i < end; ++i ) {
969 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
970 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
971 bs_center = calc_center( group_nodes );
972 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
976 // write group headers
978 fprintf(fp, "# usemtl %s\n", material.c_str());
979 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
980 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
983 for ( i = start; i < end; ++i ) {
985 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
986 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
999 command = "gzip --force --best " + file;
1000 system(command.c_str());