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();
}