Easy Way To Translate Screen to Ortho/Viewport

Discuss programming topics for the various GPL'd game engine sources.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Easy Way To Translate Screen to Ortho/Viewport

Post by Baker »

I know how to do the math manually, but I am wondering if anyone knows an easy way to translate screen coordinates (mouse click X, Y) into a 2D glOrtho/Viewport with the matrices by grabbing them and performing a calculation.

I may answer my own question.

I could do it manually, but I am looking for an "elegant" solution.

For example:
mouse click at 64, 180 in client window
Where does it hit a viewport from (100,100)-(700,500) scaled with glOrtho to have coordinates of (0,0) through (320,200)
If I manually calculate it, I'm only solving this instance of the issue. Should be a way to elegantly translate into or translate out of the viewport.

[Yes, I'm matrix rusty. I bet a year ago I would have solved this one in 10 seconds flat. :( ]
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Easy Way To Translate Screen to Ortho/Viewport

Post by Spike »

gluProject gives you vertex->screen (useful for crosshairs)
gluUnproject gives you screen->vertex (useful for cursors)

both need the viewmodel and projection matricies

so if you want to find what 3d object the user just clicked on, unproject(x,y,0) and unproject(x,y,1), trace a line between them, see what the trace hit.

you can either google the specific formulas that glu uses, or you can rip Matrix4x4_CM_UnProject/Matrix4x4_CM_Project from FTE or qrack or whatever other engines it got copied to. presumably dp has some equivelent somewhere.
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

Re: Easy Way To Translate Screen to Ortho/Viewport

Post by r00k »

hmm nope i dont have those either...
Post Reply