Skip to main content
gamedev.net
🔒 Locked

Clikin 3d objects

Started by md_lasalle 2 replies 300+ views
Original Post
md_lasalle
md_lasalle
Hi, I'm making a level editor where you can put objects on a terrain. Now, I would like to be able to click the 3d objects from my 2d mouse pointer... I'm using openGL so I looked at gluProject/UnProject .. but i have a hardtime to understand which one i sould use. Thank you for any help!
Nit
Nit
You use gluUnproject to transform 2D window coordinates (i.e. the x,y coordinates of a mouse click) into 3D world coordinates.

Quote:

The concept of window space Z is often confusing. It's the depth buffer value expressed as a GLdouble in the range 0.0 to 1.0. Assuming a default glDepthRange(), a window coordinate with a Z value of 0.0 corresponds to an eye coordinate located on the zNear clipping plane. Similarly, a window space Z value of 1.0 corresponds to an eye space coordinate located on the zFar plane. You can obtain any window space Z value by reading the depth buffer with glReadPixels().


So, to get the 3D point of your mouse click on the near plane, you put 0.0 in for the winz parameter. To get the 3D point of your mouse click on the far plane, you put 1.0 in for the winz parameter. With these two points, you have a segment that you can use to intersect with your polygons, to test for clicks.
md_lasalle
md_lasalle
ok, i googled a bit and found a web site where the guy explained some ways to get 3d space point. I pasted a function that gives two point forming a ray in 3d space.


// This function will find 2 points in world space that are on the line into the screen defined by screenspace point (x,y)	double mvmatrix[16];	double projmatrix[16];	int viewport[4];	double dX, dY, dZ, dClickY; // glUnProject uses doubles, but I'm using floats for these 3D vectors		glGetIntegerv(GL_VIEWPORT, viewport);		glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);	glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);	dClickY = double (g_WindowHeight - y); // OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top		gluUnProject ((double) x, dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);	ClickRayP1 = Vector3 ( (float) dX, (float) dY, (float) dZ );	gluUnProject ((double) x, dClickY, 1.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);	ClickRayP2 = Vector3 ( (float) dX, (float) dY, (float) dZ );


strangely, the Z value of ClickRayP1 is always 99.00000 and the Z value of ClickRayP2 is always -99.00000

how i'm I supposed to check for collisions with that ? :S

Thank you for any help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.