Sign in to follow this  

TGA alpha problem

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

Hi everyone, Sorry for my bad english but i will do my best Here is the problem I want to load a 32 bit.tga image in my game. I want to place this image as an overlay of the camera (it will serve as the game interface). I need the alpha channel to see 3D model trough the overlay image. So i create my tga object in MSVC++. Everything is fine in 24 bit mode without the alpha, but in 32 bit mode the channel alpha is not effective( my overlay image is still in full opacity). I use the glDrawPixels() function in ortho mode to draw the .tga image. Is there something i didn't enable in openGL because i can't make the tga transparent. I export it from photoshop without the RLE compression. Here is some code inspire of the book OPENGL GAME PROGRAMMING by Kevin Hawkins and Dave Astle, i add it the oop touch. // TGAFile.h: interface for the CTGAFile class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_TGAFILE_H__3854DE6A_7D66_4C65_8B35_8C9684B19FAC__INCLUDED_) #define AFX_TGAFILE_H__3854DE6A_7D66_4C65_8B35_8C9684B19FAC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CTGAFile { public: CTGAFile(); virtual ~CTGAFile(); //Fonctions int LoadTGAFile(char *filename, CTGAFile *tgaFile); void DrawTGA(long width, long height, unsigned char* bitmapImage); //Attributs //In class instead of a struct unsigned char m_imageTypeCode; short int m_imageWidth; short int m_imageHeight; unsigned char m_bitCount; unsigned char *m_imageData; }; #endif // !defined(AFX_TGAFILE_H__3854DE6A_7D66_4C65_8B35_8C9684B19FAC__INCLUDED_) ///////////////////////////////////////////// //And now the load function ////////////////////////////////// int CTGAFile::LoadTGAFile(char *filename, CTGAFile *tgaFile) { FILE *filePtr; //the file pointer unsigned char ucharBad; //garbage unsigned char data short int sintBad; //garbage short int data long imageSize; //size of TGA image int colorMode; // 4 for RGBA or 3 for RGB long imageIdx; //counter variable unsigned char colorSwap; //swap vriable //Ouvrir le fichier TGA filePtr = fopen(filename, "rb"); if(!filePtr) return 0; //Lire le deux bits inutile fread(&ucharBad, sizeof(unsigned char), 1, filePtr); fread(&ucharBad, sizeof(unsigned char), 1, filePtr); //Read in the image type fread(&tgaFile->m_imageTypeCode, sizeof(unsigned char), 1, filePtr); //for our purpose, the image type should be either a 2 or a 3 if((tgaFile->m_imageTypeCode != 2) && (tgaFile->m_imageTypeCode != 3)) { fclose(filePtr); return 0; } //read 13 bit of data we don't need fread(&sintBad, sizeof(short int),1,filePtr); fread(&sintBad, sizeof(short int),1,filePtr); fread(&ucharBad, sizeof(unsigned char),1,filePtr); fread(&sintBad, sizeof(short int),1,filePtr); fread(&sintBad, sizeof(short int),1,filePtr); // read image Dimension fread(&tgaFile->m_imageWidth,sizeof(short int), 1 , filePtr); fread(&tgaFile->m_imageHeight,sizeof(short int), 1 , filePtr); //read image bit depth fread(&tgaFile->m_bitCount, sizeof(unsigned char),1,filePtr); //read bit dont need fread(&ucharBad, sizeof(unsigned char),1,filePtr); //ColorMode -> 3 = BGR, 4 BGRA colorMode = tgaFile->m_bitCount/8; /*MessageBox(NULL,"Help, Something went wrong.", "Error", MB_ICONERROR | MB_OK);*/ imageSize = tgaFile->m_imageWidth * tgaFile->m_imageHeight * colorMode; //Allocate memory for image data tgaFile->m_imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize); //read in image data fread(tgaFile->m_imageData, sizeof(unsigned char), imageSize, filePtr); //Change BGR to RGB for(imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode) { colorSwap = tgaFile->m_imageData[imageIdx]; tgaFile->m_imageData[imageIdx] = tgaFile->m_imageData[imageIdx + 2]; tgaFile->m_imageData[imageIdx + 2] = colorSwap; } fclose(filePtr); return 1; } //////////////////////////////////// //Finally the drawing function //////////////////////////////////// void CTGAFile::DrawTGA(long width, long height, unsigned char* bitmapImage) { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glRasterPos2i(0,0); glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, bitmapImage); } //Please help !

Share this post


Link to post
Share on other sites
Try this...

void CTGAFile::DrawTGA(long width, long height, unsigned char* bitmapImage)
{

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glRasterPos2i(0,0);
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, bitmapImage);
}

For the alpha layer to work blending has to be enabled.

Take care.

Share this post


Link to post
Share on other sites

This topic is 4843 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.

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