zgintasz's picture

mouse coords to world coords

I'm making a windows forms application with opengl view. I need to get the mouse coords converted to the opengl world coords. How can I do that? Here is a bit of code, which might be useful:

        private void SetupViewport()
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
            Matrix4 projection = Matrix4.CreateOrthographic(Width / zoom, Height / zoom, 1.0f, 64.0f);
            cameraMatrix = Matrix4.CreateTranslation(0f, 0f, 0f);
            GL.LoadMatrix(ref projection);
        private void RenderView()
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadMatrix(ref cameraMatrix);



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zgintasz's picture

bump, really need that.

alarial's picture

I haven't messed with 3d translation of the mouse but I was able to convert mine to 2d using the following code in my mouse button down event handler:

float x = (e.X / (ClientRectangle.Width / 2.0f)) - 1;
float y = -(e.Y / (ClientRectangle.Height / 2.0f)) + 1;
zgintasz's picture

it doesn't converts coords correctly. The calculation should also somehow use cameraMatrix...

zgintasz's picture

EDIT: nevermind, fixed it.