Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Texture Blending

This topic is 4441 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 cant seem to get texture blending to work right. Im using SDL to load in a .tga file. The problem is I have to pass the file format to a gl function. The file formats are represented by #defines. (for instance GL_RGBA). After alot of tinkering and frustration I figered that the file format for my .tga would be GL_ARGB but I searched my include file and couldnt find it. There was a GL_ABGR_EXT which worked the best but still screwed up my colors when I tried it. So my question is is there anyway to use texture blending without having alpha values in the file? If that were the case I could just use .bmp (which I perfer). Or mabey theres a quick and easy way to reformat my .tga? Im new to graphics programming and Opengl so my problem may not be what I think it is. I would post source code but Im not home at the moment. Thanks for the help.

Share this post

Link to post
Share on other sites
there is a way to add transparancy to a certain color by loading a tga or bitmap or any 24bit color format, but as you read the bitmap you would have to convert the entire bitmap into 32bit and add the alpha value for specific colors you want.

this is how I made 255 0 255 color 100% transparent when loading a 24-bit bitmap...

#define rgb32bit(r,g,b,a) (a<<24) | (r<<16) | (g<<8) | (b)

for(index = 0; index < BFsize; index+=3)
BYTE alpha = 255;

BYTE red = buffer[index];
BYTE green = buffer[index+1];
BYTE blue = buffer[index+2];

if( red == 255 && blue == 255 )
alpha = 0;

bitmap_file->buffer[index32] = rgb32bit(red, green, blue, alpha);

I think it would all be much easier to do all this by just loading a png with all the alpha values already there.

Share this post

Link to post
Share on other sites
Original post by NegativeGeForce
I think it would all be much easier to do all this by just loading a png with all the alpha values already there.

But for what I have in mind I might need to change the alpha values dynamiclly for fading and stuff. If I wanted a texture to fade away Id have to do it that way right?

Share this post

Link to post
Share on other sites
the alpha data in the image (first or last 8 bits) just specifies the initial transparency. You can use the OpenGL blending functions to actually make the entire image fade in and out without the alpha values from the bitmap. The OpenGL blending options are very complex and you can do alot of combinations with them, but the most widely used method to make the foreground object your drawing transparent or mix to the background colors goes something like this...

this draws a textured quad...before calling this code you have to bind a texture first.


/* this function sets how opaque you want the colors, including the alpha value
witch can be 0-1.0, 1.0 being fully opaque, 0.5 is 50% transparency*/


/* this function sets the blending equation. GL_SRC_ALPHA reads from the colors
u specified from glColor4f() as the source. GL_ONE_MINUS_SRC_ALPHA basicly
asumes 1.0 as alpha then subtracts the values from glColor4f(). This specifies
how much you need to take from the background pixels.

Example: if source pixel alpha is .25, then the OpenGL will copy .75 from the background pixels.*/


// draw quad with uv
glTranslatef(x, y, -1);
glRotatef( angle, 0.0f, 0.0f, 1.0f );
glTexCoord2f(0.0f, 0.0f); glVertex2d( -width, -height); // width and height can be any values you specify
glTexCoord2f(1.0f, 0.0f); glVertex2d( width, -height);
glTexCoord2f(1.0f, 1.0f); glVertex2d( width, height);
glTexCoord2f(0.0f, 1.0f); glVertex2d( -width, height);

// disable blending

if your not to familar with all these functions and options...the opengl red book has decent explainations on everything.


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!