// based on boinc/api/gutil.cpp -- but makes an alpha map out of an RGB file for transparency
// basic pic editors such as Gimp are pretty easy to set a weird color not in the main image of course)
// or even easier -- use Gimp to set "Color to Alpha" and just embed it in the .rgb file as an RGBA (A = "alpha channel")
GLuintCreateRGBTransparentTexture(constchar*strFileName,float*transColor)// default in prototype to transColor = NULL i.e. no "filter color" required
{
// transcolor is an optional (but necessary for Z=3 i.e. RGB) which will flip the alpha of an RGB color (i.e. Magenta would be 255/0/255 passed in transColor
GLuintuiTexture=0;
intsizeX,sizeY,sizeZ;
// Load the image and store the data - this is in rgba format but the a is 255 (max)
unsignedint*pImage=read_rgb_texture(strFileName,&sizeX,&sizeY,&sizeZ);// read_rgb_texgture makes a 4channel (1 byte per R/G/B/A) anyway, so may as well use the "A"!
if(pImage==NULL)return0;
if(sizeZ!=3&&sizeZ!=4){// needs to be RGB i.e. z=3 or RGBA z=4
free(pImage);
return0;
}
// need to set transparency bytes/alpha value every place in pImage using magenta 255/0/255!
// also note image needs to be flipped vertically when you save it! (at least in gimp)
if(sizeZ==3&&transColor){//rgb -> rgba via the RGB values in transColor[3] array, if transColor not set not much use to this so just default to the RGBA created above (A=255 for all pixels)
for(inti=0;i<(sizeX*sizeY);i++)
{
unsignedchar*bb=(unsignedchar*)(pImage+i);// easy pointer to our image pixels
// just take the avg of the RGB -- as it approaches 0, the alpha goes to 0 (so basically the blacker, the more transperent)
if(*bb==g_transColor[0]
&&*(bb+1)==g_transColor[1]
&&*(bb+2)==g_transColor[2])
{
*(bb+3)=0x00;// If so, set alpha to fully transparent. (note this sets all 4 bytes to 0)
}// alpha already set to 255 if not transparent
//iTest = (*bb + *(bb+1) + *(bb+2)) / 3;
//*(bb+3) = iTest > 255 ? 255 : iTest;
}
}
// sizeZ == 4 is just "straight" RGBA where alpha is embedded in the file; which is already taken care of by the above load_file
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&uiTexture);
glBindTexture(GL_TEXTURE_2D,uiTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);// this isn't as fast supposedly, but looks pretty good
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);// also not as fast as GL_NEAREST but looks better!
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,
sizeX,sizeY,GL_RGBA,GL_UNSIGNED_BYTE,pImage);
free(pImage);// free the mem allocated by the rgb_texture function
returnuiTexture;
}
// based on boinc/api/gutil.cpp -- but makes a GL_ALPHA out of an RGB file (sums values each point to make the alpha)
// the basic idea is you can make a simple image that can translate to a complex map -- white values (1) "pass" and black values (0) "block"