如何将3d点更改为2d像素位置? [英] How to change 3d point to 2d pixel location?

查看:111
本文介绍了如何将3d点更改为2d像素位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设照相机位于原点的(0,0,1)点。 -z方向进入屏幕。 z = -100以上的对象不可见(远视平面)。在远视平面,x和y分别在x轴的右边和100的左边,在y轴的左边被-100修剪。

Assume that the camera is located at (0,0,1) point looking into the origin. -z direction is going into the screen. Objects beyond z = -100 are not visible ( far viewing plane). At the far viewing plane x and y are clipped at 100 at right and -100 at left for x-axis and likewise for y-axis.

视口窗口为600像素宽300像素高。在2d像素坐标(x2d,y2d)上,(0,0)是左上角,x2d向右增加,y2d向下增加。

The viewport window is 600 pixel wide and 300 pixel height. On the 2d pixel coordinates (x2d, y2d), (0,0) is the top left corner, x2d increases to right and y2d increases going downward.

给出上面的参数,给定一个点(x,y,z),计算(x2d,y2d)的公式是什么?除非剪切了3d点(在视口中不可见),否则每个3d点都映射到一个像素。
假设透视投影。

Given above parameters, what are the formulas that calculates (x2d,y2d) given a point (x,y,z)? Each 3d point maps to a pixel unless 3d point is clipped ( not visible in the viewport). Assume perspective projection.

请不要提供谈论3D投影理论的链接。我正在寻找给定参数的特定问题的具体解决方案。

Please don't provide a link that talks about the theory of 3d projections. I am looking for specific solution to this specific problem with the given parameters.

推荐答案

如果您要做的就是查找2D屏幕如果您知道类似的三角形,则从3D位置轻松定位。无需触发器。只需组成一些变量,该变量大约是屏幕到眼睛的距离(以像素为单位)-称为focus_length。您可以调整该数字以使其看起来更逼真(太低了,看起来真的很弹性)

If all you want to do is find 2D screen locations from 3D locations its easy if you know similar triangles. No triggers are required. just make up some variable that is about the distance in pixels out of the screen to your eyes - call that the focal_length. You can adjust that figure to make it look more realistic (too low looks really stretchy)

此解决方案用于在x中向前看,向后看只是需求和多余的线条负数z在我的拙见中只是不必要的复杂性,但我不知道您的目的。

This solution is for looking forward in x, looking backwards just needs and extra line to negative the z which is just needless complexity in my humble opinion but i don't know your purpose.

首先裁剪出超出范围的像素(只需使用几个if语句。我不会伪编码出来)

first clip out pixels outside of your range (just use a couple of if statements. I'm not gonna pseudocode that out)

然后将自己置于3D世界中:

then position yourself in the 3D world:

x0 = x - yourx
y0 = y - youry
z0 = z - yourz

(在您的示例中, yourx = 0,youry = 0,yourz = -1

然后离开项目:

x2d = focal_length * x0 / z0
y2d = focal_length * y0 / z0

简单吧?我只是通过考虑火车轨道来弥补这一点,现在正在尝试制作基本的3d游戏;)

Simple huh? I just made this up by thinking about train tracks and am now trying to make a basic 3d game ;)

如果您想了解3D旋转或隐藏的数学知识

If you want to get into the math of 3D rotation or hiding faces etc... that's when things gets ... tricky.

顺便说一下,这取决于屏幕坐标的方式,一个或另一个可能需要负号或x2d或y2d等式。嗯,你知道了。

By the way depending on how your screen coordinates are, you might need negative signs on one or the other or both of x2d or y2d equations. Meh you figure it out.

这篇关于如何将3d点更改为2d像素位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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