Textures and alpha channel

Started by
4 comments, last by JD 19 years, 6 months ago
I have a concern about displaying an image as a texture where a specific color in the image is treated as transparent. If I create the texture as GL_RGBA and then subtexture it with alpha values, with this create areas of transparency? the calls would look like this // create a texture from a 24bit chunk of data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data[i,0]); // add alpha values to the texture glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, adata[i,0]); the variable data, is a 2 dimenstional array that contains 24 bit color data in the BGR format. adata is a 2 dim array of single bytes. I have read through the data array populating the adata array based on the color in the data array, setting the adata to 255 or 0 depending on the color. However when i put the glTexSubImage2D call in, the entire texture disappears. My question is, is the concept correct and im just messing up the actual image memory? OR is the concept incorrect? What can I do to transfer a BGR image to texture and add an alpha value without actually resizing the BGR data into RGBA?
Advertisement
when you call glTexSubImage() you REPLACE the texels with the new data you are passing it, it doesnt do any sort of merge.

The sanest way to do what you want is to extend the data you pass in to RGBA and upload it all to a RGBA texture to start with.
Even when i specify the format as GL_ALPHA and only pass enough data for the alpha channel?
i assume u can only replace parts of textures with the same format
thus GL_RGBA -> GL_ALPHA is not OK, but GL_ALPHA->GL_ALPHA + GL_RGBA -> GL_RGBA is
I don't believe OpenGL provides a way for you to do that. There may be some obscure mask you can enable that tells OpenGL to write only to certain color channels, but I've never seen it. It is easy to write a function that adds an alpha channel and sets a specific color to be transparent. Some sample code would look like this:

CColor cTransparent; // Has r, g, b, and a membersunsigned char *pSource; // BGR formatunsigned char *pDest;   // RGBA formatint nSourceIndex = 0, nDestIndex = 0;for(int y=0; y<nHeight; y++){  for(int x=0; x<nWidth; x++)  {    CColor cTexel(pSource[nSourceIndex+2], pSource[nSourceIndex+1], pSource[nSourceIndex]);    pDest[nDestIndex] = cTexel.r;    pDest[nDestIndex+1] = cTexel.g;    pDest[nDestIndex+2] = cTexel.b;    pDest[nDestIndex+3] = (cTexel == cTransparent) ? 0 : 255;    nSourceIndex += 3;    nDestIndex += 4;  }}


(You can do this with one loop, but I tend to do it this way because I usually end up making use of the x and y variables when I write a loop like this.)
You can also use tga file format which has an alpha channel and some paint tools that can let you work with the alpha mask. Then in gl you can use alpha test to block color pixels from appearing based on their alpha values. You can reject pixels if their alpha is above some tresh hold. The alpha mask doesn't have to be all black or white, can be grayish if that's what you need.

This topic is closed to new replies.

Advertisement