PDA

View Full Version : Texture mapping - doesn't work



DeadHead
07-26-2009, 04:32 AM
Hello, everyone.
First of all, I did a lot of reading and coding myself before posting this one.

I'm writing an OpenGL-course final work, and the texture just doesn't appear on the object. Can someone please explain to me what exactly is wrong with the functions?


/* Load a bitmap from file and "binding" the texture */
int myLoadBitmap(char *filename)
{
int i, j=0;
int num_texture = -1;
FILE *l_file;
unsigned char *l_texture;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++;
if((l_file = fopen(filename,"rb"))==NULL)
return (-1);
fread(&fileheader, sizeof(fileheader), 1, l_file);
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&infoheader, sizeof(infoheader), 1, l_file);
l_texture = (byte *)malloc(infoheader.biWidth*infoheader.biHeight*4) ;
memset(l_texture, 0, infoheader.biWidth*infoheader.biHeight*4);
for(i=0; i<infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&amp;rgb, sizeof(rgb), 1, l_file);
l_texture[j+0] = rgb.rgbtRed;
l_texture[j+1] = rgb.rgbtGreen;
l_texture[j+2] = rgb.rgbtBlue;
l_texture[j+3] = 255;
j+=4;
}
fclose(l_file);
glBindTexture(GL_TEXTURE_2D, num_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture);
return (num_texture);
}

/* Use the previous function to cylinder (which is barrel-"baraban" with a bullets in the revolver model) */
void baraban()
{
int texture = myLoadBitmap("..\\textures\\baraban.bmp");
if (!texture)
{
printf ("Texture did not load\n");
glutDestroyWindow(win);
exit(0);
}
/*--------------------------------------------------------*/
glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);
/*----------------------------------------------------*/
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D); /* activate */
gluCylinder(b, 1.0, 1.0, 2.0, 10, 10);
glDisable(GL_TEXTURE_2D); /* d-activate */
/*---------------------------------------------------*/
gluDisk(d, 0.0, 1.0, 10, 2);
glTranslatef(0.0, 0.0, 2.0);
gluDisk(d, 0.0, 1.0, 10, 2);
glPopMatrix();
}

Full source code -> HERE (http://depositfiles.com/files/kglrxfkm3)
:confused:
P.S. I'm using free Borland C Compiler 5.5

Ilian Dinev
07-26-2009, 04:45 AM
remove these:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

DeadHead
07-26-2009, 06:49 AM
Removing those took no effect...

awhig
07-26-2009, 07:22 AM
Do not free l_texture. Within your myLoadBitmap() , you are generating image and then destroying. Free your image in main() after disabling texture or after mapping.

DeadHead
07-26-2009, 07:38 AM
I did changes, as you mentioned, and it didn't work too...


int myLoadBitmap(char *filename, unsigned char *l_texture)
{
int i, j=0;
int num_texture = -1;
FILE *l_file;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++;
if((l_file = fopen(filename,"rb"))==NULL)
return (-1);
fread(&amp;fileheader, sizeof(fileheader), 1, l_file);
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&amp;infoheader, sizeof(infoheader), 1, l_file);
l_texture = (byte *)malloc(infoheader.biWidth*infoheader.biHeight*4) ;
memset(l_texture, 0, infoheader.biWidth*infoheader.biHeight*4);
for(i=0; i<infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&amp;rgb, sizeof(rgb), 1, l_file);
l_texture[j+0] = rgb.rgbtRed;
l_texture[j+1] = rgb.rgbtGreen;
l_texture[j+2] = rgb.rgbtBlue;
l_texture[j+3] = 255;
j+=4;
}
fclose(l_file);
glBindTexture(GL_TEXTURE_2D, num_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
//free(l_texture);
return (num_texture);
}

void baraban()
{
unsigned char *l_tex = NULL;
int texture = myLoadBitmap("..\\textures\\baraban.bmp", l_tex);
if (!texture)
{
printf ("Texture did not load\n");
glutDestroyWindow(win);
exit(0);
}

glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
gluCylinder(b, 1.0, 1.0, 2.0, 10, 10);
glDisable(GL_TEXTURE_2D);
free(l_tex);

gluDisk(d, 0.0, 1.0, 10, 2);
glTranslatef(0.0, 0.0, 2.0);
gluDisk(d, 0.0, 1.0, 10, 2);
glPopMatrix();
}

Ilian Dinev
07-26-2009, 08:41 AM
Try using
glGenTextures(1,&amp;num_texture)
instead of assigning 0...N names.

deleting l_texture is of no consequence, as the data is already uploaded.

DeadHead
07-26-2009, 08:47 AM
instead of assigning 0...N names
Which lines exactly? :p

vivek vyas
07-26-2009, 10:47 AM
i m not sure but

what is significance of
int num_texture = -1;

as perhaps it
always give -1(failed in my means(i m not sure about this as it may/or not(local scope)) modified)

awhig
07-26-2009, 11:20 AM
He is incrementing num_Texture after few lines so its value becomes 0.

awhig
07-26-2009, 11:24 AM
int texture = myLoadBitmap("..\\textures\\baraban.bmp", l_tex);
if (!texture)
{
printf ("Texture did not load\n");
glutDestroyWindow(win);
exit(0);
}



The return value is 0.Inside myLoadBitmap(), the num_texture is incremented to 0.




/*----------------------------------------------------*/
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D); /* activate */
gluCylinder(b, 1.0, 1.0, 2.0, 10, 10);
glDisable(GL_TEXTURE_2D); /* d-activate */
/*---------------------------------------------------*/



Where are you mapping your texture to the cylinder??
Something like for a drawQuad() we supply texel values.

glTextureCoord2f();
glVertex()...

??

DeadHead
07-26-2009, 11:49 AM
Where are you mapping your texture to the cylinder??
Something like for a drawQuad() we supply texel values.

glTextureCoord2f();
glVertex()...

??
I guess, this is the exact problem :eek:
I simply didnt know I have to. Can you assist me? Where should I em... map the texture?

RigoN
07-26-2009, 11:54 AM
Hi!!!
View documentation for GLU, on Quadrics, and drew attention to gluQuadricNormals, gluQuadricTexture ...

awhig
07-26-2009, 12:06 PM
Rightly said by Rigon. Visit this http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18

You need to let quadratic generate texture coordinates.

Other way is to approximate your cylinder as a faceted object, then at each plane (taken as a quad) , map your texture( if you do not want to automatically generate texels).