c++ - 计算光线追踪器光线 - vector 会聚到相同的输出

标签 c++ raytracing

我一直在为乐趣而编写光线追踪器,并且已经投入了相当多的精力。我看过教程、讲座和研究/其他代码,以透视计算投影到图像的 vector 射线。不幸的是,根据我正在创建的图像的大小,在第 4-5 次迭代之后,它使用相同的 vector 射线。尽管这应该有所不同,具体取决于在图像中查看的像素。

现在我正在做一个转换,它基本上根据创建的图像的尺寸向左或向右移动像素的 x/y 光线。具体我看了这两个Raytracer - Computing Eye Rays && calculation for ray generation in ray tracer答案,尝试实现它们,调整我的代码,但没有任何进展。

作为旁注,纵向和横向实现也不起作用。它被硬编码为 width = 10 和 height = 10,因为这是我一直在玩的维度。它们是可以改变的,而且将来肯定会改变。

使用 VS2013 在 C++ 中编码。

int WIDTH = 10;
int HEIGHT = 10;

int main(int argc, char **argv) {
    std::cout << "creating rays..." << std::endl;

    Vector Y(0, 1, 0);
    Vector camPos(3, 1.5, -4);
    Vector looking_at(0, 0, 0); // may change - but want to look at center for this scene
    Vector difference(camPos - looking_at);

    Vector camDir = difference.negative().normalize();
    Vector camRight = (Y.cross(camDir)).normalize();
    Vector camDown = camRight.cross(camDir);

    double aspectRatio = (double) WIDTH / (double) HEIGHT;
    double xAMT, yAMT;  //slightly left of right from direction of camera

    for (int x = 0; x < HEIGHT; x++) {
        for (int y = 0; y < WIDTH; y++) {
            if (WIDTH > HEIGHT) {
                // landscape
                xAMT = ((x + 0.5) / WIDTH) * aspectRatio - (((WIDTH - HEIGHT) / (double) HEIGHT) /2);
                yAMT = ((HEIGHT - y) + 0.5) / HEIGHT;
            }
            else if (HEIGHT > WIDTH) {
                // portrait
                xAMT = (y + 0.5) / WIDTH;
                yAMT = (((HEIGHT - y) + 0.5) / HEIGHT) / aspectRatio - (((HEIGHT - WIDTH) / (double) WIDTH) / 2);
            }
            else {
                // square
                xAMT = (x + 0.5) / WIDTH;
                yAMT = ((HEIGHT - y) + 0.5) / HEIGHT;
            }

            // YES - this indeed does work
            Vector camRayOrigin     = camPos;
            Vector camRightDir      = camRight * (yAMT - 0.5);
            Vector camDownDir       = camDown  * (xAMT - 0.5);
            Vector camRayDirection  = (camDir + (camRightDir + camDownDir)).normalize();

            Ray camRay(camRayOrigin, camRayDirection);
            camRayDirection.print_vector();
        }
    }           
}

上面代码生成的文本是:

creating rays...             
-0.173037 0.117114 0.977928  
-0.325543 -0.458438 0.826956 
-0.517036 -0.198503 0.832629 
-0.54971 -0.326274 0.769002  
-0.575177 -0.269626 0.772316 
-0.573114 -0.295291 0.764423 
-0.575342 -0.283767 0.76711  
-0.574404 -0.288958 0.765874 
-0.574826 -0.286623 0.766435 
-0.574637 -0.287674 0.766183 
-0.574716 -0.287234 0.766288 
-0.574689 -0.287388 0.766251 
-0.574698 -0.287334 0.766264 
-0.574695 -0.287353 0.76626  
-0.574696 -0.287346 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 
-0.574696 -0.287348 0.766261 

vector 类:

#include <cmath>

class Vector {
    double x, y, z;
    int size = 3;

public:
    ~Vector() {};
    Vector() : x(0), y(0), z(0) {}
    Vector(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}

    Vector& operator=(Vector rhs);

    // Vector mathematical operations                                                                                                                                                                                               
    Vector operator+(const Vector& rhs);
    Vector& operator+=(const Vector& rhs);
    Vector operator-(const Vector& rhs);
    Vector& operator-=(const Vector& rhs);

