Picking problem

Hello to all,
my problem deals with picking.

I used that example:
http://www.opengl.org/resources/code/samples/redbook/picksquare.c
It is working so far.

I have drawn a 3D scene and in front of that is an image.
Now I want to apply picking in order to know whether I am clicking on an object in the 3D scene or on the image in front.

If I only have the 3D scene the picking is working. If I only have the 2D scene the picking is working, as well.

How do I use picking when I want to combine
gluPerspective(45.0, (float)w / h, 1, 500) and glOrtho(0, w, 0, h, -1, 1)? Has anybody an example?

I am looking forward to your answer.

How do you want the two combined? I think you need to refine your question. Are you drawing a 2D image over the top of the 3D scene (so the 2D image is always in front)? If so, see if it hits the 2D image (using the glOrtho matrix) and if it doesn’t see if it hits anything using the other matrix. It’ll require two different picks.

Hello,

I am drawing a polygon with a texture over the 3d scene.
Yes the 2D image is always in front.

How do the two picks have to be implemented?

If I use picking first for the 2d image and later for the 3d object it dosn´t work correctly. I have in that case false positives from the 2d images. In addition it is flickering.

This is the version which displays only the 3d object.

void displayA(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int w = windowWidth;
int h = windowHeight;

if (mode == PICKING_STATE_SELECT) 
{
	glSelectBuffer(SIZE_OF_SELECTBUFFER,selectBuffer);
	glGetIntegerv(GL_VIEWPORT,viewport);
	(void) glRenderMode(GL_SELECT);

	glInitNames();
	glPushName(0);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluPickMatrix(cursorX,viewport[3]-cursorY,5,5,viewport);
	gluPerspective(45.0, (float)w / h, 1, 500);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

glPushMatrix();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(Rotate,0.0f,0.0f,1.0f);
glLoadName(10);
glColor3f(0, 1, 0);
glBegin(GL_TRIANGLES);
	glVertex3f( 0.0f, 1.5f,1.0f);
	glVertex3f(-1.0f,-1.0f,1.0f);
	glVertex3f( 1.0f,-1.0f,1.0f);
glEnd();
glPopMatrix();
Rotate+=0.05f;

if (mode == PICKING_STATE_SELECT) 
{

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glFlush();
	hits = glRenderMode(GL_RENDER);
	printf("

hits = %d
", hits);
if (hits != 0){
processHits(hits,selectBuffer,0);
}
mode = PICKING_STATE_RENDER;
}
else
{
glutSwapBuffers();
}
}

And this is the version which displays only the 2d scene:
void displayB()
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gameState = 1;
reshape(windowWidth, windowHeight);
int w = windowWidth;
int h = windowHeight;

if (mode == PICKING_STATE_SELECT) 
{
	glSelectBuffer(SIZE_OF_SELECTBUFFER,selectBuffer);
	glGetIntegerv(GL_VIEWPORT,viewport);
	(void) glRenderMode(GL_SELECT);

	glInitNames();
	glPushName(0);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluPickMatrix(cursorX,viewport[3]-cursorY,5,5,viewport);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glOrtho(0, w, 0, h, -1, 1);   
	glScalef(1, -1, 1);           
	glTranslatef(0, -h, 0);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

}

int xPos = 100;
int yPos = 100;
glLoadName(20);
glBegin( GL_QUADS );
	glColor3f(1, 0, 0);
	glVertex3d(xPos, yPos, 0);
	glVertex3d(xPos + 70, yPos, 0);
	glVertex3d(xPos + 70, yPos + 70, 0);
	glVertex3d(xPos, yPos + 70, 0);
glEnd();


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
int swap = 0;		
if (mode == PICKING_STATE_SELECT) 
{

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glFlush();
	hits = glRenderMode(GL_RENDER);
	printf("

hits = %d
", hits);
if (hits != 0){
processHits(hits,selectBuffer,0);
}
mode = PICKING_STATE_RENDER;
}
else{swap = 1;}
/* For the 3D Scene */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(cursorX,viewport[3]-cursorY,5,5,viewport);
gluPerspective(45.0, (float)w / h, 1, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

if(swap)
{
	glutSwapBuffers();
}

}

void reshape(int w, int h)
{
windowWidth = w;
windowHeight = h;

glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0, (float)w / h, 1, 500);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

}

You could use "2D-picking for your 2D-image, that means:

Mouse.x > pic.x && Mouse.x < pic.x + pic.width 
// same for y

And for 3D-picking you could use ray intersections…

But if you have a “2D-Pick” you can’t have a “3D-Pick” :wink: