1 // sg_binobj.cxx -- routines to read and write low level flightgear 3d objects
3 // Written by Curtis Olson, started January 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
35 #include <cstdlib> // for system()
43 #include <simgear/bucket/newbucket.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/math/SGGeometry.hxx>
47 #include "lowlevel.hxx"
48 #include "sg_binobj.hxx"
57 SG_BOUNDING_SPHERE = 0,
66 SG_TRIANGLE_FACES = 10,
67 SG_TRIANGLE_STRIPS = 11,
72 SG_IDX_VERTICES = 0x01,
73 SG_IDX_NORMALS = 0x02,
75 SG_IDX_TEXCOORDS = 0x08
78 enum sgPropertyTypes {
84 class sgSimpleBuffer {
93 sgSimpleBuffer( unsigned int s = 0) :
106 unsigned int get_size() const { return size; }
107 char *get_ptr() const { return ptr; }
114 void resize( unsigned int s )
128 ptr = new char[size];
134 double* p = reinterpret_cast<double*>(ptr + offset);
136 if ( sgIsBigEndian() ) {
137 sgEndianSwap((uint64_t *) p + 0);
138 sgEndianSwap((uint64_t *) p + 1);
139 sgEndianSwap((uint64_t *) p + 2);
142 offset += 3 * sizeof(double);
148 float* p = reinterpret_cast<float*>(ptr + offset);
149 if ( sgIsBigEndian() ) {
150 sgEndianSwap((uint32_t *) p);
153 offset += sizeof(float);
159 float* p = reinterpret_cast<float*>(ptr + offset);
161 if ( sgIsBigEndian() ) {
162 sgEndianSwap((uint32_t *) p + 0);
163 sgEndianSwap((uint32_t *) p + 1);
166 offset += 2 * sizeof(float);
172 float* p = reinterpret_cast<float*>(ptr + offset);
174 if ( sgIsBigEndian() ) {
175 sgEndianSwap((uint32_t *) p + 0);
176 sgEndianSwap((uint32_t *) p + 1);
177 sgEndianSwap((uint32_t *) p + 2);
180 offset += 3 * sizeof(float);
186 float* p = reinterpret_cast<float*>(ptr + offset);
188 if ( sgIsBigEndian() ) {
189 sgEndianSwap((uint32_t *) p + 0);
190 sgEndianSwap((uint32_t *) p + 1);
191 sgEndianSwap((uint32_t *) p + 2);
192 sgEndianSwap((uint32_t *) p + 3);
195 offset += 4 * sizeof(float);
201 static void read_indices(char* buffer,
209 const int indexSize = sizeof(T) * std::bitset<32>(indexMask).count();
210 const int count = bytes / indexSize;
212 // fix endian-ness of the whole lot, if required
213 if (sgIsBigEndian()) {
214 int indices = bytes / sizeof(T);
215 T* src = reinterpret_cast<T*>(buffer);
216 for (int i=0; i<indices; ++i) {
221 T* src = reinterpret_cast<T*>(buffer);
222 for (int i=0; i<count; ++i) {
223 if (indexMask & SG_IDX_VERTICES) vertices.push_back(*src++);
224 if (indexMask & SG_IDX_NORMALS) normals.push_back(*src++);
225 if (indexMask & SG_IDX_COLORS) colors.push_back(*src++);
226 if (indexMask & SG_IDX_TEXCOORDS) texCoords.push_back(*src++);
227 } // of elements in the index
231 void write_indice(gzFile fp, T value)
233 sgWriteBytes(fp, sizeof(T), &value);
236 // specialize template to call endian-aware conversion methods
238 void write_indice(gzFile fp, uint16_t value)
240 sgWriteUShort(fp, value);
244 void write_indice(gzFile fp, uint32_t value)
246 sgWriteUInt(fp, value);
251 void write_indices(gzFile fp, unsigned char indexMask,
252 const int_list& vertices,
253 const int_list& normals,
254 const int_list& colors,
255 const int_list& texCoords)
257 unsigned int count = vertices.size();
258 const int indexSize = sizeof(T) * std::bitset<32>(indexMask).count();
259 sgWriteUInt(fp, indexSize * count);
261 for (unsigned int i=0; i < count; ++i) {
262 write_indice(fp, static_cast<T>(vertices[i]));
264 if (indexMask & SG_IDX_NORMALS) {
265 write_indice(fp, static_cast<T>(normals[i]));
267 if (indexMask & SG_IDX_COLORS) {
268 write_indice(fp, static_cast<T>(colors[i]));
270 if (indexMask & SG_IDX_TEXCOORDS) {
271 write_indice(fp, static_cast<T>(texCoords[i]));
277 // read object properties
278 void SGBinObject::read_object( gzFile fp,
282 group_list& vertices,
285 group_list& texCoords,
286 string_list& materials)
289 unsigned char idx_mask;
291 sgSimpleBuffer buf( 32768 ); // 32 Kb
295 if ( obj_type == SG_POINTS ) {
296 idx_mask = SG_IDX_VERTICES;
298 idx_mask = (char)(SG_IDX_VERTICES | SG_IDX_TEXCOORDS);
301 for ( j = 0; j < nproperties; ++j ) {
303 sgReadChar( fp, &prop_type );
304 sgReadUInt( fp, &nbytes );
306 char *ptr = buf.get_ptr();
307 sgReadBytes( fp, nbytes, ptr );
308 if ( prop_type == SG_MATERIAL ) {
312 strncpy( material, ptr, nbytes );
313 material[nbytes] = '\0';
314 // cout << "material type = " << material << endl;
315 } else if ( prop_type == SG_INDEX_TYPES ) {
317 //cout << std::hex << "index mask:" << idx_mask << std::dec << endl;
321 if ( sgReadError() ) {
322 cout << "We detected an error reading object properties" << endl;
326 for ( j = 0; j < nelements; ++j ) {
327 sgReadUInt( fp, &nbytes );
328 if ( sgReadError() ) {
329 cout << "We detected an error reading element size for :" << j << endl;
333 buf.resize( nbytes );
334 char *ptr = buf.get_ptr();
335 sgReadBytes( fp, nbytes, ptr );
337 if ( sgReadError() ) {
338 cout << "We detected an error reading object element:" << j << "bytes="<< nbytes << endl;
347 read_indices<uint32_t>(ptr, nbytes, idx_mask, vs, ns, cs, tcs);
349 read_indices<uint16_t>(ptr, nbytes, idx_mask, vs, ns, cs, tcs);
352 vertices.push_back( vs );
353 normals.push_back( ns );
354 colors.push_back( cs );
355 texCoords.push_back( tcs );
356 materials.push_back( material );
357 } // of element iteration
361 // read a binary file and populate the provided structures.
362 bool SGBinObject::read_bin( const string& file ) {
367 sgSimpleBuffer buf( 32768 ); // 32 Kb
369 // zero out structures
370 gbs_center = SGVec3d(0, 0, 0);
381 pt_materials.clear();
387 tri_materials.clear();
393 strip_materials.clear();
399 fan_materials.clear();
402 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
403 string filegz = file + ".gz";
404 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
405 SG_LOG( SG_EVENT, SG_ALERT,
406 "ERROR: opening " << file << " or " << filegz << " for reading!");
416 sgReadUInt( fp, &header );
417 if ( ((header & 0xFF000000) >> 24) == 'S' &&
418 ((header & 0x00FF0000) >> 16) == 'G' ) {
419 // cout << "Good header" << endl;
421 version = (header & 0x0000FFFF);
422 // cout << "File version = " << version << endl;
424 // close the file before we return
426 SG_LOG( SG_EVENT, SG_ALERT,
427 "ERROR: " << file << "has bad header");
431 // read creation time
432 unsigned int foo_calendar_time;
433 sgReadUInt( fp, &foo_calendar_time );
436 time_t calendar_time = foo_calendar_time;
437 // The following code has a global effect on the host application
438 // and can screws up the time elsewhere. It should be avoided
439 // unless you need this for debugging in which case you should
440 // disable it again once the debugging task is finished.
442 local_tm = localtime( &calendar_time );
444 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
445 SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
448 // read number of top level objects
450 if ( version >= 10) { // version 10 extends everything to be 32-bit
451 sgReadInt( fp, &nobjects );
452 } else if ( version >= 7 ) {
454 sgReadUShort( fp, &v );
458 sgReadShort( fp, &v );
462 //cout << "Total objects to read = " << nobjects << endl;
464 if ( sgReadError() ) {
465 cout << "Error while reading header of file " << file << "(.gz)" << endl;
470 for ( i = 0; i < nobjects; ++i ) {
471 // read object header
473 uint32_t nproperties, nelements;
474 sgReadChar( fp, &obj_type );
475 if ( version >= 10 ) {
476 sgReadUInt( fp, &nproperties );
477 sgReadUInt( fp, &nelements );
478 } else if ( version >= 7 ) {
480 sgReadUShort( fp, &v );
482 sgReadUShort( fp, &v );
486 sgReadShort( fp, &v );
488 sgReadShort( fp, &v );
492 //cout << "object " << i << " = " << (int)obj_type << " props = "
493 // << nproperties << " elements = " << nelements << endl;
495 if ( obj_type == SG_BOUNDING_SPHERE ) {
496 // read bounding sphere properties
497 read_properties( fp, nproperties );
499 // read bounding sphere elements
500 for ( j = 0; j < nelements; ++j ) {
501 sgReadUInt( fp, &nbytes );
502 buf.resize( nbytes );
504 char *ptr = buf.get_ptr();
505 sgReadBytes( fp, nbytes, ptr );
506 gbs_center = buf.readVec3d();
507 gbs_radius = buf.readFloat();
509 } else if ( obj_type == SG_VERTEX_LIST ) {
510 // read vertex list properties
511 read_properties( fp, nproperties );
513 // read vertex list elements
514 for ( j = 0; j < nelements; ++j ) {
515 sgReadUInt( fp, &nbytes );
516 buf.resize( nbytes );
518 char *ptr = buf.get_ptr();
519 sgReadBytes( fp, nbytes, ptr );
520 int count = nbytes / (sizeof(float) * 3);
521 wgs84_nodes.reserve( count );
522 for ( k = 0; k < count; ++k ) {
523 SGVec3f v = buf.readVec3f();
524 // extend from float to double, hmmm
525 wgs84_nodes.push_back( SGVec3d(v[0], v[1], v[2]) );
528 } else if ( obj_type == SG_COLOR_LIST ) {
529 // read color list properties
530 read_properties( fp, nproperties );
532 // read color list elements
533 for ( j = 0; j < nelements; ++j ) {
534 sgReadUInt( fp, &nbytes );
535 buf.resize( nbytes );
537 char *ptr = buf.get_ptr();
538 sgReadBytes( fp, nbytes, ptr );
539 int count = nbytes / (sizeof(float) * 4);
540 colors.reserve(count);
541 for ( k = 0; k < count; ++k ) {
542 colors.push_back( buf.readVec4f() );
545 } else if ( obj_type == SG_NORMAL_LIST ) {
546 // read normal list properties
547 read_properties( fp, nproperties );
549 // read normal list elements
550 for ( j = 0; j < nelements; ++j ) {
551 sgReadUInt( fp, &nbytes );
552 buf.resize( nbytes );
554 unsigned char *ptr = (unsigned char *)(buf.get_ptr());
555 sgReadBytes( fp, nbytes, ptr );
556 int count = nbytes / 3;
557 normals.reserve( count );
559 for ( k = 0; k < count; ++k ) {
560 SGVec3f normal( (ptr[0]) / 127.5 - 1.0,
561 (ptr[1]) / 127.5 - 1.0,
562 (ptr[2]) / 127.5 - 1.0);
563 normals.push_back(normalize(normal));
567 } else if ( obj_type == SG_TEXCOORD_LIST ) {
568 // read texcoord list properties
569 read_properties( fp, nproperties );
571 // read texcoord list elements
572 for ( j = 0; j < nelements; ++j ) {
573 sgReadUInt( fp, &nbytes );
574 buf.resize( nbytes );
576 char *ptr = buf.get_ptr();
577 sgReadBytes( fp, nbytes, ptr );
578 int count = nbytes / (sizeof(float) * 2);
579 texcoords.reserve(count);
580 for ( k = 0; k < count; ++k ) {
581 texcoords.push_back( buf.readVec2f() );
584 } else if ( obj_type == SG_POINTS ) {
585 // read point elements
586 read_object( fp, SG_POINTS, nproperties, nelements,
587 pts_v, pts_n, pts_c, pts_tc, pt_materials );
588 } else if ( obj_type == SG_TRIANGLE_FACES ) {
589 // read triangle face properties
590 read_object( fp, SG_TRIANGLE_FACES, nproperties, nelements,
591 tris_v, tris_n, tris_c, tris_tc, tri_materials );
592 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
593 // read triangle strip properties
594 read_object( fp, SG_TRIANGLE_STRIPS, nproperties, nelements,
595 strips_v, strips_n, strips_c, strips_tc,
597 } else if ( obj_type == SG_TRIANGLE_FANS ) {
598 // read triangle fan properties
599 read_object( fp, SG_TRIANGLE_FANS, nproperties, nelements,
600 fans_v, fans_n, fans_c, fans_tc, fan_materials );
602 // unknown object type, just skip
603 read_properties( fp, nproperties );
606 for ( j = 0; j < nelements; ++j ) {
607 sgReadUInt( fp, &nbytes );
608 // cout << "element size = " << nbytes << endl;
609 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
610 char *ptr = buf.get_ptr();
611 sgReadBytes( fp, nbytes, ptr );
615 if ( sgReadError() ) {
616 cout << "Error while reading object:" << i << " in file " << file << "(.gz)" << endl;
624 if ( sgReadError() ) {
625 cout << "Error while reading file " << file << "(.gz)" << endl;
632 void SGBinObject::write_header(gzFile fp, int type, int nProps, int nElements)
634 sgWriteChar(fp, (unsigned char) type);
636 sgWriteUShort(fp, nProps);
637 sgWriteUShort(fp, nElements);
639 sgWriteUInt(fp, nProps);
640 sgWriteUInt(fp, nElements);
644 unsigned int SGBinObject::count_objects(const string_list& materials)
646 unsigned int result = 0;
647 unsigned int start = 0, end = 1;
648 unsigned int count = materials.size();
651 while ( start < count ) {
652 m = materials[start];
653 for (end = start+1; (end < count) && (m == materials[end]); ++end) { }
661 void SGBinObject::write_objects(gzFile fp, int type, const group_list& verts,
662 const group_list& normals, const group_list& colors,
663 const group_list& texCoords, const string_list& materials)
669 unsigned int start = 0, end = 1;
673 while (start < materials.size()) {
674 m = materials[start];
675 // find range of objects with identical material, write out as a single object
676 for (end = start+1; (end < materials.size()) && (m == materials[end]); ++end) {}
678 const int count = end - start;
679 write_header(fp, type, 2, count);
682 sgWriteChar( fp, (char)SG_MATERIAL ); // property
683 sgWriteUInt( fp, m.length() ); // nbytes
684 sgWriteBytes( fp, m.length(), m.c_str() );
686 unsigned char idx_mask = 0;
687 if ( !verts.empty() && !verts.front().empty()) idx_mask |= SG_IDX_VERTICES;
688 if ( !normals.empty() && !normals.front().empty()) idx_mask |= SG_IDX_NORMALS;
689 if ( !colors.empty() && !colors.front().empty()) idx_mask |= SG_IDX_COLORS;
690 if ( !texCoords.empty() && !texCoords.front().empty()) idx_mask |= SG_IDX_TEXCOORDS;
691 sgWriteChar( fp, (char)SG_INDEX_TYPES ); // property
692 sgWriteUInt( fp, 1 ); // nbytes
693 sgWriteChar( fp, idx_mask );
695 // cout << "material:" << m << ", count =" << count << endl;
697 for (unsigned int i=start; i < end; ++i) {
698 const int_list& va(verts[i]);
699 const int_list& na((idx_mask & SG_IDX_NORMALS) ? normals[i] : emptyList);
700 const int_list& ca((idx_mask & SG_IDX_COLORS) ? colors[i] : emptyList);
701 const int_list& tca((idx_mask & SG_IDX_TEXCOORDS) ? texCoords[i] : emptyList);
704 write_indices<uint16_t>(fp, idx_mask, va, na, ca, tca);
706 write_indices<uint32_t>(fp, idx_mask, va, na, ca, tca);
711 } // of materials iteration
714 // write out the structures to a binary file. We assume that the
715 // groups come to us sorted by material property. If not, things
716 // don't break, but the result won't be as optimal.
717 bool SGBinObject::write_bin( const string& base, const string& name,
721 SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
722 return write_bin_file(file);
725 bool SGBinObject::write_bin_file(const SGPath& file)
730 file2.create_dir( 0755 );
731 cout << "Output file = " << file.str() << endl;
734 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
735 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
741 cout << "points size = " << pts_v.size() << " pt_materials = "
742 << pt_materials.size() << endl;
743 cout << "triangles size = " << tris_v.size() << " tri_materials = "
744 << tri_materials.size() << endl;
745 cout << "strips size = " << strips_v.size() << " strip_materials = "
746 << strip_materials.size() << endl;
747 cout << "fans size = " << fans_v.size() << " fan_materials = "
748 << fan_materials.size() << endl;
750 cout << "nodes = " << wgs84_nodes.size() << endl;
751 cout << "colors = " << colors.size() << endl;
752 cout << "normals = " << normals.size() << endl;
753 cout << "tex coords = " << texcoords.size() << endl;
756 if ((wgs84_nodes.size() < 0xffff) &&
757 (normals.size() < 0xffff) &&
758 (texcoords.size() < 0xffff)) {
759 version = 7; // use smaller indices if possible
762 // write header magic
764 /** Magic Number for our file format */
765 #define SG_FILE_MAGIC_NUMBER ( ('S'<<24) + ('G'<<16) + version )
767 sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
768 time_t calendar_time = time(NULL);
769 sgWriteLong( fp, (int32_t)calendar_time );
771 // calculate and write number of top level objects
772 int nobjects = 5; // gbs, vertices, colors, normals, texcoords
773 nobjects += count_objects(pt_materials);
774 nobjects += count_objects(tri_materials);
775 nobjects += count_objects(strip_materials);
776 nobjects += count_objects(fan_materials);
778 cout << "total top level objects = " << nobjects << endl;
780 sgWriteUShort( fp, (uint16_t) nobjects );
782 sgWriteInt( fp, nobjects );
785 // write bounding sphere
786 write_header( fp, SG_BOUNDING_SPHERE, 0, 1);
787 sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
788 sgWritedVec3( fp, gbs_center );
789 sgWriteFloat( fp, gbs_radius );
792 write_header( fp, SG_VERTEX_LIST, 0, 1);
793 sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
794 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
795 sgWriteVec3( fp, toVec3f(wgs84_nodes[i] - gbs_center));
798 // dump vertex color list
799 write_header( fp, SG_COLOR_LIST, 0, 1);
800 sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
801 for ( i = 0; i < (int)colors.size(); ++i ) {
802 sgWriteVec4( fp, colors[i]);
805 // dump vertex normal list
806 write_header( fp, SG_NORMAL_LIST, 0, 1);
807 sgWriteUInt( fp, normals.size() * 3 ); // nbytes
809 for ( i = 0; i < (int)normals.size(); ++i ) {
810 SGVec3f p = normals[i];
811 normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
812 normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
813 normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
814 sgWriteBytes( fp, 3, normal );
817 // dump texture coordinates
818 write_header( fp, SG_TEXCOORD_LIST, 0, 1);
819 sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
820 for ( i = 0; i < (int)texcoords.size(); ++i ) {
821 sgWriteVec2( fp, texcoords[i]);
824 write_objects(fp, SG_POINTS, pts_v, pts_n, pts_c, pts_tc, pt_materials);
825 write_objects(fp, SG_TRIANGLE_FACES, tris_v, tris_n, tris_c, tris_tc, tri_materials);
826 write_objects(fp, SG_TRIANGLE_STRIPS, strips_v, strips_n, strips_c, strips_tc, strip_materials);
827 write_objects(fp, SG_TRIANGLE_FANS, fans_v, fans_n, fans_c, fans_tc, fan_materials);
832 if ( sgWriteError() ) {
833 cout << "Error while writing file " << file.str() << endl;
841 // write out the structures to an ASCII file. We assume that the
842 // groups come to us sorted by material property. If not, things
843 // don't break, but the result won't be as optimal.
844 bool SGBinObject::write_ascii( const string& base, const string& name,
849 SGPath file = base + "/" + b.gen_base_path() + "/" + name;
850 file.create_dir( 0755 );
851 cout << "Output file = " << file.str() << endl;
854 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
855 cout << "ERROR: opening " << file.str() << " for writing!" << endl;
859 cout << "triangles size = " << tris_v.size() << " tri_materials = "
860 << tri_materials.size() << endl;
861 cout << "strips size = " << strips_v.size() << " strip_materials = "
862 << strip_materials.size() << endl;
863 cout << "fans size = " << fans_v.size() << " fan_materials = "
864 << fan_materials.size() << endl;
866 cout << "points = " << wgs84_nodes.size() << endl;
867 cout << "tex coords = " << texcoords.size() << endl;
869 fprintf(fp, "# FGFS Scenery\n");
870 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
872 time_t calendar_time = time(NULL);
874 local_tm = localtime( &calendar_time );
876 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
877 fprintf(fp, "# Created %s\n", time_str );
880 // write bounding sphere
881 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
882 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
886 fprintf(fp, "# vertex list\n");
887 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
888 SGVec3d p = wgs84_nodes[i] - gbs_center;
890 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
894 fprintf(fp, "# vertex normal list\n");
895 for ( i = 0; i < (int)normals.size(); ++i ) {
896 SGVec3f p = normals[i];
897 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
901 // dump texture coordinates
902 fprintf(fp, "# texture coordinate list\n");
903 for ( i = 0; i < (int)texcoords.size(); ++i ) {
904 SGVec2f p = texcoords[i];
905 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
909 // dump individual triangles if they exist
910 if ( tris_v.size() > 0 ) {
911 fprintf(fp, "# triangle groups\n");
916 while ( start < (int)tri_materials.size() ) {
918 material = tri_materials[start];
919 while ( (end < (int)tri_materials.size()) &&
920 (material == tri_materials[end]) )
922 // cout << "end = " << end << endl;
925 // cout << "group = " << start << " to " << end - 1 << endl;
928 for ( i = start; i < end; ++i ) {
929 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
930 d.expandBy(wgs84_nodes[ tris_v[i][j] ]);
934 SGVec3d bs_center = d.getCenter();
935 double bs_radius = d.getRadius();
937 // write group headers
939 fprintf(fp, "# usemtl %s\n", material.c_str());
940 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
941 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
944 for ( i = start; i < end; ++i ) {
946 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
947 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
957 // dump triangle groups
958 if ( strips_v.size() > 0 ) {
959 fprintf(fp, "# triangle strips\n");
964 while ( start < (int)strip_materials.size() ) {
966 material = strip_materials[start];
967 while ( (end < (int)strip_materials.size()) &&
968 (material == strip_materials[end]) )
970 // cout << "end = " << end << endl;
973 // cout << "group = " << start << " to " << end - 1 << endl;
977 for ( i = start; i < end; ++i ) {
978 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
979 d.expandBy(wgs84_nodes[ tris_v[i][j] ]);
983 SGVec3d bs_center = d.getCenter();
984 double bs_radius = d.getRadius();
986 // write group headers
988 fprintf(fp, "# usemtl %s\n", material.c_str());
989 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
990 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
993 for ( i = start; i < end; ++i ) {
995 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
996 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
1009 string command = "gzip --force --best " + file.str();
1010 int err = system(command.c_str());
1013 cout << "ERROR: gzip " << file.str() << " failed!" << endl;
1019 void SGBinObject::read_properties(gzFile fp, int nproperties)
1025 for ( int j = 0; j < nproperties; ++j ) {
1027 sgReadChar( fp, &prop_type );
1028 sgReadUInt( fp, &nbytes );
1029 // cout << "property size = " << nbytes << endl;
1030 if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
1031 char *ptr = buf.get_ptr();
1032 sgReadBytes( fp, nbytes, ptr );