Advertisement

How to load a Texture with an Alpha

Started by June 27, 2014 03:06 AM
2 comments, last by Cydriic 10 years, 4 months ago

Hi Everyone,

I'm trying to load a Quad with a Monster Image on it that will always face the Camera, a la Doom 2.

Now I don't know how to load an image with an Alpha part on it.

In 2D using SDL I always used tga files.

Here is my LoadTexture functions.

Does anyone have an idea?

Thanks a lot.

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec* TextureImage[12]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
//TEXTURE 0
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/Grass00.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[0]); // Create Textures
// Create Nearest Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure

}

return TRUE;

}

Hi,

If you want to have texture with alpha in OpenGL, you have to modify glTexImage2D call you have inside your LoadGLTextures function. You just need to change its 3rd parameter to 4 and 7th parameter to GL_RGBA. Texture In this case of course, in TextureImage[0]->data array every 4 bytes shoud correspond to a single pixel of image in RGBA format. Moreover, to get transparency based on alpha in OpenGL, you have to use color blending. In most cases it needs only setting blending function by calling glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) and enabling blending calculations by glEnable(GL_BLEND) call.

If it comes to loading image with alpha from a file, it seems to me that using auxDIBImageLoad function inside your LoadBMP is not a good idea, because it may not handle 32-bit bitmaps. I may be wrong here since I've never used auxDIBImageLoad function. Instead of, you might use Nehe 33 lesson of loading TGA files without GLAUX library.

By the way, you can test fast here your transparency stuff in OpenGL without loading image from a file. Simple use initialized array with 2x2 image of the form


unsigned char imagePixels[] = { 255,0,0,255,   255,255,255,128,  255,255,255,255,  255,0,0,10 };

and use it later to specify 2D texture in OpenGL by calling


glTexImage2D( GL_TEXTURE_2D, 0, 4, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagePixels);

Dietary supplements (suplementy diety)

Advertisement

Yessir works fine!

I had messed up my Texcoords like a newb thanks!!

This topic is closed to new replies.

Advertisement