Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem of Picking (selection)

Started by Geff Nov 16, 2004 at 10:20 AM 2 replies 1k views
Original Post
Geff
Geff
'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 ::


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

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)
soconne
soconne
What exactly is your problem with the code? It selects more than one object?

Personally, I've never liked using OpenGL's selection buffer. I find that the best method is to render everything in your scene to the screen with everything disabled, except color. Make each object have a unique color, then call glReadPixels where the mouse coordinates are, then loop through all your objects looking for that color ID.

It works very well.
Author Freeworld3Dhttp://www.freeworld3d.org
Geff
Geff
My code select sometimes my object, and sometimes it select nothing :(

I think, i will use your method.

thanks
khalligan
khalligan
oconnellseanm is right, its the handiest, a friend of mine done this method when writing a Chess game a few years ago. Used 64 individual colors for each of the squares. Then just check each square with the one GL gives you back, real simple.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.