PDA

View Full Version : frame buf under mouse...HOW?



Giancarlo
05-30-2010, 04:19 AM
Hello Openglrs :)

I have this problem: I textured a plane to visualize an image. Now, I have the cursor of the mouse defining a region on the image. I want to grab the correspondent data in the frame buffer and texture a squared plane with center on the cursor. The square is 20x20.

I save the texture in:
imageCursor = (unsigned char *) malloc (32*32*3*sizeof(unsigned char));

then call:
glReadPixels(cursor.center.x-cursor.radius,
cursor.center.y-cursor.radius,
32,32,GL_RGB,GL_UNSIGNED_BYTE,imageCursor);

glBindTexture(GL_TEXTURE_2D, 2); //Create an ID For a texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, imageCursor);

and for the rendering I do:
glBindTexture(GL_TEXTURE_2D, 2);
glBegin(GL_QUADS);
glTexCoord2f(0,0);glVertex2f(cursor.center.x-cursor.radius,cursor.center.y-cursor.radius);
glTexCoord2f(1,0);glVertex2f(cursor.center.x+curso r.radius,cursor.center.y-cursor.radius);
glTexCoord2f(1,1);glVertex2f(cursor.center.x+curso r.radius,cursor.center.y+cursor.radius);
glTexCoord2f(0,1);glVertex2f(cursor.center.x-cursor.radius,cursor.center.y+cursor.radius);
glEnd();

but I get a kind of dilation of the underneath image (see snapshot)Can anybody help?
http://www.facebook.com/photo.php?pid=4084931&l=c584a288ca&id=658568910 .

ZbuffeR
05-30-2010, 05:54 AM
Well the code you posted look ok, even if glPixelStorei should be called before glReadPixels.

But how do you setup your viewport and projection ?

Something like that should be ideal, as it allows to work in pixel coordinates :
http://www.opengl.org/wiki/Viewing_and_T...3D_rendering.3F (http://www.opengl.org/wiki/Viewing_and_Transformations#How_do_I_draw_2D_contr ols_over_my_3D_rendering.3F)
You will have to adapt the quad coordinates to be [0;32] instead of [0;1]

Giancarlo
05-30-2010, 06:21 AM
Hi ZBuffeR, thanks for your reply, this is how I do set up the viewport and the projection, how that might effect it?: The smaller quad in correspondence of the mouse is 20x20 big. Might it affect the result?

windw.x = w;
windw.y = h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION); //Defining the Projection
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Thanks!
Giancarlo

Giancarlo
05-30-2010, 06:59 AM
Just to precise, what is not clear to me is also the glReadPixels....expecially the width and height parameters...
if I want a block of pixel fitting the dimension of my quad which is 20x20, shouldn't I read a block of 20x20 pixels?

Giancarlo
05-30-2010, 08:06 AM
Found the problems...now it works! :)

It was a matter of defining the radius properly with a division by 2;