i'm having a weird problem with drawing a texture with alpha, i could'nt get my code to work, so i coppied the code right off the opengl game programing book, cacuts example and i still can't get it working right.
here's the texture loading code, it works.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIndex = 0;
unsigned char tempRGB;
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
{
return NULL;
}
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
if (bitmapFileHeader.bfType != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}
fread(bitmapInfoHeader,sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
if(!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}
fread(bitmapImage,1,bitmapInfoHeader->biSizeImage,filePtr);
if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}
for (imageIndex= 0; imageIndex < bitmapInfoHeader->biSizeImage; imageIndex += 3)
{
tempRGB = bitmapImage[imageIndex];
bitmapImage[imageIndex] = bitmapImage[imageIndex + 2];
bitmapImage[imageIndex + 2] = tempRGB;
}
fclose(filePtr);
return bitmapImage;
}
here's the alpha texture code, it does not work and i coppied and pasted it.
unsigned char *LoadBitmapFileWithAlpha(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
unsigned char *bitmapWithAlpha = (unsigned char *)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);
if (bitmapImage == NULL || bitmapWithAlpha == NULL)
return NULL;
// loop through the bitmap data
for (unsigned int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src +=3, dst +=4)
{
// if the pixel is black, set the alpha to 0. Otherwise, set it to 255.
if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 && bitmapImage[src+2] == 0)
bitmapWithAlpha[dst+3] = 0;
else
bitmapWithAlpha[dst+3] = 0xFF;
// copy pixel data over
bitmapWithAlpha[dst] = bitmapImage[src];
bitmapWithAlpha[dst+1] = bitmapImage[src+1];
bitmapWithAlpha[dst+2] = bitmapImage[src+2];
}
free(bitmapImage);
return bitmapWithAlpha;
}
and here's my initilization code.
BOOL Initialize (GL_Window* window, Keys* keys)
{
g_window = window;
g_keys = keys;
angle = 0.0f;
glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth (1.0f);
glDepthFunc (GL_LEQUAL);
glEnable (GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
bitmapData = LoadBitmapFileWithAlpha("checker2.bmp",&bitmapInfoHeader);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
BuildFont();
log("<p>Initilization Completed.");
log("<p>test %f", angle);
return TRUE;
}
and here's my drawing code.
void Draw (void)
{
static float y;
static float x = 0.25;
if (y > 180 || y < 0)
{
x = x / -1;
}
y +=x;
time(&start);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (0.0f, 0.0f, -6.0f);
glRotatef (angle, 0.0f, 1.0f, 0.0f);
glRotatef(y,1,0,0);
glColor3f(1,1,1);
DrawTextureCube(0,0,0);
gui();
glFlush();
time(&end);
dif = difftime(end,start);
}
and finaly here's the output,
incase angelfire screws me over with image hosting.
http://www.angelfire.com/d20/cibressus/alpha_texture_err.jpg
[edited by - cibressus on May 29, 2004 3:06:30 PM]
[edited by - cibressus on May 29, 2004 3:07:46 PM]