glCopyTexImage and the dreaded alpha channel

Started by
3 comments, last by almagest1980 15 years, 5 months ago
I want to save part of (or the whole) rasterized image to a texture, then saving it to a .png file. My problem is, that the alpha channel is not taken into account when i try to do this. The background which should have been transparent, is totally opaque (white). I take the following steps; - draw graphical elements - save part (or whole) image to a texture - map that texture to a cube (just to see how the texture looks like) - transfer the texture to my datastructure - save to file through the use of the corona library. Im pretty certain that the problem is the internalformat used in the glTexImage, glCopyTexImage and the glGetTexImage that i use. I have tried numerous combinations, but feel that GL_RGBA or GL_RGBA8 should give me correct result (although they dont) Found a similar thread, but couldnt solve my own issue with his solution. Similar problem discussed here on gamedev texturedata (to be stored in)

static unsigned char texture[4 * 128 * 128];

from my init()

glClearColor (1.0f, 1.0f, 1.0f, 0.0f);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texname);
glBindTexture(GL_TEXTURE_2D, texname);

glTexImage2D(GL_TEXTURE_2D, 0 ,4, 128, 128, 0, GL_RGBA8, GL_UNSIGNED_BYTE, texture);

from my display()


glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();

if(doonlyonce){

glViewport(0,0,128,128);
glReadBuffer(GL_BACK);
drawSuperEllipse();
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,128,128,0); // copy whole image to textureimage

glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture); // transfer textureimage to dataarea

// save the image to the hd
corona::Image *myImage = corona::CreateImage(128, 128, corona::PF_R8G8B8A8, texture);
corona::FlipImage(myImage, corona::CA_X); //Flip the image
corona::SaveImage("f:\\test.png", corona::FF_PNG, myImage); //Save it
doonlyonce=false;
} // end doonlyonce

glClearColor (0.8f, 0.8f, 0.8f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
glViewport(0,0,500,500);
glOrtho(-5,5,-5,5,-3,3);
glColor3f(1.0f,1.0f,1.0f); // white
glBindTexture(GL_TEXTURE_2D, texname);

// draw quad to see how texture looks like
glBegin(GL_QUADS);
	glTexCoord2d(0.0,0.0);
	glVertex4d(4.0,4.0,0.0,1.0);
	glTexCoord2d(1.0,0.0);
	glVertex4d(6.0,4.0,0.0,1.0);
	glTexCoord2d(1.0,1.0);
	glVertex4d(6.0,6.0,0.0,1.0);
	glTexCoord2d(0.0,1.0);
	glVertex4d(4.0,6.0,0.0,1.0);
glEnd();


As you can see, the image is saved, but the alphachannel is not saved properly. The white part should have been totally transparent. This is just like the rendered cube with the texture mapped to it look like as well, so im guessing that the corona file-save method is correctly used. Im going NUTS! Thanks for your time. -Andreas
Advertisement
UPDATE:

I've done some testing by putting the following code at several locations;

GLint AlphaBits;	glGetIntegerv(GL_ALPHA_BITS, &AlphaBits);		std::cout << "BITS: " << AlphaBits << std::endl;


It always outputs 0 as the answer. Shouldnt this be 8?
yes it should, when u create the window u can specify 8bits of alpha

btw (maybe relevant) GL_SRC_ALPHA, src == source the alpha in the window is DST destination
I've had the same problem in the past and it was because I wasn't setting the alpha bits parameter when creating my window. Your problem is probably the same.
Author Freeworld3Dhttp://www.freeworld3d.org
This is exactly the case! Thanks for helping me, both of you!

After i added GLUT_ALPHA everything worked harmoneously.

// changed fromglutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);// toglutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);[/Source]

This topic is closed to new replies.

Advertisement