Page 1 of 1

Easy Way To Translate Screen to Ortho/Viewport

Posted: Mon Aug 18, 2014 10:32 pm
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. :( ]

Re: Easy Way To Translate Screen to Ortho/Viewport

Posted: Tue Aug 19, 2014 12:19 am
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.

Re: Easy Way To Translate Screen to Ortho/Viewport

Posted: Fri Aug 22, 2014 5:05 pm
by r00k
hmm nope i dont have those either...