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(&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("..\ extures\\baraban.bmp");
if (!texture)
{
printf ("Texture did not load
");
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
P.S. I’m using free Borland C Compiler 5.5