我一直在写射线追踪器,很有趣,并且投入了很多。我已经看了一个教程,讲座和研究/其他代码,以透视图的方式计算出投影到图像上的矢量射线。不幸的是,根据我创建的图像的大小,在第4-5次迭代之后,它使用了相同的矢量射线。虽然这应该有所不同,具体取决于要在图像中查看哪个像素。

现在,我目前正在执行一种转换,该转换基本上根据要创建的图像的尺寸将光线左移或右移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


向量类:

#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来警告操作员,以防出现此类错误。

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++ - 计算raytracer射线- vector 会聚到相同的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25475570/

10-13 08:07