Sign in to follow this  

[solved]Help with my ray picker

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:
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
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: - I'm sure there's more tutorials out there
Edit: Another good explanation:
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this