my obj loader problem

Started by
2 comments, last by Graphic_Programmer 11 years, 8 months ago
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.
kamyons.png
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!='\0'; i++)
{
ydizi[i+1];
hdizi=ydizi;
}
hdizi='\0';
}
char satir_kopyala(FILE * dosya_gostergesi,char hdizi[])
{
char ww;
i=0;
while(1)
{
ww=getc(dosya_gostergesi);
hdizi[i+1];
hdizi=ww;
if(ww=='\n')
{
hdizi='\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=='0' ||
satir=='1' ||
satir=='2' ||
satir=='3' ||
satir=='4' ||
satir=='5' ||
satir=='6' ||
satir=='7' ||
satir=='8' ||
satir=='9' ||
satir=='.') &&
asama%2==0)
{
nokta[asama%2]=i;
asama++;
}
else if ((satir!='0' &&
satir!='1' &&
satir!='2' &&
satir!='3' &&
satir!='4' &&
satir!='5' &&
satir!='6' &&
satir!='7' &&
satir!='8' &&
satir!='9' &&
satir!='.')&&
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=='0' ||
satir=='1' ||
satir=='2' ||
satir=='3' ||
satir=='4' ||
satir=='5' ||
satir=='6' ||
satir=='7' ||
satir=='8' ||
satir=='9' ||
satir=='.') &&
asama%2==0)
{
nokta[asama%2]=i;
asama++;
}
else if ((satir!='0' &&
satir!='1' &&
satir!='2' &&
satir!='3' &&
satir!='4' &&
satir!='5' &&
satir!='6' &&
satir!='7' &&
satir!='8' &&
satir!='9' &&
satir!='.')&&
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[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[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[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[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[j*3+2]][0],normal[ucgennn[j*3+2]][1],normal[ucgennn[j*3+2]][2]);
glVertex3d(vertex[ucgennn[j*3]][0],vertex[ucgennn[j*3]][1],vertex[ucgennn[j*3]][2]);
}
}
glEnd();
glPopMatrix();

Thanks.
Advertisement
Is there nobody to help?
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.
Check out my open source code projects/libraries! My Homepage You may learn something.
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.

This topic is closed to new replies.

Advertisement