Original Post
'lo 'll coder, I try to code a chess game using OpenGL and C# via CsGL. It works well, but when i want to select an object of my scene, this object is selected once on two :( this is my code for the picking :: it returns the number object. Left mouse button & move to move the scene Right mouse button to select an object You can download the code (for VS.net 2003) here :: http://geffd.free.fr/Coding/Chess3D.zip (349Ko with 3D files)
public uint SelObj(double X, double Y)
{
GL.glPushAttrib(GL.GL_ALL_ATTRIB_BITS); /* on sauvegarde tout */
GL.glDisable(GL.GL_LIGHTING); /* pas besoin de la lumiere pour le picking, desactiver tous ce qui est inutile */
const int BUFSIZE=512;
uint[] SelectBuf=new uint[BUFSIZE];
int[] viewport=new int[4];
double hits;
uint Idx=0;
double MinZ=22147483647;
uint NameNos=0;
uint Selected=1000;
Size s= Size;
double GlRatio =s.Width /s.Height ;
GL.glSelectBuffer (BUFSIZE, SelectBuf);
GL.glGetIntegerv (GL.GL_VIEWPORT, viewport);
GL.glRenderMode (GL.GL_SELECT);
GL.glInitNames();
GL.glPushName(0);
GL.glMatrixMode (GL.GL_PROJECTION);
GL.glPushMatrix ();
GL.glLoadIdentity ();
GL.gluPickMatrix ( X, viewport[3] - Y , 2, 2, viewport);
GL.gluPerspective( 45.0f,GlRatio, 1f,1000.0f);
GL.glMatrixMode(GL.GL_MODELVIEW);
GL.glLoadIdentity();
glDraw();
hits = GL.glRenderMode (GL.GL_RENDER);
GL.glMatrixMode (GL.GL_PROJECTION);
GL.glPopMatrix();
GL.glMatrixMode (GL.GL_MODELVIEW);
if (hits!=0)
{
for (int i=0;i<hits;i++)
{
NameNos = SelectBuf[Idx];
if( (SelectBuf[Idx + 1] < MinZ) && (NameNos > 0))
{
MinZ = SelectBuf[Idx + 1];
Selected = SelectBuf[Idx + 3];
}
Idx = Idx+ 3 + NameNos;
}
}
GL.glPopAttrib ();
return Selected;
}