Jump to content

  • Log In with Google      Sign In   
  • Create Account

Graphic_Programmer

Member Since 09 Aug 2012
Offline Last Active Aug 22 2012 03:38 AM

Topics I've Started

my obj loader problem

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.

My Screenshot Problem with SDL and OpenGL in C

11 August 2012 - 06:52 AM

Hi,
I wanted to write screen saver function for my mother OpenGL Suface yesterday.
First, i thinked this is very easy!
I wrote SDL_SaveBMP(screen,"deneme.bmp");
The End..
That can be comic for you but unluckily it did not be. Posted Image
The program showed an error message me and it was grotesque for me.
Then, I found a function from a web site but does not work correctly.When it saved a bmp file, it only showed a color on all surface.The color was color of glClearColor function.
The Screenshot Code:
int Screenshot(char *filename, SDL_Surface *screen)
{
SDL_Surface *temp;
unsigned char *pixels;
int i;

if (!(screen->flags & SDL_OPENGL))
{
  SDL_SaveBMP(temp, filename);
  return 0;
}

temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000FF, 0x0000FF00, 0x00FF0000, 0
#else
0x00FF0000, 0x0000FF00, 0x000000FF, 0
#endif
);
if (temp == NULL)
  return -1;
pixels = malloc(3 * screen->w * screen->h);
if (pixels == NULL)
{
  SDL_FreeSurface(temp);
  return -1;
}
	glReadBuffer(GL_FRONT_AND_BACK);
glReadPixels(0, 0,screen->w, screen->h,GL_RGB , GL_UNSIGNED_BYTE, pixels);
for (i=0; i<ekran->h; i++)
  memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (ekran->h-i-1), screen->w*3);

free(pixels);
SDL_SaveBMP(temp, filename);
SDL_FreeSurface(temp);
return 0;
}
Not : For first error the reason is not different between screen,"filename" and "filename",screen.SDL_SaveBMP and Screenshot are different fuctions.
Thanks.

A Problem in drawing 2D transparent shapes with ortho

09 August 2012 - 05:30 AM

First, my English isn't very good, i can say wrong words so sorry.
I am beginner to making 2D/3D Graphics in Opengl and i have a problem about drawing 3D shapes under 2D ortho graphics with alpha transparent.
The ScreenShot
Posted Image
I don't know could i tell. I think you understood my problem from the picture.
These are my drawing codes;

