简体   繁体   English

从像素到世界坐标公式说明

[英]From pixel to world coordinate formula explanation

I have the following (relatively easy) problem. 我有以下(相对容易)的问题。 I am developing a ray tracer and I am following the tutorial explained in this link: 我正在开发光线追踪器,并且正在遵循此链接中说明的教程:

http://www.scratchapixel.com/code.php?id=3&origin=/lessons/3d-basic-rendering/introduction-to-ray-tracing http://www.scratchapixel.com/code.php?id=3&origin=/lessons/3d-basic-rendering/introduction-to-ray-tracing

There is a formula I don't get though, which is used to map the pixel (i,j) to world coordinates. 有一个我不知道的公式,用于将像素(i,j)映射到世界坐标。 The formula is the following: 公式如下:

float fov = 30, aspectratio = width / height; 
float angle = tan(M_PI * 0.5 * fov / 180.);
float xx = (2 * ((x + 0.5) * invWidth) - 1) * angle * aspectratio; 
float yy = (1 - 2 * ((y + 0.5) * invHeight)) * angle;

In this tutorial the camera is placed at (0,0,0) and up/right/lookAt vector are not used at all. 在本教程中,相机位于(0,0,0)处,完全不使用up / right / lookAt向量。 It seems that in every tutorial a different formula is used to map a pixel and I don't manage to get the reason. 似乎在每个教程中,都使用不同的公式来映射像素,但我无法弄清原因。 Moreover, what about if my camera is not placed at (0,0,0) but in another position that I can decide? 此外,如果我的相机不是放在(0,0,0)处,而是在我可以决定的其他位置,该怎么办? How would the formula vary? 公式将如何变化? May you kindly help me? 你能帮我吗? Thanks! 谢谢!

http://scratchapixel.com/lessons/3d-basic-rendering/3d-viewing-pinhole-camera http://scratchapixel.com/lessons/3d-basic-rendering/3d-viewing-pinhole-camera

If you had made some more research on this website you would have found the answer. 如果您对此网站进行了更多研究,您将找到答案。 The whole point of this website is to explain that sort of technique. 该网站的重点是解释这种技术。 Just please make an effort before expecting anyone on SO to answer for you. 请期待SO上的任何人为您解答,然后再努力。 Your question should be heavily down voted. 您的问题应被否决。 This is pure lazyness. 这是纯粹的懒惰。

I recommend you to follow this link: https://www.opengl.org/wiki/Compute_eye_space_from_window_space 我建议您点击以下链接: https : //www.opengl.org/wiki/Compute_eye_space_from_window_space

Your x range is in screen coords [0, WindowWidth]. 您的x范围在屏幕坐标[0,WindowWidth]中。 In the first line you compute it in Normalized Device Coords (NDC), which are in the Range of [-1,1]. 在第一行中,您以[-1,1]范围内的归一化设备坐标(NDC)进行计算。

x_NDC = ( x_Screen *2 / WindowWidth ) - 1;

You can check it by setting x_Screen to Maximum (WindowWidth) and Minimum (0). 您可以通过将x_Screen设置为Maximum(WindowWidth)和Minimum(0)进行检查。 You get 1 and -1. 您得到1和-1。 It's the same for the y Coord: y坐标相同:

y_NDC = ( y_Screen *2 / WindowHeight ) - 1;

Your z Range is [Near,Far]. 您的z范围是[近,远]。

z_NDC = ( z_Screen - Near ) / ( Far - Near);  // Here you have it scaled to [0,1]
z_NDC = 2*NDC -1 //Scaled to [-1,1];

After you got your NDC coords, just multiply it with the inverse of the Projection Matrix and the inverse ModelView Matrix to get world coords. 获得NDC坐标后,只需将其与Projection Matrix的逆和ModelView Matrix的逆相乘即可获得世界坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM