Advertisement

Clicking and getting the 3D position

Started by February 12, 2004 05:09 AM
2 comments, last by yoshito 21 years ago
Hello, i''m trying to understand how it is possible to click on my GL window, and then somehow get the 3d position of the mouse pointer. I want to control my player clicking mouse on some location in my window. How this can be done? Regards.
Depending on how you create your window (ie. win32, GLUT, Qt or whatever), there will be a way to get the screen coordinates where the mouse click occurred. This is just a 2D coordinate in screen space, usually the top left corner is 0,0 and the bottom right is 640,480 or however big your viewport is.

From there, you need to cast a ray from the camera''s eyepoint through this screen coordinate and into your 3D scene. Then you can do intersection tests and determine what the ray hits, and thus what the user clicked the mouse pointer on in the scene.

Check out the gluUnproject function. It will basically do this stuff for you.

The code below is from one of my camera classes. The code relies on the fact that you maintain at least a current view matrix for a camera, and the viewport is available.

//-----------------------------------------------------------------------------vector3 GLPerspectiveCamera::GetEyePoint(){	// figure out the proper eyepoint from the inverse view matrix	matrix44 m = matrix44(matView);	m.invert();	return vector3(m[3].x,m[3].y,m[3].z);}//-----------------------------------------------------------------------------/*! Casts a ray into the scene from the point given by screen coords X and Y.	Returns 2 vectors describing the origin and direction of the ray	from the scene to the point. The ray direction vector is normalized.*/ void GLPerspectiveCamera::CastRayIntoScene(int x, int y, vector3 &rayOrigin, vector3 &rayDir){	GLint       realy;	GLdouble    farx,   fary,   farz;	// fix the y coordinate so opengl is happy about it	realy = viewport[3] - (GLint) y - 1;	// project this point onto the far clip plane	gluUnProject((GLdouble)x, (GLdouble)realy, 1.0, matView, matProj, viewport, &farx, &fary, &farz);	// generate the ray	rayOrigin   = GetEyePoint();	rayDir      = vector3(farx,fary,farz) - rayOrigin;	rayDir.normalize();}


Once you have this ray into your 3D scene, just start doing intersection testing in order to see what in the scene the ray has hit, and you''ll know what was clicked on.
Advertisement
http://nehe.gamedev.net/data/articles/article.asp?article=13
This is a easier way but only works when you are drawing filled polygons
============================== Videoman Library Project
I have a demo on my site that not only allows you to get the 3d point you clicked on, but drag an object around the screen in 3d.

It requires MFC to run, but if you don''t have MFC you should still be able to look at the code.

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

This topic is closed to new replies.

Advertisement