Loading RAW files

Started by
7 comments, last by max621 23 years, 7 months ago
I''m having troubles figuring out how to load RAW files (3d format) The format basically goes like this: [NAME] [vertex1 xyz] [vertex2 xyz] [vertex3 xyz] and one line is one triangle now what i need to do is to not read in the name of the object, and just read the 3 values, than move to the next vertex... read it, and so on. My main problem is making sure I''m not reading the name. ||--------------------------|| Black Hole Productions www.bhpro.com simon@bhpro.com ||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
Advertisement
What you''re trying to do is called "parsing." You can search for it on the web and find many tutorials AND functions that will show you how to parse strings and get the tokens you need. I would suggest looking at the function strstr(), strcpy(), atof(), strchr(), etc...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Either I suck at searching, or I''m lazy. You decide But I can''t find anything thats related to C++ and parsing hehe. Got any links you might have?

||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
Erg, use stdio.h and all you need to do is:

char Text[256];
Vertex v;
FILE *fil = fopen(filename, "rt");
fscanf(fil, "%s\n", &Text[0]); // for the name
fscanf(fil, "%f %f %f", &v.x, &v.y, &v.z); // for each point
fclose(fil);

its that simple, although enclose some stuff in a loop, basically those standard functions do the parsing for you... I know this works becuase I did it last night



Dæmin
(Dominik Grabiec)
sdgrab@eisa.net.au
Daemin(Dominik Grabiec)
Ahh thanks I couldn''t figure out how to do it Well accutally I''m getting places with code I saw on a OBJ loader, but that is pretty different. I accutally read a string, and than parse the string. So I guess I could use that function "readnextline" which checks for comments, empty space, or anything like that.
And that is passed a string which it inserts the next good line. It''s real nice. I got it from glvelocity.gamedev.net
Thanks...that gave me a good idea (how to check for text... i was accutally pretty dumb, and was having a fuss on how to figure out how many lines there are, but I could just go till FEOF() finds eof.. )
And if anyone wants to see my code once I finish, please contact me:
Simon@bhpro.com


||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
Accutally I looked at my code and I do read from the file I just use a string to see what the next stuff is.... guess I need a reworking on the.. umm "engine"

||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
My code is going screwy. After I saw your post... I deleted all my code..and started almost from the beginning and used the text loading thing, but now my program keeps yelling at me that it can''t load the floating point from the raw file!!! Any suggestions? I even delted the text and the text code but it still wont work.

||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
This is getting VERY messed up that I can''t even explain what its doing. Basically I just made a vertex to test it out, and than using the %f thing doesnt work.. and than I do %d.. and it loads the first number + some hex code, and than when I change it to %f it works. But when I make it for all the verticies in the mesh with %f it starts giving me the runtime errors again or just gives me null for each value...I think I''m replying to myself too much Wish I had the old code.. I think I posted it on the fourms, but can''t find it now!

||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
This is my code for loading a .raw file (specifically one produced by Rhinoceros3D). In Rhino, all my meshes are named ''object#'' (ex: object14). so i just look to see if theres an ''object'' string in there. and to for numbers, i expect a ''.'' (decimal) somewhere in the number. this is kinda restrictive but it was easier. I''ll try to include structures so it won''t be so messy:



// Copyright (c) 2000 David Osborn. Go ahead and copy it!

// you don''t really need to worry about my joint structure...
typedef struct
{
int nmesh1, nmesh2;
float x1, y1, z1;
float x2, y2, z2;
float rx, ry, rz;
} _JOINTS;

typedef struct
{
int ntriangleoffset;
int ntriangles;
int ntexture;
unsigned int dl;
} _MESHES;

typedef struct
{
int nmeshes;
int njoints;
_MESHES *pmeshes;
_JOINTS *pjoints;
} _MODELS;
_MODELS *pmodels;
int nmodels = 0;

// i might have missed some variable declarations here so...
char sout[256];
int line;

int lineheight = 20;

char s[256];
char c;

int ntriangles_read = 0;
int ntriangles_allocated = 0;
int ncoordinates = 0;

// .raw model
if ( ( fload = fopen( s, "rb" ) ) == NULL )
{
sprintf( sout, "error: could not open %s for reading", s );
cTextOut( hdc, 0, line * lineheight, sout, strlen( sout ) );
line++;
active = false;
goto StartCompilation;
}

if ( details )
{
sprintf( sout, "adding model #%i \"%s\"", nmodels, s );
cTextOut( hdc, 0, line * lineheight, sout, strlen( sout ) );
line++;
}

nmodels++;
pmodels = ( _MODELS * )realloc( pmodels, sizeof( _MODELS ) * nmodels );
pmodels[nmodels - 1].nmeshes = 0;
pmodels[nmodels - 1].njoints = 0;
pmodels[nmodels - 1].pmeshes = NULL;
pmodels[nmodels - 1].pjoints = NULL;

strset( s, 0 );