ciz()
{
glViewport(0,0,ewidth,eheight);
glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
   glPushMatrix();
    glOrtho(-1,1,-1,1,-20,20);
glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
    glColor4f(1,1,0,1);
    glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(((float)mouse_x-(ewidth/2))/(ewidth/2),-((float)mouse_y-(eheight/2))/(eheight/2),0.1);
glBindTexture(GL_TEXTURE_2D,NULL);
    glBegin(GL_POLYGON);
    for(i=0; i<20; i++)
{
	 glVertex3f(cos(18*i*pi/180)/32,sin(18*i*pi/180)/32,0);
}
glEnd();  
glColor4f(1,1,1,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,tex[0]);
glPopMatrix();
   /* glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex3f(-0.9,0.9,0);
glTexCoord2i(1,0); glVertex3f(-0.6,0.9,0);
glTexCoord2i(1,1); glVertex3f(-0.6,0.6,0);
glTexCoord2i(0,1); glVertex3f(-0.9,0.6,0);
glEnd();
 
   glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex3f(-0.6,0.9,0);
glTexCoord2i(1,0); glVertex3f(-0.3,0.9,0);
glTexCoord2i(1,1); glVertex3f(-0.3,0.6,0);
glTexCoord2i(0,1); glVertex3f(-0.6,0.6,0);
glEnd();*/
//glDisable(GL_BLEND);
//glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D,tex[1]);
glBegin(GL_QUADS);
for(i=0,j=0; i<=yazi_uz; i++)
{
  if(yazi[i]=='A')
  {
   ii=0;
   jj=0;
   glTexCoord2f((1+(float)ii+(float)ii*13)/512,(1+(float)jj+(float)jj*18)/256); glVertex3f(((0+(float)i*50 )/640)*2-1,-(((0+(float)j*80 )/480)*2-1),0);
   glTexCoord2f((1+(float)ii+((float)ii+1)*13)/512,(1+(float)jj+(float)jj*18)/256); glVertex3f(((0+((float)i+1)*50 )/640)*2-1,-(((0+(float)j*80 )/480)*2-1),0);
   glTexCoord2f((1+(float)ii+((float)ii+1)*13)/512,(1+(float)jj+((float)jj+1)*18)/256); glVertex3f(((0+((float)i+1)*50 )/640)*2-1,-(((0+((float)j+1)*80 )/480)*2-1),0);
   glTexCoord2f((1+(float)ii+(float)ii*13)/512,(1+(float)jj+((float)jj+1)*18)/256); glVertex3f(((0+(float)i*50 )/640)*2-1,-(((0+((float)j+1)*80 )/480)*2-1),0);
  }
}
glEnd();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
   glPopMatrix();
    gluPerspective(45,(float)(ewidth/eheight),.1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(c_x+cos((dir+180)*pi/180)*cos(-der*pi/180)*uz,c_y+sin((dir+180)*pi/180)*cos(-der*pi/180)*uz,c_z+sin(-der*pi/180)*uz,c_x,c_y,c_z, 0,1,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glColor3f(1,1,1);
//glDisable(GL_TEXTURE_2D);
/* glBegin(GL_POLYGON);
glTexCoord2i(0,0);
glVertex3f(-15,15,0);
glTexCoord2i(1,0);
glVertex3f(15,15,0);
glTexCoord2i(1,1);
glVertex3f(15,-15,0);
glTexCoord2i(0,1);
glVertex3f(-15,-15,0);
glEnd();*/
//glEnable(GL_TEXTURE_2D);
/*glBegin(GL_TRIANGLES);
for(i=0; i<10; i++)
{
  glNormal3f(cos((i+0.5)*36*pi/180)*cos(90-atan(10/7)*pi/180),sin((i+0.5)*36*pi/180)*cos(90-atan(10/7)*pi/180),sin(90-atan(10/7)*pi/180));
  glVertex3f(0,0,5);
  glVertex3f(cos(36*i*pi/180)*7,sin(36*i*pi/180)*7,-5);
  glVertex3f(cos(36*(i+1)*pi/180)*7,sin(36*(i+1)*pi/180)*7,-5);
}
glEnd();*/
glColor4f(1,1,1, 1);
//glDisable(GL_DEPTH_TEST);
//glDepthMask(GL_TRUE);
//glEnable(GL_ALPHA_TEST);
    glBindTexture(GL_TEXTURE_2D, tex[2]);

glPushMatrix();
glTranslatef(obj[0].x,obj[0].y,obj[0].z);
glScalef(0.5,0.5,0.5);
glRotatef(der2*5,0.7,0.3,0.5);
//glBindTexture(GL_TEXTURE_2D,tex[1]);
glBegin(GL_QUADS);
//alt
glNormal3f(0,0,-1);
glTexCoord2i(0,0);
glVertex3f(-8,8,-8);
glTexCoord2i(1,0);
glVertex3f(8,8,-8);
glTexCoord2i(1,1);
glVertex3f(8,-8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
//üst
glNormal3f(0,0,1);
glTexCoord2i(0,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,8);
//sağ
glNormal3f(1,0,0);
glTexCoord2i(0,0);
glVertex3f(8,8,-8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,8);
glTexCoord2i(0,1);
glVertex3f(8,-8,-8);
//sol
glNormal3f(-1,0,0);
glTexCoord2i(0,0);
glVertex3f(-8,8,-8);
glTexCoord2i(1,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,1);
glVertex3f(-8,-8,8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
//yukarı
glNormal3f(0,1,0);
glTexCoord2i(0,0);
glVertex3f(-8,8,8);
glTexCoord2i(1,0);
glVertex3f(8,8,8);
glTexCoord2i(1,1);
glVertex3f(8,8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,8,-8);
//aşağı
glNormal3f(0,-1,0);
glTexCoord2i(0,0);
glVertex3f(-8,-8,8);
glTexCoord2i(1,0);
glVertex3f(8,-8,8);
glTexCoord2i(1,1);
glVertex3f(8,-8,-8);
glTexCoord2i(0,1);
glVertex3f(-8,-8,-8);
glEnd();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
/* glClearColor(1,0,0,1);
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);*/
//resim_yukle("font.bmp");
SDL_GL_SwapBuffers();
}
Help please, Thanks for your helps.

PARTNERS