I’m developing a c# app based upon opengl wrapping (from ogl dll) where I show a 3d surfaces (obtained from glquads that interpolates my control points).
I want to select and move dinamically these controlpoints. The problem is that I get coordinates from mouse event handler especially from MouseEventArgs but these coords is completely unbounded from my view.
There is a way to map current xy coords of my mouse on control up xyz of my ogl shene??
Thankyou in advance.
I also would be interested in learning how to do this. I am writing a game engine and for the level editor I want people to be able to select verticies in 3d space… but i dont know where to even start in going from mouse cooords to 3d space coords
I would recommend gluUnproject. This method allows you transform a mouse coordinate into an object space coordinate, given the viewport, projection, and modelview matrices. gluProject does the opposite.
I have the same problem as the TS and I also tried it with gluUnProject but didnt succeeded.
I get strange values of which I know, they arent correct.
Here is my code to do it:
GLint viewport; // Where The Viewport Values Will Be Stored glGetIntegerv(GL_VIEWPORT, viewport); // Retrieves The Viewport Values (X, Y, Width, Height) GLdouble modelview; // Where The 16 Doubles Of The Modelview Matrix Are To Be Stored glGetDoublev(GL_MODELVIEW_MATRIX, modelview); // Retrieve The Modelview Matrix GLdouble projection; // Where The 16 Doubles Of The Projection Matrix Are To Be Stored glGetDoublev(GL_PROJECTION_MATRIX, projection); // Retrieve The Projection Matrix POINT mouse; // Stores The X And Y Coords For The Current Mouse Position GetCursorPos(&mouse); // Gets The Current Cursor Coordinates (Mouse Coordinates) ScreenToClient(hwnd, &mouse); GLfloat winX, winY, winZ; // Holds Our X, Y and Z Coordinates winX = (float)mouse.x; // Holds The Mouse X Coordinate winY = (float)mouse.y; // Holds The Mouse Y Coordinate winY = (float)viewport - winY; // Subtract The Current Mouse Y Coordinate From The Screen Height. glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); GLdouble posX, posY, posZ; // Hold The Final Values gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
another approach would be using gluProject to calculate all vertices’ window coordinates and thereby finding the vertex which is the closest to the mouse pointer’s position. if you do it like this, you don’t have to fight with the z-coordinate.