如何让 OpenGL 相机和 Ray-tracer 相机显示相同的图像?

How to make OpenGL camera and Ray-tracer camera show the same image?

我正在编写一个简单的路径跟踪器,我想用 OpenGL 进行预览。 但由于 OpenGL 管道使用投影矩阵,因此使用 OpenGL 和路径跟踪渲染的图像有点不同。

对于 OpenGL 渲染,我使用基本管道,其中最终转换矩阵为:

m_transformation = PersProjTrans * CameraRotateTrans * CameraTranslationTrans * TranslationTrans * RotateTrans * ScaleTrans

透视投影参数为:45.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 0.01, 10000.0f.

在我的路径追踪器中,我像这样从相机生成光线:

m_width_recp = 1.0 /0m_width;
m_height_recp = 1.0 / m_height;
m_ratio = (double)m_width / m_height;

m_x_spacing = (2.0 * m_ratio) / (double)m_width;
m_y_spacing = (double)2.0 / (double)m_height;
m_x_spacing_half = m_x_spacing * 0.5;
m_y_spacing_half = m_y_spacing * 0.5;

x_jitter = (Xi1 * m_x_spacing) - m_x_spacing_half;
y_jitter = (Xi2 * m_y_spacing) - m_y_spacing_half;

Vec pixel = m_position + m_direction * 2.0;
pixel = pixel - m_x_direction * m_ratio + m_x_direction * (x * 2.0 * m_ratio * m_width_recp) + x_jitter;
pixel = pixel + m_y_direction - m_y_direction*(y * 2.0 * m_height_recp + y_jitter);

return Ray(m_position, (pixel-m_position).norm());

这是由 OpenGL (1) 和 Path Tracer(2) 渲染的图像。

问题是场景和相机之间的距离。我的意思是,它不是一成不变的。在某些情况下,路径追踪器图像看起来 "closer" 对象,在某些情况下反之亦然。

我在 google 中没有找到任何内容。

反转用于 OpenGL 渲染的视图投影矩阵,并使用它将屏幕像素位置转换为光线追踪器的光线。