Increase the current mouse position in OpenGL using GLM functions

I am desperate to zoom in on the current mouse position in OpenGL. I tried many different things and read other posts about this, but I could not adapt possible solutions to my specific problem. So as I understand it, you will need to get the current coordinates of the mouse cursor window and then not create them to get the world coordinates and finally translate them to those world coordinates.

To find the current mouse positions, I use the following code in my GLUT mouse callback function every time I right-click.

if(button == 2)
{
    mouse_current_x = x;
    mouse_current_y = y;
...

      

Then, before setting up the ModelView and Projection matrices, I don't create the current mouse positions in my display function, which also works great:

// Unproject Window Coordinates
float mouse_current_z;
glReadPixels(mouse_current_x, mouse_current_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouse_current_z);
glm::vec3 windowCoordinates = glm::vec3(mouse_current_x, mouse_current_y, mouse_current_z);
glm::vec4 viewport = glm::vec4(0.0f, 0.0f, (float)width, (float)height);
glm::vec3 worldCoordinates = glm::unProject(windowCoordinates, modelViewMatrix, projectionMatrix, viewport);
printf("(%f, %f, %f)\n", worldCoordinates.x, worldCoordinates.y, worldCoordinates.z);

      

Now the translation begins where the problem begins. I am currently drawing a cube with dimensions (X dimension, Y dimension, Z dimension) and translate to the center of this cube, so my scaling happens to the center point as well. I achieve scaling by translating to z-direction (dolly):

// Set ModelViewMatrix
modelViewMatrix = glm::mat4(1.0); // Start with the identity as the transformation matrix
modelViewMatrix = glm::translate(modelViewMatrix, glm::vec3(0.0, 0.0, -translate_z)); // Zoom in or out by translating in z-direction based on user input 
modelViewMatrix = glm::rotate(modelViewMatrix, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the whole szene in x-direction based on user input
modelViewMatrix = glm::rotate(modelViewMatrix,  rotate_y, glm::vec3(0.0f, 1.0f, 0.0f)); // Rotate the whole szene in y-direction based on user input
modelViewMatrix = glm::rotate(modelViewMatrix, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the camera by 90 degrees in negative x-direction to get a frontal look on the szene
modelViewMatrix = glm::translate(modelViewMatrix, glm::vec3(-dimensionX/2.0f, -dimensionY/2.0f, -dimensionZ/2.0f)); // Translate the origin to be the center of the cube
glBindBuffer(GL_UNIFORM_BUFFER, globalMatricesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(modelViewMatrix));
glBindBuffer(GL_UNIFORM_BUFFER, 0);

      

I tried to replace the translation with the center of the cube by translating it to the worldCoordinates vector, but that didn't work. I also tried to scale the vector in width or height. Am I missing an important step here?

+3


source to share


1 answer


It might not work in your case. But to me it seems like the best way to handle this. Use glulookat () to look at the xyz position of the mouse click you already found. Then change gluPerspective () to a smaller viewing angle to achieve the actual magnification.



0


source







All Articles