光线追踪中的正交投影

发布于 2025-01-17 17:53:54 字数 1575 浏览 0 评论 0原文

我在 vulkan 有一个基于透视投影的光线追踪渲染器,我想将其转换为正交投影,我尝试用正交投影矩阵替换透视投影矩阵,并将光线原点作为像素坐标,但似乎没有工作,显然有些问题。

    uint randSeed = initRand(gl_LaunchIDEXT.x + gl_LaunchIDEXT.y * gl_LaunchSizeEXT.x, ubo.frameID, 16);

    //vec4 origin           = ubo.viewInverse * vec4(0.0, 0.0, 0.0, 1.0); //for perpective
    vec4 origin         = ubo.viewInverse * vec4(gl_LaunchIDEXT.x, gl_LaunchIDEXT.y, 0.0,  1.0); //for ortho

    // ---- [Apply jitter to anti alias] ---- 
    vec2 jitter         = vec2(nextRand(randSeed), nextRand(randSeed)) - 0.5;
    //vec4 target           = ubo.projInverse * vec4((gl_LaunchIDEXT.xy + jitter) / gl_LaunchSizeEXT.xy * 2.0 - 1.0, 0.0, 1.0); //for perpective
    vec4 target = ubo.orthoInverse * vec4((gl_LaunchIDEXT.xy + jitter) / gl_LaunchSizeEXT.xy * 2.0 - 1.0, 0.0, 1.0); //for ortho
    vec4 direction = ubo.viewInverse * vec4(normalize(target.xyz), 0.0);

我正在使用 glm::ortho 函数计算正交矩阵, 我也尝试使用下面的代码进行计算,但我没有得到所需的结果 -

// set the OpenGL orthographic projection matrix
void glOrtho(
    in float b, in float t, in float l, in float r,
    in float n, in float f,
    inout mat4 M)
{
    // set OpenGL perspective projection matrix
    M[0][0] = 2 / (r - l);
    M[0][1] = 0;
    M[0][2] = 0;
    M[0][3] = 0;

    M[1][0] = 0;
    M[1][1] = 2 / (t - b);
    M[1][2] = 0;
    M[1][3] = 0;

    M[2][0] = 0;
    M[2][1] = 0;
    M[2][2] = -2 / (f - n);
    M[2][3] = 0;

    M[3][0] = -(r + l) / (r - l);
    M[3][1] = -(t + b) / (t - b);
    M[3][2] = -(f + n) / (f - n);
    M[3][3] = 1;
}

如何解决这个问题?

i have a working perspective projection based ray tracing renderer in vulkan, i want to convert it into an orthographic projection, i tried replacing the perspective projection matrix by orthographic projection matrix and taking the ray origin as the pixel coordinates, but it seems to have not working, clearly something is wrong.

    uint randSeed = initRand(gl_LaunchIDEXT.x + gl_LaunchIDEXT.y * gl_LaunchSizeEXT.x, ubo.frameID, 16);

    //vec4 origin           = ubo.viewInverse * vec4(0.0, 0.0, 0.0, 1.0); //for perpective
    vec4 origin         = ubo.viewInverse * vec4(gl_LaunchIDEXT.x, gl_LaunchIDEXT.y, 0.0,  1.0); //for ortho

    // ---- [Apply jitter to anti alias] ---- 
    vec2 jitter         = vec2(nextRand(randSeed), nextRand(randSeed)) - 0.5;
    //vec4 target           = ubo.projInverse * vec4((gl_LaunchIDEXT.xy + jitter) / gl_LaunchSizeEXT.xy * 2.0 - 1.0, 0.0, 1.0); //for perpective
    vec4 target = ubo.orthoInverse * vec4((gl_LaunchIDEXT.xy + jitter) / gl_LaunchSizeEXT.xy * 2.0 - 1.0, 0.0, 1.0); //for ortho
    vec4 direction = ubo.viewInverse * vec4(normalize(target.xyz), 0.0);

i am calculating the orthographic matrix using glm::ortho function,
i also tried calculating using the below code, but i am not getting the desired result -

// set the OpenGL orthographic projection matrix
void glOrtho(
    in float b, in float t, in float l, in float r,
    in float n, in float f,
    inout mat4 M)
{
    // set OpenGL perspective projection matrix
    M[0][0] = 2 / (r - l);
    M[0][1] = 0;
    M[0][2] = 0;
    M[0][3] = 0;

    M[1][0] = 0;
    M[1][1] = 2 / (t - b);
    M[1][2] = 0;
    M[1][3] = 0;

    M[2][0] = 0;
    M[2][1] = 0;
    M[2][2] = -2 / (f - n);
    M[2][3] = 0;

    M[3][0] = -(r + l) / (r - l);
    M[3][1] = -(t + b) / (t - b);
    M[3][2] = -(f + n) / (f - n);
    M[3][3] = 1;
}

How to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文