从 3D 坐标转换为 2D 坐标问题

发布于 2024-10-31 14:12:55 字数 317 浏览 3 评论 0原文

我使用 OpenGL 和 C++ 来制作游戏。现在,我想知道如何将 3D 坐标转换为 2D 位置,例如:在 3D 模型的头部上绘制 2D 健康栏,或者沿着这些路线绘制一些东西。 xD

所以无论如何,我想知道如何设置它可以在 SFML 中使用。现在我正在查找一些信息,但我不完全理解这些方法...

基本上,我问我如何使用 SFML 将 3D opengl 坐标转换为 2D 坐标到屏幕上,

我也想知道如何将 2D 坐标转换回 3D,我也知道这一点。

还有一些示例代码会很好,这样我就可以得到更好的图片,

感谢阅读

-Molma

Im using OpenGL with c++ to make a game. and right now, im wondering how i would be able to turn 3D coordinates into 2D positions for example: drawing a 2D health bar over a 3D model's head, or something along those lines. xD

so anyways, im wondering how i would be able to set this up where it can be used in SFML. right now im looking up some information but i dont completely understand the methods...

Basicaly, im asking how would i make 3D opengl coordinates convert to 2D coordinates onto the screen using SFML

im also wondering how i would convert 2D coordinates back to 3D, just so i know that too.

also some example code would be nice, just so i get a better picture

thanks for reading

-Molma

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

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

发布评论

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

评论(4

最后的乘客 2024-11-07 14:12:55

由于您使用的是 OpenGL,我建议 gluProject用于从 3D 映射到 2D 和 gluUnProject 用于从 2D 映射到 3D(请注意 @ChrisF 对取消投影的评论)。
虽然我必须承认,我没有真正的 OpenGL 经验,我只是知道你想要的术语并用谷歌搜索它。 :)

Since you're using OpenGL, I'd suggest gluProject for mapping from 3D to 2D and gluUnProject for mapping from 2D to 3D (note @ChrisF's comment on unprojecting).
Though I gotta admit, I got no real experience with OpenGL, I just know the terminology for what you wanted and googled for it. :)

狼亦尘 2024-11-07 14:12:55

绘制健康条等的常用方法是将一些边界体积(边界框、边界球体等)或锚点转换到屏幕空间中。这很简单,因为您只需遵循 OpenGL 转换管道:

clip_position = Projection_Matrix * Modelview_Matrix * vertex_position
viewport_position.{x,y} = viewport.{x,y} + viewport.{width,height} * (0.5 + clip_position.{x,y} / 2)

如果不提供额外信息,2D 到 3D 是不可能的。

The usual approach for drawing health bars and this like is transforming some bounding volume (bounding box, bounding sphere, etc.) or an anchor point into screen space. This is easy enough, since you only have to follow the OpenGL transformation pipeline:

clip_position = Projection_Matrix * Modelview_Matrix * vertex_position
viewport_position.{x,y} = viewport.{x,y} + viewport.{width,height} * (0.5 + clip_position.{x,y} / 2)

2D to 3D is not possible without supplying extra information.

美人骨 2024-11-07 14:12:55

这段简短的文字解释了 3D 坐标的 2D 投影的基础知识。相反的方法有点困难,因为您只有两个已知值(二维坐标),并且不足以准确提取三个坐标。您能得到的最好的结果是一条线(从观察者开始,穿过屏幕上的点并延伸到无穷远。3D 点基本上可以位于屏幕上点后面的任何位置。但是如果您知道一些额外的信息,到相机或知道其 2D 坐标的物体的一部分位置,您可以确定 3 个坐标。

This short text explains the basics of 2D projection of 3D coordinates. The other way around is a bit more difficult because you have only two known values (the 2D coordinates) and that is not enough exactly extract the three coordinates. The best you can get is a line (starting at the viewer, going through the point on the screen and extending to infinity. The 3D point can basically be anywhere behind the point on the screen. But if you know something extra, the distance to the camera or a part of the position of the thing of which you know the 2D coordinates, you can determine the 3 coordinates.

独﹏钓一江月 2024-11-07 14:12:55

您只需对坐标进行与 openGL 相同的转换即可。这通常涉及以下步骤:

  1. 将其乘以平移矩阵(即相机矩阵)。这会将您的 3D 坐标置于面向屏幕的坐标系中

  2. 将其乘以投影矩阵。这会将坐标投影到屏幕上

假设您有这些矩阵,因为您必须将它们加载到 openGL 中,但您也可以要求它们。

You just have to do the same transform as openGL does to the coordinates. This typically involves the following steps:

  1. multiply it by the translation matrix (i.e. camera matrix). This puts your 3d coordinate in a screen oriented coordinate system

  2. multiply it by the projection matrix. This will project the coordinate onto the screen

Presumably you have those matrices, since you have to load them in openGL, but you can also ask for them.

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