3D到2D投影 [英] 3D to 2D projection

查看:368
本文介绍了3D到2D投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java(具有处理,实际上是)的游戏编写一些具有酷炫的3D效果。

I'm trying to write a game in Java (Processing, actually) with some cool 3D effects.

我可以选择两个3D渲染器,但是都没有默认渲染器的质量或灵活性。我当时在想,如果我能得到一个能投影的函数

I have a choice of two 3D renderers, but neither have the quality or flexibility of the default renderer. I was thinking that if I could get a function to proje

所以说我有一组在3D空间中漂浮的坐标(x,y,z)。我如何在2D屏幕上找到应该绘制点的位置(视角)?

So say I have a set of coordinates (x, y, z) floating in 3D space. How would I get where on the 2D screen that point should be drawn (perspective)?

为了澄清,我只需要最低裸线 (不考虑摄像机的位置,我可以通过偏移点数来获得效果)-我不是在重新编写OpenGL。

Just to clarify, I need only the bare minimum (not taking the position of the camera into account, I can get that effect just by offsetting the points) - I'm not re-writing OpenGL.

是的,我看到有关此问题还有很多其他问题,但似乎没有一个确切的答案。

And yes, I see that there are many other questions on this - but none of them seem to really have a definitive answer.

推荐答案

您的最低要求。

column = X*focal/Z + width/2
row = -Y*focal/Z  + height/2

符号为:

X,Y,Z 是3D坐标,以毫米或米为单位;

X, Y, Z are 3D coordinates in distance units such as mm or meters;

焦距是摄像机的焦距,以像素为单位(对于VGA分辨率,焦距= 500是一个合理的选择,因为它将产生一个查看约60度;如果图像尺寸较大,请按比例缩放焦距);注意,物理焦点〜1cm << Z,从而简化了上一个答案中给出的公式。

focal is a focal length of the camera in pixels (focal= 500 for VGA resolution is a reasonable choice since it will generate a field of view about 60 deg; if you have a larger image size scale your focal length proportionally); note that physically focal~1cm << Z, which simplifies formulas presented in the previous answer.

高度和宽度是图像或传感器的尺寸(以像素为单位);

height and width are the dimensions of the image or sensor in pixels;

行,列-是图像像素坐标(注意:行从顶部开始并向下移动,而Y向上)。这是一组标准的坐标系,其中心位于相机中心(对于X,Y,Z)和图像的左上角(对于行,列,请参见绿线)。

row, column - are image pixels coordinates (note: row starts on top and goes down, while Y goes up). This is a standard set of coordinate systems centered on camera center (for X, Y, Z) and on the upper-left image corner (for row, column, see green lines).

您不确实不需要使用OpenGL,因为这些公式易于实现。但是会有一些副作用,例如,当您的对象具有多个曲面时,它们将无法正确显示,因为您无法模拟遮挡。因此,您可以添加一个深度缓冲区,该深度缓冲区是一个简单的2D数组,具有浮点Z值,可以跟踪哪些像素更近,哪些像素更远;如果尝试在同一投影位置进行多次写入,则较近的像素将始终获胜。祝你好运。

You don't need to use OpenGL indeed since these formulas are easy to implement. But there will be some side-effects such as whenever your object has several surfaces they won't display correctly since you have no way to simulate occlusions. Thus you can add a depth buffer which is a simple 2D array with float Z values that keeps track which pixels are closer and which are further; if there is an attempt to write more than once at the same projected location, the closer pixel always wins. Good luck.

这篇关于3D到2D投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