[GAMES101] Homework 7 Path Tracing Doubts & Answers about the camera ray direction dir in Renderer::Render()

[GAMES101] Homework 7 Path Tracing Doubts & Answers about Renderer::Render() camera ray direction dir

Many similar problems are actually caused by deviations in the understanding of the principles. Here, it mainly refers to the confusion of the coordinate space .

Doubt: why the light vector from the camera in void Renderer::Render() is (-x, y, 1)

Careful friends should have discovered that void Renderer::Render(const Scene&)the camera light vectors in homework 6 and homework 7 dir: Vector3fare slightly different:

Homework 6:Vector3f dir = normalize(Vector3f(x, y, -1));

Homework 7:Vector3f dir = normalize(Vector3f(-x, y, 1));

I don’t know if everyone’s situation at that time was the same as that of the author: when I was doing homework 6, I still felt that the given dir was very reasonable, and after derivation, I found that there was no problem; when it was time for homework 7, I found that the dir direction of the light emitted by the camera was different—— - "Except for y, the rest are reversed", I will think about how it came about for a while, and even think that the code provided is wrong, but when I try the code that has already worked, I find that such a dir is correct , so it is difficult to understand.

Now it seems that the root of this doubt is that I have problems with the understanding of ray tracing principles and camera space . This article solves my confusion:

  1. I confuse some concepts of rasterization and ray tracing: both can be regarded as a function of the mapping of objects in the scene to the pixels of the 2D image; rasterization requires MVP transformation, and ray tracing is calculated in world space , the P transformation is not required, and the inverse transformation of the V transformation is required to convert the camera coordinates of pixels into world coordinates, and the M transformation is usually implied in the model file. Looking at the code framework of ray tracing-related operations, you will find that there is no code for MVP transformation.

  2. I mistakenly thought that the ray tracing process was a process in camera space. In fact, the rays emitted by ray tracing and subsequent tracing calculations are relative to the world coordinate system rather than the camera coordinate system. In assignments 6 and 7, we transformed from the screen space to the camera space, and then obtained Vector from the camera to a pixel ( x , y , − 1 ) (x, y, -1)(x,y,1 ) , the ray vector in this step is still in the camera space,we also need to transform it into a vector in the world coordinate system, and do a transformation from the camera space to the world space, that is, to rotate and translate the camera.

  3. We know that, according to the agreement in the course, the Gaze direction of the camera after View transformation points to the negative direction of the z-axis, the Up direction points to the positive direction of the y-axis, and the Right direction points to the positive direction of the x-axis. The so-called z-axis, y-axis and x-axis here, That is, the axis of the camera coordinate system. The reason why I mention this here is to remind that the direction of Gaze is not the positive axis of the camera coordinate system. The so-called camera looks towards the -z axis. The z axis here is the z axis of the camera coordinate system, not the world coordinate system.

Combining the above new understanding, we now know that the reason why the dir vectors in assignments 6 and 7 are different is that the placement of the camera in the world space is inconsistent (including position and direction, and the influencing factor here is direction). We now know the screen coordinates ( x , y ) of the center of the pixel (x,y)(x,y ) corresponding to the position in the camera space is( x , y , − 1 ) (x,y,-1)(x,y,1 ) , the camera of the camera space is located at the origin( 0 , 0 , 0 ) (0,0,0)(0,0,0 ) , so the ray vector from the camera to a certain pixel is( x , y , − 1 ) (x,y,-1)(x,y,1 ) , which is consistent in camera space, and we now know that the world coordinates of the dir vector in assignment 6 are( x , y , − 1 ) (x,y,-1)(x,y,1 ) , the world coordinates of the dir vector in assignment 7 are( − x , y , 1 ) (-x,y,1)(x,y,1 ) , equivalent to knowing the input and output vectors, we can inversely find the transformation matrix from camera space to world space.

Suppose the camera's world coordinates are ( xe , ye , ze ) (x_e,y_e,z_e)(xe,ye,ze)

Homework 6:

[ ? ? ? x e ? ? ? y e ? ? ? z e 0 0 0 1 ] c a m _ t o _ w o r l d ( x y − 1 0 ) = ( x y − 1 0 ) \begin{bmatrix} ? & ? & ? & x_e \\ ? & ?& ? & y_e \\ ? & ? & ? & z_e \\ 0 & 0 & 0 & 1 \\ \end{bmatrix}_{cam\_to\_world}\begin{pmatrix}x \\ y \\ -1 \\ 0\end{pmatrix}=\begin{pmatrix}x \\ y \\ -1 \\ 0\end{pmatrix} ???0???0???0xeyeze1 cam_to_world xy10 = xy10

It is not difficult to understand:

M c a m _ t o _ w o r l d _ H W 6 = [ 1 0 0 x e 0 1 0 y e 0 0 1 z e 0 0 0 1 ] M_{cam\_to\_world\_HW6} = \begin{bmatrix} 1 & 0 & 0 & x_e \\ 0 & 1& 0 & y_e \\ 0 & 0 & 1 & z_e \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} Mcam_to_world_HW6= 100001000010xeyeze1

This shows that the LookAt direction of the camera in Job 6 is aligned with the -z axis of the world coordinate system (note that LookAt points to the negative direction of the z-axis of the camera coordinate system), the Up direction is aligned with the y-axis of the world coordinate system, and the Right direction is aligned with the world coordinate system. The x-axis, combined with the position of the camera and the scene object in assignment 6 ( Vector3f eye_pos(-1, 5, 10);see the .obj file for the camera position and the vertex information of the scene object), the camera looks in the negative direction of the z-axis, emm, reasonable.

Homework 7:

[ ? ? ? x e ? ? ? y e ? ? ? z e 0 0 0 1 ] c a m _ t o _ w o r l d ( x y − 1 0 ) = ( − x y 1 0 ) \begin{bmatrix} ? & ? & ? & x_e \\ ? & ?& ? & y_e \\ ? & ? & ? & z_e \\ 0 & 0 & 0 & 1 \\ \end{bmatrix}_{cam\_to\_world}\begin{pmatrix}x \\ y \\ -1 \\ 0\end{pmatrix}=\begin{pmatrix}-x \\ y \\ 1 \\ 0\end{pmatrix} ???0???0???0xeyeze1 cam_to_world xy10 = xy10

It is not difficult to understand:

M c a m _ t o _ w o r l d _ H W 7 = [ − 1 0 0 x e 0 1 0 y e 0 0 − 1 z e 0 0 0 1 ] M_{cam\_to\_world\_HW7} = \begin{bmatrix} -1 & 0 & 0 & x_e \\ 0 & 1& 0 & y_e \\ 0 & 0 & -1 & z_e \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} Mcam_to_world_HW7= 100001000010xeyeze1

This means that the LookAt direction of the camera in Job 7 is aligned with the z-axis of the world coordinate system, the Up direction is aligned with the y-axis of the world coordinate system, and the Right direction is aligned with the -x-axis of the world coordinate system. Combined with the camera and scene objects in Job 7 The location situation (camera location Vector3f eye_pos(278, 273, -800), scene object vertex information see .obj file), the camera looks forward to the z-axis of the world, emm, is also reasonable.

It can be seen that in assignments 6 and 7, the transformation matrix of the camera ray from the camera space to the world space is omitted, and the ray vector after the action is directly given, but this is also because the placement of the camera in these two assignments is basically consistent with the world The axes of the coordinate system are aligned.

I hope this record can help you~ (If there are any mistakes, you are welcome to criticize and correct)

References

[1] Generate camera rays: grid space-NDC-screen space-world

Guess you like

Origin blog.csdn.net/weixin_42430021/article/details/127651671