I am drawing a rectangle and I need to pick it. It works fine but if I rotate or translate the rectangle I cannot pick anymore.
Here is the code.
void GLWidget::ProcessSelection(int x, int y)
{
static GLuint selectBuffer[512];
GLint hits;
GLint viewport[4];
glSelectBuffer(512, selectBuffer);
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
(void) glRenderMode(GL_SELECT);
glLoadIdentity();
gluPickMatrix(x, viewport[3]-y, 5, 5, viewport);
glOrtho(-1.5, 1.5, -1.5, 1.5, -1000, 1000);
glInitNames();
glPushName(0);
//Rectangle to pick
drawSliceRect(GL_SELECT);
hits = glRenderMode(GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
qDebug() << hits << endl;
}
And code for rectangle:
void GLWidget::drawSliceRect(GLenum mode)
{
glEnable(GL_LINE_SMOOTH);
// displacement range [-1, 1];
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
if(mode == GL_SELECT)
glLoadName(1);
switch(mCurSliceDirection)
{
case X:
glRotatef(90, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, mSliceDisplacement);
break;
case Y:
glRotatef(90, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, -mSliceDisplacement);
break;
case Z:
glTranslatef(0.0, 0.0, mSliceDisplacement);
break;
default:
glPopMatrix();
if(mode == GL_SELECT)
glPopName();
return;
}
glBegin(GL_LINE_LOOP);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glDisable(GL_LINE_SMOOTH);
if(mode == GL_SELECT)
glPopName();
glPopMatrix();
}
I am not sure if it matters, I using Qt 4.0.
qDebug() << hits << endl; gives 1 only in case Z when mSliceDisplacement is 0.0, otherwise prints zero.
Thanks.