我一直在写一个光线追踪器以获得乐趣并且已经获得了相当多的数量。我已经观看了教程,讲座和研究/其他代码,以便在投影到图像时计算矢量光线。不幸的是,在第4-5次迭代之后,取决于我正在创建的图像的大小,它使用相同的矢量光线。虽然这应该是不同的,这取决于在图像中查看哪个像素。
现在我正在进行转换,基本上会根据创建的图像尺寸左右移动像素左右x/y
。具体来说,我已经看过这两个Raytracer - Computing Eye Rays&& calculation for ray generation in ray tracer答案,尝试过实施它们,调整我的代码,并且没有任何地方。
作为旁注,纵向和横向实现也不起作用。它是宽度= 10和高度= 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 class:
#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;
}
答案 0 :(得分:2)
问题出在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;
}