static int read_error = false ;
static int write_error = false ;
-int sgReadError (void) { return read_error ; }
-int sgWriteError (void) { return write_error ; }
+void sgClearReadError() { read_error = false; }
+void sgClearWriteError() { write_error = false; }
+int sgReadError() { return read_error ; }
+int sgWriteError() { return write_error ; }
void sgReadChar ( gzFile fd, char *var )
{
- if ( gzread ( fd, var, sizeof(char) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(char) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteChar ( gzFile fd, const char var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadFloat ( gzFile fd, float *var )
{
- if ( gzread ( fd, var, sizeof(float) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(float) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteFloat ( gzFile fd, const float var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadDouble ( gzFile fd, double *var )
{
- if ( gzread ( fd, var, sizeof(double) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(double) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteDouble ( gzFile fd, const double var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadUInt ( gzFile fd, unsigned int *var )
{
- if ( gzread ( fd, var, sizeof(unsigned int) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(unsigned int) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteUInt ( gzFile fd, const unsigned int var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadInt ( gzFile fd, int *var )
{
- if ( gzread ( fd, var, sizeof(int) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(int) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteInt ( gzFile fd, const int var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadLong ( gzFile fd, long int *var )
{
- if ( gzread ( fd, var, sizeof(long int) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(long int) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteLong ( gzFile fd, const long int var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadUShort ( gzFile fd, unsigned short *var )
{
- if ( gzread ( fd, var, sizeof(unsigned short) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(unsigned short) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteUShort ( gzFile fd, const unsigned short var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadShort ( gzFile fd, short *var )
{
- if ( gzread ( fd, var, sizeof(short) ) == 1 ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(short) ) > 0 ) return ;
+ read_error = true ;
}
void sgWriteShort ( gzFile fd, const short var )
{
- if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) == 1 ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) > 0 ) return ;
+ write_error = true ;
}
void sgReadFloat ( gzFile fd, const unsigned int n, float *var )
{
- if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadDouble ( gzFile fd, const unsigned int n, double *var )
{
- if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadBytes ( gzFile fd, const unsigned int n, void *var )
{
- if ( n == 0)
- return;
- if ( gzread ( fd, var, n ) == 1 )
- return ;
- read_error = true ;
+ if ( n == 0)
+ return;
+ if ( gzread ( fd, var, n ) > 0 )
+ return ;
+ read_error = true ;
}
void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var )
{
- if ( n == 0)
- return;
- if ( gzwrite ( fd, (void *)var, n ) == 1 )
- return ;
- write_error = true ;
+ if ( n == 0)
+ return;
+ if ( gzwrite ( fd, (void *)var, n ) > 0 )
+ return ;
+ write_error = true ;
}
void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var )
{
- if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadShort ( gzFile fd, const unsigned int n, short *var )
{
- if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteShort ( gzFile fd, const unsigned int n, const short *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var )
{
- if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadInt ( gzFile fd, const unsigned int n, int *var )
{
- if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ;
- read_error = true ;
+ if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ;
+ read_error = true ;
}
void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
{
- if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ;
- write_error = true ;
+ if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ;
+ write_error = true ;
}
void sgReadString ( gzFile fd, char **var )
{
- int i ;
- char s [ MAX_ENTITY_NAME_LENGTH ] ;
+ int i ;
+ char s [ MAX_ENTITY_NAME_LENGTH ] ;
- for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ )
- {
- int c = gzgetc ( fd ) ;
- s [ i ] = c ;
+ for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ )
+ {
+ int c = gzgetc ( fd ) ;
+ s [ i ] = c ;
- if ( c == '\0' )
- break ;
- }
+ if ( c == '\0' )
+ break ;
+ }
- if ( i >= MAX_ENTITY_NAME_LENGTH-1 )
- s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ;
+ if ( i >= MAX_ENTITY_NAME_LENGTH-1 )
+ s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ;
- if ( s[0] == '\0' )
- *var = NULL ;
- else
- {
- *var = new char [ strlen(s)+1 ] ;
- strcpy ( *var, s ) ;
- }
+ if ( s[0] == '\0' )
+ *var = NULL ;
+ else
+ {
+ *var = new char [ strlen(s)+1 ] ;
+ strcpy ( *var, s ) ;
+ }
}
void sgWriteString ( gzFile fd, const char *var )
{
- if ( var != NULL ) {
- if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) ==
- (int)(strlen(var) + 1) )
- return ;
- } else {
- gzputc( fd, 0 );
- }
+ if ( var != NULL ) {
+ if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) ==
+ (int)(strlen(var) + 1) )
+ return ;
+ } else {
+ gzputc( fd, 0 );
+ }
}
// write out the structures to an ASCII file. We assume that the
// groups come to us sorted by material property. If not, things
// 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,
const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords,
FILE *fp;
if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
cout << "ERROR: opening " << file << " for writing!" << endl;
- exit(-1);
+ return false;
}
cout << "triangles size = " << tris_v.size() << " tri_materials = "
command = "gzip --force --best " + file;
system(command.c_str());
+
+ return true;
}
// 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,
point_list& wgs84_nodes, point_list& normals,
point_list& texcoords,
string_list& fan_materials )
{
Point3D p;
- int i, j, k, m;
+ int i, j, k;
+ char material[256];
// zero out structures
wgs84_nodes.clear();
if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
cout << "ERROR: opening " << file << " or " << filegz
<< "for reading!" << endl;
- exit(-1);
+ return false;
}
}
+ sgClearReadError();
+
// read headers
int header, version;
sgReadInt( fp, &header );
- if ( ((header & 0xFF000000) >> 24) == 'T' &&
+ if ( ((header & 0xFF000000) >> 24) == 'S' &&
((header & 0x00FF0000) >> 16) == 'G' ) {
cout << "Good header" << endl;
// read file version
version = (header & 0x0000FFFF);
cout << "File version = " << version << endl;
+ } else {
+ return false;
}
// read creation time
sgReadBytes( fp, nbytes, ptr );
int count = nbytes / 3;
for ( k = 0; k < count; ++k ) {
- p = Point3D( ptr[0] / 256.0, ptr[1] / 256.0,
- ptr[2] / 256.0 );
+ p = Point3D( ptr[0] / 128.0 - 1.0,
+ ptr[1] / 128.0 - 1.0,
+ ptr[2] / 128.0 - 1.0 );
cout << "normal = " << p << endl;
normals.push_back( p );
ptr += 3;
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) {
- char material[256];
strncpy( material, ptr, nbytes );
material[nbytes] = '\0';
cout << "material type = " << material << endl;
- tri_materials.push_back( material );
}
}
char buf[nbytes];
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
- int count = nbytes / (sizeof(short) * 2 * 3);
+ int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr;
int_list vs, tcs;
+ vs.clear(); tcs.clear();
for ( k = 0; k < count; ++k ) {
- vs.clear(); tcs.clear();
- for ( m = 0; m < 3; ++m ) {
- vs.push_back( sptr[0] );
- tcs.push_back( sptr[1] );
- cout << sptr[0] << "/" << sptr[1] << " ";
- sptr += 2;
- }
- cout << endl;
- tris_v.push_back( vs );
- tris_tc.push_back( tcs );
+ vs.push_back( sptr[0] );
+ tcs.push_back( sptr[1] );
+ cout << sptr[0] << "/" << sptr[1] << " ";
+ sptr += 2;
}
+ cout << endl;
+ tris_v.push_back( vs );
+ tris_tc.push_back( tcs );
+ tri_materials.push_back( material );
}
} else if ( obj_type == SG_TRIANGLE_STRIPS ) {
// read triangle strip properties
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) {
- char material[256];
strncpy( material, ptr, nbytes );
material[nbytes] = '\0';
cout << "material type = " << material << endl;
- strip_materials.push_back( material );
}
}
char buf[nbytes];
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
- int count = nbytes / (sizeof(short) * 2 * 3);
+ int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr;
int_list vs, tcs;
vs.clear(); tcs.clear();
cout << endl;
strips_v.push_back( vs );
strips_tc.push_back( tcs );
+ strip_materials.push_back( material );
}
} else if ( obj_type == SG_TRIANGLE_FANS ) {
// read triangle fan properties
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
if ( prop_type == SG_MATERIAL ) {
- char material[256];
strncpy( material, ptr, nbytes );
material[nbytes] = '\0';
cout << "material type = " << material << endl;
- fan_materials.push_back( material );
}
}
char buf[nbytes];
char *ptr = buf;
sgReadBytes( fp, nbytes, ptr );
- int count = nbytes / (sizeof(short) * 2 * 3);
+ int count = nbytes / (sizeof(short) * 2);
short *sptr = (short *)ptr;
int_list vs, tcs;
vs.clear(); tcs.clear();
cout << endl;
fans_v.push_back( vs );
fans_tc.push_back( tcs );
+ fan_materials.push_back( material );
}
} else {
// unknown object type, just skip
// close the file
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
// groups come to us sorted by material property. If not, things
// 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,
const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords,
gzFile fp;
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
cout << "ERROR: opening " << file << " for writing!" << endl;
- exit(-1);
+ return false;
}
+ sgClearWriteError();
+
cout << "triangles size = " << tris_v.size() << " tri_materials = "
<< tri_materials.size() << endl;
cout << "strips size = " << strips_v.size() << " strip_materials = "
char normal[3];
for ( i = 0; i < (int)normals.size(); ++i ) {
p = normals[i];
- normal[0] = (char)(p.x() * 256);
- normal[1] = (char)(p.y() * 256);
- normal[2] = (char)(p.z() * 256);
+ normal[0] = (char)((p.x() + 1.0) * 128);
+ normal[1] = (char)((p.y() + 1.0) * 128);
+ normal[2] = (char)((p.z() + 1.0) * 128);
sgWriteBytes( fp, 3, normal );
}
// close the file
gzclose(fp);
+
+ if ( sgWriteError() ) {
+ cout << "We detected an error while writing the file." << endl;
+ return false;
+ }
+
+ return true;
}
#include <simgear/compiler.h>
#include <simgear/constants.h>
+#include <simgear/math/sg_types.hxx>
+#include <simgear/bucket/newbucket.hxx>
#include <stdio.h>
#include <time.h>
#include <list>
#include STL_STRING
-#include <simgear/math/sg_types.hxx>
typedef vector < int_list > group_list;
// write out the structures to an ASCII file. We assume that the
// groups come to us sorted by material property. If not, things
// 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,
const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords,
// 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,
point_list& wgs84_nodes, point_list& normals,
point_list& texcoords,
// write out the structures to a binary file. We assume that the
// groups come to us sorted by material property. If not, things
// 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,
const point_list& wgs84_nodes, const point_list& normals,
const point_list& texcoords,