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

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

问题描述

我正在尝试用 Java(Processing,实际上)编写一个带有一些很酷的 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;

focal 是相机的焦距(以像素为单位)(对于 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;

row, column - 是图像像素坐标(注意:行从顶部开始向下,而 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,因为这些公式很容易实现.但是会有一些副作用,例如当您的对象有多个表面时,它们将无法正确显示,因为您无法模拟遮挡.因此,您可以添加一个深度缓冲区,它是一个简单的二维数组,带有浮动 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天全站免登陆