Advertisement

Textures & Transparency

Started by January 14, 2002 08:12 AM
1 comment, last by Crash_Kid 23 years, 1 month ago
Hi, how can tell OpenGL that the color black in a texture is transparent ? I mean i''ve got a texture with a fire and a black background. So how can i set the black color transparent ? Thx
Adding an alpha channel is one way...


---
No game will ever rule more than CBT!
---Mikael Lax
Advertisement
Use an alpha texture.. instead of r,g,b, use r,g,b,a. I use a function like this:

  char *Load24BitTextureWithAlpha(char *fName, short &width, short &height, unsigned char r, unsigned char g, unsigned char b) //r,g,b are the r,g,b to make invisible! width/height are return values!{ FILE *in = fopen(fName); unsigned char *data; short tx,ty; unsigned long tSize, tOff; if (!in)  return 0; //0 = NULL, file not found! if (getc(in)!=''B'' || getc(in)!=''M'') //Valid bitmap file? {  fclose(in);  return 0; } fseek(in,18,0); width=getw(in); fseek(in,22,0); height=getw(in); fseek(in,28,0); if (getc(in)!=24) //24-bit image? {  fclose(in);  return 0; } tSize = width*height*4; data = new unsigned char[tSize]; if (!data) //Not enough memory! {  fclose(in);  return 0; } fseek(in,54,0); //Jump to beginning of header! for (ty=height-1;ty!=-1;--ty) //Bitmaps are upside down! {  tOff=ty*width*3;  for (tx=0;tx!=width;++tx)  {   data[tOff+2]=getc(in); //Blue   data[tOff+1]=getc(in); //Green   data[tOff+0]=getc(in); //Red//Check if this is our invisible color!   if (data[tOff+2]==b && data[tOff+1]==g && data[tOff+0]==r)    data[tOff+3]=0; //Alpha = invisible!   else    data[tOff+3]=255; //Alpha = visible!   tOff+=4;  } } fclose(in); return data;}int LoadTexture(char *fName)//Can change this to pass our invisible color if needed{ short width,height; int tNum; unsigned char *data; data = Load24BitTextureWithAlpha(fName,width,height,0,0,0); //0,0,0 = BLACK! if (!data) {  return -1; //Not loaded!! } glGenTextures(1,&tNum); blBindTexture(GL_TEXTURE_2D,tNum);glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); delete data; //Free our memory! return tNum; //Return our textures ID #!!!}  


Simply call:
FlameID = LoadTexture("Flame.bmp";

Billy - BillyB@mrsnj.com

Disclaimer: This was typed in this text-box, so may contain errors. If you need any further help, email me at the address above.

This topic is closed to new replies.

Advertisement