Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenInventor, STL file reader/parser?

Started by rumble Aug 5, 2008 at 7:20 AM 0 replies 2.5k views
Original Post
rumble
rumble
Need C/C++ source for a simple Open Inventor or STL file reader/parser. I would think they would be all over the web, but searches yielded no useful results. I would prefer not writing one myself, as a different format may be used in the future, and these are temporary solutions. Thanks
Manen
Manen
//----------------------------------------------------------------------
void __fastcall StL_FileReader::ReadASCII( AnsiString filename, StLFRstr &me )
{
FILE* stlfp;

int i,
j = 0,
n = 3,
cnt;


double x = 0,
y = 0,
z = 0;

char a[100];
char str[40];

me.Xmin = 0;
me.Xmax = 0;
me.Ymin = 0;
me.Ymax = 0;
me.Zmin = 0;
me.Zmax = 0;

stlfp = fopen( filename.c_str(), "r" );

if( !stlfp )
return;
else
{
me.lid = glGenLists( 1 );
glNewList( me.lid, GL_COMPILE );

glColor3f( 1.0f, 1.0f, 1.0f );

do
{
i = 0;
cnt = 0;

fgets( a, 100, stlfp );

while( a != '\0' )
{
if( !islower( (int)a ) && !isupper( (int)a ) && a != ' ' )
break;
cnt++;
i++;
}

while( a[cnt] != '\0' )
{
str[j] = a[cnt];
cnt++;
j++;
}

str[j] = '\0';
j = 0;

if( sscanf( str, "%lf%lf%lf", &x, &y, &z ) == 3 )
{
n++;

if( n % 4 == 0 )
{
glBegin( GL_TRIANGLES );
glNormal3f( x, y, z );
}
else
{
glVertex3f( x, y, z );
me.CompareXYZ( x, y, z, me );
}

if( ( n % 4) == 3 )
glEnd();
}
} while( !feof( stlfp ) );

glEndList();
fclose( stlfp );
}

}
//-----------------------------------------------------------------------------
void __fastcall StL_FileReader::ReadBinary( AnsiString filename, StLFRstr &me )
{
ifstream file;

int fCnt = 0;

float xn = 0, yn = 0, zn = 0;
float x1 = 0, y1 = 0, z1 = 0;
float x2 = 0, y2 = 0, z2 = 0;
float x3 = 0, y3 = 0, z3 = 0;

char a[80];
char str[40];
char fill[2];

me.Xmin = 0;
me.Xmax = 0;
me.Ymin = 0;
me.Ymax = 0;
me.Zmin = 0;
me.Zmax = 0;

try
{
file.open( filename.c_str(), std::ios_base::binary );

if( !file )
return;
else
{
me.lid = glGenLists( 1 );
glNewList( me.lid, GL_COMPILE );

glColor3f( 1.0f, 1.0f, 1.0f );

file.read( a, 80 ); // Header
file.read( (char*)&fCnt, 4 ); // Facet Count

for( int i = 0; i < fCnt; i++ )
{
glBegin( GL_TRIANGLES );

file.read((char*)&xn, 4); file.read((char*)&yn, 4); file.read((char*)&zn, 4);
glNormal3f( xn, yn, zn );

file.read((char*)&x1, 4); file.read((char*)&y1, 4); file.read((char*)&z1, 4);
file.read((char*)&x2, 4); file.read((char*)&y2, 4); file.read((char*)&z2, 4);
file.read((char*)&x3, 4); file.read((char*)&y3, 4); file.read((char*)&z3, 4);

glVertex3f( x1, y1, z1 );
glVertex3f( x3, y3, z3 );
glVertex3f( x2, y2, z2 );

glEnd();

file.read( fill, 2 ); // Alignment (2 bytes)

me.CompareXYZ( x1, y1, z1, me );
me.CompareXYZ( x2, y2, z2, me );
me.CompareXYZ( x3, y3, z3, me );
}

glEndList();
}

file.close();
}
catch(...)
{
ShowMessage( "StL_FileReader Error!" );
}

}
//-----------------------------------------------------------------------------

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.