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

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

问题描述

假设相机位于 (0,0,1) 点,面向原点.-z 方向进入屏幕.超出 z = -100 的对象不可见(远视平面).在远视平面上,x 和 y 在右侧被裁剪为 100,对于 x 轴和 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 像素.在二维像素坐标 (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.

推荐答案

如果您只想从 3D 位置找到 2D 屏幕位置,那么如果您知道相似三角形就很容易了.不需要触发器.只需组成一些变量,该变量大约是屏幕到您眼睛的像素距离 - 将其称为焦距.你可以调整那个数字让它看起来更真实(太低看起来真的很有弹性)

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天全站免登陆