CarbonX
2004.11.15, 05:09 PM
I am writing an obj loader / camera control demo and I am having a major problem with it crashing, it says in the debugger that it can't access memory at 0x7...
here is all of the relevant code:
- (void) loadObject: (NSString*) name Texture: (NSString*) texture Scale: (float) scale NormalType: (int) normalType
{
FILE *objFile;
float x,y,z;
float t1, t2, t3;
int iV1, iV2, iV3, iV4, iN1, iN2, iN3, iN4, iT1, iT2, iT3, iT4;
objFile = fopen([name UTF8String], "rt");
if (objFile == nil) NSLog(@" there is no file to load ");
int numParts = -1;
int h = -1;
NSString* fileString = [NSString stringWithContentsOfFile: name];
NSArray* fileArray = [fileString componentsSeparatedByString: @"\n"];
//NSLog(@"Loading .obj model: %@", name);
//NSLog([fileArray description]);
unsigned int i;
for (i = 0; i < [fileArray count]; i++)
{
const char *oneline = [[fileArray objectAtIndex:i] UTF8String];
printf("%s\n", oneline);
char materialNameString[50];
if (sscanf(oneline, "mtllib %s", materialNameString) == 1)
{
//printf("%s\n", materialNameString);
numParts++;
h++;
printf("%d\n", h);
theObject[h] = (OBJECT*)malloc(sizeof(OBJECT));
theObject[h]->numVerticies = theObject[h]->startVerticies = theObject[h-1]->numVerticies;
theObject[h]->numTextures = theObject[h]->startTextures = theObject[h-1]->numTextures;
theObject[h]->numNormals = theObject[h]->startNormals = theObject[h-1]->numNormals;
theObject[h]->numTriangles = theObject[h]->startTriangles = theObject[h-1]->numTriangles;
theObject[h]->numQuads = theObject[h]->startQuads = theObject[h-1]->numQuads;
theObject[h]->maxVerticies.x = theObject[h]->maxVerticies.y = theObject[h]->maxVerticies.z = 0;
theObject[h]->minVerticies.x = theObject[h]->minVerticies.y = theObject[h]->minVerticies.z = 0;
theObject[h]->hasTextures = NO;
theObject[h]->hasNormals = NO;
}
else if (sscanf(oneline, "v %f %f %f", &x, &y, &z) == 3)
{
x *= scale/100.0;
y *= scale/100.0;
z *= scale/100.0;
theObject[h]->objVerts[theObject[h]->numVerticies].x = x;
theObject[h]->objVerts[theObject[h]->numVerticies].y = y;
theObject[h]->objVerts[theObject[h]->numVerticies].z = z;
theObject[h]->origin.x += x;
theObject[h]->origin.y += y;
theObject[h]->origin.z += z;
theObject[h]->numVerticies++;
if (x > theObject[h]->maxVerticies.x)
theObject[h]->maxVerticies.x = x;
if (y > theObject[h]->maxVerticies.y)
theObject[h]->maxVerticies.y = y;
if (z > theObject[h]->maxVerticies.z)
theObject[h]->maxVerticies.z = z;
if (x < theObject[h]->minVerticies.x)
theObject[h]->minVerticies.x = x;
if (y < theObject[h]->minVerticies.y)
theObject[h]->minVerticies.y = y;
if (z < theObject[h]->minVerticies.z)
theObject[h]->minVerticies.z = z;
NSLog(@"Loaded Vertex: %f %f %f", x, y, z);
}
else if (sscanf(oneline, "vt %f %f %f", &t1, &t2, &t3) == 3)
{
theObject[h]->objTexts[theObject[h]->numTextures].u = t1;
theObject[h]->objTexts[theObject[h]->numTextures].v = t2;
theObject[h]->objTexts[theObject[h]->numTextures].w = t3;
theObject[h]->numTextures++;
theObject[h]->hasTextures = true;
}
else if (sscanf(oneline, "vt %f %f", &t1, &t2) == 2)
{
theObject[h]->objTexts[theObject[h]->numTextures].u = t1;
theObject[h]->objTexts[theObject[h]->numTextures].v = t2;
theObject[h]->objTexts[theObject[h]->numTextures].w = 0.0;
theObject[h]->numTextures++;
theObject[h]->hasTextures = true;
}
else if (sscanf(oneline, "vn %f %f %f", &x, &y, &z) == 3)
{
theObject[h]->objNorms[theObject[h]->numNormals].x = x;
theObject[h]->objNorms[theObject[h]->numNormals].y = y;
theObject[h]->objNorms[theObject[h]->numNormals].z = z;
theObject[h]->numNormals++;
theObject[h]->hasNormals = true;
NSLog(@"Loaded Normal: %f %f %f", x, y, z);
}
else if (sscanf(oneline, "f %d//%d %d//%d %d//%d %d//%d", &iV1, &iN1, &iV2, iN2, &iV3, &iN3, &iV4, &iN4) == 8)
{
NSLog(@"Loading Face: %d, %d, %d, %d", iV1, iV2, iV3, iV4);
NSLog(@"%d", theObject[h]->numQuads);
theObject[h]->objQuads[theObject[h]->numQuads].v1 = iV1;
theObject[h]->objQuads[theObject[h]->numQuads].v2 = iV2;
theObject[h]->objQuads[theObject[h]->numQuads].v3 = iV3;
theObject[h]->objQuads[theObject[h]->numQuads].v4 = iV4;
theObject[h]->objQuads[theObject[h]->numQuads].t1 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t2 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t3 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t4 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].n1 = iN1;
theObject[h]->objQuads[theObject[h]->numQuads].n2 = iN2;
theObject[h]->objQuads[theObject[h]->numQuads].n3 = iN3;
theObject[h]->objQuads[theObject[h]->numQuads].n4 = iN4;
theObject[h]->numQuads++;
NSLog(@"Finished Loading Face");
}
else if (sscanf(oneline, "f %d//%d %d//%d %d//%d", &iV1, &iN1, &iV2, &iN2, iV3, iN3) == 6)
{
theObject[h]->objTngls[theObject[h]->numTriangles].v1 = iV1;
theObject[h]->objTngls[theObject[h]->numTriangles].v2 = iV2;
theObject[h]->objTngls[theObject[h]->numTriangles].v3 = iV3;
theObject[h]->objTngls[theObject[h]->numTriangles].t1 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].t2 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].t3 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].n1 = iN1;
theObject[h]->objTngls[theObject[h]->numTriangles].n2 = iN2;
theObject[h]->objTngls[theObject[h]->numTriangles].n3 = iN3;
theObject[h]->numTriangles++;
}
}
fclose(objFile);
}
and here is the header with the object struct in it
typedef struct _VERTEX
{ GLfloat x,y,z; } VERTEX;
typedef struct _TEXCOORD
{ GLfloat u,v,w; } TEXCOORD;
typedef struct _FACE
{
int v1, v2, v3, v4;
int t1, t2, t3, t4;
int n1, n2, n3, n4;
} FACE;
typedef struct _OBJECT
{
TEXCOORD objTexts[MAX_VERTICIES];
VERTEX objNorms[2 * MAX_FACES];
VERTEX objVerts[MAX_VERTICIES];
FACE objTngls[MAX_FACES];
FACE objQuads[MAX_FACES];
VERTEX origin;
int numVerticies;
int numTextures;
int numNormals;
int numTriangles;
int numQuads;
int startVerticies;
int startTextures;
int startNormals;
int startTriangles;
int startQuads;
bool hasTextures;
bool hasNormals;
float radius;
VERTEX maxVerticies;
VERTEX minVerticies;
} OBJECT;
I can't for the life of me figure out why it is crashing, it finishes loading all of the faces of my cube and then crashes, I can't really pinpoint where it crashes either, the debugger doesn't give me any hints... Please help me!
here is all of the relevant code:
- (void) loadObject: (NSString*) name Texture: (NSString*) texture Scale: (float) scale NormalType: (int) normalType
{
FILE *objFile;
float x,y,z;
float t1, t2, t3;
int iV1, iV2, iV3, iV4, iN1, iN2, iN3, iN4, iT1, iT2, iT3, iT4;
objFile = fopen([name UTF8String], "rt");
if (objFile == nil) NSLog(@" there is no file to load ");
int numParts = -1;
int h = -1;
NSString* fileString = [NSString stringWithContentsOfFile: name];
NSArray* fileArray = [fileString componentsSeparatedByString: @"\n"];
//NSLog(@"Loading .obj model: %@", name);
//NSLog([fileArray description]);
unsigned int i;
for (i = 0; i < [fileArray count]; i++)
{
const char *oneline = [[fileArray objectAtIndex:i] UTF8String];
printf("%s\n", oneline);
char materialNameString[50];
if (sscanf(oneline, "mtllib %s", materialNameString) == 1)
{
//printf("%s\n", materialNameString);
numParts++;
h++;
printf("%d\n", h);
theObject[h] = (OBJECT*)malloc(sizeof(OBJECT));
theObject[h]->numVerticies = theObject[h]->startVerticies = theObject[h-1]->numVerticies;
theObject[h]->numTextures = theObject[h]->startTextures = theObject[h-1]->numTextures;
theObject[h]->numNormals = theObject[h]->startNormals = theObject[h-1]->numNormals;
theObject[h]->numTriangles = theObject[h]->startTriangles = theObject[h-1]->numTriangles;
theObject[h]->numQuads = theObject[h]->startQuads = theObject[h-1]->numQuads;
theObject[h]->maxVerticies.x = theObject[h]->maxVerticies.y = theObject[h]->maxVerticies.z = 0;
theObject[h]->minVerticies.x = theObject[h]->minVerticies.y = theObject[h]->minVerticies.z = 0;
theObject[h]->hasTextures = NO;
theObject[h]->hasNormals = NO;
}
else if (sscanf(oneline, "v %f %f %f", &x, &y, &z) == 3)
{
x *= scale/100.0;
y *= scale/100.0;
z *= scale/100.0;
theObject[h]->objVerts[theObject[h]->numVerticies].x = x;
theObject[h]->objVerts[theObject[h]->numVerticies].y = y;
theObject[h]->objVerts[theObject[h]->numVerticies].z = z;
theObject[h]->origin.x += x;
theObject[h]->origin.y += y;
theObject[h]->origin.z += z;
theObject[h]->numVerticies++;
if (x > theObject[h]->maxVerticies.x)
theObject[h]->maxVerticies.x = x;
if (y > theObject[h]->maxVerticies.y)
theObject[h]->maxVerticies.y = y;
if (z > theObject[h]->maxVerticies.z)
theObject[h]->maxVerticies.z = z;
if (x < theObject[h]->minVerticies.x)
theObject[h]->minVerticies.x = x;
if (y < theObject[h]->minVerticies.y)
theObject[h]->minVerticies.y = y;
if (z < theObject[h]->minVerticies.z)
theObject[h]->minVerticies.z = z;
NSLog(@"Loaded Vertex: %f %f %f", x, y, z);
}
else if (sscanf(oneline, "vt %f %f %f", &t1, &t2, &t3) == 3)
{
theObject[h]->objTexts[theObject[h]->numTextures].u = t1;
theObject[h]->objTexts[theObject[h]->numTextures].v = t2;
theObject[h]->objTexts[theObject[h]->numTextures].w = t3;
theObject[h]->numTextures++;
theObject[h]->hasTextures = true;
}
else if (sscanf(oneline, "vt %f %f", &t1, &t2) == 2)
{
theObject[h]->objTexts[theObject[h]->numTextures].u = t1;
theObject[h]->objTexts[theObject[h]->numTextures].v = t2;
theObject[h]->objTexts[theObject[h]->numTextures].w = 0.0;
theObject[h]->numTextures++;
theObject[h]->hasTextures = true;
}
else if (sscanf(oneline, "vn %f %f %f", &x, &y, &z) == 3)
{
theObject[h]->objNorms[theObject[h]->numNormals].x = x;
theObject[h]->objNorms[theObject[h]->numNormals].y = y;
theObject[h]->objNorms[theObject[h]->numNormals].z = z;
theObject[h]->numNormals++;
theObject[h]->hasNormals = true;
NSLog(@"Loaded Normal: %f %f %f", x, y, z);
}
else if (sscanf(oneline, "f %d//%d %d//%d %d//%d %d//%d", &iV1, &iN1, &iV2, iN2, &iV3, &iN3, &iV4, &iN4) == 8)
{
NSLog(@"Loading Face: %d, %d, %d, %d", iV1, iV2, iV3, iV4);
NSLog(@"%d", theObject[h]->numQuads);
theObject[h]->objQuads[theObject[h]->numQuads].v1 = iV1;
theObject[h]->objQuads[theObject[h]->numQuads].v2 = iV2;
theObject[h]->objQuads[theObject[h]->numQuads].v3 = iV3;
theObject[h]->objQuads[theObject[h]->numQuads].v4 = iV4;
theObject[h]->objQuads[theObject[h]->numQuads].t1 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t2 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t3 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].t4 = -1;
theObject[h]->objQuads[theObject[h]->numQuads].n1 = iN1;
theObject[h]->objQuads[theObject[h]->numQuads].n2 = iN2;
theObject[h]->objQuads[theObject[h]->numQuads].n3 = iN3;
theObject[h]->objQuads[theObject[h]->numQuads].n4 = iN4;
theObject[h]->numQuads++;
NSLog(@"Finished Loading Face");
}
else if (sscanf(oneline, "f %d//%d %d//%d %d//%d", &iV1, &iN1, &iV2, &iN2, iV3, iN3) == 6)
{
theObject[h]->objTngls[theObject[h]->numTriangles].v1 = iV1;
theObject[h]->objTngls[theObject[h]->numTriangles].v2 = iV2;
theObject[h]->objTngls[theObject[h]->numTriangles].v3 = iV3;
theObject[h]->objTngls[theObject[h]->numTriangles].t1 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].t2 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].t3 = -1;
theObject[h]->objTngls[theObject[h]->numTriangles].n1 = iN1;
theObject[h]->objTngls[theObject[h]->numTriangles].n2 = iN2;
theObject[h]->objTngls[theObject[h]->numTriangles].n3 = iN3;
theObject[h]->numTriangles++;
}
}
fclose(objFile);
}
and here is the header with the object struct in it
typedef struct _VERTEX
{ GLfloat x,y,z; } VERTEX;
typedef struct _TEXCOORD
{ GLfloat u,v,w; } TEXCOORD;
typedef struct _FACE
{
int v1, v2, v3, v4;
int t1, t2, t3, t4;
int n1, n2, n3, n4;
} FACE;
typedef struct _OBJECT
{
TEXCOORD objTexts[MAX_VERTICIES];
VERTEX objNorms[2 * MAX_FACES];
VERTEX objVerts[MAX_VERTICIES];
FACE objTngls[MAX_FACES];
FACE objQuads[MAX_FACES];
VERTEX origin;
int numVerticies;
int numTextures;
int numNormals;
int numTriangles;
int numQuads;
int startVerticies;
int startTextures;
int startNormals;
int startTriangles;
int startQuads;
bool hasTextures;
bool hasNormals;
float radius;
VERTEX maxVerticies;
VERTEX minVerticies;
} OBJECT;
I can't for the life of me figure out why it is crashing, it finishes loading all of the faces of my cube and then crashes, I can't really pinpoint where it crashes either, the debugger doesn't give me any hints... Please help me!