java 透视投影:确定3D空间(x,y,z)中点的2D屏幕坐标(x,y)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/701504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Perspective Projection: determine the 2D screen coordinates (x,y) of points in 3D space (x,y,z)
提问by
I wish to determine the 2D screen coordinates (x,y) of points in 3D space (x,y,z).
我希望确定 3D 空间 (x,y,z) 中点的 2D 屏幕坐标 (x,y)。
The points I wish to project are real-world points represented by GPS coordinates and elevation above sea level.
我希望投影的点是由 GPS 坐标和海拔高度表示的真实世界点。
For example: Point (Lat:49.291882, Long:-123.131676, Height: 14m)
例如:点(纬度:49.291882,经度:-123.131676,高度:14m)
The camera position and height can also be determined as a x,y,z point. I also have the heading of the camera (compass degrees), its degree of tilt (above/below horizon) and the roll (around the z axis).
相机位置和高度也可以确定为 ax,y,z 点。我还有相机的航向(罗盘度数)、倾斜度(高于/低于地平线)和滚动(围绕 z 轴)。
I have no experience of 3D programming, therefore, I have read around the subject of perspective projection and learnt that it requires knowledge of matrices, transformations etc - all of which completely confuse me at present.
我没有 3D 编程经验,因此,我阅读了透视投影的主题,并了解到它需要矩阵、变换等方面的知识——目前所有这些都让我感到困惑。
I have been told that OpenGL may be of use to construct a 3D model of the real-world points, set up the camera orientation and retrieve the 2D coordinates of the 3D points.
有人告诉我,OpenGL 可用于构建真实世界点的 3D 模型、设置相机方向并检索 3D 点的 2D 坐标。
However, I am not sure if using OpenGL is the best solution to this problem and even if it is I have no idea how to create models, set up cameras etc
但是,我不确定使用 OpenGL 是否是这个问题的最佳解决方案,即使是我也不知道如何创建模型、设置相机等
Could someone suggest the best method to solve my problem? If OpenGL is a feasible solution i'd have to use OpenGL ES if that makes any difference. Oh and whatever solution I choose it must execute quickly.
有人可以建议解决我的问题的最佳方法吗?如果 OpenGL 是一个可行的解决方案,我必须使用 OpenGL ES,如果这有什么不同的话。哦,无论我选择什么解决方案,它都必须快速执行。
回答by rofrankel
Here's a very general answer. Say the camera's at (Xc, Yc, Zc) and the point you want to project is P = (X, Y, Z). The distance from the camera to the 2D plane onto which you are projecting is F (so the equation of the plane is Z-Zc=F). The 2D coordinates of P projected onto the plane are (X', Y').
这是一个非常笼统的答案。假设相机位于 (Xc, Yc, Zc) 并且您要投影的点是 P = (X, Y, Z)。从相机到您要投影到的 2D 平面的距离为 F(因此平面的方程为 Z-Zc=F)。投影到平面上的 P 的二维坐标是 (X', Y')。
Then, very simply:
然后,非常简单:
X' = ((X - Xc) * (F/Z)) + Xc
X' = ((X - Xc) * (F/Z)) + Xc
Y' = ((Y - Yc) * (F/Z)) + Yc
Y' = ((Y - Yc) * (F/Z)) + Yc
If your camera is the origin, then this simplifies to:
如果您的相机是原点,那么这可以简化为:
X' = X * (F/Z)
X' = X * (F/Z)
Y' = Y * (F/Z)
Y' = Y * (F/Z)
回答by Judge Maygarden
You do indeed need a perspective projectionand matrix operationsgreatly simplify doing so. I assume you are already aware that your spherical coordinatesmust be transformed to Cartesian coordinatesfor these calculations.
你确实需要一个透视投影和矩阵运算大大简化了这样做。我假设您已经知道您的球坐标必须转换为笛卡尔坐标才能进行这些计算。
Using OpenGLwould likely save you a lot of work over rolling your own software rasterizer. So, I would advise trying itfirst. You can prototype your system on a PC since OpenGL ES is not too different as long as you keep it simple.
与滚动您自己的软件光栅器相比,使用OpenGL可能会为您节省大量工作。所以,我建议先尝试一下。您可以在 PC 上对您的系统进行原型设计,因为只要您保持简单,OpenGL ES 就没有太大区别。
回答by fa.
If just need to compute coordinates of some points, you should only need some algebra skills, not 3D programming with openGL.
如果只需要计算一些点的坐标,你应该只需要一些代数技能,而不是用openGL进行3D编程。
Moreover openGL does not deal with Geographic coordinates
此外,openGL 不处理地理坐标
First get some doc about WGS84and geodesic coordinates, you have first to convert your GPS data into a cartesian frame ( for instance the earth centric cartesian frame in which is defined the WGS84 ellipsoid ).
首先获得一些关于WGS84和测地线坐标的文档,您必须首先将 GPS 数据转换为笛卡尔坐标系(例如定义 WGS84 椭圆体的以地球为中心的笛卡尔坐标系)。
Then the computations with matrix can take place. The chain of transformations is roughly :
然后可以进行矩阵计算。转换链大致是:
- WGS84
- earth centric coordinates
- some local frame
- camera frame
- 2D projection
- WGS84
- 地心坐标
- 一些局部框架
- 相机框
- 二维投影
For the first conversion see thisThe last involves a projection matrix The others are only coordinates rotations and translation. The "some local frame" is the local cartesian frame with origin as your camera location tangent to the ellipsoid.
对于第一次转换,请参阅此最后一个涉及投影矩阵 其他只是坐标旋转和平移。“一些局部坐标系”是局部笛卡尔坐标系,其原点为与椭球相切的相机位置。
回答by shouston
I'd recommend "Mathematics for 3D Game Programming and Computer Graphics" by Eric Lengyel. It covers matrices, transformations, the view frustum, perspective projection and more.
我推荐 Eric Lengyel 的“3D 游戏编程和计算机图形数学”。它涵盖了矩阵、变换、视锥体、透视投影等。
There is also a good chapter in The OpenGL Programming Guide (red book) on viewing transformations and setting up a camera (including how to use gluLookAt).
The OpenGL Programming Guide(红皮书)中还有一个关于查看转换和设置相机(包括如何使用 gluLookAt)的好章节。
If you aren't interested in displaying the 3D scene and are limited to using OpenGL ES then it may be better to just write your own code to do the mapping from 3D to 2D window coords. As a starting point you could download Mesa 3D, an open source implementation of OpenGL, to see how they implement gluPerspective (to set a projection matrix), gluLookAt (to set a camera transformation) and gluProject (to project a 3D point to 2D window coords).
如果您对显示 3D 场景不感兴趣并且仅限于使用 OpenGL ES,那么最好编写自己的代码来执行从 3D 到 2D 窗口坐标的映射。作为起点,您可以下载Mesa 3D(OpenGL 的开源实现),以了解它们如何实现 gluPerspective(设置投影矩阵)、gluLookAt(设置相机变换)和 gluProject(将 3D 点投影到 2D 窗口)坐标)。

