Jump to content
  • Advertisement
Sign in to follow this  
Wizumwalt

help w/ picking

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone help with my problem. It seems somehow I'm losing my projection or pick matrix, though I don't know how (or really even know if that's true). I'm still trying to make the triangular elements of a mesh highlight when my mouse hovers over them. No matter where I put the mouse, I'm always getting one hit, even when the mouse is off the model. I only have one object being displayed. Does anyone see anything wrong w/ what I have below.
public void display(GLDrawable drawable) {
	GL gl = drawable.getGL();
	GLU glu = drawable.getGLU();
		
	float[] transf = new float[16];
	
	this.gldrawable = drawable;
		
	if (!selectedFile) {
		return;
	}
		
	eyeZ = centroid.getZ() < 0 ? centroid.getZ() * -2: centroid.getZ() * 2;

	switch (cmd) {
		case UPDATE:
		   	// clear the window
			gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
			
			// reset the modelview matrix
			gl.glColor3f(1.0f, 1.0f, 1.0f);
			gl.glMatrixMode(GL.GL_MODELVIEW);
			gl.glLoadIdentity();

			// viewing transformation
			glu.gluLookAt(
				centroid.getX() + xTrans, 
				centroid.getY() + yTrans, 
				centroid.getZ() + zTrans + eyeZ,
				centroid.getX() + xTrans, 
				centroid.getY() + yTrans, 
				centroid.getZ(), 
				0.0, 1.0, 0.0);
				
			// model tranformation
			xScale = yScale < xScale ? yScale: xScale;
			yScale = xScale < yScale ? xScale: yScale;

			{
				gl.glScalef(xScale, yScale, 0.0f);
				arcBall.transform.get(transf);
				gl.glMultMatrixf(transf);
				drawModel(drawable, gl);
			}
			
			break;

		case SELECT:
			int hits = 0;
			int buffSize = 512;
			int[] viewPort = new int[4];
			double x = (double) currMouseX, y = (double) currMouseY;
			
			IntBuffer selectBuffer = BufferUtils.newIntBuffer(buffSize);
			gl.glSelectBuffer(buffSize, selectBuffer);
			gl.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
			
			gl.glRenderMode(GL.GL_SELECT);
			
			gl.glInitNames();
			gl.glPushName(0);
			
			gl.glMatrixMode(GL.GL_MODELVIEW);
			gl.glLoadIdentity();
			
			glu.gluLookAt(
			    centroid.getX() + xTrans, 
			    centroid.getY() + yTrans, 
			    centroid.getZ() + zTrans + eyeZ,
			    centroid.getX() + xTrans, 
			    centroid.getY() + yTrans, 
			    centroid.getZ(), 
			    0.0, 1.0, 0.0);
			
			// Save the current projection because we have a new
			// clipping volume around the mouse cursor now.
			gl.glMatrixMode(GL.GL_PROJECTION);
			gl.glPushMatrix();
			gl.glLoadIdentity();
			
			// Establish the new clipping pane. Multiplies the current 
			// projection matrix by the picking matrix.
			glu.gluPickMatrix(x, (double) viewPort[3] - y,
				3.0d, 3.0d, viewPort);
			
			xScale = yScale < xScale ? yScale: xScale;
			yScale = xScale < yScale ? xScale: yScale;

			{
				gl.glScalef(xScale, yScale, 0.0f);
				arcBall.transform.get(transf);
				gl.glMultMatrixf(transf);
				drawModel(drawable, gl);
			}

			gl.glMatrixMode(GL.GL_PROJECTION);
			gl.glPopMatrix();
			hits = gl.glRenderMode(GL.GL_RENDER);
			processHits(hits, selectBuffer);
			gl.glMatrixMode(GL.GL_MODELVIEW);
			break;
	}
	
	cmd = UPDATE;
	
	return;
}
And here's what kicks off the picking.
public void mouseMoved(MouseEvent me) {
	currMouseX = me.getX();
	currMouseY = me.getY();

	cmd = SELECT;
	this.gldrawable.display();

	// Now draw the hits visually. 
	cmd = UPDATE;
	this.gldrawable.display();
		
        return;
}
Any help much appreciated. [Edited by - Wizumwalt on August 15, 2005 9:44:38 AM]

Share this post


Link to post
Share on other sites
Advertisement
It seems my problem, as someone told me, is that I'm overwriting my current projection matrix with the pick matrix by pushing a matrix on the projection stack and calling glLoadIdentity.

Though I don't understand this. Supposedly I need to multiply the pick matrix by my usual projection matrix, like maybe w/ a glOrtho call. Can anyone help me out here a bit?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!