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;
192 unsigned short version;
193 sgReadUInt( fp, &header );
194 if ( ((header & 0xFF000000) >> 24) == 'S' &&
195 ((header & 0x00FF0000) >> 16) == 'G' ) {
196 // cout << "Good header" << endl;
198 version = (header & 0x0000FFFF);
199 // cout << "File version = " << version << endl;
201 // close the file before we return
207 // read creation time
208 time_t calendar_time;
209 sgReadLong( fp, &calendar_time );
211 local_tm = localtime( &calendar_time );
213 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
214 // cout << "File created on " << time_str << endl;
216 // read number of top level objects
218 sgReadShort( fp, &nobjects );
219 // cout << "Total objects to read = " << nobjects << endl;
222 for ( i = 0; i < nobjects; ++i ) {
223 // read object header
225 short nproperties, nelements;
226 sgReadChar( fp, &obj_type );
227 sgReadShort( fp, &nproperties );
228 sgReadShort( fp, &nelements );
230 // cout << "object " << i << " = " << (int)obj_type << " props = "
231 // << nproperties << " elements = " << nelements << endl;
233 if ( obj_type == SG_BOUNDING_SPHERE ) {
234 // read bounding sphere properties
235 for ( j = 0; j < nproperties; ++j ) {
237 sgReadChar( fp, &prop_type );
239 sgReadUInt( fp, &nbytes );
240 // cout << "property size = " << nbytes << endl;
241 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
242 char *ptr = buf.get_ptr();
243 sgReadBytes( fp, nbytes, ptr );
246 // read bounding sphere elements
247 for ( j = 0; j < nelements; ++j ) {
248 sgReadUInt( fp, &nbytes );
249 // cout << "element size = " << nbytes << endl;
250 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
251 char *ptr = buf.get_ptr();
252 sgReadBytes( fp, nbytes, ptr );
254 double *dptr = (double *)ptr;
255 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
256 // cout << "Center = " << gbs_center << endl;
257 ptr += sizeof(double) * 3;
259 float *fptr = (float *)ptr;
260 gbs_radius = fptr[0];
261 // cout << "Bounding radius = " << gbs_radius << endl;
263 } else if ( obj_type == SG_VERTEX_LIST ) {
264 // read vertex list properties
265 for ( j = 0; j < nproperties; ++j ) {
267 sgReadChar( fp, &prop_type );
269 sgReadUInt( fp, &nbytes );
270 // cout << "property size = " << nbytes << endl;
271 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
272 char *ptr = buf.get_ptr();
273 sgReadBytes( fp, nbytes, ptr );
276 // read vertex list elements
277 for ( j = 0; j < nelements; ++j ) {
278 sgReadUInt( fp, &nbytes );
279 // cout << "element size = " << nbytes << endl;
280 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
281 char *ptr = buf.get_ptr();
282 sgReadBytes( fp, nbytes, ptr );
283 int count = nbytes / (sizeof(float) * 3);
284 float *fptr = (float *)ptr;
285 for ( k = 0; k < count; ++k ) {
286 p = Point3D( fptr[0], fptr[1], fptr[2] );
287 // cout << "node = " << p << endl;
288 wgs84_nodes.push_back( p );
292 } else if ( obj_type == SG_NORMAL_LIST ) {
293 // read normal list properties
294 for ( j = 0; j < nproperties; ++j ) {
296 sgReadChar( fp, &prop_type );
298 sgReadUInt( fp, &nbytes );
299 // cout << "property size = " << nbytes << endl;
300 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
301 char *ptr = buf.get_ptr();
302 sgReadBytes( fp, nbytes, ptr );
305 // read normal list elements
306 for ( j = 0; j < nelements; ++j ) {
307 sgReadUInt( fp, &nbytes );
308 // cout << "element size = " << nbytes << endl;
309 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
310 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
311 sgReadBytes( fp, nbytes, ptr );
312 int count = nbytes / 3;
313 for ( k = 0; k < count; ++k ) {
314 p = Point3D( ptr[0] / 128.0 - 1.0,
315 ptr[1] / 128.0 - 1.0,
316 ptr[2] / 128.0 - 1.0 );
317 // cout << "normal = " << p << endl;
318 normals.push_back( p );
322 } else if ( obj_type == SG_TEXCOORD_LIST ) {
323 // read texcoord list properties
324 for ( j = 0; j < nproperties; ++j ) {
326 sgReadChar( fp, &prop_type );
328 sgReadUInt( fp, &nbytes );
329 // cout << "property size = " << nbytes << endl;
330 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
331 char *ptr = buf.get_ptr();
332 sgReadBytes( fp, nbytes, ptr );
335 // read texcoord list elements
336 for ( j = 0; j < nelements; ++j ) {
337 sgReadUInt( fp, &nbytes );
338 // cout << "element size = " << nbytes << endl;
339 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
340 char *ptr = buf.get_ptr();
341 sgReadBytes( fp, nbytes, ptr );
342 int count = nbytes / (sizeof(float) * 2);
343 float *fptr = (float *)ptr;
344 for ( k = 0; k < count; ++k ) {
345 p = Point3D( fptr[0], fptr[1], 0 );
346 // cout << "texcoord = " << p << endl;
347 texcoords.push_back( p );
351 } else if ( obj_type == SG_TRIANGLE_FACES ) {
352 // read triangle face properties
353 for ( j = 0; j < nproperties; ++j ) {
355 sgReadChar( fp, &prop_type );
357 sgReadUInt( fp, &nbytes );
358 // cout << "property size = " << nbytes << endl;
359 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
360 char *ptr = buf.get_ptr();
361 sgReadBytes( fp, nbytes, ptr );
362 if ( prop_type == SG_MATERIAL ) {
363 strncpy( material, ptr, nbytes );
364 material[nbytes] = '\0';
365 // cout << "material type = " << material << endl;
369 // read triangle face elements
370 for ( j = 0; j < nelements; ++j ) {
371 sgReadUInt( fp, &nbytes );
372 // cout << "element size = " << nbytes << endl;
373 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
374 char *ptr = buf.get_ptr();
375 sgReadBytes( fp, nbytes, ptr );
376 int count = nbytes / (sizeof(short) * 2);
377 short *sptr = (short *)ptr;
379 vs.clear(); tcs.clear();
380 for ( k = 0; k < count; ++k ) {
381 vs.push_back( sptr[0] );
382 tcs.push_back( sptr[1] );
383 // cout << sptr[0] << "/" << sptr[1] << " ";
387 tris_v.push_back( vs );
388 tris_tc.push_back( tcs );
389 tri_materials.push_back( material );
391 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
392 // read triangle strip properties
393 for ( j = 0; j < nproperties; ++j ) {
395 sgReadChar( fp, &prop_type );
397 sgReadUInt( fp, &nbytes );
398 // cout << "property size = " << nbytes << endl;
399 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
400 char *ptr = buf.get_ptr();
401 sgReadBytes( fp, nbytes, ptr );
402 if ( prop_type == SG_MATERIAL ) {
403 strncpy( material, ptr, nbytes );
404 material[nbytes] = '\0';
405 // cout << "material type = " << material << endl;
409 // read triangle strip elements
410 for ( j = 0; j < nelements; ++j ) {
411 sgReadUInt( fp, &nbytes );
412 // cout << "element size = " << nbytes << endl;
413 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
414 char *ptr = buf.get_ptr();
415 sgReadBytes( fp, nbytes, ptr );
416 int count = nbytes / (sizeof(short) * 2);
417 short *sptr = (short *)ptr;
419 vs.clear(); tcs.clear();
420 for ( k = 0; k < count; ++k ) {
421 vs.push_back( sptr[0] );
422 tcs.push_back( sptr[1] );
423 // cout << sptr[0] << "/" << sptr[1] << " ";
427 strips_v.push_back( vs );
428 strips_tc.push_back( tcs );
429 strip_materials.push_back( material );
431 } else if ( obj_type == SG_TRIANGLE_FANS ) {
432 // read triangle fan properties
433 for ( j = 0; j < nproperties; ++j ) {
435 sgReadChar( fp, &prop_type );
437 sgReadUInt( fp, &nbytes );
438 // cout << "property size = " << nbytes << endl;
439 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
440 char *ptr = buf.get_ptr();
441 sgReadBytes( fp, nbytes, ptr );
442 if ( prop_type == SG_MATERIAL ) {
443 strncpy( material, ptr, nbytes );
444 material[nbytes] = '\0';
445 // cout << "material type = " << material << endl;
449 // read triangle fan elements
450 for ( j = 0; j < nelements; ++j ) {
451 sgReadUInt( fp, &nbytes );
452 // cout << "element size = " << nbytes << endl;
453 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
454 char *ptr = buf.get_ptr();
455 sgReadBytes( fp, nbytes, ptr );
456 int count = nbytes / (sizeof(short) * 2);
457 short *sptr = (short *)ptr;
459 vs.clear(); tcs.clear();
460 for ( k = 0; k < count; ++k ) {
461 vs.push_back( sptr[0] );
462 tcs.push_back( sptr[1] );
463 // cout << sptr[0] << "/" << sptr[1] << " ";
467 fans_v.push_back( vs );
468 fans_tc.push_back( tcs );
469 fan_materials.push_back( material );
472 // unknown object type, just skip
475 for ( j = 0; j < nproperties; ++j ) {
477 sgReadChar( fp, &prop_type );
479 sgReadUInt( fp, &nbytes );
480 // cout << "property size = " << nbytes << endl;
481 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
482 char *ptr = buf.get_ptr();
483 sgReadBytes( fp, nbytes, ptr );
487 for ( j = 0; j < nelements; ++j ) {
488 sgReadUInt( fp, &nbytes );
489 // cout << "element size = " << nbytes << endl;
490 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
491 char *ptr = buf.get_ptr();
492 sgReadBytes( fp, nbytes, ptr );
500 if ( sgReadError() ) {
501 cout << "We detected an error while reading the file." << endl;
509 // write out the structures to a binary file. We assume that the
510 // groups come to us sorted by material property. If not, things
511 // don't break, but the result won't be as optimal.
512 bool SGBinObject::write_bin( const string& base, const string& name,
520 string dir = base + "/" + b.gen_base_path();
521 string command = "mkdir -p " + dir;
523 system( (string("mkdir ") + dir).c_str() );
525 system(command.c_str());
528 string file = dir + "/" + name + ".gz";
529 cout << "Output file = " << file << endl;
532 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
533 cout << "ERROR: opening " << file << " for writing!" << endl;
539 cout << "triangles size = " << tris_v.size() << " tri_materials = "
540 << tri_materials.size() << endl;
541 cout << "strips size = " << strips_v.size() << " strip_materials = "
542 << strip_materials.size() << endl;
543 cout << "fans size = " << fans_v.size() << " fan_materials = "
544 << fan_materials.size() << endl;
546 cout << "points = " << wgs84_nodes.size() << endl;
547 cout << "tex coords = " << texcoords.size() << endl;
549 // write header magic
550 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
551 time_t calendar_time = time(NULL);
552 sgWriteLong( fp, (long int)calendar_time );
554 // calculate and write number of top level objects
559 nobjects++; // for gbs
560 nobjects++; // for vertices
561 nobjects++; // for normals
562 nobjects++; // for texcoords
567 while ( start < (int)tri_materials.size() ) {
568 material = tri_materials[start];
569 while ( (end < (int)tri_materials.size()) &&
570 (material == tri_materials[end]) ) {
574 start = end; end = start + 1;
581 while ( start < (int)strip_materials.size() ) {
582 material = strip_materials[start];
583 while ( (end < (int)strip_materials.size()) &&
584 (material == strip_materials[end]) ) {
588 start = end; end = start + 1;
595 while ( start < (int)fan_materials.size() ) {
596 material = fan_materials[start];
597 while ( (end < (int)fan_materials.size()) &&
598 (material == fan_materials[end]) ) {
602 start = end; end = start + 1;
606 cout << "total top level objects = " << nobjects << endl;
607 sgWriteShort( fp, nobjects );
609 // write bounding sphere
610 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
611 sgWriteShort( fp, 0 ); // nproperties
612 sgWriteShort( fp, 1 ); // nelements
614 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
616 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
617 sgWritedVec3( fp, center );
618 sgWriteFloat( fp, gbs_radius );
621 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
622 sgWriteShort( fp, 0 ); // nproperties
623 sgWriteShort( fp, 1 ); // nelements
624 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
625 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
626 p = wgs84_nodes[i] - gbs_center;
627 sgSetVec3( pt, p.x(), p.y(), p.z() );
628 sgWriteVec3( fp, pt );
631 // dump vertex normal list
632 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
633 sgWriteShort( fp, 0 ); // nproperties
634 sgWriteShort( fp, 1 ); // nelements
635 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
637 for ( i = 0; i < (int)normals.size(); ++i ) {
639 normal[0] = (unsigned char)((p.x() + 1.0) * 128);
640 normal[1] = (unsigned char)((p.y() + 1.0) * 128);
641 normal[2] = (unsigned char)((p.z() + 1.0) * 128);
642 sgWriteBytes( fp, 3, normal );
645 // dump texture coordinates
646 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
647 sgWriteShort( fp, 0 ); // nproperties
648 sgWriteShort( fp, 1 ); // nelements
649 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
650 for ( i = 0; i < (int)texcoords.size(); ++i ) {
652 sgSetVec2( t, p.x(), p.y() );
653 sgWriteVec2( fp, t );
656 // dump individual triangles if they exist
657 if ( tris_v.size() > 0 ) {
661 while ( start < (int)tri_materials.size() ) {
663 material = tri_materials[start];
664 while ( (end < (int)tri_materials.size()) &&
665 (material == tri_materials[end]) )
667 // cout << "end = " << end << endl;
670 // cout << "group = " << start << " to " << end - 1 << endl;
672 // write group headers
673 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
674 sgWriteShort( fp, 1 ); // nproperties
675 sgWriteShort( fp, 1 ); // nelements
677 sgWriteChar( fp, (char)SG_MATERIAL ); // property
678 sgWriteUInt( fp, material.length() ); // nbytes
679 sgWriteBytes( fp, material.length(), material.c_str() );
681 sgWriteUInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
684 for ( i = start; i < end; ++i ) {
685 for ( j = 0; j < 3; ++j ) {
686 sgWriteShort( fp, (short)tris_v[i][j] );
687 sgWriteShort( fp, (short)tris_tc[i][j] );
696 // dump triangle strips
697 if ( strips_v.size() > 0 ) {
701 while ( start < (int)strip_materials.size() ) {
703 material = strip_materials[start];
704 while ( (end < (int)strip_materials.size()) &&
705 (material == strip_materials[end]) )
707 // cout << "end = " << end << endl;
710 // cout << "group = " << start << " to " << end - 1 << endl;
712 // write group headers
713 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
714 sgWriteShort( fp, 1 ); // nproperties
715 sgWriteShort( fp, end - start ); // nelements
717 sgWriteChar( fp, (char)SG_MATERIAL ); // property
718 sgWriteUInt( fp, material.length() ); // nbytes
719 sgWriteBytes( fp, material.length(), material.c_str() );
722 for ( i = start; i < end; ++i ) {
724 sgWriteUInt( fp, strips_v[i].size() * 2 * sizeof(short) );
725 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
726 sgWriteShort( fp, (short)strips_v[i][j] );
727 sgWriteShort( fp, (short)strips_tc[i][j] );
736 // dump triangle fans
737 if ( fans_v.size() > 0 ) {
741 while ( start < (int)fan_materials.size() ) {
743 material = fan_materials[start];
744 while ( (end < (int)fan_materials.size()) &&
745 (material == fan_materials[end]) )
747 // cout << "end = " << end << endl;
750 // cout << "group = " << start << " to " << end - 1 << endl;
752 // write group headers
753 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
754 sgWriteShort( fp, 1 ); // nproperties
755 sgWriteShort( fp, end - start ); // nelements
757 sgWriteChar( fp, (char)SG_MATERIAL ); // property
758 sgWriteUInt( fp, material.length() ); // nbytes
759 sgWriteBytes( fp, material.length(), material.c_str() );
762 for ( i = start; i < end; ++i ) {
764 sgWriteUInt( fp, fans_v[i].size() * 2 * sizeof(short) );
765 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
766 sgWriteShort( fp, (short)fans_v[i][j] );
767 sgWriteShort( fp, (short)fans_tc[i][j] );
779 if ( sgWriteError() ) {
780 cout << "We detected an error while writing the file." << endl;
788 // write out the structures to an ASCII file. We assume that the
789 // groups come to us sorted by material property. If not, things
790 // don't break, but the result won't be as optimal.
791 bool SGBinObject::write_ascii( const string& base, const string& name,
797 string dir = base + "/" + b.gen_base_path();
798 string command = "mkdir -p " + dir;
800 system( (string("mkdir ") + dir).c_str() );
802 system(command.c_str());
805 // string file = dir + "/" + b.gen_index_str();
806 string file = dir + "/" + name;
807 cout << "Output file = " << file << endl;
810 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
811 cout << "ERROR: opening " << file << " for writing!" << endl;
815 cout << "triangles size = " << tris_v.size() << " tri_materials = "
816 << tri_materials.size() << endl;
817 cout << "strips size = " << strips_v.size() << " strip_materials = "
818 << strip_materials.size() << endl;
819 cout << "fans size = " << fans_v.size() << " fan_materials = "
820 << fan_materials.size() << endl;
822 cout << "points = " << wgs84_nodes.size() << endl;
823 cout << "tex coords = " << texcoords.size() << endl;
825 fprintf(fp, "# FGFS Scenery\n");
826 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
828 time_t calendar_time = time(NULL);
830 local_tm = localtime( &calendar_time );
832 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
833 fprintf(fp, "# Created %s\n", time_str );
836 // write bounding sphere
837 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
838 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
842 fprintf(fp, "# vertex list\n");
843 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
844 p = wgs84_nodes[i] - gbs_center;
846 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
850 fprintf(fp, "# vertex normal list\n");
851 for ( i = 0; i < (int)normals.size(); ++i ) {
853 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
857 // dump texture coordinates
858 fprintf(fp, "# texture coordinate list\n");
859 for ( i = 0; i < (int)texcoords.size(); ++i ) {
861 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
865 // dump individual triangles if they exist
866 if ( tris_v.size() > 0 ) {
867 fprintf(fp, "# triangle groups\n");
872 while ( start < (int)tri_materials.size() ) {
874 material = tri_materials[start];
875 while ( (end < (int)tri_materials.size()) &&
876 (material == tri_materials[end]) )
878 // cout << "end = " << end << endl;
881 // cout << "group = " << start << " to " << end - 1 << endl;
883 // make a list of points for the group
884 point_list group_nodes;
887 double bs_radius = 0;
888 for ( i = start; i < end; ++i ) {
889 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
890 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
891 bs_center = calc_center( group_nodes );
892 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
896 // write group headers
898 fprintf(fp, "# usemtl %s\n", material.c_str());
899 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
900 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
903 for ( i = start; i < end; ++i ) {
905 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
906 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
916 // dump triangle groups
917 if ( strips_v.size() > 0 ) {
918 fprintf(fp, "# triangle strips\n");
923 while ( start < (int)strip_materials.size() ) {
925 material = strip_materials[start];
926 while ( (end < (int)strip_materials.size()) &&
927 (material == strip_materials[end]) )
929 // cout << "end = " << end << endl;
932 // cout << "group = " << start << " to " << end - 1 << endl;
934 // make a list of points for the group
935 point_list group_nodes;
938 double bs_radius = 0;
939 for ( i = start; i < end; ++i ) {
940 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
941 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
942 bs_center = calc_center( group_nodes );
943 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
947 // write group headers
949 fprintf(fp, "# usemtl %s\n", material.c_str());
950 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
951 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
954 for ( i = start; i < end; ++i ) {
956 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
957 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
970 command = "gzip --force --best " + file;
971 system(command.c_str());