Additional bug fixes after testing.

This commit is contained in:
curt
2001-01-05 00:04:20 +00:00
parent 70f495b4e1
commit 260dbeb3d2
4 changed files with 155 additions and 131 deletions
+95 -93
View File
@@ -28,222 +28,224 @@
static int read_error = false ; static int read_error = false ;
static int write_error = false ; static int write_error = false ;
int sgReadError (void) { return read_error ; } void sgClearReadError() { read_error = false; }
int sgWriteError (void) { return write_error ; } void sgClearWriteError() { write_error = false; }
int sgReadError() { return read_error ; }
int sgWriteError() { return write_error ; }
void sgReadChar ( gzFile fd, char *var ) void sgReadChar ( gzFile fd, char *var )
{ {
if ( gzread ( fd, var, sizeof(char) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(char) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteChar ( gzFile fd, const char var ) void sgWriteChar ( gzFile fd, const char var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadFloat ( gzFile fd, float *var ) void sgReadFloat ( gzFile fd, float *var )
{ {
if ( gzread ( fd, var, sizeof(float) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(float) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteFloat ( gzFile fd, const float var ) void sgWriteFloat ( gzFile fd, const float var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadDouble ( gzFile fd, double *var ) void sgReadDouble ( gzFile fd, double *var )
{ {
if ( gzread ( fd, var, sizeof(double) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(double) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteDouble ( gzFile fd, const double var ) void sgWriteDouble ( gzFile fd, const double var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadUInt ( gzFile fd, unsigned int *var ) void sgReadUInt ( gzFile fd, unsigned int *var )
{ {
if ( gzread ( fd, var, sizeof(unsigned int) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(unsigned int) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteUInt ( gzFile fd, const unsigned int var ) void sgWriteUInt ( gzFile fd, const unsigned int var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadInt ( gzFile fd, int *var ) void sgReadInt ( gzFile fd, int *var )
{ {
if ( gzread ( fd, var, sizeof(int) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(int) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteInt ( gzFile fd, const int var ) void sgWriteInt ( gzFile fd, const int var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadLong ( gzFile fd, long int *var ) void sgReadLong ( gzFile fd, long int *var )
{ {
if ( gzread ( fd, var, sizeof(long int) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(long int) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteLong ( gzFile fd, const long int var ) void sgWriteLong ( gzFile fd, const long int var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadUShort ( gzFile fd, unsigned short *var ) void sgReadUShort ( gzFile fd, unsigned short *var )
{ {
if ( gzread ( fd, var, sizeof(unsigned short) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(unsigned short) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteUShort ( gzFile fd, const unsigned short var ) void sgWriteUShort ( gzFile fd, const unsigned short var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadShort ( gzFile fd, short *var ) void sgReadShort ( gzFile fd, short *var )
{ {
if ( gzread ( fd, var, sizeof(short) ) == 1 ) return ; if ( gzread ( fd, var, sizeof(short) ) > 0 ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteShort ( gzFile fd, const short var ) void sgWriteShort ( gzFile fd, const short var )
{ {
if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) == 1 ) return ; if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) > 0 ) return ;
write_error = true ; write_error = true ;
} }
void sgReadFloat ( gzFile fd, const unsigned int n, float *var ) void sgReadFloat ( gzFile fd, const unsigned int n, float *var )
{ {
if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var ) void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
void sgReadDouble ( gzFile fd, const unsigned int n, double *var ) void sgReadDouble ( gzFile fd, const unsigned int n, double *var )
{ {
if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var ) void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
void sgReadBytes ( gzFile fd, const unsigned int n, void *var ) void sgReadBytes ( gzFile fd, const unsigned int n, void *var )
{ {
if ( n == 0) if ( n == 0)
return; return;
if ( gzread ( fd, var, n ) == 1 ) if ( gzread ( fd, var, n ) > 0 )
return ; return ;
read_error = true ; read_error = true ;
} }
void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var ) void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var )
{ {
if ( n == 0) if ( n == 0)
return; return;
if ( gzwrite ( fd, (void *)var, n ) == 1 ) if ( gzwrite ( fd, (void *)var, n ) > 0 )
return ; return ;
write_error = true ; write_error = true ;
} }
void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var ) void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var )
{ {
if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var ) void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
void sgReadShort ( gzFile fd, const unsigned int n, short *var ) void sgReadShort ( gzFile fd, const unsigned int n, short *var )
{ {
if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteShort ( gzFile fd, const unsigned int n, const short *var ) void sgWriteShort ( gzFile fd, const unsigned int n, const short *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var ) void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var )
{ {
if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var ) void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
void sgReadInt ( gzFile fd, const unsigned int n, int *var ) void sgReadInt ( gzFile fd, const unsigned int n, int *var )
{ {
if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ; if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ;
read_error = true ; read_error = true ;
} }
void sgWriteInt ( gzFile fd, const unsigned int n, const int *var ) void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
{ {
if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ; if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ;
write_error = true ; write_error = true ;
} }
@@ -252,41 +254,41 @@ void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
void sgReadString ( gzFile fd, char **var ) void sgReadString ( gzFile fd, char **var )
{ {
int i ; int i ;
char s [ MAX_ENTITY_NAME_LENGTH ] ; char s [ MAX_ENTITY_NAME_LENGTH ] ;
for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ ) for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ )
{ {
int c = gzgetc ( fd ) ; int c = gzgetc ( fd ) ;
s [ i ] = c ; s [ i ] = c ;
if ( c == '\0' ) if ( c == '\0' )
break ; break ;
} }
if ( i >= MAX_ENTITY_NAME_LENGTH-1 ) if ( i >= MAX_ENTITY_NAME_LENGTH-1 )
s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ; s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ;
if ( s[0] == '\0' ) if ( s[0] == '\0' )
*var = NULL ; *var = NULL ;
else else
{ {
*var = new char [ strlen(s)+1 ] ; *var = new char [ strlen(s)+1 ] ;
strcpy ( *var, s ) ; strcpy ( *var, s ) ;
} }
} }
void sgWriteString ( gzFile fd, const char *var ) void sgWriteString ( gzFile fd, const char *var )
{ {
if ( var != NULL ) { if ( var != NULL ) {
if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) == if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) ==
(int)(strlen(var) + 1) ) (int)(strlen(var) + 1) )
return ; return ;
} else { } else {
gzputc( fd, 0 ); gzputc( fd, 0 );
} }
} }
+4 -2
View File
@@ -81,8 +81,10 @@ void sgWriteVec4 ( gzFile fd, const sgVec4 var ) ;
void sgReadMat4 ( gzFile fd, sgMat4 var ) ; void sgReadMat4 ( gzFile fd, sgMat4 var ) ;
void sgWriteMat4 ( gzFile fd, const sgMat4 var ) ; void sgWriteMat4 ( gzFile fd, const sgMat4 var ) ;
int sgReadError ( void ) ; void sgClearReadError();
int sgWriteError ( void ) ; void sgClearWriteError();
int sgReadError();
int sgWriteError();
#endif // _SG_LOWLEVEL_HXX #endif // _SG_LOWLEVEL_HXX
+51 -32
View File
@@ -110,7 +110,7 @@ double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
// write out the structures to an ASCII file. We assume that the // write out the structures to an ASCII file. We assume that the
// groups come to us sorted by material property. If not, things // groups come to us sorted by material property. If not, things
// don't break, but the result won't be as optimal. // don't break, but the result won't be as optimal.
void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
Point3D gbs_center, float gbs_radius, Point3D gbs_center, float gbs_radius,
const point_list& wgs84_nodes, const point_list& normals, const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords, const point_list& texcoords,
@@ -139,7 +139,7 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
FILE *fp; FILE *fp;
if ( (fp = fopen( file.c_str(), "w" )) == NULL ) { if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
cout << "ERROR: opening " << file << " for writing!" << endl; cout << "ERROR: opening " << file << " for writing!" << endl;
exit(-1); return false;
} }
cout << "triangles size = " << tris_v.size() << " tri_materials = " cout << "triangles size = " << tris_v.size() << " tri_materials = "
@@ -299,11 +299,13 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
command = "gzip --force --best " + file; command = "gzip --force --best " + file;
system(command.c_str()); system(command.c_str());
return true;
} }
// read a binary file and populate the provided structures. // read a binary file and populate the provided structures.
void sgReadBinObj( const string& file, bool sgReadBinObj( const string& file,
Point3D &gbs_center, float *gbs_radius, Point3D &gbs_center, float *gbs_radius,
point_list& wgs84_nodes, point_list& normals, point_list& wgs84_nodes, point_list& normals,
point_list& texcoords, point_list& texcoords,
@@ -315,7 +317,8 @@ void sgReadBinObj( const string& file,
string_list& fan_materials ) string_list& fan_materials )
{ {
Point3D p; Point3D p;
int i, j, k, m; int i, j, k;
char material[256];
// zero out structures // zero out structures
wgs84_nodes.clear(); wgs84_nodes.clear();
@@ -342,19 +345,23 @@ void sgReadBinObj( const string& file,
if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) { if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
cout << "ERROR: opening " << file << " or " << filegz cout << "ERROR: opening " << file << " or " << filegz
<< "for reading!" << endl; << "for reading!" << endl;
exit(-1); return false;
} }
} }
sgClearReadError();
// read headers // read headers
int header, version; int header, version;
sgReadInt( fp, &header ); sgReadInt( fp, &header );
if ( ((header & 0xFF000000) >> 24) == 'T' && if ( ((header & 0xFF000000) >> 24) == 'S' &&
((header & 0x00FF0000) >> 16) == 'G' ) { ((header & 0x00FF0000) >> 16) == 'G' ) {
cout << "Good header" << endl; cout << "Good header" << endl;
// read file version // read file version
version = (header & 0x0000FFFF); version = (header & 0x0000FFFF);
cout << "File version = " << version << endl; cout << "File version = " << version << endl;
} else {
return false;
} }
// read creation time // read creation time
@@ -470,8 +477,9 @@ void sgReadBinObj( const string& file,
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
int count = nbytes / 3; int count = nbytes / 3;
for ( k = 0; k < count; ++k ) { for ( k = 0; k < count; ++k ) {
p = Point3D( ptr[0] / 256.0, ptr[1] / 256.0, p = Point3D( ptr[0] / 128.0 - 1.0,
ptr[2] / 256.0 ); ptr[1] / 128.0 - 1.0,
ptr[2] / 128.0 - 1.0 );
cout << "normal = " << p << endl; cout << "normal = " << p << endl;
normals.push_back( p ); normals.push_back( p );
ptr += 3; ptr += 3;
@@ -521,11 +529,9 @@ void sgReadBinObj( const string& file,
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) { if ( prop_type == SG_MATERIAL ) {
char material[256];
strncpy( material, ptr, nbytes ); strncpy( material, ptr, nbytes );
material[nbytes] = '\0'; material[nbytes] = '\0';
cout << "material type = " << material << endl; cout << "material type = " << material << endl;
tri_materials.push_back( material );
} }
} }
@@ -537,21 +543,20 @@ void sgReadBinObj( const string& file,
char buf[nbytes]; char buf[nbytes];
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
int count = nbytes / (sizeof(short) * 2 * 3); int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr; short *sptr = (short *)ptr;
int_list vs, tcs; int_list vs, tcs;
vs.clear(); tcs.clear();
for ( k = 0; k < count; ++k ) { for ( k = 0; k < count; ++k ) {
vs.clear(); tcs.clear(); vs.push_back( sptr[0] );
for ( m = 0; m < 3; ++m ) { tcs.push_back( sptr[1] );
vs.push_back( sptr[0] ); cout << sptr[0] << "/" << sptr[1] << " ";
tcs.push_back( sptr[1] ); sptr += 2;
cout << sptr[0] << "/" << sptr[1] << " ";
sptr += 2;
}
cout << endl;
tris_v.push_back( vs );
tris_tc.push_back( tcs );
} }
cout << endl;
tris_v.push_back( vs );
tris_tc.push_back( tcs );
tri_materials.push_back( material );
} }
} else if ( obj_type == SG_TRIANGLE_STRIPS ) { } else if ( obj_type == SG_TRIANGLE_STRIPS ) {
// read triangle strip properties // read triangle strip properties
@@ -566,11 +571,9 @@ void sgReadBinObj( const string& file,
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) { if ( prop_type == SG_MATERIAL ) {
char material[256];
strncpy( material, ptr, nbytes ); strncpy( material, ptr, nbytes );
material[nbytes] = '\0'; material[nbytes] = '\0';
cout << "material type = " << material << endl; cout << "material type = " << material << endl;
strip_materials.push_back( material );
} }
} }
@@ -582,7 +585,7 @@ void sgReadBinObj( const string& file,
char buf[nbytes]; char buf[nbytes];
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
int count = nbytes / (sizeof(short) * 2 * 3); int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr; short *sptr = (short *)ptr;
int_list vs, tcs; int_list vs, tcs;
vs.clear(); tcs.clear(); vs.clear(); tcs.clear();
@@ -595,6 +598,7 @@ void sgReadBinObj( const string& file,
cout << endl; cout << endl;
strips_v.push_back( vs ); strips_v.push_back( vs );
strips_tc.push_back( tcs ); strips_tc.push_back( tcs );
strip_materials.push_back( material );
} }
} else if ( obj_type == SG_TRIANGLE_FANS ) { } else if ( obj_type == SG_TRIANGLE_FANS ) {
// read triangle fan properties // read triangle fan properties
@@ -609,11 +613,9 @@ void sgReadBinObj( const string& file,
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) { if ( prop_type == SG_MATERIAL ) {
char material[256];
strncpy( material, ptr, nbytes ); strncpy( material, ptr, nbytes );
material[nbytes] = '\0'; material[nbytes] = '\0';
cout << "material type = " << material << endl; cout << "material type = " << material << endl;
fan_materials.push_back( material );
} }
} }
@@ -625,7 +627,7 @@ void sgReadBinObj( const string& file,
char buf[nbytes]; char buf[nbytes];
char *ptr = buf; char *ptr = buf;
sgReadBytes( fp, nbytes, ptr ); sgReadBytes( fp, nbytes, ptr );
int count = nbytes / (sizeof(short) * 2 * 3); int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr; short *sptr = (short *)ptr;
int_list vs, tcs; int_list vs, tcs;
vs.clear(); tcs.clear(); vs.clear(); tcs.clear();
@@ -638,6 +640,7 @@ void sgReadBinObj( const string& file,
cout << endl; cout << endl;
fans_v.push_back( vs ); fans_v.push_back( vs );
fans_tc.push_back( tcs ); fans_tc.push_back( tcs );
fan_materials.push_back( material );
} }
} else { } else {
// unknown object type, just skip // unknown object type, just skip
@@ -669,13 +672,20 @@ void sgReadBinObj( const string& file,
// close the file // close the file
gzclose(fp); gzclose(fp);
if ( sgReadError() ) {
cout << "We detected an error while reading the file." << endl;
return false;
}
return true;
} }
// write out the structures to a binary file. We assume that the // write out the structures to a binary file. We assume that the
// groups come to us sorted by material property. If not, things // groups come to us sorted by material property. If not, things
// don't break, but the result won't be as optimal. // don't break, but the result won't be as optimal.
void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
Point3D gbs_center, float gbs_radius, Point3D gbs_center, float gbs_radius,
const point_list& wgs84_nodes, const point_list& normals, const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords, const point_list& texcoords,
@@ -705,9 +715,11 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
gzFile fp; gzFile fp;
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) { if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
cout << "ERROR: opening " << file << " for writing!" << endl; cout << "ERROR: opening " << file << " for writing!" << endl;
exit(-1); return false;
} }
sgClearWriteError();
cout << "triangles size = " << tris_v.size() << " tri_materials = " cout << "triangles size = " << tris_v.size() << " tri_materials = "
<< tri_materials.size() << endl; << tri_materials.size() << endl;
cout << "strips size = " << strips_v.size() << " strip_materials = " cout << "strips size = " << strips_v.size() << " strip_materials = "
@@ -808,9 +820,9 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
char normal[3]; char normal[3];
for ( i = 0; i < (int)normals.size(); ++i ) { for ( i = 0; i < (int)normals.size(); ++i ) {
p = normals[i]; p = normals[i];
normal[0] = (char)(p.x() * 256); normal[0] = (char)((p.x() + 1.0) * 128);
normal[1] = (char)(p.y() * 256); normal[1] = (char)((p.y() + 1.0) * 128);
normal[2] = (char)(p.z() * 256); normal[2] = (char)((p.z() + 1.0) * 128);
sgWriteBytes( fp, 3, normal ); sgWriteBytes( fp, 3, normal );
} }
@@ -947,4 +959,11 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
// close the file // close the file
gzclose(fp); gzclose(fp);
if ( sgWriteError() ) {
cout << "We detected an error while writing the file." << endl;
return false;
}
return true;
} }
+5 -4
View File
@@ -34,6 +34,8 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/constants.h> #include <simgear/constants.h>
#include <simgear/math/sg_types.hxx>
#include <simgear/bucket/newbucket.hxx>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
@@ -41,7 +43,6 @@
#include <list> #include <list>
#include STL_STRING #include STL_STRING
#include <simgear/math/sg_types.hxx>
typedef vector < int_list > group_list; typedef vector < int_list > group_list;
@@ -79,7 +80,7 @@ double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes );
// write out the structures to an ASCII file. We assume that the // write out the structures to an ASCII file. We assume that the
// groups come to us sorted by material property. If not, things // groups come to us sorted by material property. If not, things
// don't break, but the result won't be as optimal. // don't break, but the result won't be as optimal.
void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
Point3D gbs_center, float gbs_radius, Point3D gbs_center, float gbs_radius,
const point_list& wgs84_nodes, const point_list& normals, const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords, const point_list& texcoords,
@@ -92,7 +93,7 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b,
// read a binary file object and populate the provided structures. // read a binary file object and populate the provided structures.
void sgReadBinObj( const string& file, bool sgReadBinObj( const string& file,
Point3D &gbs_center, float *gbs_radius, Point3D &gbs_center, float *gbs_radius,
point_list& wgs84_nodes, point_list& normals, point_list& wgs84_nodes, point_list& normals,
point_list& texcoords, point_list& texcoords,
@@ -106,7 +107,7 @@ void sgReadBinObj( const string& file,
// write out the structures to a binary file. We assume that the // write out the structures to a binary file. We assume that the
// groups come to us sorted by material property. If not, things // groups come to us sorted by material property. If not, things
// don't break, but the result won't be as optimal. // don't break, but the result won't be as optimal.
void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b,
Point3D gbs_center, float gbs_radius, Point3D gbs_center, float gbs_radius,
const point_list& wgs84_nodes, const point_list& normals, const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords, const point_list& texcoords,