[彼得·雪莉(Peter Shirley)在一个周末工作时的光线追踪,当我尝试用红色渲染球体时,我遇到了一个问题。我的数学似乎是准确的,但是它是并排输出看起来像2个球体并且也倾斜/伸展的东西,而不是场景中的1个球体。我有点怀疑,这与光线穿过的视图的宽度有关,但不确定如何确认。这是代码:
main.cpp
#include "vectors.h"
#include "Geometry3D.h"
#include <fstream>
vec3 Color(const Ray& r, const Sphere& s) {
if (Raycast(s, r) != -1)
return vec3(1, 0, 0);
else
return vec3(0, 0, 0);
}
int main() {
const int WIDTH = 200;
const int HEIGHT = 100;
std::ofstream out;
out.open("image.ppm");
if (out.is_open()) {
out << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n";
vec3 lowerLeftCorner(-2.0f, -1.0f, -1.0f);
vec3 horizontal(4.0f, 0.0f, 0.0f);
vec3 vertical(0.0, 2.0f, 0.0);
vec3 origin(0.0, 0.0, 0.0);
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
float u = float(i) / float(WIDTH);
float v = float(j) / float(HEIGHT);
Sphere s(vec3(0, 0, -5.0f), 1.0f);
Ray r(origin, lowerLeftCorner + (horizontal * u) + (vertical * v));
vec3 color = Color(r, s);
int ir = int(255.99f * color.x);
int ig = int(255.99f * color.y);
int ib = int(255.99f * color.z);
out << ir << ' ' << ig << ' ' << ib << '\n';
}
}
}
}
Geometry3D.h
#ifndef _GEOMETRY_3D_H
#define _GEOMETRY_3D_H
#include "vectors.h"
#include <cmath>
#include <cfloat>
typedef vec3 Point;
typedef struct Ray {
Point origin;
vec3 direction;
Ray() : direction(0.0f, 0.0f, 1.0f) { }
Ray(const Point& o, const vec3& d) : origin(o), direction(d) {
NormalizeDirection();
}
inline void NormalizeDirection() {
Normalize(direction);
}
} Ray;
typedef struct Sphere {
Point position;
float radius;
Sphere() : radius(1.0f) { }
Sphere(const Point& p, float r) : position(p), radius(r) { }
} Sphere;
float Raycast(const Sphere& sphere, const Ray& ray);
#endif
Geometry3D.cpp
#include "Geometry3D.h"
#include <iostream>
float Raycast(const Sphere& sphere, const Ray& ray) {
vec3 e = sphere.position - ray.origin;
float rSq = sphere.radius * sphere.radius;
float eSq = MagnitudeSq(e);
float a = Dot(e, ray.direction);
float bSq = eSq - (a * a);
float f = sqrt(rSq - bSq);
if (rSq - (eSq - (a * a)) < 0.0f)
return -1;
else if (eSq < rSq) {
return a + f;
}
return a - f;
}
这里是输出:
感谢您的任何帮助。
错误在于写入.ppm图像。
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
float u = float(i) / float(WIDTH);
float v = float(j) / float(HEIGHT);
...
out << ir << ' ' << ig << ' ' << ib << '\n';
}
}
您正在使用普通的PPM图像(P3)。根据PPM Specification,栅格(颜色数据)应如下所示:
高度行的栅格,从上到下的顺序。每行由宽度像素组成,按从左到右的顺序排列。每个像素依次是红色,绿色和蓝色样本的三元组。
但是,您正在通过循环构建具有HEIGHT像素的WIDTH行的栅格。外循环创建一行,内循环创建该行中的像素。更改索引将解决此问题。
//switch the loop indices
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
float u = float(i) / float(HEIGHT);
float v = float(j) / float(WIDTH);
...
//added spaces between each color value
//this way each pixel is separated with 2 spaces
out << ' '<< ir << ' ' << ig << ' ' << ib << ' ';
}
//and each row ends on a newline
//this makes it easier to read them with a text editor
out << '\n';
}
您正在体验以下内容:宽度是高度的两倍,
const int WIDTH = 200;
const int HEIGHT = 100;
然后您将200行100像素的行写入ppm图像。但是,图像查看器会读取100行200像素,这会导致“镜像”效果。考虑下图:
B是黑色像素,R是红色像素
我试图用绘画图像进行说明,但实际上图像数据只不过是一个矩阵;)
宽度:8
高度:4
B B B B B B B B
B B B B R R R R
R R R R B B B B
B B B B B B B B
此图像的顶部有一条黑色像素线,然后是一条半黑色,半红色像素的线。下一行具有相同的模式,正好相反:一半红色,一半黑色。最后一行是纯黑色。
您的代码基本上会转置图像。它用高度切换宽度,并写成:
B B R B
B B R B
B B R B
B B R B
B R B B
B R B B
B R B B
B R B B
因此图像阅读器将读取以下内容:
B B R B B B R B
B B R B B B R B
B R B B B R B B
B R B B B R B B
如果浏览每一行,您会看到图像在第四行之后被镜像。这就是您在图像中看到的图像,但是由于使用了圆圈,因此由于对称性,图像看起来像写了两次。您不会看到图像上下颠倒。