使用 GLM 功能放大 OpenGL 中的当前鼠标位置
Zoom in on current mouse position in OpenGL using GLM functionality
我对在OpenGL中放大当前鼠标位置的任务感到绝望。我已经尝试了很多不同的东西并阅读了有关此的其他帖子,但我无法根据我的特定问题调整可能的解决方案。因此,据我了解,您必须获取鼠标光标的当前窗口坐标,然后取消投影以获取世界坐标,最后转换为这些世界坐标。
为了查找当前鼠标位置,每次单击鼠标右键时,我都会在 GLUT 鼠标回调函数中使用以下代码。
if(button == 2)
{
mouse_current_x = x;
mouse_current_y = y;
...
接下来,我在设置模型视图和投影矩阵之前取消投影显示函数中的当前鼠标位置,这似乎也可以正常工作:
// 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);
现在翻译是麻烦开始的地方。目前,我正在绘制一个具有维度(维度X,维度Y,dimensionZ)的立方体并转换为该立方体的中心,因此我的缩放也发生在中心点。我通过向 z 方向(小车)平移来实现缩放:
// 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);
我试图通过转换为 worldT坐标向量来替换对立方体中心的平移,但这不起作用。我还尝试按宽度或高度缩放矢量。我在这里错过了一些重要的步骤吗?
也许这在您的情况下不起作用。 但对我来说,这似乎是处理这个问题的最好方法。使用 glulookat() 查看您已经找到的鼠标单击的 xyz 位置。 然后将 gluPerspective() 更改为较小的视角以实现实际缩放。
相关文章:
- 使用 dlib 记录鼠标位置
- 获取窗口中的鼠标位置
- 将文本添加到鼠标位置时,请单击“给出分割故障”
- 在鼠标位置移动精灵
- 如何在Mac OSX上的窗口客户端内获取真实的鼠标位置?
- 如何始终获得QT鼠标位置
- QT QgraphicView和Qgraphicscene鼠标位置
- QT:QGraphicsScene鼠标位置始终(0,0)
- SFML鼠标位置和碰撞从文件加载
- 根据鼠标位置缩小到窗口中
- 如何根据鼠标位置旋转精灵
- OpenGL获取离当前鼠标位置最近的世界三维点的三维坐标
- C++ SDL 根据鼠标位置绘制动态矩形
- 如何检索WM_COMMAND中的鼠标位置
- Qt mouseMoveEvent-跟踪鼠标位置
- OpenGl获取鼠标位置
- 获取RGBDemo的鼠标位置坐标
- 通过互联网发送鼠标位置
- 如何使用可可获得鼠标位置差异的变化
- 鼠标位置并单击 sfml 中的检测