Jump to content
  • Advertisement
Sign in to follow this  
adam17

OpenGL Efficient OpenGL Mouse Coordinate Transformation

This topic is 3891 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

ive got my program to translate the mouse coordinates into opengl coordinates, but i dont think its very fast and efficient. to test it out, i translate the coordinates every frame. under the cursor, i draw a simple GL_POINT. when i move the cursor around, the point seems to always play catch up until the cursor stops moving. am i doing too much of something? is there an alternative to gluUnproject? here are how the variables are defined: POINT MouseCur, MousePrev; GLfloat OGLMouseCur[3], OGLMousePrev[3]; GLfloat depth;
        GetCursorPos(&MouseCur);
	ScreenToClient(hWnd, &MouseCur);

	CheckMouseButtons();

        //         (1/zNear)-(1/Z)
        //depth =--------------------
        //        (1/zNear)-(1-zFar)
	depth = ((1/0.1)-(1/MouseWheel))/((1/0.1)-(1/10000.1));
	OGLMouseCur[2] = depth;

	//Project cursor coordinates into OGL coordinates
	//   Depth value was pre-calculated
	gluUnProject((GLdouble)MouseCur.x, (GLdouble)(view[3]-MouseCur.y), depth, model, proj, view, &OGLMouseCur[0], &OGLMouseCur[1], &OGLMouseCur[2]);		

	//Project previous cursor coordinates into OGL coordinates
	gluUnProject((GLdouble)MousePrev.x, (GLdouble)(view[3]-MousePrev.y), depth, model, proj, view, &OGLMousePrev[0], &OGLMousePrev[1], &OGLMousePrev[2]);
	
	OGLMouseCur[1] = -OGLMouseCur[1];
	OGLMousePrev[1]= -OGLMousePrev[1];

i dont know if it will make a difference but im using a perspective matrix, and not an ortho. i could be using the wrong type of matrix though and making this difficult. essentially what im trying to build is a map editor. right now this is just the front view. i plan to have 4 views: top, right, front, and camera. i also want to be able to zoom in and out and pan around (panning is finshed but zooming is still buggy). if anyone could provide some insight on zooming that would help out too! thanks!

Share this post


Link to post
Share on other sites
Advertisement
Well, a first optimization could be to store the previous unprojected mouse coords and not to recalculate them every frame.

Second, if you use gluUnProject more than once per frame, you could calculate the inverse of the modelview and projection matrices yourself (every time they change) and do the unprojection yourself. I personally don't use gluUnProject, but it seems to calculate the inverse of the given matrices every time you call it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
Well, a first optimization could be to store the previous unprojected mouse coords and not to recalculate them every frame.

Second, if you use gluUnProject more than once per frame, you could calculate the inverse of the modelview and projection matrices yourself (every time they change) and do the unprojection yourself. I personally don't use gluUnProject, but it seems to calculate the inverse of the given matrices every time you call it.


could you suggest some places where i can read up on how to build my own unproject algorithm? im rusty on my matrix math.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!