将鼠标位置转换为世界位置 OpenGL

发布于 2024-09-06 16:12:37 字数 100 浏览 0 评论 0原文

嘿,正在为我的游戏开发地图编辑器,我正在尝试将鼠标位置转换为游戏世界中的位置,视图是使用 gluPerspective< 设置的/em>

Hey, I'm working on a map editor for my game, and I'm trying to convert the mouse position to a position in the game world, the view is set up using gluPerspective

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

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

发布评论

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

评论(2

烟花易冷人易散 2024-09-13 16:12:37

一个好的起点是函数 gluUnProject ,它获取鼠标坐标并计算对象空间坐标。看看 http://nehe.gamedev.net/data/ articles/article.asp?article=13 了解基本教程。

更新:

您必须启用深度缓冲才能使该文章中的代码正常工作。鼠标坐标的 Z 值是根据该点深度缓冲区中的值确定的。

在初始化代码中,请确保执行以下操作:

glEnable(GL_DEPTH);

A good place to start would be the function gluUnProject, which takes mouse coordinates and calculates object space coordinates. Take a look at http://nehe.gamedev.net/data/articles/article.asp?article=13 for a basic tutorial.

UPDATE:

You must enable depth buffering for the code in that article to work. The Z value for mouse coordinates is determined based on the value in the depth buffer at that point.

In your initialization code, make sure you do the following:

glEnable(GL_DEPTH);
阪姬 2024-09-13 16:12:37

屏幕上的点代表 3D 空间中的整条线(无限的点集)。

大多数有与您类似问题的人实际上是在尝试通过单击来选择一个对象。如果这就是您所追求的,OpenGL 提供了一种选择模式,通常比尝试将屏幕坐标转换为真实世界坐标更有效。

使用选择模式(通常)非常简单:从 开始gluPickMatrix,用于指定点击点周围的小框。然后,您可以在选择模式中绘制场景。当您完成后,它不会实际绘制任何内容,而是返回您指定的框中绘制内容的记录。如果没记错的话,它们是按 Z 顺序排列的,因此列表中的第一个是显示在最前面的(即您通常想要的)。

A point on the screen represents an entire line (an infinite set of points) in 3D space.

Most people with questions similar to yours are really trying to select an object by clicking on it. If that's what you're after, OpenGL offers a selection mode that's generally more effective than trying to convert the screen coordinate into real-world coordinates.

Using selection mode is (usually) pretty simple: you start with gluPickMatrix, which you use to specify a small box around the click point. You then draw your scene in selection mode. When you're done, instead of actually drawing anything, it gives you back records of what would have been drawn in the box you specified. If memory serves, those are arranged in Z order, so the first one in the list is what would have displayed front-most (i.e., the one you usually want).

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