Original Post
/*reposted from the general programming forum*/
I''m having some problems with my obj loader heres the source
It dies on the dynamic allocation although I dont know if thats the only problem
extern int loadmodel(int texindex,model* obj,char* str){
int vers,texs,nors,poly,index,index2,vertex_index=0,texture_index=0,normal_index=0,poly_index;
float *pver,*ptex,*pnor;
int *ppoly;
char str2[100],str3[100],str_a[100],str_b[100],str_c[100];
FILE *file;
if((file=fopen(str,"r"))==NULL){
return(2);
}
while(!feof(file)){
fgets(str2,100,file);
if(str2[0]==''v'' && str2[1]=='' ''){
vers++;
}
if(str2[0]==''v'' && str2[1]==''t''){
texs++;
}
if(str2[0]==''v'' && str2[1]==''n''){
nors++;
}
if(str2[0]==''f'' && str2[1]=='' ''){
poly++;
}
}
pver=(float *)malloc(sizeof(float)*vers*3);
if(pver==NULL){
return(3);
}
ptex=(float *)malloc(sizeof(float)*texs*2);
if(ptex==NULL){
return(4);
}
pnor=(float *)malloc(sizeof(float)*nors*3);
if(pnor==NULL){
return(5);
}
ppoly=(int *)malloc(sizeof(int)*poly*9);
if(ppoly==NULL){
return(6);
}
rewind(file);
while(!feof(file)){
fgets(str2,100,file);
if(str2[0]==''v'' && str2[1]=='' ''){//load vertecies
index=2;
index2=0;
while(str2[index]!='' ''){
str_a[index2]=str2[index];
index++;
index2++;
}
index++;
while(str2[index]!='' ''){
str_b[index2]=str2[index];
index++;
index2++;
}
index++;
while(str2[index]!=0){
str_c[index2]=str2[index];
index++;
index2++;
}
*(pver+vertex_index)=(float)atof(str_a);
*(pver+vertex_index+1)=(float)atof(str_b);
*(pver+vertex_index+2)=(float)atof(str_c);
vertex_index+=3;
}
if(str2[0]==''v'' && str2[1]==''t''){//load texture cords
index=2;
index2=0;
while(str2[index]!='' ''){
str_a[index2]=str2[index];
index++;
index2++;
}
index++;
while(str2[index]!=0){
str_b[index2]=str2[index];
index++;
index2++;
}
*(pver+texture_index)=(float)atof(str_a);
*(pver+texture_index+1)=(float)atof(str_b);
vertex_index+=2;
}
if(str2[0]==''v'' && str2[1]==''n''){//load normals
index=2;
index2=0;
while(str2[index]!='' ''){
str_a[index2]=str2[index];
index++;
index2++;
}
index++;
while(str2[index]!='' ''){
str_b[index2]=str2[index];
index++;
index2++;
}
index++;
while(str2[index]!=0){
str_c[index2]=str2[index];
index++;
index2++;
}
*(pnor+normal_index)=(float)atof(str_a);
*(pnor+normal_index+1)=(float)atof(str_b);
*(pnor+normal_index+2)=(float)atof(str_c);
vertex_index+=3;
}
if(str2[0]==''f'' && str2[1]=='' ''){//load polys
index=2;
index2=0;
for(int x2=0;x2<9;x2++){
while(isdigit(str2[index])){
str3[index2]=str2[index];
index++;
index2++;
}//end while
index++;
*(ppoly+poly_index++)=atoi(str3);
index2=0;
}//end for
}//end if
}//end while !feof
fclose(file);
obj->tex=texindex;
obj->nump=poly;
obj->vers=pver;
obj->texcords=ptex;
obj->normals=pnor;
obj->polys=ppoly;
return(0);
}