Jump to content

  • Log In with Google      Sign In   
  • Create Account


[solved]Help with my ray picker


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 kidman171   Members   -  Reputation: 498

Like
0Likes
Like

Posted 29 January 2013 - 09:01 AM

Hey guys I'm trying to implement a simple ray picker in Opengl. In my little scene I have a green house that I would like to detect when the user clicks it. I am drawing the rays to the screen so I can debug them, so please have a look at these screenshots:
 
example1i.jpg
 
example2u.jpg
 
 
The red lines are the rays I have cast. The places I clicked are in the first screenshot on the outer endpoints of each ray. The problem is this projection to the far plane does not give accurate results if my goal is to determine if the user clicked the house or not. Should I be orthogonally projecting the rays from their origins? What can I do to fix this? Here is my code:
 
	Ray computePickingRay(int x, int y)
	{
		
		glm::mat4 viewProj = projectionMatrix * camera->getViewMatrix();
		glm::mat4 invViewProj = glm::inverse(viewProj);
	
		int sx = gScreenWidth / 2;
		int sy = gScreenHeight / 2;
	
		glm::vec4 inV;
		inV.x = 1.0f * (x - sx) / (1.0f*sx);
		inV.y = 1.0f * ((gScreenHeight - y) - sy) / (1.0f * sy);
		inV.z = -1.0f;
		inV.w = 1.0f;
	
		glm::vec4 nearPlane = invViewProj * inV;
		inV.z = 1.0f;
		glm::vec4 farPlane = invViewProj * inV;
	
		glm::vec4 dir = (farPlane - nearPlane);
	
		Ray ray;
		ray.origin = vec3(nearPlane.x/nearPlane.w, nearPlane.y/nearPlane.w, nearPlane.z/nearPlane.w);
		ray.direction = glm::normalize(-vec3(dir.x/dir.w, dir.y/dir.w,dir.z/dir.w));
		return ray;
	}

 

 



Sponsor:

#2 Milcho   Crossbones+   -  Reputation: 1175

Like
0Likes
Like

Posted 29 January 2013 - 09:22 AM

Just a suggestion - If your only goal is to determine if the user clicked the house or not, there's a slightly easier way, that I think a lot of people use.
There's a (somewhat old) description here: http://www.lighthouse3d.com/opengl/picking/index.php3?color1 - I'm sure there's more tutorials out there
Edit: Another good explanation: http://content.gpwiki.org/index.php/OpenGL_Selection_Using_Unique_Color_IDs
Basically,
1) Disable all shading - setting it to flat
2) Assign each object you want to pick a unique color. I did this once by basically making a list of the objects, and making each object's color be their list number, transformed to hex - so object at location 0 had color #000000, 1 had #000001, 2 - #000002... 10 - #00000A, etc.
3) Clear the backbuffer (or a FBO) with some color, I go with white because of the above
4) Draw the objects you want to pick
5) Read the pixel which the user clicked from the backbuffer
6) Translate the read in color back into a number ( hex->decimal conversion)
7) Clear the backbuffer again for future rendering

That's pretty much a pixel-perfect way to do picking.

Edited by Milcho, 29 January 2013 - 09:23 AM.


#3 swiftcoder   Senior Moderators   -  Reputation: 9856

Like
2Likes
Like

Posted 29 January 2013 - 09:53 AM

The origin of your rays appear to be wrong. For accurate picking, the origin of the ray should be the point on the near plane where the user clicked, and the direction of the ray should be the direction from the camera position to that point on the near plane.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#4 kidman171   Members   -  Reputation: 498

Like
1Likes
Like

Posted 29 January 2013 - 03:42 PM

Thanks swiftcoder, that solved the problem! I changed the origin to the cameras position and direction to normalize(nearPlane - cameraPosition) and it works like a charm!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS