从 3D 坐标转换为 2D 坐标问题
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您使用的是 OpenGL,我建议
gluProject
用于从 3D 映射到 2D 和
gluUnProject
用于从 2D 映射到 3D(请注意 @ChrisF 对取消投影的评论)。虽然我必须承认,我没有真正的 OpenGL 经验,我只是知道你想要的术语并用谷歌搜索它。 :)
Since you're using OpenGL, I'd suggest
gluProject
for mapping from 3D to 2D andgluUnProject
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. :)
绘制健康条等的常用方法是将一些边界体积(边界框、边界球体等)或锚点转换到屏幕空间中。这很简单,因为您只需遵循 OpenGL 转换管道:
如果不提供额外信息,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:
2D to 3D is not possible without supplying extra information.
这段简短的文字解释了 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.
您只需对坐标进行与 openGL 相同的转换即可。这通常涉及以下步骤:
将其乘以平移矩阵(即相机矩阵)。这会将您的 3D 坐标置于面向屏幕的坐标系中
,
将其乘以投影矩阵。这会将坐标投影到屏幕上
假设您有这些矩阵,因为您必须将它们加载到 openGL 中,但您也可以要求它们。
You just have to do the same transform as openGL does to the coordinates. This typically involves the following steps:
multiply it by the translation matrix (i.e. camera matrix). This puts your 3d coordinate in a screen oriented coordinate system
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.