Jump to content
  • Advertisement
Sign in to follow this  
vbbartlett

Textures and alpha channel

This topic is 5412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 members
unsigned char *pSource; // BGR format
unsigned char *pDest; // RGBA format

int 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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!