View Full Version : 2D sprite with transparency aera clic detection ?

03-04-2004, 08:14 PM

i want to detect a clic in a 2D sprite ?
my sprite is a 2D texture quad (TGA) with transparency.
i detect a clic correctly in the quad with the picking methode, but how can i detect a clic in the pixels of the sprite ?

i work in double buffering :
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

i haved try to draw the sprite in the buffer GL_AUX0, and read the pixel color at the clic coordonate, but i think i dont read the good buffer ?

GLubyte* pixel;
GLint* drawBuffer;
GLint* readBuffer;

pixel = new GLubyte[4];
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;

drawBuffer = new GLint[1];
readBuffer = new GLint[1];
/* saving the courant buffer */
glGetIntegerv(GL_DRAW_BUFFER, drawBuffer);
glGetIntegerv(GL_READ_BUFFER, readBuffer);
/* draw in the GL_AUX0 buffer */
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
/* drawing the sprite (quad+texture) */
/* read in the GL_AUX0 buffer */
/* read the pixel color */
glReadPixels(x, y, 1, 1,GL_RGB, GL_UNSIGNED_BYTE, pixel);

cout << "pixel[0]= " << (unsigned int)pixel[0] << endl;
cout << "pixel[1]= " << (unsigned int)pixel[1] << endl;
cout << "pixel[2]= " << (unsigned int)pixel[2] << endl;
cout << "pixel[3]= " << (unsigned int)pixel[3] << endl;

/* restore read and draw buffer */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

/* testing if the pixel is background or not */
if (pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255)
cout << "non-touche" << endl;
delete pixel;
delete drawBuffer;
delete readBuffer;
cout << "touche" << endl;
delete pixel;
delete drawBuffer;
delete readBuffer;

but it is not working ?

have you a solution ?


03-06-2004, 04:50 AM
Hi !

Reading colors from the framebuffer could be tricky, if you have other sprites that may overlap for example and so on.

If it is all in two why use picking at all,
it's easy to compare the mouse position with the position/width/height of the sprite and detect a hit, then you can easy find the local x,y coordinate inside the sprite.

And if you keep a copy of the sprite in ram you can make a fast color lookup in that, that would be much faster then useing picking.