while ( !feof( fload ) )
{
c = fgetc( fload );
if ( c > 32 )
{
s[strlen( s ) + 1] = 0;
s[strlen( s )] = c;
}
else
{
strlwr( s );

if ( strstr( s, "object" ) != NULL )
{
// start a new mesh.
if ( details )
{
sprintf( sout, "adding mesh #%i of model %i", pmodels[nmodels - 1].nmeshes, nmodels - 1, s );
cTextOut( hdc, 0, line * lineheight, sout, strlen( sout ) );
line++;
}
pmodels[nmodels - 1].nmeshes++;
pmodels[nmodels - 1].pmeshes = ( _MESHES * )realloc( pmodels[nmodels - 1].pmeshes, sizeof( _MESHES ) * pmodels[nmodels - 1].nmeshes );
pmodels[nmodels - 1].pmeshes[pmodels[nmodels - 1].nmeshes - 1].ntriangleoffset = ntriangles_read;
pmodels[nmodels - 1].pmeshes[pmodels[nmodels - 1].nmeshes - 1].ntriangles = 0;
pmodels[nmodels - 1].pmeshes[pmodels[nmodels - 1].nmeshes - 1].ntexture = 0xffffffff;
pmodels[nmodels - 1].pmeshes[pmodels[nmodels - 1].nmeshes - 1].dl = 0;
}
else
if ( strstr( s, "." ) != NULL )
{
// found a coordinate.
if ( ptriangles == NULL )
{
// make sure we have allocated space for triangles
ntriangles_allocated = 1000;
ptriangles = ( _TRIANGLES * )realloc( ptriangles, sizeof( _TRIANGLES ) * ntriangles_allocated );
}
ncoordinates++;
// what coordinate does s refer to?
switch( ncoordinates )
{
case 1:
ptriangles[ntriangles].x1 = atof( s );
break;

case 2:
ptriangles[ntriangles].z1 = atof( s );
break;

case 3:
ptriangles[ntriangles].y1 = atof( s );
break;

case 4:
ptriangles[ntriangles].x2 = atof( s );
break;

case 5:
ptriangles[ntriangles].z2 = atof( s );
break;

case 6:
ptriangles[ntriangles].y2 = atof( s );
break;

case 7:
ptriangles[ntriangles].x3 = atof( s );
break;

case 8:
ptriangles[ntriangles].z3 = atof( s );
break;

case 9:
ptriangles[ntriangles].y3 = atof( s );
break;
}
if ( ncoordinates == 9 )
{
// got one full triangle.
// if we need more, allocate
// memory for another 1000.
ntriangles++;
ntriangles_read++;
pmodels[nmodels - 1].pmeshes[pmodels[nmodels - 1].nmeshes - 1].ntriangles++;
ptriangles[ntriangles - 1].tx1 = 1.0;
ptriangles[ntriangles - 1].ty1 = -1.0;
ptriangles[ntriangles - 1].tx2 = -1.0;
ptriangles[ntriangles - 1].ty2 = 1.0;
ptriangles[ntriangles - 1].tx3 = 1.0;
ptriangles[ntriangles - 1].ty3 = 1.0;
ptriangles[ntriangles - 1].r1 = 1.0;
ptriangles[ntriangles - 1].g1 = 0.0;
ptriangles[ntriangles - 1].b1 = 0.0;
ptriangles[ntriangles - 1].a1 = 1.0;
ptriangles[ntriangles - 1].r2 = 1.0;
ptriangles[ntriangles - 1].g2 = 0.0;
ptriangles[ntriangles - 1].b2 = 0.0;
ptriangles[ntriangles - 1].a2 = 1.0;
ptriangles[ntriangles - 1].r3 = 1.0;
ptriangles[ntriangles - 1].g3 = 0.0;
ptriangles[ntriangles - 1].b3 = 0.0;
ptriangles[ntriangles - 1].a3 = 1.0;
if ( ntriangles_allocated == ntriangles )
{
ntriangles_allocated += 1000;
ptriangles = ( _TRIANGLES * )realloc( ptriangles, sizeof( _TRIANGLES ) * ntriangles_allocated );
}
ncoordinates = 0;
}
}
strset( s, 0 );
}
}

// wrap the texture around the entire mesh
// haven''t a clue how to do this...
// find the length of triangle edges.
// there should be 10 pixels/meter.
for ( i = 0; i < pmodels[nmodels - 1].nmeshes; i++ )
{
for ( i2 = pmodels[nmodels - 1].pmeshes.ntriangles; i2 < pmodels[nmodels - 1].pmeshes.ntriangles + pmodels[nmodels - 1].pmeshes.ntriangleoffset; i2++ )<br> {<br> }<br> }<br><br> // produce joints by finding solid object<br> // intersections.<br> for ( i = 0; i < pmodels[nmodels - 1].nmeshes; i++ )<br> {<br> }<br><br> fclose( fload );<br> }<br> }<br></CODE> </i> <br><br>Thanks! Blue, Blue Grass…

This topic is closed to new replies.

Advertisement