# 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 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.

#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 on other sites
Quote:
 Original post by NegativeGeForceI 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 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 colorsu specified from glColor4f() as the source.  GL_ONE_MINUS_SRC_ALPHA basiclyasumes 1.0 as alpha then subtracts the values from glColor4f().  This specifieshow 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 uvglPushMatrix();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 blendingglDisable(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/

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627648
• Total Posts
2978393
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33