    // Vector scalar operations
    Vector operator+(const double& rhs);
    Vector operator-(const double& rhs);
    Vector operator*(const double& rhs);
    Vector operator/(const double& rhs);

    Vector cross(const Vector& rhs); // Cross-Product
    double dot(const Vector& rhs); // Dot-Product
    Vector normalize(); // Normalize
    Vector negative(); // Negative 
    double mag(); // Magnitude

    void swap(Vector& rhs);
    void print_vector();
};

Vector& Vector::operator=(Vector rhs) {
    swap(rhs);
    return *this;
}

Vector Vector::operator+(const Vector& rhs) {
    Vector result(*this);
    result += rhs;
    return result;
}

Vector& Vector::operator+=(const Vector& rhs) {
    this->x += rhs.x;
    this->y += rhs.y;
    this->z += rhs.z;

    return *this;
}

Vector Vector::operator-(const Vector& rhs) {
    Vector result(*this);
    result -= rhs;
    return result;
}

Vector& Vector::operator-=(const Vector& rhs) {
    this->x -= rhs.x;
    this->y -= rhs.y;
    this->z -= rhs.z;

    return *this;
}

Vector Vector::operator+(const double& rhs) {
    this->x += rhs;
    this->y += rhs;
    this->z += rhs;

    return *this;
}

Vector Vector::operator-(const double& rhs) {
    this->x -= rhs;
    this->y -= rhs;
    this->z -= rhs;

    return *this;
}

Vector Vector::operator*(const double& rhs) {
    this->x *= rhs;
    this->y *= rhs;
    this->z *= rhs;

    return *this;
}

Vector Vector::operator/(const double& rhs) {
    this->x /= rhs;
    this->y /= rhs;
    this->z /= rhs;

    return *this;
}

Vector Vector::cross(const Vector& rhs) {
    double a = (y * rhs.z) - (z * rhs.y);
    double b = (z * rhs.x) - (x * rhs.z);
    double c = (x * rhs.y) - (y * rhs.x);
    Vector product(a, b, c);
    return product;
}

double Vector::dot(const Vector& rhs) {
    double scalar = (x * rhs.x) + (y * rhs.y) + (x * rhs.z);
    return scalar;
}

double Vector::mag() {
    return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}

Vector Vector::normalize() {
    double mag = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));

    if (mag != 0) {
        this->x /= mag;
        this->y /= mag;
        this->z /= mag;
    }
    return *this;
}

Vector Vector::negative() {
    this->x *= -1;
    this->y *= -1;
    this->z *= -1;
    return *this;
}

void Vector::swap(Vector& rhs) {
    using std::swap;

    swap(this->x, rhs.x);
    swap(this->y, rhs.y);
    swap(this->z, rhs.z);
}

void Vector::print_vector() {
    std::cout 
        << x 
        << " " 
        << y 
        << " "
        << z 
        << std::endl;
}

最佳答案

问题在 Vector 类中。

你实现了+, -, *, / (double) 与您实现 +=-= (const Vector&) 的方式相同:您更改 this 的值.

在实现二元运算符时(第一个操作数是this,第二个操作数是rhs),你通常不想改变操作数的值。在这些情况下,我强烈建议您使用 const 让运算符(operator)在出现此类错误时收到警告。

Vector operator+(const double& rhs) const;

代替:

Vector operator+(const double& rhs);

然后,实现是:

Vector Vector::operator+(const double& rhs) const {
    Vector result(*this);
    result.x += rhs;
    result.y += rhs;
    result.z += rhs;

    return result;
}

关于c++ - 计算光线追踪器光线 - vector 会聚到相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475570/

相关文章:

c++ - 在 C/C++ 中,是否保证 volatile 变量在线程之间具有最终一致的语义?

c++ - 在多核 MacOSX 上,以下 c++ 代码是线程安全的吗?

c++ - 标准库中聚合可初始化性的类型特征?

c++ - 为什么我会用完堆内存?

c++ - 有效地计算体素数据的梯度

algorithm - 计算在基于瓦片的游戏中哪些瓦片被点亮 ("raytracing")

c++ - 在 C++ 中禁用隐式转换

c++ - 如何从保存 RGBA 格式图像的缓冲区在 opencv 中创建图像

video - 对于简单的视频格式,我有哪些选择?

c# - Unity3d 霰弹枪锥形射击