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>
37 #include <simgear/bucket/newbucket.hxx>
39 #include "lowlevel.hxx"
40 #include "sg_binobj.hxx"
43 FG_USING_STD( string );
44 FG_USING_STD( vector );
50 SG_BOUNDING_SPHERE = 0,
56 SG_TRIANGLE_FACES = 10,
57 SG_TRIANGLE_STRIPS = 11,
66 class sgSimpleBuffer {
75 inline sgSimpleBuffer( unsigned int s )
81 cout << "Creating a new buffer of size = " << size << endl;
85 inline ~sgSimpleBuffer() {
89 inline unsigned int get_size() const { return size; }
90 inline char *get_ptr() const { return ptr; }
91 inline void resize( unsigned int s ) {
99 cout << "resizing buffer to size = " << size << endl;
100 ptr = new char[size];
106 // calculate the center of a list of points, by taking the halfway
107 // point between the min and max points.
108 static Point3D calc_center( point_list& wgs84_nodes ) {
111 if ( wgs84_nodes.size() ) {
112 min = max = wgs84_nodes[0];
114 min = max = Point3D( 0 );
117 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
120 if ( p.x() < min.x() ) { min.setx( p.x() ); }
121 if ( p.y() < min.y() ) { min.sety( p.y() ); }
122 if ( p.z() < min.z() ) { min.setz( p.z() ); }
124 if ( p.x() > max.x() ) { max.setx( p.x() ); }
125 if ( p.y() > max.y() ) { max.sety( p.y() ); }
126 if ( p.z() > max.z() ) { max.setz( p.z() ); }
129 return ( min + max ) / 2.0;
132 // calculate the bounding sphere. Center is the center of the
133 // tile and zero elevation
134 double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
136 double radius_squared = 0;
138 for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
139 dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
140 if ( dist_squared > radius_squared ) {
141 radius_squared = dist_squared;
145 return sqrt(radius_squared);
149 // read a binary file and populate the provided structures.
150 bool SGBinObject::read_bin( const string& file ) {
155 static sgSimpleBuffer buf( 32768 ); // 32 Kb
157 // zero out structures
158 gbs_center = Point3D( 0 );
167 tri_materials.clear();
171 strip_materials.clear();
175 fan_materials.clear();
178 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
179 string filegz = file + ".gz";
180 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
181 // cout << "ERROR: opening " << file << " or " << filegz
182 // << "for reading!" << endl;
191 unsigned short version;
192 sgReadUInt( fp, &header );
193 if ( ((header & 0xFF000000) >> 24) == 'S' &&
194 ((header & 0x00FF0000) >> 16) == 'G' ) {
195 // cout << "Good header" << endl;
197 version = (header & 0x0000FFFF);
198 // cout << "File version = " << version << endl;
203 // read creation time
204 time_t calendar_time;
205 sgReadLong( fp, &calendar_time );
207 local_tm = localtime( &calendar_time );
209 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
210 // cout << "File created on " << time_str << endl;
212 // read number of top level objects
214 sgReadShort( fp, &nobjects );
215 // cout << "Total objects to read = " << nobjects << endl;
218 for ( i = 0; i < nobjects; ++i ) {
219 // read object header
221 short nproperties, nelements;
222 sgReadChar( fp, &obj_type );
223 sgReadShort( fp, &nproperties );
224 sgReadShort( fp, &nelements );
226 // cout << "object " << i << " = " << (int)obj_type << " props = "
227 // << nproperties << " elements = " << nelements << endl;
229 if ( obj_type == SG_BOUNDING_SPHERE ) {
230 // read bounding sphere properties
231 for ( j = 0; j < nproperties; ++j ) {
233 sgReadChar( fp, &prop_type );
235 sgReadUInt( fp, &nbytes );
236 // cout << "property size = " << nbytes << endl;
237 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
238 char *ptr = buf.get_ptr();
239 sgReadBytes( fp, nbytes, ptr );
242 // read bounding sphere elements
243 for ( j = 0; j < nelements; ++j ) {
244 sgReadUInt( fp, &nbytes );
245 // cout << "element size = " << nbytes << endl;
246 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
247 char *ptr = buf.get_ptr();
248 sgReadBytes( fp, nbytes, ptr );
250 double *dptr = (double *)ptr;
251 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
252 // cout << "Center = " << gbs_center << endl;
253 ptr += sizeof(double) * 3;
255 float *fptr = (float *)ptr;
256 gbs_radius = fptr[0];
257 // cout << "Bounding radius = " << gbs_radius << endl;
259 } else if ( obj_type == SG_VERTEX_LIST ) {
260 // read vertex list properties
261 for ( j = 0; j < nproperties; ++j ) {
263 sgReadChar( fp, &prop_type );
265 sgReadUInt( fp, &nbytes );
266 // cout << "property size = " << nbytes << endl;
267 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
268 char *ptr = buf.get_ptr();
269 sgReadBytes( fp, nbytes, ptr );
272 // read vertex list elements
273 for ( j = 0; j < nelements; ++j ) {
274 sgReadUInt( fp, &nbytes );
275 // cout << "element size = " << nbytes << endl;
276 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
277 char *ptr = buf.get_ptr();
278 sgReadBytes( fp, nbytes, ptr );
279 int count = nbytes / (sizeof(float) * 3);
280 float *fptr = (float *)ptr;
281 for ( k = 0; k < count; ++k ) {
282 p = Point3D( fptr[0], fptr[1], fptr[2] );
283 // cout << "node = " << p << endl;
284 wgs84_nodes.push_back( p );
288 } else if ( obj_type == SG_NORMAL_LIST ) {
289 // read normal list properties
290 for ( j = 0; j < nproperties; ++j ) {
292 sgReadChar( fp, &prop_type );
294 sgReadUInt( fp, &nbytes );
295 // cout << "property size = " << nbytes << endl;
296 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
297 char *ptr = buf.get_ptr();
298 sgReadBytes( fp, nbytes, ptr );
301 // read normal list elements
302 for ( j = 0; j < nelements; ++j ) {
303 sgReadUInt( fp, &nbytes );
304 // cout << "element size = " << nbytes << endl;
305 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
306 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
307 sgReadBytes( fp, nbytes, ptr );
308 int count = nbytes / 3;
309 for ( k = 0; k < count; ++k ) {
310 p = Point3D( ptr[0] / 128.0 - 1.0,
311 ptr[1] / 128.0 - 1.0,
312 ptr[2] / 128.0 - 1.0 );
313 // cout << "normal = " << p << endl;
314 normals.push_back( p );
318 } else if ( obj_type == SG_TEXCOORD_LIST ) {
319 // read texcoord list properties
320 for ( j = 0; j < nproperties; ++j ) {
322 sgReadChar( fp, &prop_type );
324 sgReadUInt( fp, &nbytes );
325 // cout << "property size = " << nbytes << endl;
326 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
327 char *ptr = buf.get_ptr();
328 sgReadBytes( fp, nbytes, ptr );
331 // read texcoord list elements
332 for ( j = 0; j < nelements; ++j ) {
333 sgReadUInt( fp, &nbytes );
334 // cout << "element size = " << nbytes << endl;
335 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
336 char *ptr = buf.get_ptr();
337 sgReadBytes( fp, nbytes, ptr );
338 int count = nbytes / (sizeof(float) * 2);
339 float *fptr = (float *)ptr;
340 for ( k = 0; k < count; ++k ) {
341 p = Point3D( fptr[0], fptr[1], 0 );
342 // cout << "texcoord = " << p << endl;
343 texcoords.push_back( p );
347 } else if ( obj_type == SG_TRIANGLE_FACES ) {
348 // read triangle face properties
349 for ( j = 0; j < nproperties; ++j ) {
351 sgReadChar( fp, &prop_type );
353 sgReadUInt( fp, &nbytes );
354 // cout << "property size = " << nbytes << endl;
355 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
356 char *ptr = buf.get_ptr();
357 sgReadBytes( fp, nbytes, ptr );
358 if ( prop_type == SG_MATERIAL ) {
359 strncpy( material, ptr, nbytes );
360 material[nbytes] = '\0';
361 // cout << "material type = " << material << endl;
365 // read triangle face elements
366 for ( j = 0; j < nelements; ++j ) {
367 sgReadUInt( fp, &nbytes );
368 // cout << "element size = " << nbytes << endl;
369 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
370 char *ptr = buf.get_ptr();
371 sgReadBytes( fp, nbytes, ptr );
372 int count = nbytes / (sizeof(short) * 2);
373 short *sptr = (short *)ptr;
375 vs.clear(); tcs.clear();
376 for ( k = 0; k < count; ++k ) {
377 vs.push_back( sptr[0] );
378 tcs.push_back( sptr[1] );
379 // cout << sptr[0] << "/" << sptr[1] << " ";
383 tris_v.push_back( vs );
384 tris_tc.push_back( tcs );
385 tri_materials.push_back( material );
387 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
388 // read triangle strip properties
389 for ( j = 0; j < nproperties; ++j ) {
391 sgReadChar( fp, &prop_type );
393 sgReadUInt( fp, &nbytes );
394 // cout << "property size = " << nbytes << endl;
395 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
396 char *ptr = buf.get_ptr();
397 sgReadBytes( fp, nbytes, ptr );
398 if ( prop_type == SG_MATERIAL ) {
399 strncpy( material, ptr, nbytes );
400 material[nbytes] = '\0';
401 // cout << "material type = " << material << endl;
405 // read triangle strip elements
406 for ( j = 0; j < nelements; ++j ) {
407 sgReadUInt( fp, &nbytes );
408 // cout << "element size = " << nbytes << endl;
409 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
410 char *ptr = buf.get_ptr();
411 sgReadBytes( fp, nbytes, ptr );
412 int count = nbytes / (sizeof(short) * 2);
413 short *sptr = (short *)ptr;
415 vs.clear(); tcs.clear();
416 for ( k = 0; k < count; ++k ) {
417 vs.push_back( sptr[0] );
418 tcs.push_back( sptr[1] );
419 // cout << sptr[0] << "/" << sptr[1] << " ";
423 strips_v.push_back( vs );
424 strips_tc.push_back( tcs );
425 strip_materials.push_back( material );
427 } else if ( obj_type == SG_TRIANGLE_FANS ) {
428 // read triangle fan properties
429 for ( j = 0; j < nproperties; ++j ) {
431 sgReadChar( fp, &prop_type );
433 sgReadUInt( fp, &nbytes );
434 // cout << "property size = " << nbytes << endl;
435 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
436 char *ptr = buf.get_ptr();
437 sgReadBytes( fp, nbytes, ptr );
438 if ( prop_type == SG_MATERIAL ) {
439 strncpy( material, ptr, nbytes );
440 material[nbytes] = '\0';
441 // cout << "material type = " << material << endl;
445 // read triangle fan elements
446 for ( j = 0; j < nelements; ++j ) {
447 sgReadUInt( fp, &nbytes );
448 // cout << "element size = " << nbytes << endl;
449 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
450 char *ptr = buf.get_ptr();
451 sgReadBytes( fp, nbytes, ptr );
452 int count = nbytes / (sizeof(short) * 2);
453 short *sptr = (short *)ptr;
455 vs.clear(); tcs.clear();
456 for ( k = 0; k < count; ++k ) {
457 vs.push_back( sptr[0] );
458 tcs.push_back( sptr[1] );
459 // cout << sptr[0] << "/" << sptr[1] << " ";
463 fans_v.push_back( vs );
464 fans_tc.push_back( tcs );
465 fan_materials.push_back( material );
468 // unknown object type, just skip
471 for ( j = 0; j < nproperties; ++j ) {
473 sgReadChar( fp, &prop_type );
475 sgReadUInt( fp, &nbytes );
476 // cout << "property size = " << nbytes << endl;
477 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
478 char *ptr = buf.get_ptr();
479 sgReadBytes( fp, nbytes, ptr );
483 for ( j = 0; j < nelements; ++j ) {
484 sgReadUInt( fp, &nbytes );
485 // cout << "element size = " << nbytes << endl;
486 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
487 char *ptr = buf.get_ptr();
488 sgReadBytes( fp, nbytes, ptr );
496 if ( sgReadError() ) {
497 cout << "We detected an error while reading the file." << endl;
505 // write out the structures to a binary file. We assume that the
506 // groups come to us sorted by material property. If not, things
507 // don't break, but the result won't be as optimal.
508 bool SGBinObject::write_bin( const string& base, const string& name,
516 string dir = base + "/" + b.gen_base_path();
517 string command = "mkdir -p " + dir;
519 system( (string("mkdir ") + dir).c_str() );
521 system(command.c_str());
524 string file = dir + "/" + name + ".gz";
525 cout << "Output file = " << file << endl;
528 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
529 cout << "ERROR: opening " << file << " for writing!" << endl;
535 cout << "triangles size = " << tris_v.size() << " tri_materials = "
536 << tri_materials.size() << endl;
537 cout << "strips size = " << strips_v.size() << " strip_materials = "
538 << strip_materials.size() << endl;
539 cout << "fans size = " << fans_v.size() << " fan_materials = "
540 << fan_materials.size() << endl;
542 cout << "points = " << wgs84_nodes.size() << endl;
543 cout << "tex coords = " << texcoords.size() << endl;
545 // write header magic
546 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
547 time_t calendar_time = time(NULL);
548 sgWriteLong( fp, (long int)calendar_time );
550 // calculate and write number of top level objects
555 nobjects++; // for gbs
556 nobjects++; // for vertices
557 nobjects++; // for normals
558 nobjects++; // for texcoords
563 while ( start < (int)tri_materials.size() ) {
564 material = tri_materials[start];
565 while ( (end < (int)tri_materials.size()) &&
566 (material == tri_materials[end]) ) {
570 start = end; end = start + 1;
577 while ( start < (int)strip_materials.size() ) {
578 material = strip_materials[start];
579 while ( (end < (int)strip_materials.size()) &&
580 (material == strip_materials[end]) ) {
584 start = end; end = start + 1;
591 while ( start < (int)fan_materials.size() ) {
592 material = fan_materials[start];
593 while ( (end < (int)fan_materials.size()) &&
594 (material == fan_materials[end]) ) {
598 start = end; end = start + 1;
602 cout << "total top level objects = " << nobjects << endl;
603 sgWriteShort( fp, nobjects );
605 // write bounding sphere
606 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
607 sgWriteShort( fp, 0 ); // nproperties
608 sgWriteShort( fp, 1 ); // nelements
610 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
612 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
613 sgWritedVec3( fp, center );
614 sgWriteFloat( fp, gbs_radius );
617 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
618 sgWriteShort( fp, 0 ); // nproperties
619 sgWriteShort( fp, 1 ); // nelements
620 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
621 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
622 p = wgs84_nodes[i] - gbs_center;
623 sgSetVec3( pt, p.x(), p.y(), p.z() );
624 sgWriteVec3( fp, pt );
627 // dump vertex normal list
628 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
629 sgWriteShort( fp, 0 ); // nproperties
630 sgWriteShort( fp, 1 ); // nelements
631 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
633 for ( i = 0; i < (int)normals.size(); ++i ) {
635 normal[0] = (unsigned char)((p.x() + 1.0) * 128);
636 normal[1] = (unsigned char)((p.y() + 1.0) * 128);
637 normal[2] = (unsigned char)((p.z() + 1.0) * 128);
638 sgWriteBytes( fp, 3, normal );
641 // dump texture coordinates
642 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
643 sgWriteShort( fp, 0 ); // nproperties
644 sgWriteShort( fp, 1 ); // nelements
645 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
646 for ( i = 0; i < (int)texcoords.size(); ++i ) {
648 sgSetVec2( t, p.x(), p.y() );
649 sgWriteVec2( fp, t );
652 // dump individual triangles if they exist
653 if ( tris_v.size() > 0 ) {
657 while ( start < (int)tri_materials.size() ) {
659 material = tri_materials[start];
660 while ( (end < (int)tri_materials.size()) &&
661 (material == tri_materials[end]) )
663 // cout << "end = " << end << endl;
666 // cout << "group = " << start << " to " << end - 1 << endl;
668 // write group headers
669 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
670 sgWriteShort( fp, 1 ); // nproperties
671 sgWriteShort( fp, 1 ); // nelements
673 sgWriteChar( fp, (char)SG_MATERIAL ); // property
674 sgWriteUInt( fp, material.length() ); // nbytes
675 sgWriteBytes( fp, material.length(), material.c_str() );
677 sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
680 for ( i = start; i < end; ++i ) {
681 for ( j = 0; j < 3; ++j ) {
682 sgWriteShort( fp, (short)tris_v[i][j] );
683 sgWriteShort( fp, (short)tris_tc[i][j] );
692 // dump triangle strips
693 if ( strips_v.size() > 0 ) {
697 while ( start < (int)strip_materials.size() ) {
699 material = strip_materials[start];
700 while ( (end < (int)strip_materials.size()) &&
701 (material == strip_materials[end]) )
703 // cout << "end = " << end << endl;
706 // cout << "group = " << start << " to " << end - 1 << endl;
708 // write group headers
709 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
710 sgWriteShort( fp, 1 ); // nproperties
711 sgWriteShort( fp, end - start ); // nelements
713 sgWriteChar( fp, (char)SG_MATERIAL ); // property
714 sgWriteUInt( fp, material.length() ); // nbytes
715 sgWriteBytes( fp, material.length(), material.c_str() );
718 for ( i = start; i < end; ++i ) {
720 sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
721 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
722 sgWriteShort( fp, (short)strips_v[i][j] );
723 sgWriteShort( fp, (short)strips_tc[i][j] );
732 // dump triangle fans
733 if ( fans_v.size() > 0 ) {
737 while ( start < (int)fan_materials.size() ) {
739 material = fan_materials[start];
740 while ( (end < (int)fan_materials.size()) &&
741 (material == fan_materials[end]) )
743 // cout << "end = " << end << endl;
746 // cout << "group = " << start << " to " << end - 1 << endl;
748 // write group headers
749 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
750 sgWriteShort( fp, 1 ); // nproperties
751 sgWriteShort( fp, end - start ); // nelements
753 sgWriteChar( fp, (char)SG_MATERIAL ); // property
754 sgWriteUInt( fp, material.length() ); // nbytes
755 sgWriteBytes( fp, material.length(), material.c_str() );
758 for ( i = start; i < end; ++i ) {
760 sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
761 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
762 sgWriteShort( fp, (short)fans_v[i][j] );
763 sgWriteShort( fp, (short)fans_tc[i][j] );
775 if ( sgWriteError() ) {
776 cout << "We detected an error while writing the file." << endl;
784 // write out the structures to an ASCII file. We assume that the
785 // groups come to us sorted by material property. If not, things
786 // don't break, but the result won't be as optimal.
787 bool SGBinObject::write_ascii( const string& base, const string& name,
793 string dir = base + "/" + b.gen_base_path();
794 string command = "mkdir -p " + dir;
796 system( (string("mkdir ") + dir).c_str() );
798 system(command.c_str());
801 // string file = dir + "/" + b.gen_index_str();
802 string file = dir + "/" + name;
803 cout << "Output file = " << file << endl;
806 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
807 cout << "ERROR: opening " << file << " for writing!" << endl;
811 cout << "triangles size = " << tris_v.size() << " tri_materials = "
812 << tri_materials.size() << endl;
813 cout << "strips size = " << strips_v.size() << " strip_materials = "
814 << strip_materials.size() << endl;
815 cout << "fans size = " << fans_v.size() << " fan_materials = "
816 << fan_materials.size() << endl;
818 cout << "points = " << wgs84_nodes.size() << endl;
819 cout << "tex coords = " << texcoords.size() << endl;
821 fprintf(fp, "# FGFS Scenery\n");
822 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
824 time_t calendar_time = time(NULL);
826 local_tm = localtime( &calendar_time );
828 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
829 fprintf(fp, "# Created %s\n", time_str );
832 // write bounding sphere
833 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
834 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
838 fprintf(fp, "# vertex list\n");
839 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
840 p = wgs84_nodes[i] - gbs_center;
842 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
846 fprintf(fp, "# vertex normal list\n");
847 for ( i = 0; i < (int)normals.size(); ++i ) {
849 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
853 // dump texture coordinates
854 fprintf(fp, "# texture coordinate list\n");
855 for ( i = 0; i < (int)texcoords.size(); ++i ) {
857 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
861 // dump individual triangles if they exist
862 if ( tris_v.size() > 0 ) {
863 fprintf(fp, "# triangle groups\n");
868 while ( start < (int)tri_materials.size() ) {
870 material = tri_materials[start];
871 while ( (end < (int)tri_materials.size()) &&
872 (material == tri_materials[end]) )
874 // cout << "end = " << end << endl;
877 // cout << "group = " << start << " to " << end - 1 << endl;
879 // make a list of points for the group
880 point_list group_nodes;
883 double bs_radius = 0;
884 for ( i = start; i < end; ++i ) {
885 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
886 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
887 bs_center = calc_center( group_nodes );
888 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
892 // write group headers
894 fprintf(fp, "# usemtl %s\n", material.c_str());
895 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
896 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
899 for ( i = start; i < end; ++i ) {
901 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
902 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
912 // dump triangle groups
913 if ( strips_v.size() > 0 ) {
914 fprintf(fp, "# triangle strips\n");
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 // make a list of points for the group
931 point_list group_nodes;
934 double bs_radius = 0;
935 for ( i = start; i < end; ++i ) {
936 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
937 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
938 bs_center = calc_center( group_nodes );
939 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
943 // write group headers
945 fprintf(fp, "# usemtl %s\n", material.c_str());
946 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
947 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
950 for ( i = start; i < end; ++i ) {
952 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
953 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
966 command = "gzip --force --best " + file;
967 system(command.c_str());