PDA

View Full Version : Replace texture alpha with colour values from another texture



grasshop
09-07-2012, 08:10 AM
Hi guys, I am trying to replace a texture with a new texture replacing the alpha values of the old texture with that of the colour gradient of another texture. Ie like this:

http://i.imgur.com/qkIIz.png

This is the framework of the C++ code I am using:


unsigned tex_saft(int tex, int copy_tex)
{
//tex is the texture to set the alpha to, copy_tex is the texture to take the colour gradients from for the alpha values
GLuint texture = tex, copy_texture = copy_tex;
glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glColor4f(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture);
int w, h;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTUR E_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTUR E_HEIGHT, &h);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_SRC_COLOR);
glBindTexture(GL_TEXTURE_2D, copy_texture);
char* bitmap = new char[(h<<(lgpp2(w)+2))|2]; //lgpp2() function is used to get powers of two
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);

GLuint tex_new;
glGenTextures(1, &tex_new);
glBindTexture(GL_TEXTURE_2D, tex_new);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, void(bitmap));
if (interpolate_textures)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}
else
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
delete[] bitmap;
glPopAttrib();
return tex_new;
}
All this code winds up doing is replacing the old texture straight with the copy_tex, as you can't blend bound textures like this apparently. Does anybody please know the correct code I could use to blend the two textures like I have described and create a new texture from this? Thanks for any help.