Jump to content
  • Advertisement
Sign in to follow this  
kidman171

[solved]Help with my ray picker

This topic is 2087 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

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

 

 

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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!

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!