Sign in to follow this  
ForeverNoobie

OpenGL Texture Blending

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);
index32++;
}




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
Quote:
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.


glEnable(GL_BLEND);

/* 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*/

glColor4f(1.0f,1.0f,1.0f,alpha);

/* 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.*/

glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// draw quad with uv
glPushMatrix();
glTranslatef(x, y, -1);
glRotatef( angle, 0.0f, 0.0f, 1.0f );
glBegin(GL_QUADS);
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);
glEnd();
glPopMatrix();

// disable blending
glDisable(GL_BLEND);












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

http://fly.srk.fer.hr/~unreal/theredbook/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this