PDA

View Full Version : Transparency



07-29-2002, 05:12 AM
I figured it out finally. If anyone else needs to work with bitmaps here is my code:


AUX_RGBImageRec* LoadBMP(const char* bmpFileName);
bool drawBMP( float x, float y, const char* fname, bool trans );

AUX_RGBImageRec* LoadBMP(const char* bmpFileName)
{
if (!bmpFileName)
{
return NULL;
}
FILE* inFile = fopen(bmpFileName,"rb");
if (inFile)
{
fclose(inFile);
return auxDIBImageLoad(bmpFileName);
}
return NULL;
}


bool drawBMP( float x, float y, const char* fname, bool trans )
{
unsigned char * n;
unsigned char * r,*a;
int cnt;

AUX_RGBImageRec* img = LoadBMP( fname );
if (img)
{

/* TEST TRANSPARENCY CODE */
if( trans )
{
n = (unsigned char*)malloc( img->sizeX * img->sizeY * 4 );
r = img->data;
a = n;
for( cnt=0;cnt<img->sizeX*img->sizeY;cnt++)
{
*a = *r;
*(a+1) = *(r+1);
*(a+2) = *(r+2);
if( (*a < 0x1) && (*(a+1) < 0x1) && (*(a+2) < 0x1) )
*(a+3) = 0x00;
else
*(a+3) = 0xFF;
a+=4;
r+=3;
}
}
/* END OF TRANSPARENCY CODE */

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, .95f);

glRasterPos2f( x, y );
if( trans )
glDrawPixels( img->sizeX , img->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, n );
else
glDrawPixels( img->sizeX , img->sizeY, GL_RGB, GL_UNSIGNED_BYTE, img->data );

free(img);
return( true );
}
return( false );
}

void display( void )
{
glClear(GL_COLOR_BUFFER_BIT);
drawBMP( 1,1,"bg.bmp", false );
drawBMP( 1,1,"test.bmp", true );
glutSwapBuffers();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 200, 200 );
glutCreateWindow("test");

glOrtho( 0.0, 200, 0.0, 200, -1.0, 1.0 ); // Define co-ords
glClearColor(0.0, 0.0, 0.0, 0.0); // Back Ground Color

glutDisplayFunc(display);

glutMainLoop();
return 0;
}