Easy Way To Translate Screen to Ortho/Viewport

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Easy Way To Translate Screen to Ortho/Viewport

Postby Baker » Mon Aug 18, 2014 10:32 pm

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 ..
User avatar
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Easy Way To Translate Screen to Ortho/Viewport

Postby Spike » Tue Aug 19, 2014 12:19 am

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.
Posts: 2911
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: Easy Way To Translate Screen to Ortho/Viewport

Postby r00k » Fri Aug 22, 2014 5:05 pm

hmm nope i dont have those either...
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest