PDA

View Full Version : how to make texture background transparent?



beginner620824
05-01-2003, 01:09 AM
Hi,
i have a question about the message below regarding how to draw a tree posted long time ago. After loading Bmp into my own structure allocated in mem and making changes to it, how do i bind this Bmp (to assign its ID)? anyone be so kind as to give me some detial? I only know assign ID with auxDIBImageLoad(Filename);
Thanks


************************************
generate an alpha channel to the tree.bmp so when the color is black the alpha value is 0 and else 255, then use blending with glBlendfunc(gl_src_alpha, gl_one_minus_src_alpha) or glAlphaFunc(GL_Greater,0.5)
there are other ways, but try this first.

shinpaughp
05-01-2003, 06:24 AM
glGenTextures for id
glBindTexture to bind the texture id with a target
glTexImage to load the texture data to a target
glTexParameter to change texture parameters
glTexEnv to change texture environment parameters

beginner620824
05-01-2003, 07:22 AM
hi
thanks for reply. But i didn't get what i wanted. Below is my code to convert RGB to RGBA. My question is after calling this, what should I do? I know how to load and bind a BMP without making any change to it.(now the converted "file" is in the memory).

If anyone be so kind to answer, please give me such an answer/instruction that i know how to follow.
Thanks

typedef struct
{
GLubyte* colors;
int width;
int height;

} Texture;

void loadBitmap(char* filename, Texture* tex, int trans_color[3])
{
FILE* bmpfile;
BITMAPFILEHEADER bmfileheader;
BITMAPINFOHEADER bminfoheader;
RGBTRIPLE rgb;

int i,j,texpos = 0;

bmpfile = fopen(filename, "rb");

if (!bmpfile)
exit(1);

fread(&bmfileheader, sizeof(BITMAPFILEHEADER), 1, bmpfile);
fread(&bminfoheader, sizeof(BITMAPINFOHEADER), 1, bmpfile);


tex->width = bminfoheader.biWidth;
tex->height = bminfoheader.biHeight;


tex->colors = (GLubyte*)malloc(tex->width * tex->height *4);

// read color
for (i = 0; i < tex->width; i++)
{
for(j = tex->height - 1; j>=0 ; j--)
{ // is coord (1.0).
fread(&rgb, sizeof(RGBTRIPLE),1,bmpfile);
tex->colors[texpos] = rgb.rgbtRed ;
tex->colors[texpos+1] = rgb.rgbtGreen;
tex->colors[texpos+2] = rgb.rgbtBlue;

//test if color should be transparent
if ((rgb.rgbtRed==trans_color[0])&&(rgb.rgbtGreen==trans_color[1])&&(rgb.rgbtGreen==trans_color[2]))
tex->colors[texpos+3] = 0;
else
tex->colors[texpos+3] = 255;

texpos += 4;
}

}
fclose(bmpfile);
}

shinpaughp
05-01-2003, 08:09 AM
There isn't much difference. If you converted from GL_RGB to GL_RGBA then the format parameter you pass to glTexImage will be GL_RGBA instead of GL_RGB, and your internal format should also be GL_RGBA. Everythng else should probably be the same.

beginner620824
05-01-2003, 09:13 AM
hi

I had just fixed it by searching some old message here before reading your reply. you are right, it's because my poor understanding of glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->colors);
Thank you all