Jump to content


- - - - -

Mouse -> Ray


4 replies to this topic

#1 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 27 December 2004 - 12:16 PM

Hi, in my little program I'm trying to transform the mouse position into a ray, and then find the ray's intersection with a plane at 0,0,0 (normal 0, 1, 0). And of course, it doesn't work...


I've tried using gluUnproject, but the position it gave always seemed varying degrees off. After that, I tried doing it manually like so:

(Unoptimised, that's for once it works :P)

	GLint viewport[4];	
	glGetIntegerv (GL_VIEWPORT, viewport);

	/* Some constants */
	float near = 0.1, far = 4000, ratio = (float)viewport[2] / (float)viewport[3];
	float fov = DegToRad(45);
	
	InputMouseInfo i ( InputSystem::GetS().GetMouseInfo() );
	
	float tangent ( tanf(fov * 0.5) );
	
	/* Normalised mouse coordinates in view space */
	float dx ( (2 * (float)i.x / (float)viewport[2] - 1) / ratio );
	float dy ( 1 - 2 * (float)i.y / (float)viewport[3] );
	
	vector3 n (dx * tangent * near, dy * tangent * near, near);
	vector3 f (dx * tangent * far, dy * tangent * far, far);
	
	/* Get the modelview matrix, convert it to matrix44 and find inverse */
	GLdouble glModMtx[16];
	glGetDoublev (GL_MODELVIEW_MATRIX, glModMtx);
	matrix44 modMtx (vector4 ((float)glModMtx[0], (float)glModMtx[1], (float)glModMtx[2], (float)glModMtx[3]),
    vector4 ((float)glModMtx[4], (float)glModMtx[5], (float)glModMtx[6], (float)glModMtx[7]),
    vector4 ((float)glModMtx[8], (float)glModMtx[9], (float)glModMtx[10], (float)glModMtx[11]),
    vector4 ((float)glModMtx[12], (float)glModMtx[13], (float)glModMtx[14], (float)glModMtx[15]));
	
	matrix44 invModMtx ( InvertMatrix44 (modMtx) );
	
	n = invModMtx * n;
	f = invModMtx * f;
	
	/* Find point when y = 0 */
	vector3 dir ((n - f).normalize());
	float units (n.y / -dir.y);
	vector3 strike (n + dir * units);

Can anyone give me some hints/tips as to where I'm going wrong?
Thanks
Ed Mack

#2 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 30 December 2004 - 12:48 AM

Can anyone point me in the direction of any sort of openGL mouse->ray implimentation? It'd be greatly appreciated. (I'm beginning to think this should have been in the Graphics forum, move it if you feel so)

#3 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 30 December 2004 - 02:49 AM

sure:

clicky

#4 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 30 December 2004 - 10:12 AM

Thank you! Now why didn't google find that :)

#5 jebus

    New Member

  • Members
  • Pip
  • 4 posts

Posted 04 January 2005 - 05:41 PM

i use gluUnproject and it works fine. note that you should call it twice: once with z=0 and once with z=1. this gives you all the components to create your ray.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users