Jump to content
  • Advertisement
Sign in to follow this  
SilverLogic

Blending only black...

This topic is 4992 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

Ok, i just decided to add a little HUD to my game and now its making me mad, what blend function would you use to make ONLY the black part of an RGB image transparent? I cant loop through the pixels to assign an alpha value (im using sdl, so i dont think i can access the individual pixel data), and i dont want to use masking... http://www.logizone.com/images/trans_0.jpg Thats the result im getting by using glBlendFunc(GL_SRC_ALPHA, GL_ONE); i tryed a few other combinations but im not getting the result i want, please help!

Share this post


Link to post
Share on other sites
Advertisement
Did you try using glBlendFunc(GL_SRC_ALPHA,GL_SRC_ALPHA_MINUS_ONE)?

Not that I think it'll help, but it's worth a shot anyway.

Sorry, I honestly don't know how to help you besides to use a picture format that supports an alpha channel. What are you using atm?

Share this post


Link to post
Share on other sites
Quote:
Original post by SilverLogic
Ok, i just decided to add a little HUD to my game and now its making me mad, what blend function would you use to make ONLY the black part of an RGB image transparent?


well, correct me if i'm wrong, but how can you blend with no alpha values? why not just open up the bitmap in whatever paint program you use and assign all black pixels to be totally aplha'd out. load the image as RGBA then use your blend function. it should work fine. otherwise, if you have no alpha channel in your image i don't understand how openGL would blend out only the black.

-me

Share this post


Link to post
Share on other sites
i think it would be easiest here if you just rebuilt the image with the custom alpha map, if you only do it at load time it shouldnt be a problem.

here is a source snippet i put together for you, tell me if it works :)


// pixel data structure for ease
struct pixel_t {
BYTE r,g,b,a;
};

// pixel functions (see below for implementation)
pixel_t getPixel(BYTE *img, int size, int x, int y);
void setPixel(BYTE *img, int size, int x, int y, pixel_t& px);

// ***********************************************
// creates an alpha channel, returns the new OpenGL texture id
GLuint createAlphaChannel( GLuint glTextureName, int size ) {

// storage for readback an new image
BYTE *imageData = new BYTE[size * size * 4];

glBindTexture( GL_TEXTURE_2D, glTextureName );
glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData );

// create the new image with the alpha
for(int x=0; x<size; x++) {
for(int y=0; y<size; y++) {
pixel_t p = getPixel(imageData, size, x, y);
if( p.r == 0 && p.g == 0 && p.b == 0 ) {
p.a = 0; //set alpha to zero
setPixel(imageData, size, x, y, p);
}
}
}

GLuint newTexture = 0;
glGenTextures(1, &newTexture);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

// build the new image with the alpha map
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, size, size, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

delete [] imageData;
imageData = NULL;

return newTexture;
}

// ****************************************************
// These functions may not work, but should be trivial
// to fix.
// ****************************************************

// gets a pixel, only works with power of two image
pixel_t getPixel(BYTE *img, int size, int x, int y) {
pixel_t px;

px.r = img[x + (y * size) * 4 + 0];
px.g = img[x + (y * size) * 4 + 1];
px.b = img[x + (y * size) * 4 + 2];
px.a = img[x + (y * size) * 4 + 3]; //if the image is RGB, glGetTexImage will set A = 1

return px;
}

// sets a pixel, only works with power of two image
void setPixel(BYTE *img, int size, int x, int y, pixel_t& px) {
img[x + (y * size) * 4 + 0] = px.r;
img[x + (y * size) * 4 + 1] = px.g;
img[x + (y * size) * 4 + 2] = px.b;
img[x + (y * size) * 4 + 3] = px.a;
}


Share this post


Link to post
Share on other sites
No it doesnt work, first off, the image im using isnt a square image, and second, im using sdl, so i cant modify the pixel data like ordinary, so thats kinda useless to me, i said in my first post i couldnt modify the pixel data :/. thx for the attempt tho

Share this post


Link to post
Share on other sites
Hi
MMMMM as everyone said... hard to blend if no blend info.

a. Why don't you add alpha info and make your life easier?
b. I you dont want it, just blend using color/invsrccolor (as oposed to standar alpha/invsrcalpha). That way black color will be treated as transparent, but other colors will be blended at 50% opacity. If you want more opacity, render the same image again.

Its the only think that comes to my mind in this case. Sorry if not much help.

Luck!
Guimo


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!