Screen coordinates to camera coordinates?

Started by
3 comments, last by magbo 19 years, 12 months ago
Hi, Is there a good way to move from screen(x,y)-coords to camera (x,y,z)-coords? I want to create a ray from my mouseposition straigth through the world? that is obtain p(t)=(x0,y0,z0)+t(dx,dy,dz) from (x,y) I know there''s a picking lesson available, but i don''t want to use any glu functions, or the OpenGL selection mode method.. //Regards Magnus
Advertisement
You need to do an inverse transformation, so instead of the final step of going from camera space into screen space you need to inverse this matrix and apply it to your screen co-ords.
------------------------See my games programming site at: www.toymaker.info
Here''s what I use in my engine:

Ray Camera::ComputePickRay(short sx, short sy){	float fx = (float) sx;	float fy = (float) sy;	float fw = (float) mViewport.width;	float fh = (float) mViewport.height;	Vector3 pick, dir;	Point3 origin;	// Compute screen-space pick vector.	pick.x = +(((2.0f * fx) / fw) - 1.0f) / mProject.x.x;	pick.y = -(((2.0f * fy) / fh) - 1.0f) / mProject.y.y;	pick.z = 1.0f;	// Transform the screen space pick ray into 3D space	dir.x = pick.x * mWorldMatrix.x.x + pick.y * mWorldMatrix.y.x + pick.z * mWorldMatrix.z.x;	dir.y = pick.x * mWorldMatrix.x.y + pick.y * mWorldMatrix.y.y + pick.z * mWorldMatrix.z.y;	dir.z = pick.x * mWorldMatrix.x.z + pick.y * mWorldMatrix.y.z + pick.z * mWorldMatrix.z.z;	origin	= mWorldMatrix.w;	// Return the ray that points	// from the origin to the target.	return Ray(origin, dir);}


Basically, what the above does is find the vector that intersects the projection plane at the coordinates corresponding to the screen coordinates (sx, sy). Haven''t got an explanation of the theory at hand...

HTH,

Jim Offerman
Innovade.
Jim OffermanNixxes Software
Hi,
thanks for the quick and informative reponse

// Compute screen-space pick vector.
pick.x = +(((2.0f * fx) / fw) - 1.0f) / mProject.x.x;
pick.y = -(((2.0f * fy) / fh) - 1.0f) / mProject.y.y;

just to clarify, how do you calculate the mProject.x.y and .y.y values ?
Those are the x.x (1,1) and y.y (2,2) elements of my projection matrix.

I use a homebrew projection function, but you could just use D3DXMatrixPerspectiveLH() (or the OpenGL equivalent).

Good luck,

Jim Offerman
Innovade.
Jim OffermanNixxes Software

This topic is closed to new replies.

Advertisement