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 // write out the structures to an ASCII file. We assume that the
111 // groups come to us sorted by material property. If not, things
112 // don't break, but the result won't be as optimal.
113 void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
114 Point3D gbs_center, float gbs_radius,
115 const point_list& wgs84_nodes, const point_list& normals,
116 const point_list& texcoords,
117 const group_list& tris_v, const group_list& tris_tc,
118 const string_list& tri_materials,
119 const group_list& strips_v, const group_list& strips_tc,
120 const string_list& strip_materials,
121 const group_list& fans_v, const group_list& fans_tc,
122 const string_list& fan_materials )
127 string dir = base + "/" + b.gen_base_path();
128 string command = "mkdir -p " + dir;
130 fg_mkdir( dir.c_str() );
132 system(command.c_str());
135 // string file = dir + "/" + b.gen_index_str();
136 string file = dir + "/" + name;
137 cout << "Output file = " << file << endl;
140 if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
141 cout << "ERROR: opening " << file << " for writing!" << endl;
145 cout << "triangles size = " << tris_v.size() << " tri_materials = "
146 << tri_materials.size() << endl;
147 cout << "strips size = " << strips_v.size() << " strip_materials = "
148 << strip_materials.size() << endl;
149 cout << "fans size = " << fans_v.size() << " fan_materials = "
150 << fan_materials.size() << endl;
152 cout << "points = " << wgs84_nodes.size() << endl;
153 cout << "tex coords = " << texcoords.size() << endl;
155 fprintf(fp, "# FGFS Scenery\n");
156 fprintf(fp, "# Version %s\n", SG_SCENERY_FILE_FORMAT);
158 time_t calendar_time = time(NULL);
160 local_tm = localtime( &calendar_time );
162 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
163 fprintf(fp, "# Created %s\n", time_str );
166 // write bounding sphere
167 fprintf(fp, "# gbs %.5f %.5f %.5f %.2f\n",
168 gbs_center.x(), gbs_center.y(), gbs_center.z(), gbs_radius);
172 fprintf(fp, "# vertex list\n");
173 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
174 p = wgs84_nodes[i] - gbs_center;
176 fprintf(fp, "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
180 fprintf(fp, "# vertex normal list\n");
181 for ( i = 0; i < (int)normals.size(); ++i ) {
183 fprintf(fp, "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
187 // dump texture coordinates
188 fprintf(fp, "# texture coordinate list\n");
189 for ( i = 0; i < (int)texcoords.size(); ++i ) {
191 fprintf(fp, "vt %.5f %.5f\n", p.x(), p.y() );
195 // dump individual triangles if they exist
196 if ( tris_v.size() > 0 ) {
197 fprintf(fp, "# triangle groups\n");
202 while ( start < (int)tri_materials.size() ) {
204 material = tri_materials[start];
205 while ( (end < (int)tri_materials.size()) &&
206 (material == tri_materials[end]) )
208 // cout << "end = " << end << endl;
211 // cout << "group = " << start << " to " << end - 1 << endl;
213 // make a list of points for the group
214 point_list group_nodes;
217 double bs_radius = 0;
218 for ( i = start; i < end; ++i ) {
219 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
220 group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
221 bs_center = calc_center( group_nodes );
222 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
226 // write group headers
228 fprintf(fp, "# usemtl %s\n", material.c_str());
229 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
230 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
233 for ( i = start; i < end; ++i ) {
235 for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
236 fprintf(fp, " %d/%d", tris_v[i][j], tris_tc[i][j] );
246 // dump triangle groups
247 if ( strips_v.size() > 0 ) {
248 fprintf(fp, "# triangle strips\n");
253 while ( start < (int)strip_materials.size() ) {
255 material = strip_materials[start];
256 while ( (end < (int)strip_materials.size()) &&
257 (material == strip_materials[end]) )
259 // cout << "end = " << end << endl;
262 // cout << "group = " << start << " to " << end - 1 << endl;
264 // make a list of points for the group
265 point_list group_nodes;
268 double bs_radius = 0;
269 for ( i = start; i < end; ++i ) {
270 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
271 group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
272 bs_center = calc_center( group_nodes );
273 bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
277 // write group headers
279 fprintf(fp, "# usemtl %s\n", material.c_str());
280 fprintf(fp, "# bs %.4f %.4f %.4f %.2f\n",
281 bs_center.x(), bs_center.y(), bs_center.z(), bs_radius);
284 for ( i = start; i < end; ++i ) {
286 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
287 fprintf(fp, " %d/%d", strips_v[i][j], strips_tc[i][j] );
300 command = "gzip --force --best " + file;
301 system(command.c_str());
305 // read a binary file and populate the provided structures.
306 void sgReadBinObj( const string& file,
307 Point3D &gbs_center, float *gbs_radius,
308 point_list& wgs84_nodes, point_list& normals,
309 point_list& texcoords,
310 group_list& tris_v, group_list& tris_tc,
311 string_list& tri_materials,
312 group_list& strips_v, group_list& strips_tc,
313 string_list& strip_materials,
314 group_list& fans_v, group_list& fans_tc,
315 string_list& fan_materials )
320 // zero out structures
327 tri_materials.clear();
331 strip_materials.clear();
335 fan_materials.clear();
337 cout << "Loading binary input file = " << file << endl;
340 if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
341 string filegz = file + ".gz";
342 if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
343 cout << "ERROR: opening " << file << " or " << filegz
344 << "for reading!" << endl;
351 sgReadInt( fp, &header );
352 if ( ((header & 0xFF000000) >> 24) == 'T' &&
353 ((header & 0x00FF0000) >> 16) == 'G' ) {
354 cout << "Good header" << endl;
356 version = (header & 0x0000FFFF);
357 cout << "File version = " << version << endl;
360 // read creation time
361 time_t calendar_time;
362 sgReadLong( fp, &calendar_time );
364 local_tm = localtime( &calendar_time );
366 strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
367 cout << "File created on " << time_str << endl;
369 // read number of top level objects
371 sgReadShort( fp, &nobjects );
372 cout << "Total objects to read = " << nobjects << endl;
375 for ( i = 0; i < nobjects; ++i ) {
376 // read object header
378 short nproperties, nelements;
379 sgReadChar( fp, &obj_type );
380 sgReadShort( fp, &nproperties );
381 sgReadShort( fp, &nelements );
383 cout << "object " << i << " = " << (int)obj_type << " props = "
384 << nproperties << " elements = " << nelements << endl;
386 if ( obj_type == SG_BOUNDING_SPHERE ) {
387 // read bounding sphere properties
388 for ( j = 0; j < nproperties; ++j ) {
390 sgReadChar( fp, &prop_type );
393 sgReadInt( fp, &nbytes );
394 cout << "property size = " << nbytes << endl;
397 sgReadBytes( fp, nbytes, ptr );
400 // read bounding sphere elements
401 for ( j = 0; j < nelements; ++j ) {
403 sgReadInt( fp, &nbytes );
404 cout << "element size = " << nbytes << endl;
407 sgReadBytes( fp, nbytes, ptr );
409 double *dptr = (double *)ptr;
410 gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
411 cout << "Center = " << gbs_center << endl;
412 ptr += sizeof(double) * 3;
414 float *fptr = (float *)ptr;
415 *gbs_radius = fptr[0];
416 cout << "Bounding radius = " << *gbs_radius << endl;
418 } else if ( obj_type == SG_VERTEX_LIST ) {
419 // read vertex list properties
420 for ( j = 0; j < nproperties; ++j ) {
422 sgReadChar( fp, &prop_type );
425 sgReadInt( fp, &nbytes );
426 cout << "property size = " << nbytes << endl;
429 sgReadBytes( fp, nbytes, ptr );
432 // read vertex list elements
433 for ( j = 0; j < nelements; ++j ) {
435 sgReadInt( fp, &nbytes );
436 cout << "element size = " << nbytes << endl;
439 sgReadBytes( fp, nbytes, ptr );
440 int count = nbytes / (sizeof(float) * 3);
441 float *fptr = (float *)ptr;
442 for ( k = 0; k < count; ++k ) {
443 p = Point3D( fptr[0], fptr[1], fptr[2] );
444 cout << "node = " << p << endl;
445 wgs84_nodes.push_back( p );
449 } else if ( obj_type == SG_NORMAL_LIST ) {
450 // read normal list properties
451 for ( j = 0; j < nproperties; ++j ) {
453 sgReadChar( fp, &prop_type );
456 sgReadInt( fp, &nbytes );
457 cout << "property size = " << nbytes << endl;
460 sgReadBytes( fp, nbytes, ptr );
463 // read normal list elements
464 for ( j = 0; j < nelements; ++j ) {
466 sgReadInt( fp, &nbytes );
467 cout << "element size = " << nbytes << endl;
470 sgReadBytes( fp, nbytes, ptr );
471 int count = nbytes / 3;
472 for ( k = 0; k < count; ++k ) {
473 p = Point3D( ptr[0] / 256.0, ptr[1] / 256.0,
475 cout << "normal = " << p << endl;
476 normals.push_back( p );
480 } else if ( obj_type == SG_TEXCOORD_LIST ) {
481 // read texcoord list properties
482 for ( j = 0; j < nproperties; ++j ) {
484 sgReadChar( fp, &prop_type );
487 sgReadInt( fp, &nbytes );
488 cout << "property size = " << nbytes << endl;
491 sgReadBytes( fp, nbytes, ptr );
494 // read texcoord list elements
495 for ( j = 0; j < nelements; ++j ) {
497 sgReadInt( fp, &nbytes );
498 cout << "element size = " << nbytes << endl;
501 sgReadBytes( fp, nbytes, ptr );
502 int count = nbytes / (sizeof(float) * 2);
503 float *fptr = (float *)ptr;
504 for ( k = 0; k < count; ++k ) {
505 p = Point3D( fptr[0], fptr[1], 0 );
506 cout << "texcoord = " << p << endl;
507 texcoords.push_back( p );
511 } else if ( obj_type == SG_TRIANGLE_FACES ) {
512 // read triangle face properties
513 for ( j = 0; j < nproperties; ++j ) {
515 sgReadChar( fp, &prop_type );
518 sgReadInt( fp, &nbytes );
519 cout << "property size = " << nbytes << endl;
522 sgReadBytes( fp, nbytes, ptr );
523 if ( prop_type == SG_MATERIAL ) {
525 strncpy( material, ptr, nbytes );
526 material[nbytes] = '\0';
527 cout << "material type = " << material << endl;
528 tri_materials.push_back( material );
532 // read triangle face elements
533 for ( j = 0; j < nelements; ++j ) {
535 sgReadInt( fp, &nbytes );
536 cout << "element size = " << nbytes << endl;
539 sgReadBytes( fp, nbytes, ptr );
540 int count = nbytes / (sizeof(short) * 2 * 3);
541 short *sptr = (short *)ptr;
543 for ( k = 0; k < count; ++k ) {
544 vs.clear(); tcs.clear();
545 for ( m = 0; m < 3; ++m ) {
546 vs.push_back( sptr[0] );
547 tcs.push_back( sptr[1] );
548 cout << sptr[0] << "/" << sptr[1] << " ";
552 tris_v.push_back( vs );
553 tris_tc.push_back( tcs );
556 } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
557 // read triangle strip properties
558 for ( j = 0; j < nproperties; ++j ) {
560 sgReadChar( fp, &prop_type );
563 sgReadInt( fp, &nbytes );
564 cout << "property size = " << nbytes << endl;
567 sgReadBytes( fp, nbytes, ptr );
568 if ( prop_type == SG_MATERIAL ) {
570 strncpy( material, ptr, nbytes );
571 material[nbytes] = '\0';
572 cout << "material type = " << material << endl;
573 strip_materials.push_back( material );
577 // read triangle strip elements
578 for ( j = 0; j < nelements; ++j ) {
580 sgReadInt( fp, &nbytes );
581 cout << "element size = " << nbytes << endl;
584 sgReadBytes( fp, nbytes, ptr );
585 int count = nbytes / (sizeof(short) * 2 * 3);
586 short *sptr = (short *)ptr;
588 vs.clear(); tcs.clear();
589 for ( k = 0; k < count; ++k ) {
590 vs.push_back( sptr[0] );
591 tcs.push_back( sptr[1] );
592 cout << sptr[0] << "/" << sptr[1] << " ";
596 strips_v.push_back( vs );
597 strips_tc.push_back( tcs );
599 } else if ( obj_type == SG_TRIANGLE_FANS ) {
600 // read triangle fan properties
601 for ( j = 0; j < nproperties; ++j ) {
603 sgReadChar( fp, &prop_type );
606 sgReadInt( fp, &nbytes );
607 cout << "property size = " << nbytes << endl;
610 sgReadBytes( fp, nbytes, ptr );
611 if ( prop_type == SG_MATERIAL ) {
613 strncpy( material, ptr, nbytes );
614 material[nbytes] = '\0';
615 cout << "material type = " << material << endl;
616 fan_materials.push_back( material );
620 // read triangle fan elements
621 for ( j = 0; j < nelements; ++j ) {
623 sgReadInt( fp, &nbytes );
624 cout << "element size = " << nbytes << endl;
627 sgReadBytes( fp, nbytes, ptr );
628 int count = nbytes / (sizeof(short) * 2 * 3);
629 short *sptr = (short *)ptr;
631 vs.clear(); tcs.clear();
632 for ( k = 0; k < count; ++k ) {
633 vs.push_back( sptr[0] );
634 tcs.push_back( sptr[1] );
635 cout << sptr[0] << "/" << sptr[1] << " ";
639 fans_v.push_back( vs );
640 fans_tc.push_back( tcs );
643 // unknown object type, just skip
646 for ( j = 0; j < nproperties; ++j ) {
648 sgReadChar( fp, &prop_type );
651 sgReadInt( fp, &nbytes );
652 cout << "property size = " << nbytes << endl;
655 sgReadBytes( fp, nbytes, ptr );
659 for ( j = 0; j < nelements; ++j ) {
661 sgReadInt( fp, &nbytes );
662 cout << "element size = " << nbytes << endl;
665 sgReadBytes( fp, nbytes, ptr );
675 // write out the structures to a binary file. We assume that the
676 // groups come to us sorted by material property. If not, things
677 // don't break, but the result won't be as optimal.
678 void sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
679 Point3D gbs_center, float gbs_radius,
680 const point_list& wgs84_nodes, const point_list& normals,
681 const point_list& texcoords,
682 const group_list& tris_v, const group_list& tris_tc,
683 const string_list& tri_materials,
684 const group_list& strips_v, const group_list& strips_tc,
685 const string_list& strip_materials,
686 const group_list& fans_v, const group_list& fans_tc,
687 const string_list& fan_materials )
694 string dir = base + "/" + b.gen_base_path();
695 string command = "mkdir -p " + dir;
697 fg_mkdir( dir.c_str() );
699 system(command.c_str());
702 string file = dir + "/" + name + ".gz";
703 cout << "Output file = " << file << endl;
706 if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
707 cout << "ERROR: opening " << file << " for writing!" << endl;
711 cout << "triangles size = " << tris_v.size() << " tri_materials = "
712 << tri_materials.size() << endl;
713 cout << "strips size = " << strips_v.size() << " strip_materials = "
714 << strip_materials.size() << endl;
715 cout << "fans size = " << fans_v.size() << " fan_materials = "
716 << fan_materials.size() << endl;
718 cout << "points = " << wgs84_nodes.size() << endl;
719 cout << "tex coords = " << texcoords.size() << endl;
721 // write header magic
722 sgWriteInt( fp, SG_FILE_MAGIC_NUMBER );
723 time_t calendar_time = time(NULL);
724 sgWriteLong( fp, (long int)calendar_time );
726 // calculate and write number of top level objects
731 nobjects++; // for gbs
732 nobjects++; // for vertices
733 nobjects++; // for normals
734 nobjects++; // for texcoords
739 while ( start < (int)tri_materials.size() ) {
740 material = tri_materials[start];
741 while ( (end < (int)tri_materials.size()) &&
742 (material == tri_materials[end]) ) {
746 start = end; end = start + 1;
753 while ( start < (int)strip_materials.size() ) {
754 material = strip_materials[start];
755 while ( (end < (int)strip_materials.size()) &&
756 (material == strip_materials[end]) ) {
760 start = end; end = start + 1;
767 while ( start < (int)fan_materials.size() ) {
768 material = fan_materials[start];
769 while ( (end < (int)fan_materials.size()) &&
770 (material == fan_materials[end]) ) {
774 start = end; end = start + 1;
778 cout << "total top level objects = " << nobjects << endl;
779 sgWriteShort( fp, nobjects );
781 // write bounding sphere
782 sgWriteChar( fp, (char)SG_BOUNDING_SPHERE ); // type
783 sgWriteShort( fp, 0 ); // nproperties
784 sgWriteShort( fp, 1 ); // nelements
786 sgWriteInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
788 sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
789 sgWritedVec3( fp, center );
790 sgWriteFloat( fp, gbs_radius );
793 sgWriteChar( fp, (char)SG_VERTEX_LIST ); // type
794 sgWriteShort( fp, 0 ); // nproperties
795 sgWriteShort( fp, 1 ); // nelements
796 sgWriteInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
797 for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
798 p = wgs84_nodes[i] - gbs_center;
799 sgSetVec3( pt, p.x(), p.y(), p.z() );
800 sgWriteVec3( fp, pt );
803 // dump vertex normal list
804 sgWriteChar( fp, (char)SG_NORMAL_LIST ); // type
805 sgWriteShort( fp, 0 ); // nproperties
806 sgWriteShort( fp, 1 ); // nelements
807 sgWriteInt( fp, normals.size() * 3 ); // nbytes
809 for ( i = 0; i < (int)normals.size(); ++i ) {
811 normal[0] = (char)(p.x() * 256);
812 normal[1] = (char)(p.y() * 256);
813 normal[2] = (char)(p.z() * 256);
814 sgWriteBytes( fp, 3, normal );
817 // dump texture coordinates
818 sgWriteChar( fp, (char)SG_TEXCOORD_LIST ); // type
819 sgWriteShort( fp, 0 ); // nproperties
820 sgWriteShort( fp, 1 ); // nelements
821 sgWriteInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
822 for ( i = 0; i < (int)texcoords.size(); ++i ) {
824 sgSetVec2( t, p.x(), p.y() );
825 sgWriteVec2( fp, t );
828 // dump individual triangles if they exist
829 if ( tris_v.size() > 0 ) {
833 while ( start < (int)tri_materials.size() ) {
835 material = tri_materials[start];
836 while ( (end < (int)tri_materials.size()) &&
837 (material == tri_materials[end]) )
839 // cout << "end = " << end << endl;
842 // cout << "group = " << start << " to " << end - 1 << endl;
844 // write group headers
845 sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
846 sgWriteShort( fp, 1 ); // nproperties
847 sgWriteShort( fp, 1 ); // nelements
849 sgWriteChar( fp, (char)SG_MATERIAL ); // property
850 sgWriteInt( fp, material.length() ); // nbytes
851 sgWriteBytes( fp, material.length(), material.c_str() );
853 sgWriteInt( fp, (end - start) * 3 * 2 * sizeof(short) ); // nbytes
856 for ( i = start; i < end; ++i ) {
857 for ( j = 0; j < 3; ++j ) {
858 sgWriteShort( fp, (short)tris_v[i][j] );
859 sgWriteShort( fp, (short)tris_tc[i][j] );
868 // dump triangle strips
869 if ( strips_v.size() > 0 ) {
873 while ( start < (int)strip_materials.size() ) {
875 material = strip_materials[start];
876 while ( (end < (int)strip_materials.size()) &&
877 (material == strip_materials[end]) )
879 // cout << "end = " << end << endl;
882 // cout << "group = " << start << " to " << end - 1 << endl;
884 // write group headers
885 sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
886 sgWriteShort( fp, 1 ); // nproperties
887 sgWriteShort( fp, end - start ); // nelements
889 sgWriteChar( fp, (char)SG_MATERIAL ); // property
890 sgWriteInt( fp, material.length() ); // nbytes
891 sgWriteBytes( fp, material.length(), material.c_str() );
894 for ( i = start; i < end; ++i ) {
896 sgWriteInt( fp, strips_v[i].size() * 2 * sizeof(short) );
897 for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
898 sgWriteShort( fp, (short)strips_v[i][j] );
899 sgWriteShort( fp, (short)strips_tc[i][j] );
908 // dump triangle fans
909 if ( fans_v.size() > 0 ) {
913 while ( start < (int)fan_materials.size() ) {
915 material = fan_materials[start];
916 while ( (end < (int)fan_materials.size()) &&
917 (material == fan_materials[end]) )
919 // cout << "end = " << end << endl;
922 // cout << "group = " << start << " to " << end - 1 << endl;
924 // write group headers
925 sgWriteChar( fp, (char)SG_TRIANGLE_FANS ); // type
926 sgWriteShort( fp, 1 ); // nproperties
927 sgWriteShort( fp, end - start ); // nelements
929 sgWriteChar( fp, (char)SG_MATERIAL ); // property
930 sgWriteInt( fp, material.length() ); // nbytes
931 sgWriteBytes( fp, material.length(), material.c_str() );
934 for ( i = start; i < end; ++i ) {
936 sgWriteInt( fp, fans_v[i].size() * 2 * sizeof(short) );
937 for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
938 sgWriteShort( fp, (short)fans_v[i][j] );
939 sgWriteShort( fp, (short)fans_tc[i][j] );