Jump to content

  • Log In with Google      Sign In   
  • Create Account

my obj loader problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Graphic_Programmer   Members   -  Reputation: 107

Like
0Likes
Like

Posted 18 August 2012 - 06:48 AM

Hi,
I wrote my own obj loader codes and printed values of vertex,texture,normal and face to screen.The values in console screen was correct values but i cant draw the model properly.It looked distorted.
Posted Image
These are my codes about to load obj model:

#include <stdio.h>
#include <math.h>
int i,a,b,isaret,bb;
int chartoint(char chr)
{
if(chr=='0') return 0;
else if(chr=='1') return 1;
else if(chr=='2') return 2;
else if(chr=='3') return 3;
else if(chr=='4') return 4;
else if(chr=='5') return 5;
else if(chr=='6') return 6;
else if(chr=='7') return 7;
else if(chr=='8') return 8;
else if(chr=='9') return 9;
else return 0;
}
char satir_atla(FILE * dosya_gostergesi)
{
char ww;
while(! feof(dosya_gostergesi))
{
  ww=getc(dosya_gostergesi);
  if(ww=='\n')
   break;
}
ww=getc(dosya_gostergesi);
return ww;
}
mtn_kopyala(char ydizi[],char hdizi[])
{
for(i=0; ydizi[i]!='\0'; i++)
{
  ydizi[i+1];
  hdizi[i]=ydizi[i];
}
hdizi[i]='\0';
}
char satir_kopyala(FILE * dosya_gostergesi,char hdizi[])
{
char ww;
i=0;
while(1)
{
  ww=getc(dosya_gostergesi);
  hdizi[i+1];
  hdizi[i]=ww;
  if(ww=='\n')
  {
hdizi[i]='\0';  break;
  }
  i++;
}
ww=getc(dosya_gostergesi);
return ww;
}
main()
{
FILE * gosterge;
int yedek,sayiin,xxx,yyy,tip,atla,asama,nokta[3],j,ucgen_sayisi=0,vrtx=0,textu=0,nrml=0,ucgennn[10000][9];
char aaaa,satir[100],deneme[]="Aptal",deneme2[]="akilli";
double sayiif=0,texture[10000][3],vertex[10000][3],normal[10000][3];
gosterge=fopen("ucgen2.obj","r");
aaaa=getc(gosterge);
while(! feof(gosterge))
{
if(aaaa=='#')
{
  aaaa=satir_atla(gosterge);
}
else if(aaaa=='v')
{
  aaaa=getc(gosterge);
  if(aaaa==' ') {tip=0;}
  else if(aaaa=='t') {tip=1;}
  else if(aaaa=='n')  {tip=2;}
  //satırı çözümlemeye başla

  {
   atla=1;
   asama=0;
   aaaa=satir_kopyala(gosterge,satir);
   for(i=0; 1; i++)
   {
	if((satir[i]=='0' ||
		satir[i]=='1' ||
	 satir[i]=='2' ||
	 satir[i]=='3' ||
	 satir[i]=='4' ||
	 satir[i]=='5' ||
	 satir[i]=='6' ||
	 satir[i]=='7' ||
	 satir[i]=='8' ||
	 satir[i]=='9' ||
	 satir[i]=='.') &&
	asama%2==0)
  {
  nokta[asama%2]=i;
  asama++;
  }
else if ((satir[i]!='0' &&
			  satir[i]!='1' &&
		   satir[i]!='2' &&
		   satir[i]!='3' &&
		   satir[i]!='4' &&
		   satir[i]!='5' &&
		   satir[i]!='6' &&
		   satir[i]!='7' &&
		   satir[i]!='8' &&
		   satir[i]!='9' &&
		   satir[i]!='.')&&
		   asama%2==1	  )
{
	   nokta[asama%2]=i-1;
	asama++;
	atla=0;
}
if(!atla)
{
  if((asama%2)==0)
  {
		 sayiif=0;
		 if(satir[nokta[0]-1]=='-') isaret=-1; else isaret=1;
					
		 for(yyy=0, j=nokta[1]; j>=nokta[0]; j--)
   {
		  sayiif+=(chartoint(satir[j])*pow(10,(double)yyy));
	if(satir[j]=='.')
	 nokta[2]=j;
	 else yyy++;
   }
   sayiif/=pow(10,(double)(nokta[1]-nokta[2]));
   sayiif*=isaret;
   //printf("%f\n",sayiif);//sayiif/=1;//(float)pow(10,(double)(nokta[2]-j));
   switch(tip)
   {
	case 0:
		   vertex[vrtx][asama/2-1]=sayiif;
	 atla=1;
	case 1:
		   texture[textu][asama/2-1]=sayiif;
	 atla=1;
	default:
		   normal[nrml][asama/2-1]=sayiif;
	 atla=1;
	 break;
   }
  }
}  
if(tip==2)
{
  if(asama==6)
  {
   nrml++;
   break;
  }
}
else if(tip==1)
{
  if(asama==4)
  {
   textu++;
   break;
  }
}
else if(tip==0)
{
  if(asama==6)
  {
   vrtx++;
   break;
  }
}
   }
  }
}
else if(aaaa=='f')
{
  //satırı çözümlemeye başla
  {
   atla=1;
   asama=0;
   aaaa=satir_kopyala(gosterge,satir);
   for(i=0; 1; i++)
   {
	if((satir[i]=='0' ||
		satir[i]=='1' ||
	 satir[i]=='2' ||
	 satir[i]=='3' ||
	 satir[i]=='4' ||
	 satir[i]=='5' ||
	 satir[i]=='6' ||
	 satir[i]=='7' ||
	 satir[i]=='8' ||
	 satir[i]=='9' ||
	 satir[i]=='.') &&
	asama%2==0)
  {
	 nokta[asama%2]=i;
  asama++;
  }
else if ((satir[i]!='0' &&
			  satir[i]!='1' &&
		   satir[i]!='2' &&
		   satir[i]!='3' &&
		   satir[i]!='4' &&
		   satir[i]!='5' &&
		   satir[i]!='6' &&
		   satir[i]!='7' &&
		   satir[i]!='8' &&
		   satir[i]!='9' &&
		   satir[i]!='.')&&
		   asama%2==1	  )
{
	  
	   nokta[asama%2]=i-1;
	asama++;
	atla=0;
}
if(!atla)
{
  if((asama%2)==0)
  {
		 sayiin=0;
		 yedek=0;
		 for(yyy=0, j=nokta[1]; j>=nokta[0]; j--,yyy++)
   {
		  yedek=(int)(chartoint(satir[j]));
		  for(b=0; b<yyy; b++)
		  {
		   yedek*=10;		
		  }
		  sayiin+=yedek;
   }
		 ucgennn[ucgen_sayisi][asama/2-1]=(int)sayiin;
		 atla=1;
  }
}  
  if(asama==18)
  {
   ucgen_sayisi++;
   break;
  }
   }
  }
}
else
{
  aaaa=getc(gosterge);
}
}
fclose(gosterge);
//printf("%d %d %d %f\n",nokta[0],nokta[1],t, sayiif);
for(i=0; i<vrtx; i++)
{
printf("v ");
for(j=0; j<3; j++)
{
printf("%f ",vertex[i][j]);
if(j==2) printf("\n");
}
}
printf("\n");
for(i=0; i<textu; i++)
{
printf("vt ");
for(j=0; j<2; j++)
{
printf("%f ",texture[i][j]);
if(j==1) printf("\n");
}
}
printf("\n");
for(i=0; i<nrml; i++)
{
printf("vn ");
for(j=0; j<3; j++)
{
printf("%f ",normal[i][j]);
if(j==2) printf("\n");
}
}
printf("\n");
for(i=0; i<ucgen_sayisi; i++)
{
printf("f ");
for(j=0; j<9; j++)
{
  if((j%3)!=0) printf("/");
  else
  printf(" ");
  printf("%i",ucgennn[i][j]);
  if(j==8) printf("\n");
}
}
while(1)
{
}  
}

