11/19/2022 0 Comments Opengl getting mouse coordinates![]() ![]() I use this code to pick 2D objects but I think it may be useful. I hope this gets you a little further, and if you do find the soution please post it here, since I need the answer too. So what you (and I) need is to be able to make a vector from the camera that passes through the x,y,z from gluUnproject above and find out where it intersects the z=0 plane. I've tried putting different values in where I have 0.0 above, but It didn't really do what I want. z will be the camera's z and x and y will be there, not where z=0 which is what you want. The reason we see 'screenHeight - initalMouseY' or 'screenHeight - currentMouseY' is because most implements of 2D graphics measures the mouse coordinate from the top-left corner of the screen. Unfortunately this gives coordinates that are pretty mush useless. The variables 'currentMouseX', and 'currentMouseY' represent the current XY coordinates of the mouse while the use still has the mouse button down. GluUnProject ( (double) mouse_x, (double) mouse_y, 0.0, modelMatrix, projMatrix, viewPort, &x, &y, &z ) each coordinate in opengl actually has four components, x, y, z, and w by default, the following are enabled: dragging the map and zooming the map by multitouch or double-click on touch screen devices dragging the map with the mouse, double-click zooming and right-click area selection on all other devices find the gps coordinates of any address. You can you gluUnProject to convert the screen coordinates to your model's coordinate system. The next part is where I've been having trouble. Now you've got the model's screen coordinates, I'll leave it up to you to decide if the mouse is over it and if it is clicked or whatever. I'm not exactly sure what screen_z is all about. ![]() GLdouble modelMatrix GLdouble projMatrix GLint viewPort glGetDoublev ( GL_MODELVIEW_MATRIX, modelMatrix ) glGetDoublev ( GL_PROJECTION_MATRIX, projMatrix ) glGetIntegerv ( GL_VIEWPORT, viewPort ) gluProject ( sprite_x, sprite_y, sprite_z, modelMatrix, projMatrix, viewPort, &screen_x, &screen_y, &screen_z ) I'm using gluProject to get the model's position in screen coordinates like so: I'm kinda having the same problem as you. I needed the world coordinates of my mouse, not a picker. The mouse position is given in viewport coordinates with the. I think that somewhere I am not taking into account the zoomOut factor when maintaining the mouse position.ĮDIT * *********** here is my final code that I am still struggling with. Those familiar with OpenGL know this as the view matrix (or rolled into the modelview matrix). In this next example we get the mouse position when the left mouse button is pressed on the image. ![]() Seems to work, but frustrum jumps when I make a new click. (0 - mouseStoreY) * zoomOut + mouseStoreY, (window_height - mouseStoreY) * zoomOut + mouseStoreY, (window_width - mouseStoreX) * zoomOut + mouseStoreX, ![]() (where mouseStoreX and mouseStoreY are the position saved on the first click) glOrtho( (0 -mouseStoreX )* zoomOut + mouseStoreX, My question is, how do I zoom in using the mouse position as the center? glOrtho( 0 * zoomOut, window_width * zoomOut, window_height * zoomOut, 0 * zoomOut, 100, -100) When I execute the zoom function in the mouse callback, I multiply the frust edges by the zoom factor. Then I set glOrtho so that my frustrum forces the screen coordinates to match the world coordinates. With the color value, we will figure out which object are there(picked. I have set the viewport to standard width and height. 2) Read the color value and depth value from the back buffer given by the mouse position. I am currently using glOrtho for zooming and panning a 2D plot I am presenting. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |