• Advertisement
Sign in to follow this  

[SOLVED] Screen space to object space

This topic is 3228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I am looking for a way to convert a screen spaced 2d vector to a object space 2d vector. Basically I have a gui that can be rotated and what to be able to quickly tell if the mouse is inside the object. Currently I am simply check the x, x+width, y, +height. However once rotation comes into play these, of course, no longer work. I have heard from several people that gluUnproject will do what I need which ha led me to build my own unproject function, as I needed to use floats and not doubles. The code below tends to give me skewed results. With no rotation the Y axis is mirrored over the x axis, and when rotated 90 degrees the x axis becomes skewed. Any ideas??
 void Window::Unproject( float winx, float winy, float* p, int* view, float* ox, float* oy ){
	nv::vec4<float> in = nv::vec4<float>(	((winx - view[0]) * 2.0) / view[2] - 1.0,
        ((winy - view[1]) * 2.0) / view[3] - 1.0,						-1.0,											1.0 );

	nv::matrix4<float> proj;
	proj.set_value( p );

	nv::vec4<float> ret = inverse( proj * Modelview ) * in;

	if( ret.w == 0.0 )

	*ox = ret.x / ret.w;
	*oy = ret.y / ret.w;

SOLVED: Inverted the in.y and it seems to work fine now.. [Edited by - cherryyosh on April 22, 2009 6:57:47 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement