Well I’m once again trying to write an obj loader but it keeps on crashing on me. Through debugging I have narrowed the crash down to one line. My obj loader is currently two passes, the first pass counts how much of each type there is, then it allocates memory accordingly, then it goes through again and assigns the memory. The problem I’m running into is with sscanf and I’m not worried about actually filling up the memory right now, just getting it to recongnize vertices, texture cords, and normals as needed. Does anybody see the problem with sscanf? Thanks
void readstr(FILE *f,char *string) // Reads A String From File (f)
{
do // Do This
{
fgets(string, 255, f); // Gets A String Of 255 Chars Max From f (File)
} while ((string[0] == ‘/’) | | (string[0] == ’
')); // Until End Of Line Is Reached
return; // Return
}int loadOBJ(const char *filename) {
char strLine[255] = { 0 };
// Check to make sure the extension is obj
int length;// Vertice memory
float *vertx;
float *verty;
float *vertz;// Texture Coordinate memory
float *texcordx;
float *texcordy;// Lighting normal memory
float *lightingx;
float *lightingy;
float *lightingz;int verts = 0; // how many vertices
int cords = 0; // how many texture coordinates
int normals = 0; // how many normals
int groups = 0; // how many groups
char c; // a simple buffer
char b; // another buffer
char d; // yet another bufferchar type[10]; // What type is the current line
float values[3]; // The values being read in
int counter[3] = { 0, 0, 0 }; // A counter for which line we are on
char charbuffer[255];
int percentRight = 0; // The percentage of values successfully matchedlength = strlen(filename);
if( length < 5) {
cout << “Filename given to loadOBJ is too short to be complete. Aborting” << endl;
exit(-1);
}
if(FILE *inputfile = fopen(filename, “r”)) {
// Our first task will be to allocate all of the appropriate space for the vertices, texture cords, and lighting normals
while(feof(inputfile)==0) {
c = fgetc(inputfile);
if(c==‘v’) {
b = fgetc(inputfile);
if(b==’ ') { // Its a vertex
verts++;
//while(fgetc(inputfile) != ’
’ | | ‘\r’) { }
}
else if(b==‘t’) { // Its a texture cordinate
cords++;
//while(fgetc(inputfile) != ’
’ | | ‘\r’) { }
}
else if(b==‘n’){ // If its niether then its a lighting normal
normals++;
//while(fgetc(inputfile) != ’
’ | | ‘\r’) { }
}
}
if(c==‘g’) {
groups++;
//while(fgetc(inputfile) != ’
’ | | ‘\r’) { }
}
}
// Now establish memory for storing all of themvertx = new float [verts]; verty = new float [verts]; vertz = new float [verts]; texcordx = new float [cords]; texcordy = new float [cords]; lightingx = new float [normals]; lightingy = new float [normals]; lightingz = new float [normals]; // Now read in all of the values to the memory after going back to the beginning fseek(inputfile,0,SEEK_SET); // Reset to the beginning of the file while(feof(inputfile)==0) { readstr(inputfile, charbuffer); sscanf(charbuffer, "%s %f %f %f", type, values[0], values[1], values[2]); // Its a vertex or a normal if(strcmp(type, "v")==0) { cout << "vertex" << endl; } else if(strcmp(type, "vn")==0) { cout << "lighting normal" << endl; } else if(strcmp(type, "vt")==0) { cout << "Texture coordinate" << endl; } else { cout << "skipped line" << endl; } } cout << verts << " " << cords << " " << normals << " " << groups << endl; fclose(inputfile); return true;
}
else {
return false;
}
}