【问题标题】:Path Tracing - Generate Camera Rays with a Left Handed coordinate system路径追踪 - 使用左手坐标系生成相机光线
【发布时间】:2016-09-07 16:05:25
【问题描述】:

在为我的渲染器实现摄像头时遇到了一些问题。正如问题所述,我想知道生成这样一个相机的必要步骤。包括视野和纵横比。重要的是坐标系是左撇子,这样 -z 将相机推离屏幕(如我明白了)。我尝试在网上查找,但大多数实现都不完整或失败了。感谢您的帮助。谢谢。

【问题讨论】:

    标签: rendering raytracing


    【解决方案1】:

    我遇到了麻烦,花了很长时间才弄清楚。这是相机类的代码。

    #ifndef CAMERA_H_
    #define CAMERA_H_
    
    #include "common.h"
    
    struct Camera {
        Vec3fa position, direction;
        float fovDist, aspectRatio;
        double imgWidth, imgHeight;
        Mat4 camMatrix;
    
        Camera(Vec3fa pos, Vec3fa cRot, Vec3fa cDir, float cfov, int width, int height) {
            position = pos;
            aspectRatio = width / (float)height;
            imgWidth = width;
            imgHeight = height;
    
            Vec3fa angle = Vec3fa(cRot.x, cRot.y, -cRot.z);
            camMatrix.setRotationRadians(angle * M_PI / 180.0f);
    
            direction = Vec3fa(0.0f, 0.0f, -1.0f);
            camMatrix.rotateVect(direction);
    
            fovDist = 2.0f * tan(M_PI * 0.5f * cfov / 180.0);
        }
    
        Vec3fa getRayDirection(float x, float y) {
            Vec3fa delta = Vec3fa((x-0.5f) * fovDist * aspectRatio, (y-0.5f) * fovDist, 0.0f);
            camMatrix.rotateVect(delta);
            return (direction + delta);
        }
    };
    
    #endif
    

    如果您需要 Mat4 类中的 rotateVect() 代码

    void Mat4::rotateVect(Vector3& vect) const
    {
        Vector3 tmp = vect;
        vect.x = tmp.x * (*this)[0] + tmp.y * (*this)[4] + tmp.z * (*this)[8];
        vect.y = tmp.x * (*this)[1] + tmp.y * (*this)[5] + tmp.z * (*this)[9];
        vect.z = tmp.x * (*this)[2] + tmp.y * (*this)[6] + tmp.z * (*this)[10];
    }
    

    这是我们的 setRotationRadians 代码

    void Mat4::setRotationRadians(Vector3 rotation)
    {
        const float cr = cos(rotation.x);
        const float sr = sin(rotation.x);
        const float cp = cos(rotation.y);
        const float sp = sin(rotation.y);
        const float cy = cos(rotation.z);
        const float sy = sin(rotation.z);
    
        (*this)[0] = (cp * cy);
        (*this)[1] = (cp * sy);
        (*this)[2] = (-sp);
    
        const float srsp = sr * sp;
        const float crsp = cr * sp;
    
        (*this)[4] = (srsp * cy - cr * sy);
        (*this)[5] = (srsp * sy + cr * cy);
        (*this)[6] = (sr * cp);
    
        (*this)[8] = (crsp * cy + sr * sy);
        (*this)[9] = (crsp * sy - sr * cy);
        (*this)[10] = (cr * cp);
    }
    

    【讨论】:

    • 感谢您的回答!已经实现了一个,但它不是最好的,并且有一些问题。所以,渴望尝试别的东西。无论如何,很少有问题。凸轮矩阵初始化为什么?是单位矩阵还是其他一些 4x4 值。其次,我可以看看设置的旋转弧度代码吗?我想比较一些东西。最后只是好奇心比什么都重要-为什么要使用矩阵而不是通常的叉积方法?就是这样。再次......非常感谢:)
    • CamMatrix 被初始化为身份矩阵。不用矩阵也可以实现同样的效果,但我最近成为了矩阵的粉丝,所以我使用了它。
    • 哦,好吧。我想我们都是不同的。因为我真的很讨厌矩阵。大声笑。直到绝对必要时才使用它们。无论如何,谢谢你所做的一切。我一试就会告诉你!!该代码非常有意义。
    • 如果答案有用,请在此处更新。另外,我曾经和你一样讨厌矩阵,后来了解了它们,现在开始喜欢它们
    • 是的,我发现它非常有用!谢谢。仍然使用旧的右手。但我能够帮助一位朋友也正在使用此处提供的信息实施一个。所以......再次感谢。 :)
    猜你喜欢
    • 2012-06-19
    • 2021-07-24
    • 1970-01-01
    • 2011-08-29
    • 2012-11-13
    • 1970-01-01
    • 2022-10-02
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多