my function Load returns -3 as a value, this is returned when I try to open the file "object", the strange thing however is that it opens the file also with then return -1 if reads fails but there it does not fail,
what am i doing wrong? or is it not related to the read?
Code: Select all
int Obj::Load(const char *object, const char *material) {
char ReadBuffer[256];
char Textures[200][256];
char sBuffer[200];
unsigned int vc=0,nc=0,tc=0, fc=0;
unsigned int i=0;
unsigned int j,k,l;
unsigned int iNumberOfFaces = 0 ;
unsigned int iNumberOfVertices = 0;
unsigned int iNumberOfTexCoords = 0;
unsigned int iNumberOfTextures = 0;
unsigned int iNumberOfGroups = 0;
iNumberOfFaces = 0;
// read the object file
FILE *fp = NULL;
if ((fp = fopen(object, "rb")) == NULL) { return -1; }
while(!feof(fp))
{
fgets(ReadBuffer, 256, fp);
if (strncmp("g default", ReadBuffer, 9) == 0 ) { iNumberOfGroups++;}
else if (strncmp("v ", ReadBuffer, 2) == 0 ) { iNumberOfVertices++; }
else if (strncmp("vt ", ReadBuffer, 3) == 0 ) { iNumberOfTexCoords++; }
else if (strncmp("f ", ReadBuffer, 2) == 0 ) { iNumberOfFaces++; }
}
fclose(fp);
// read the textures:
iNumberOfTextures = 0;
if ((fp = fopen(material, "rb")) == NULL) { return -2; }
while(!feof(fp)) {
fgets(ReadBuffer, 256, fp);
if (strncmp("map_Kd ", ReadBuffer, 7) == 0 ) {
sprintf(Textures[iNumberOfTextures],"Textures/%s", strtok((ReadBuffer+7),"#"));
iNumberOfTextures++;
}
}
fclose(fp);
ScePspFVector3 Vertices[ iNumberOfVertices ];
ScePspFVector2 TexCoords[ iNumberOfTexCoords ];
ObjFaceNew Faces[ iNumberOfFaces ];
iNumberOfParts = iNumberOfGroups;
ObjMeshParts = (ObjMeshPart*)malloc(iNumberOfGroups * sizeof(ObjMeshPart));
unsigned int Correction = 0;
iNumberOfGroups = 0;
vc=0;
nc=0;
tc=0;
fc=0;
if ((fp = fopen(object, "rb")) == NULL) { return -3; }
while(!feof(fp))
{
fgets(ReadBuffer, 256, fp);
if (strncmp("EndGroup", ReadBuffer, 8) == 0 )
{
ObjMeshParts[iNumberOfGroups].Vertices = (ObjVertexB*)malloc((fc-Correction) * 3 * sizeof(ObjVertexB));
//tmpObj->ObjMeshPart[iNumberOfGroups].iNumberOfFaces = fc - Correction;
ObjMeshParts[iNumberOfGroups].iFaces = (fc-Correction);
//tmpObj->iNumberOfFaces[iNumberOfGroups] = (fc-Correction);
l = 0;
for (j=Correction;j<fc;j++)
{
for (k=0; k<3; k++)
{
ObjMeshParts[iNumberOfGroups].Vertices[l].u = TexCoords[Faces[j].textc[k]].x;
ObjMeshParts[iNumberOfGroups].Vertices[l].v = -(TexCoords[Faces[j].textc[k]].y);
ObjMeshParts[iNumberOfGroups].Vertices[l].color = 0xffffffff;
ObjMeshParts[iNumberOfGroups].Vertices[l].x = Vertices[Faces[j].vertices[k]].x;
ObjMeshParts[iNumberOfGroups].Vertices[l].y = Vertices[Faces[j].vertices[k]].y;
ObjMeshParts[iNumberOfGroups].Vertices[l].z = Vertices[Faces[j].vertices[k]].z;
l++;
}
}
// the texture for this part.
sprintf(sBuffer, "%s",Textures[iNumberOfGroups]);
ObjMeshParts[iNumberOfGroups].texture = loadImage(sBuffer);
if (ObjMeshParts[iNumberOfGroups].texture == NULL) { return -4; }
// increment the group number.
Correction = fc;
iNumberOfGroups++;
}
else if (strncmp("v ", ReadBuffer, 2) == 0 )
{
sscanf((ReadBuffer+2), "%f%f%f",&Vertices[ vc ].x, &Vertices[ vc ].y, &Vertices[ vc ].z);
vc++;
}
else if (strncmp("vt ", ReadBuffer, 3) == 0 )
{
sscanf((ReadBuffer+3), "%f%f",&TexCoords[ tc ].x, &TexCoords[ tc ].y);
tc++;
}
else if (strncmp("f ", ReadBuffer, 2) == 0 )
{
char *pSplitString = NULL;
int Waste;
i=0;
pSplitString = strtok((ReadBuffer+2)," \t\n");
do {
sscanf((pSplitString), "%d/%d/%d",&Faces[ fc ].vertices[ i ], &Faces[ fc ].textc[ i ],&Waste);
Faces[ fc ].textc[ i ] -= 1; // 1 down because the obj file objects start at 1 and arrays start at 0
Faces[ fc ].vertices[ i ] -= 1;
pSplitString = strtok(NULL," \t\n");
i += 1;
}
while( pSplitString );
fc++;
}
}
fclose(fp);
return 0;
};