Question about nearZ, farZ, and camera position relationship in perspective projection
Posted: October 27th, 2021, 3:40 pm
I have 600x600 plane square. My goal is render it on screen in 1:1 ratio.
Here is my perspective projection and camera code
When I set nearZ = 1.0f, the camera position.z (range) need to set 800.0f (ScreenWidth) to fill the height (see nearZ_1.png)
However, when I set nearZ = 0.5f, the range need to set 400.0f (half of ScreenWidth) to fill the height. (see nearZ_05.png)
Why? I can't figure out the relationship between nearZ and the screen width
Also the square disappears when I set range = 1000.0f. Why? 1000.0f is not greater than the farZ.
Here is my perspective projection and camera code
Code: Select all
//Perspective
gfx.SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 1.0f / Graphics::AspectRatio, 1.0f, 1000.0f));
//camera
//every parameter is 0.0f except range
const auto position =
DX::XMVector3Transform(
DX::XMVectorSet(0.0f, 0.0f, -range, 0.0f),
DX::XMMatrixRotationRollPitchYaw(aroundX, -aroundY, 0.0f));
return
DX::XMMatrixLookAtLH(position, DX::XMVectorZero(),
DX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f)) *
DX::XMMatrixRotationRollPitchYaw(pitch, -yaw, roll);
However, when I set nearZ = 0.5f, the range need to set 400.0f (half of ScreenWidth) to fill the height. (see nearZ_05.png)
Why? I can't figure out the relationship between nearZ and the screen width
Also the square disappears when I set range = 1000.0f. Why? 1000.0f is not greater than the farZ.