keyed images. The new function which I have so far takes in an additional boolean argument which is true if all black colored pixels
should be keyed out. Otherwise, if it is false, the function should work normally. By the way, it works fine whenever I don't use color keying... I just don't see any transparency when I do use color keying and it looks entirely the same as if I didn't use it all. Here is my function:
void CreateTexture( unsigned int* intPtr, char* filename, bool color_key)
{
int channels;
unsigned char * newData;
SDL_Surface *pBitmap[1];
pBitmap[0] = IMG_Load( filename );
if(pBitmap[0] == NULL)
return;
glGenTextures(1, intPtr );
glBindTexture(GL_TEXTURE_2D, *intPtr );
int width = pBitmap[0] -> w;
int height = pBitmap[0] -> h;
unsigned char * data = (unsigned char *) (pBitmap[0] -> pixels);
if( !color_key)
{
channels = 3;
newData = new unsigned char[width*height*3];
}
else
{
channels = 4;
newData = new unsigned char[width*height*4];
}
int BytesPerPixel = pBitmap[0] -> format -> BytesPerPixel ;
for( int i = 0 ; i < (height / 2) ; ++i )
for( int j = 0 ; j < width * BytesPerPixel; j += BytesPerPixel )
for(int k = 0; k < BytesPerPixel; ++k)
{
unsigned char temp = data[ (i * width * BytesPerPixel) + j + k];
data[ (i * width * BytesPerPixel) + j + k] = data[ ( (height - i - 1) * width * BytesPerPixel ) + j + k];
data[ ( (height - i - 1) * width * BytesPerPixel ) + j + k] = temp;
}
for(i = 0; i < (width * height); ++i)
{
unsigned char r,g,b;
Uint32 pixel_value = 0;
for(int j = BytesPerPixel - 1 ; j >=0; --j)
{
pixel_value = pixel_value << 8;
pixel_value = pixel_value | data[ (i * BytesPerPixel) + j ];
}
SDL_GetRGB(pixel_value, pBitmap[0] -> format, (Uint8 *)&r, (Uint8 *)&g, (Uint8 *)&b);
newData[(i * channels) + 0] = r;
newData[(i * channels) + 1] = g;
newData[(i * channels) + 2] = b;
if( color_key )
{
if( r == 0 && g == 0 && b == 0 )
newData[(i * channels) + 3] == 0;
else
newData[(i * channels) + 3] == 255;
}
pixel_value = 0;
}
if( color_key )
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap[0]->w, pBitmap[0]->h, GL_RGBA, GL_UNSIGNED_BYTE, newData);
else
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap[0]->w, pBitmap[0]->h, GL_RGB, GL_UNSIGNED_BYTE, newData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
delete [] newData;
SDL_FreeSurface(pBitmap[0]);
}
Then in my rendering loop I do this:
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
before I actually enable my texturing and draw my textured polies. But the black colored part of the texture still shows.I have no idea what I could be doing wrong.
Any help would greatly be appreciated.
[edited by - Khaos Dragon on June 2, 2004 12:10:38 AM]
[edited by - Khaos Dragon on June 2, 2004 12:11:30 AM]
[edited by - Khaos Dragon on June 2, 2004 12:14:37 AM]
[edited by - Khaos Dragon on June 2, 2004 12:16:14 AM]