I tried the codes in Dev C++.
You can try it to see values.For now, no problem but i couldnt draw properly when i tried these codes in Visual C++ with fist codes.
glPushMatrix();
glScaled(1,1,1);
glRotated(0,0,0,0);
glTranslated(0,0,0);
glBegin(GL_TRIANGLES);
for(i=0; i<ucgen_sayisi; i++)
{
  for(j=0; j<3; j++)
  {
   glNormal3d(normal[ucgennn[i][j*3+2]][0],normal[ucgennn[i][j*3+2]][1],normal[ucgennn[i][j*3+2]][2]);
   glVertex3d(vertex[ucgennn[i][j*3]][0],vertex[ucgennn[i][j*3]][1],vertex[ucgennn[i][j*3]][2]);
  }
}
glEnd();
glPopMatrix();
Thanks.

Edited by Graphic_Programmer, 18 August 2012 - 12:07 PM.


Sponsor:

#2 Graphic_Programmer   Members   -  Reputation: 107

Like
0Likes
Like

Posted 18 August 2012 - 10:14 AM

Is there nobody to help?

#3 ajm113   Members   -  Reputation: 311

Like
0Likes
Like

Posted 18 August 2012 - 12:04 PM

It's difficult for a lot of people to run through your code because it's partly in another language, so it's hard to debug. Not to be rude or anything, but if your going to write code in another language you may want to post it on a website that speaks that language. Just a wild guest, but it appears your indexes are out of wack. So just step through each line of your code and see if your indexes are calling the correct vertexes. Make a simple triangulated quad and see if everything is in the right order. Also you may want to format your code a little better so others can read whats going on so people can assist you better.

If it at all helps you can look at my OBJ loader in my Obj to JSON converter to help structure your code for example. Just click the link in my sig to get the source code. It doesn't do OpenGL calls, but I'm sure you can get the idea.

P.S ditch DEV C++, just get VS2010-2012. That program is older then I am and you will fall in love with Visual Studio for sure.

Edited by ajm113, 18 August 2012 - 12:14 PM.

Check out my open source code projects/libraries! My Homepage You may learn something.

#4 Graphic_Programmer   Members   -  Reputation: 107

Like
0Likes
Like

Posted 18 August 2012 - 12:25 PM

OKEEEEYY !!!
I got it ! I understood why it dont draw.The reason of problem is a index.I used the index from 1 to end value but i might use from 0 to end-1.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS