I wish you all a happy new year!
I tried to render a scene in OptiX with the same camera and projection parameters that are used in the OpenGL pass, to basically achieve rendered images from the same point of view. I converted code from the tutorial I try to follow to basically use glm instead of gdt to generate the ray direction used in the optixTrace-call:
const glm::vec2 screen(glm::vec2(ix + .5f, iy + .5f) / glm::vec2(optixLaunchParams.frame.size));
glm::vec3 ray_dir = glm::normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + (screen.y - 0.5f) * camera.vertical);
camera.horizontal and camera.vertical are computed as follows (field of view in radians), aspect is width/height):
launch_params.camera.horizontal = cam_projection.field_of_view * aspect * glm::normalize(shared_camera.get_right());
launch_params.camera.vertical = cam_projection.field_of_view * glm::normalize(shared_camera.get_up());
this produces actually quite similar images, but the further a certain pixel is located towards the border the more they ray traced image deviates from the rasterized image (ignore the colour, the issue is actually about deviating visible areas):
OptiX Image
So I thought, maybe this is due to the non-linear behaviour of perspective projection matrices and I tried to use the same matrices used in the OpenGL part which Sibaku already summarized. So I added projection and view matrices along with their inverted versions to the launch parameters and used them like this to generate the ray directions:
auto dims = optixGetLaunchDimensions();
int width = dims.x;
int height = dims.y;
float x_screen = (ix + .5f) / width;
float y_screen = (iy + .5f) / height;
float x_ndc = x_screen * 2.f - 1.f;
float y_ndc = y_screen * 2.f - 1.f;
glm::vec4 homogenious_ndc = glm::vec4(x_ndc, y_ndc, 1.f, 1.f);
glm::vec4 p_viewspace = optixLaunchParams.matrices.inverse_projection * homogenious_ndc;
glm::vec4 p_worldspace = optixLaunchParams.matrices.inverse_view* p_viewspace;
glm::vec3 ray_dir = glm::normalize(glm::vec3(p_worldspace.x, p_worldspace.y, p_worldspace.z));
This produced the following images, which appear to suffer from the same issues, however, if the value of the far clipping plane is large (third image) OpenGL and OptiX produce images with the same parameters.
OptiX with new Code. Dragon is smaller
It somehow appears like the Vertical Field of View is different with OptiX, however when I set the far clipping plane to a value of ~50.000 the images appear to be the same
OptiX image with 50.000 fcp
I thought I was familiar with how a projection matrix works, so this catches me pretty off-guard as I really don’t know why the far clipping plane should affect the vertical field of view (as it appears) in any way and consequently also the direction of the rays. Could somebody please explain? Or is there a more elegant way to achieve consistent camera and projection parameters between OptiX and OpenGL?