PDA

View Full Version : picking



Serezha
05-20-2006, 04:09 PM
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.

mikael_aronsson
05-22-2006, 03:12 AM
glMatrixMode( GL_MODELVIEW);.... what matrix are your glTranslate/glRotate messing with ?

Serezha
05-22-2006, 08:26 PM
Thanks, I got it. Before drawing rectangle I had to put the sequence:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(r*cos(prot*deg)*cos(trot*deg),r*sin(prot *deg),r*cos(prot*deg)*sin(trot*deg), 0,0,0, 0,1,0);

which is executed during repainting.

Sergey.