Commit 1afb0498 authored by dickelbeck's avatar dickelbeck

beautify

parent 4eac2675
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: 3d_read_mesh.cpp // Name: 3d_read_mesh.cpp
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
...@@ -22,155 +23,162 @@ ...@@ -22,155 +23,162 @@
int Struct3D_Master:: ReadData() int Struct3D_Master:: ReadData()
/************************************/ /************************************/
{ {
char line[1024], *text; char line[1024], * text;
wxString fullfilename; wxString fullfilename;
FILE * file; FILE* file;
int LineNum = 0; int LineNum = 0;
if ( m_Shape3DName.IsEmpty() ) if( m_Shape3DName.IsEmpty() )
{ {
return 1; return 1;
} }
if ( wxIsAbsolutePath(m_Shape3DName) ) fullfilename.Empty(); if( wxIsAbsolutePath( m_Shape3DName ) )
else fullfilename = g_RealLibDirBuffer + LIB3D_PATH; fullfilename.Empty();
else
fullfilename = g_RealLibDirBuffer + LIB3D_PATH;
fullfilename += m_Shape3DName; fullfilename += m_Shape3DName;
#if defined (__WINDOWS__) #if defined (__WINDOWS__)
fullfilename.Replace(UNIX_STRING_DIR_SEP,WIN_STRING_DIR_SEP); fullfilename.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
#else #else
#if defined (__UNIX__) #if defined (__UNIX__)
fullfilename.Replace(WIN_STRING_DIR_SEP,UNIX_STRING_DIR_SEP); fullfilename.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
#endif #endif
#endif #endif
file = wxFopen( fullfilename, wxT("rt") ); file = wxFopen( fullfilename, wxT( "rt" ) );
if ( file == NULL ) if( file == NULL )
{ {
return -1; return -1;
} }
// Switch the locale to standard C (needed to print floating point numbers like 1.3) // Switch the locale to standard C (needed to print floating point numbers like 1.3)
setlocale(LC_NUMERIC, "C"); setlocale( LC_NUMERIC, "C" );
while ( GetLine(file, line, &LineNum, 512) ) while( GetLine( file, line, &LineNum, 512 ) )
{ {
text = strtok(line, " \t\n\r"); text = strtok( line, " \t\n\r" );
if ( stricmp (text, "DEF" ) == 0 ) if( stricmp( text, "DEF" ) == 0 )
{ {
while ( GetLine(file, line, &LineNum, 512) ) while( GetLine( file, line, &LineNum, 512 ) )
{ {
text = strtok(line, " \t\n\r"); text = strtok( line, " \t\n\r" );
if ( text == NULL ) continue; if( text == NULL )
if ( * text == '}' ) break; continue;
if ( stricmp (text, "children") == 0 ) if( *text == '}' )
break;
if( stricmp( text, "children" ) == 0 )
{ {
ReadChildren(file, &LineNum); ReadChildren( file, &LineNum );
} }
} }
} }
} }
fclose (file); fclose( file );
setlocale(LC_NUMERIC, ""); // revert to the current locale setlocale( LC_NUMERIC, "" ); // revert to the current locale
return 0; return 0;
} }
/*********************************************************/ /*********************************************************/
int Struct3D_Master:: ReadMaterial(FILE * file, int *LineNum) int Struct3D_Master:: ReadMaterial( FILE* file, int* LineNum )
/*********************************************************/ /*********************************************************/
/* /*
analyse la description du type: * analyse la description du type:
material DEF yellow Material { * material DEF yellow Material {
diffuseColor 1.00000 1.00000 0.00000e+0 * diffuseColor 1.00000 1.00000 0.00000e+0
emissiveColor 0.00000e+0 0.00000e+0 0.00000e+0 * emissiveColor 0.00000e+0 0.00000e+0 0.00000e+0
specularColor 1.00000 1.00000 1.00000 * specularColor 1.00000 1.00000 1.00000
ambientIntensity 1.00000 * ambientIntensity 1.00000
transparency 0.00000e+0 * transparency 0.00000e+0
shininess 1.00000 * shininess 1.00000
} * }
ou du type: * ou du type:
material USE yellow * material USE yellow
*/ */
{ {
char line[512], * text, * command; char line[512], * text, * command;
wxString mat_name; wxString mat_name;
S3D_Material * material = NULL; S3D_Material* material = NULL;
// Lecture de la commande: // Lecture de la commande:
command = strtok(NULL, " \t\n\r"); command = strtok( NULL, " \t\n\r" );
text = strtok(NULL, " \t\n\r"); text = strtok( NULL, " \t\n\r" );
mat_name = CONV_FROM_UTF8(text); mat_name = CONV_FROM_UTF8( text );
if ( stricmp(command, "USE") == 0 ) if( stricmp( command, "USE" ) == 0 )
{ {
for( material = m_Materials; material != NULL;
for ( material = m_Materials; material != NULL; material = (S3D_Material*) material->Pnext )
material = (S3D_Material *) material->Pnext)
{ {
if ( material->m_Name == mat_name) if( material->m_Name == mat_name )
{ {
material->SetMaterial(); material->SetMaterial();
return 1; return 1;
} }
} }
printf("ReadMaterial error: material not found\n"); printf( "ReadMaterial error: material not found\n" );
return 0; return 0;
} }
if ( stricmp(command, "DEF") == 0 ) if( stricmp( command, "DEF" ) == 0 )
{ {
material = new S3D_Material(this, mat_name); material = new S3D_Material( this, mat_name );
material->Pnext = m_Materials; material->Pnext = m_Materials;
m_Materials = material; m_Materials = material;
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line," \t\n\r"); text = strtok( line, " \t\n\r" );
if ( text == NULL ) continue; if( text == NULL )
if ( text[0] == '}' ) continue;
if( text[0] == '}' )
{ {
material->SetMaterial(); material->SetMaterial();
return 0; return 0;
} }
if ( stricmp (text, "diffuseColor") == 0 ) if( stricmp( text, "diffuseColor" ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.x = atof(text); material->m_DiffuseColor.x = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.y = atof(text); material->m_DiffuseColor.y = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_DiffuseColor.z = atof(text); material->m_DiffuseColor.z = atof( text );
} }
else if ( stricmp (text, "emissiveColor") == 0 ) else if( stricmp( text, "emissiveColor" ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.x = atof(text); material->m_EmissiveColor.x = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.y = atof(text); material->m_EmissiveColor.y = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_EmissiveColor.z = atof(text); material->m_EmissiveColor.z = atof( text );
} }
else if ( strnicmp (text, "specularColor", 13 ) == 0 ) else if( strnicmp( text, "specularColor", 13 ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.x = atof(text); material->m_SpecularColor.x = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.y = atof(text); material->m_SpecularColor.y = atof( text );
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_SpecularColor.z = atof(text); material->m_SpecularColor.z = atof( text );
} }
else if ( strnicmp (text, "ambientIntensity", 16 ) == 0 ) else if( strnicmp( text, "ambientIntensity", 16 ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_AmbientIntensity = atof(text); material->m_AmbientIntensity = atof( text );
} }
else if ( strnicmp (text, "transparency", 12 ) == 0 ) else if( strnicmp( text, "transparency", 12 ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_Transparency = atof(text); material->m_Transparency = atof( text );
} }
else if ( strnicmp (text, "shininess", 9 ) == 0 ) else if( strnicmp( text, "shininess", 9 ) == 0 )
{ {
text = strtok(NULL," \t\n\r"); text = strtok( NULL, " \t\n\r" );
material->m_Shininess = atof(text); material->m_Shininess = atof( text );
} }
} }
} }
...@@ -179,57 +187,61 @@ S3D_Material * material = NULL; ...@@ -179,57 +187,61 @@ S3D_Material * material = NULL;
/**********************************************************/ /**********************************************************/
int Struct3D_Master::ReadChildren(FILE * file, int *LineNum) int Struct3D_Master::ReadChildren( FILE* file, int* LineNum )
/***********************************************************/ /***********************************************************/
{ {
char line[1024], *text; char line[1024], * text;
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line, " \t\n\r"); text = strtok( line, " \t\n\r" );
if ( * text == ']' ) return 0; if( *text == ']' )
if ( * text == ',' ) continue; return 0;
if( *text == ',' )
continue;
if ( stricmp (text, "Shape" ) == 0 ) if( stricmp( text, "Shape" ) == 0 )
{ {
ReadShape(file, LineNum); ReadShape( file, LineNum );
} }
else else
{ {
printf ("ReadChildren error line %d <%s> \n", *LineNum, text); printf( "ReadChildren error line %d <%s> \n", *LineNum, text );
break; break;
} }
} }
return 1; return 1;
} }
/********************************************************/ /********************************************************/
int Struct3D_Master::ReadShape(FILE * file, int *LineNum) int Struct3D_Master::ReadShape( FILE* file, int* LineNum )
/********************************************************/ /********************************************************/
{ {
char line[1024], *text; char line[1024], * text;
int err = 1; int err = 1;
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line, " \t\n\r"); text = strtok( line, " \t\n\r" );
if ( * text == '}' ) if( *text == '}' )
{ {
err = 0; err = 0;
break; break;
} }
if ( stricmp (text, "appearance" ) == 0 ) if( stricmp( text, "appearance" ) == 0 )
{ {
ReadAppearance(file, LineNum); ReadAppearance( file, LineNum );
} }
else if ( stricmp (text, "geometry" ) == 0 ) else if( stricmp( text, "geometry" ) == 0 )
{ {
ReadGeometry(file, LineNum); ReadGeometry( file, LineNum );
} }
else else
{ {
printf ("ReadShape error line %d <%s> \n", *LineNum, text); printf( "ReadShape error line %d <%s> \n", *LineNum, text );
break; break;
} }
} }
...@@ -237,28 +249,29 @@ int err = 1; ...@@ -237,28 +249,29 @@ int err = 1;
return err; return err;
} }
/*************************************************************/ /*************************************************************/
int Struct3D_Master::ReadAppearance(FILE * file, int *LineNum) int Struct3D_Master::ReadAppearance( FILE* file, int* LineNum )
/*************************************************************/ /*************************************************************/
{ {
char line[1024], *text; char line[1024], * text;
int err = 1; int err = 1;
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line, " \t\n\r"); text = strtok( line, " \t\n\r" );
if ( * text == '}' ) if( *text == '}' )
{ {
err = 0; break; err = 0; break;
} }
if ( stricmp (text, "material" ) == 0 ) if( stricmp( text, "material" ) == 0 )
{ {
ReadMaterial(file, LineNum ); ReadMaterial( file, LineNum );
} }
else else
{ {
printf ("ReadAppearance error line %d <%s> \n", *LineNum, text); printf( "ReadAppearance error line %d <%s> \n", *LineNum, text );
break; break;
} }
} }
...@@ -266,54 +279,56 @@ int err = 1; ...@@ -266,54 +279,56 @@ int err = 1;
return err; return err;
} }
#define BUFSIZE 2000 #define BUFSIZE 2000
/************************************************************************************/ /************************************************************************************/
double * ReadCoordsList(FILE * file, char * text_buffer, int * bufsize, int *LineNum) double* ReadCoordsList( FILE* file, char* text_buffer, int* bufsize, int* LineNum )
/************************************************************************************/ /************************************************************************************/
/* Read a coordinate liste like: /* Read a coordinate liste like:
coord Coordinate { point [ * coord Coordinate { point [
-5.24489 6.57640e-3 -9.42129e-2, * -5.24489 6.57640e-3 -9.42129e-2,
-5.11821 6.57421e-3 0.542654, * -5.11821 6.57421e-3 0.542654,
-3.45868 0.256565 1.32000 ] } * -3.45868 0.256565 1.32000 ] }
or: * or:
normal Normal { vector [ * normal Normal { vector [
0.995171 -6.08102e-6 9.81541e-2, * 0.995171 -6.08102e-6 9.81541e-2,
0.923880 -4.09802e-6 0.382683, * 0.923880 -4.09802e-6 0.382683,
0.707107 -9.38186e-7 0.707107] * 0.707107 -9.38186e-7 0.707107]
} * }
*
Return the coordinate list * Return the coordinate list
text_buffer contains the first line of this node : * text_buffer contains the first line of this node :
"coord Coordinate { point [" * "coord Coordinate { point ["
*/ */
{ {
double * data_list = NULL; double* data_list = NULL;
unsigned int ii = 0, jj = 0, nn = BUFSIZE; unsigned int ii = 0, jj = 0, nn = BUFSIZE;
char * text; char* text;
bool HasData = FALSE; bool HasData = FALSE;
bool StartData = FALSE; bool StartData = FALSE;
bool EndData = FALSE; bool EndData = FALSE;
bool EndNode = FALSE; bool EndNode = FALSE;
char string_num[512]; char string_num[512];
text = text_buffer; text = text_buffer;
while ( !EndNode ) while( !EndNode )
{ {
if ( * text == 0 ) // Needs data ! if( *text == 0 ) // Needs data !
{ {
text = text_buffer; text = text_buffer;
GetLine(file, text_buffer, LineNum, 512); GetLine( file, text_buffer, LineNum, 512 );
} }
while ( !EndNode && *text ) while( !EndNode && *text )
{ {
switch ( *text ) switch( *text )
{ {
case '[': case '[':
StartData = TRUE; StartData = TRUE;
jj = 0; string_num[jj] = 0; jj = 0; string_num[jj] = 0;
data_list = (double *) MyZMalloc(nn * sizeof(double) ); data_list = (double*) MyZMalloc( nn * sizeof(double) );
break; break;
case '}': case '}':
...@@ -325,17 +340,18 @@ char string_num[512]; ...@@ -325,17 +340,18 @@ char string_num[512];
case ' ': case ' ':
case ',': case ',':
jj = 0; jj = 0;
if ( ! StartData || !HasData) break; if( !StartData || !HasData )
data_list[ii] = atof(string_num); break;
data_list[ii] = atof( string_num );
string_num[jj] = 0; string_num[jj] = 0;
ii++; ii++;
if ( ii >= nn ) if( ii >= nn )
{ {
nn *= 2; nn *= 2;
data_list = (double *) realloc(data_list, (nn * sizeof(double)) ); data_list = (double*) realloc( data_list, ( nn * sizeof(double) ) );
} }
HasData = FALSE; HasData = FALSE;
if ( *text == ']' ) if( *text == ']' )
{ {
StartData = FALSE; StartData = FALSE;
EndData = TRUE; EndData = TRUE;
...@@ -343,46 +359,51 @@ char string_num[512]; ...@@ -343,46 +359,51 @@ char string_num[512];
break; break;
default: default:
if ( ! StartData ) break; if( !StartData )
if ( jj >= sizeof(string_num) ) break; break;
if( jj >= sizeof(string_num) )
break;
string_num[jj] = *text; string_num[jj] = *text;
jj++; string_num[jj] = 0; jj++; string_num[jj] = 0;
HasData = TRUE; HasData = TRUE;
break; break;
} }
text++; text++;
} }
} }
if ( data_list ) if( data_list )
data_list = (double *) realloc(data_list, (ii * sizeof(double)) ); data_list = (double*) realloc( data_list, ( ii * sizeof(double) ) );
if ( bufsize ) *bufsize = ii; if( bufsize )
*bufsize = ii;
return data_list; return data_list;
} }
/***********************************************************/ /***********************************************************/
int Struct3D_Master::ReadGeometry(FILE * file, int *LineNum) int Struct3D_Master::ReadGeometry( FILE* file, int* LineNum )
/***********************************************************/ /***********************************************************/
{ {
char line[1024], buffer[1024], *text; char line[1024], buffer[1024], * text;
int err = 1; int err = 1;
int nn = BUFSIZE; int nn = BUFSIZE;
double * points = NULL; double* points = NULL;
int * index = NULL; int* index = NULL;
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
strcpy(buffer,line); strcpy( buffer, line );
text = strtok(buffer, " \t\n\r"); text = strtok( buffer, " \t\n\r" );
if ( * text == '}' ) if( *text == '}' )
{ {
err = 0; break; err = 0; break;
} }
if ( stricmp (text, "normalPerVertex" ) == 0 ) if( stricmp( text, "normalPerVertex" ) == 0 )
{ {
text = strtok(NULL, " ,\t\n\r"); text = strtok( NULL, " ,\t\n\r" );
if( stricmp (text, "TRUE") == 0 ) if( stricmp( text, "TRUE" ) == 0 )
{ {
} }
else else
...@@ -391,10 +412,10 @@ int * index = NULL; ...@@ -391,10 +412,10 @@ int * index = NULL;
continue; continue;
} }
if ( stricmp (text, "colorPerVertex" ) == 0 ) if( stricmp( text, "colorPerVertex" ) == 0 )
{ {
text = strtok(NULL, " ,\t\n\r"); text = strtok( NULL, " ,\t\n\r" );
if( stricmp (text, "TRUE") == 0 ) if( stricmp( text, "TRUE" ) == 0 )
{ {
} }
else else
...@@ -403,119 +424,132 @@ int * index = NULL; ...@@ -403,119 +424,132 @@ int * index = NULL;
continue; continue;
} }
if ( stricmp (text, "normal" ) == 0 ) if( stricmp( text, "normal" ) == 0 )
{ {
int coord_number; int coord_number;
double * buf_points = ReadCoordsList(file, line, &coord_number, LineNum); double* buf_points = ReadCoordsList( file, line, &coord_number, LineNum );
continue; continue;
free(buf_points); free( buf_points );
continue; continue;
} }
if ( stricmp (text, "normalIndex" ) == 0 ) if( stricmp( text, "normalIndex" ) == 0 )
{ {
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line, " ,\t\n\r"); text = strtok( line, " ,\t\n\r" );
while ( text ) while( text )
{ {
if ( *text == ']') break; if( *text == ']' )
text = strtok(NULL, " ,\t\n\r"); break;
text = strtok( NULL, " ,\t\n\r" );
} }
if ( text && (*text == ']') ) break;
if( text && (*text == ']') )
break;
} }
continue; continue;
} }
if ( stricmp (text, "color" ) == 0 ) if( stricmp( text, "color" ) == 0 )
{ {
int coord_number; int coord_number;
double * buf_points = ReadCoordsList(file, line, &coord_number, LineNum); double* buf_points = ReadCoordsList( file, line, &coord_number, LineNum );
continue; continue;
free(buf_points); free( buf_points );
continue; continue;
} }
if ( stricmp (text, "colorIndex" ) == 0 ) if( stricmp( text, "colorIndex" ) == 0 )
{ {
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
text = strtok(line, " ,\t\n\r"); text = strtok( line, " ,\t\n\r" );
while ( text ) while( text )
{ {
if ( *text == ']') break; if( *text == ']' )
text = strtok(NULL, " ,\t\n\r"); break;
text = strtok( NULL, " ,\t\n\r" );
} }
if ( text && (*text == ']') ) break;
if( text && (*text == ']') )
break;
} }
continue; continue;
} }
if ( stricmp (text, "coord" ) == 0 ) if( stricmp( text, "coord" ) == 0 )
{ {
int coord_number; int coord_number;
points = ReadCoordsList(file, line, &coord_number, LineNum); points = ReadCoordsList( file, line, &coord_number, LineNum );
} }
else if ( stricmp (text, "coordIndex" ) == 0 ) else if( stricmp( text, "coordIndex" ) == 0 )
{ {
index = (int *) MyMalloc(nn * sizeof(int) ); index = (int*) MyMalloc( nn * sizeof(int) );
S3D_Vertex * coords = S3D_Vertex* coords =
(S3D_Vertex *) MyMalloc(nn * sizeof(S3D_Vertex) ); (S3D_Vertex*) MyMalloc( nn * sizeof(S3D_Vertex) );
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
int coord_count = 0, jj; int coord_count = 0, jj;
text = strtok(line, " ,\t\n\r"); text = strtok( line, " ,\t\n\r" );
while ( text ) while( text )
{ {
if ( *text == ']') break; if( *text == ']' )
jj = atoi(text); break;
if ( jj < 0 ) jj = atoi( text );
if( jj < 0 )
{ {
S3D_Vertex * curr_coord = coords; S3D_Vertex* curr_coord = coords;
for ( jj = 0; jj < coord_count; jj ++ ) for( jj = 0; jj < coord_count; jj++ )
{ {
int kk = index[jj] * 3; int kk = index[jj] * 3;
curr_coord->x = points[kk]; curr_coord->x = points[kk];
curr_coord->y = points[kk+1]; curr_coord->y = points[kk + 1];
curr_coord->z = points[kk+2]; curr_coord->z = points[kk + 2];
curr_coord++; curr_coord++;
} }
Set_Object_Coords(coords, coord_count );
Set_Object_Data(coords, coord_count ); Set_Object_Coords( coords, coord_count );
Set_Object_Data( coords, coord_count );
coord_count = 0; coord_count = 0;
} }
else else
{ {
index[coord_count++] = jj; index[coord_count++] = jj;
} }
text = strtok(NULL, " ,\t\n\r"); text = strtok( NULL, " ,\t\n\r" );
} }
if ( text && (*text == ']') ) break;
if( text && (*text == ']') )
break;
} }
free(index);
free(coords); free( index );
free( coords );
} }
else else
{ {
printf ("ReadGeometry error line %d <%s> \n", *LineNum, text); printf( "ReadGeometry error line %d <%s> \n", *LineNum, text );
break; break;
} }
} }
if ( points ) free (points);
if( points )
free( points );
return err; return err;
} }
/*********************************************************/ /*********************************************************/
int Struct3D_Shape:: ReadData(FILE * file, int *LineNum) int Struct3D_Shape:: ReadData( FILE* file, int* LineNum )
/*********************************************************/ /*********************************************************/
{ {
char line[512]; char line[512];
while ( GetLine(file, line, LineNum, 512) ) while( GetLine( file, line, LineNum, 512 ) )
{ {
} }
return -1; return -1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment