Hi,
I am trying to do an MFC-based OpenGL molecular viewer. I need to implement atom selection, so my code looks something like this:
//Selection function
OnSelect(CPoint pt)
{
GLint hits;
GLint viewport[4];
GLuint selectBuff [512];
glSelectBuffer(512,selectBuff);
glGetIntegerv(GL_VIEWPORT,viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
glLoadIdentity();
gluPickMatrix(pt.x,viewport[3] - pt.y,5,5,viewport);
gluPerspective(45,AspectRatio,1,10);
OnRender(SELECT) ;
hits=glRenderMode(GL_RENDER);
if(hits)
{
Proces****s(selectBuff[3]);
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
OnRender(GLenum mode)
{
glPushMatrix();
glRotatef(rotate.x,1.0,0.0,0.0);
glRotatef(rotate.y,0.0,1.0,0.0);
RenderMolecule(
RenderMolecule(GL_SELECT);
glPopMatrix()
}
RenderMolecule(GLenum mode)
{
glInitNames();
glPushName(0);
register int i;
for(i=0;i<m_molecule.m_atom.size();i++)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color );
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color );
glPushMatrix();
glTranslatef(m_molecule.m_atom[i].c.x/m_scale,
m_molecule.m_atom[i].c.y/m_scale,
m_molecule.m_atom[i].c.z/m_scale);
///if in selection mode load a name to the stack (the objects id)
if (mode == GL_SELECT)
glLoadName(i + 1) ;
gluSphere(atomSphere,0.2 / m_scale,GRAIN,GRAIN);
glPopMatrix();
}
}
The problem is that when molecule is rotated, the atom selection is incorrect (wrong numbers are given)
Any idea?