OpenGL:鼠标移动对象,对象可以关注鼠标

发布于 2025-02-11 19:17:33 字数 859 浏览 0 评论 0原文

用鼠标选择对象后,我希望能够使用鼠标移动对象。首先,我将鼠标位置转换为世界位置,然后使用glreadpixels()将对象的深度读取为z的深度:

double xpos, ypos, zpos;
glfwGetCursorPos(window_ptr, &xpos, &ypos);
float xPercent = (xpos + 0.5f) / scr_width_ * 2.0f - 1;  // range is -1 to +1
float yPercent = (ypos + 0.5f) / scr_height_ * 2.0f - 1;  // range is -1 to +1
yPercent = -yPercent;

glReadPixels(xpos, scr_height_ - ypos - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zPercent);

然后,我们移动鼠标直到要释放鼠标为止。

last_position = (xPercent, yPercent, zPercent);

最后,我们使用与Z值相同的值并计算X的世界位置和Y的位置:

current_position = (xPercent, yPercent, zPercent);

然后,我们翻译对象模型:

model = glm::translate(model, current_position - last_postion);

问题是:

  • 对象的速度与鼠标的速度不同。

等待您的答案。

After picking an object with the mouse, I want to be able to move the object using the mouse. First, I translate mouse position to world position, and use glReadPixels() to read the depth of the object as z's:

double xpos, ypos, zpos;
glfwGetCursorPos(window_ptr, &xpos, &ypos);
float xPercent = (xpos + 0.5f) / scr_width_ * 2.0f - 1;  // range is -1 to +1
float yPercent = (ypos + 0.5f) / scr_height_ * 2.0f - 1;  // range is -1 to +1
yPercent = -yPercent;

glReadPixels(xpos, scr_height_ - ypos - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zPercent);

then we move the mouse until we want to release the mouse.

last_position = (xPercent, yPercent, zPercent);

Finally we use the same value as z's value and calculate the x's world position and y's position:

current_position = (xPercent, yPercent, zPercent);

then we translate the object model:

model = glm::translate(model, current_position - last_postion);

the issue is:

  • the object's speed is not same as mouse's.

waiting for your answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-02-18 19:17:33

问题是由屏幕与世界之间坐标系的差异引起的。您的光标位置为“百分比”(屏幕宽度的一部分),但是您的对象可能放置在由您的投影矩阵确定的其他坐标系统中(例如,以米为单位)。除非您使用的是拼字图投影,并且对象的世界空间坐标在与屏幕空间坐标的空间相同的空间中,否则您将获得不同的运动。

例如,即使您有拼字法投影,它也可以配置为使屏幕映射到20米宽的游戏世界的区域,因此将鼠标移动到[-1,1]范围内的任何位置(跨完整屏幕的宽度)仅将其转换为屏幕1/10的对象。

此外,您可能正在使用视角投影。在这种情况下,您的世界坐标不仅与屏幕坐标有所不同,而且实际上屏幕坐标和世界坐标之间存在非线性转换,这会导致对象的运动在屏幕边缘附近变形。您可以使用glm :: unvroject将鼠标光标的屏幕空间坐标坐在世界坐标中来纠正此问题。 这里是对此过程的很好的解释。

The problem is caused by the difference in coordinate systems between the screen and the world. Your cursor position is in "percent" (fraction of the screen width), but your objects are likely placed in some other coordinate system that is determined by your projection matrix (for example, in meters). Unless you are using an orthographic projection and the world-space coordinates of the object are in the same space as the screen-space coordinates, you will get different motion.

For example, even if you had an orthographic projection, it may be configured such that the screen maps to a region of game world that is 20 meters wide, so moving your mouse anywhere within the range [-1, 1] (across the full width of the screen) will only translate to the object moving over 1/10th of the screen.

Furthermore, you may be working with a perspective projection. In that case, not only will your world coordinates differ from the screen coordinates, but there is actually a nonlinear transformation between the screen coordinates and the world coordinates that will cause the object's motion to be distorted near the edges of the screen. You can correct for this by un-projecting your mouse cursor's screen-space coordinates back into world coordinates using glm::unproject. Here is a good explanation of this process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文