obj file problem

Started by
10 comments, last by neonoblivion 21 years, 8 months ago
Welp, show us a little more of your code ''n I may post mine when I get back to work Monday. My OBJ loader is certainly not perfect, but I think it should suffice for your purposes. Could compare code...
Advertisement
quote:Original post by neonoblivion
nope I''m using milkshape all models are triangulated(sp)
see


the problem list :

1. When reading in the normals and uv''s, you were incrementing the vertex_index rather than normal_index & uv_index respectively.

2. When reading in the vertices,normals & uv co-ords, the index accessing str_b & str_c was never reset causing the second and third values to be written somewhere in the middle of str_b & str_c respectively. When atof() was then called, the value returned was 0.000000 for all values.

3. You never terminated any of the strings causing a few funny glitches in the data.

4. I changed around the way that you were reading in the index values so that they were a little easier to place in the drawing function.

5. Variables were left unitialised which caused a couple of errors.


As a general tip, when trying to pull some values from a string, you''ll probably find sscanf a lot easier to use


  float x,y,z;char *str = "0.1 0.2 0.3";sscanf(str,"%f%f%f",&x,&y,&z);  


With the case of the face indices, you can use sscanf by placing the characters you want to ignore in there, ie


  int x,y,z;char *str = "1/2/3";sscanf(str,"%d/%d/%d",&x,&y,&z);  


Anyhow, I sent you a few resources, and here''s the working version :


  int displaymodel(float x,float y,float z,model* obj){	glPushMatrix();				//	use the translate command cos it''s a bit better!!!!		//		glTranslatef(x,y,z);				//	can place the begin and end around the for loop		glBegin(GL_TRIANGLES);				for(int i=0,j=0;i<obj->nump*3;i++,j+=3)		{			glNormal3f  (obj->normals [ 3*obj->polys[j  ]     ],						 obj->normals [ 3*obj->polys[j  ] + 1 ],						 obj->normals [ 3*obj->polys[j  ] + 2 ]);			glTexCoord2f(obj->texcords[ 2*obj->polys[j+1]     ],						 obj->texcords[ 2*obj->polys[j+1] + 1 ]);			glVertex3f  (obj->vers    [ 3*obj->polys[j+2]     ],						 obj->vers    [ 3*obj->polys[j+2] + 1 ],						 obj->vers    [ 3*obj->polys[j+2] + 2 ]);		}		glEnd();	glPopMatrix();	return(1);}int loadmodel(int texindex,model* obj,char* str){		int vers = 0,		texs = 0,		nors = 0,		poly = 0,		index = 0,		index2 = 0,		vertex_index=0,		texture_index=0,		normal_index=0,		poly_index = 0;	float *pver = NULL,		  *ptex = NULL,		  *pnor = NULL;		int   *ppoly = NULL;	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++;						}								str_a[index2] = NULL;				index2=0;				index++;							while(str2[index]!='' '')			{								str_b[index2]=str2[index];					index++;						index2++;						}								str_b[index2] = NULL;				index2=0;				index++;					while(str2[index]!=0)			{								str_c[index2]=str2[index];				index++;						index2++;						}				str_c[index2] = NULL;			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++;					}							str_a[index2] = NULL;			index2=0;					index++;					while(str2[index]!=0)			{							str_b[index2]=str2[index];						index++;							index2++;					}						str_b[index2] = NULL;							ptex[texture_index  ]=(float)atof(str_a);			ptex[texture_index+1]=(float)atof(str_b);			texture_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++;						}			str_a[index2] = NULL;			index2=0;						index++;							while(str2[index]!='' '')			{								str_b[index2]=str2[index];					index++;								index2++;					}					str_b[index2] = NULL;					index2=0;				index++;						while(str2[index]!=0)			{								str_c[index2]=str2[index];					index++;								index2++;						}					str_c[index2] = NULL;						pnor[normal_index  ]=(float)atof(str_a);			pnor[normal_index+1]=(float)atof(str_b);				pnor[normal_index+2]=(float)atof(str_c);					normal_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);					--ppoly[poly_index++];				index2=0;						}//end for				*/			//sorry, didn''t like the above			sscanf(&str2[2],"%d/%d/%d %d/%d/%d %d/%d/%d",					&ppoly[poly_index+2],&ppoly[poly_index+1],&ppoly[poly_index  ],					&ppoly[poly_index+5],&ppoly[poly_index+4],&ppoly[poly_index+3],					&ppoly[poly_index+8],&ppoly[poly_index+7],&ppoly[poly_index+6]);			for(int i=0;i<9;i++)			{				--ppoly[poly_index++];			}		}//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);}  

This topic is closed to new replies.

Advertisement