This commit is contained in:
Shuo Feng 2024-03-19 18:56:58 -04:00
parent f5936b2ada
commit 05914ac768
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
3 changed files with 38 additions and 32 deletions

View file

@ -11,11 +11,9 @@ using std::vector;
class Output {
public:
Output(const Vector3f &bgc, string path, int w, int h)
: red(vector<float>(w * h + 1, bgc.x())),
green(vector<float>(w * h + 1, bgc.y())),
blue(vector<float>(w * h + 1, bgc.z())), path(path), width(w),
height(h) {}
Output(string path, int w, int h)
: red(vector<float>(w * h + 1)), green(vector<float>(w * h + 1)),
blue(vector<float>(w * h + 1)), path(path), width(w), height(h) {}
void write();

View file

@ -28,16 +28,19 @@ Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &pxUpperLeft,
return Ray(camPos, pxUpperLeft + x * du + y * dv - camPos);
}
void RayTracer::calculateColor(const HitRecord &hit, int i) {
void writeColor(int i, const Vector3f &color) {
Output::current->r(i, color.x());
Output::current->g(i, color.y());
Output::current->b(i, color.z());
}
Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const {
Vector3f result(0, 0, 0);
for (auto light : lights)
result += light->isUse() ? light->illumination(hit, geometries)
: Vector3f::Zero();
result = result.cwiseMax(0.0f).cwiseMin(1.0f);
Output::current->r(i, result.x());
Output::current->g(i, result.y());
Output::current->b(i, result.z());
return result.cwiseMax(0.0f).cwiseMin(1.0f);
}
int getGridWidth(Eigen::VectorXi data) {
@ -52,6 +55,8 @@ int getRayNumber(Eigen::VectorXi data) {
return data.size() == 2 ? data.y() : (data.size() == 3 ? data.z() : 1);
}
Vector3f trace() { return Vector3f::Zero(); }
void RayTracer::render() {
int width = Scene::current->width();
int height = Scene::current->height();
@ -68,8 +73,7 @@ void RayTracer::render() {
Vector3f vpUpperLeft = cameraPos + lookAt - u / 2.0 - v / 2.0;
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
Output::current = new Output(Scene::current->backgroundColor(),
Scene::current->name(), width, height);
Output::current = new Output(Scene::current->name(), width, height);
Eigen::VectorXi data = Scene::current->raysPerPixel();
int gridWidth = getGridWidth(data);
@ -84,31 +88,35 @@ void RayTracer::render() {
}
for (int y = 0; y < height; ++y) {
// print progress bar
utils::Progress::of((y + 1.0f) / height);
for (int x = 0; x < width; ++x)
for (int j = 0; j < gridHeight; ++j)
for (int i = 0; i < gridWidth; ++i) {
if (Scene::current->globalIllum()) {
// TODO: Path tracing for global illumination
} else {
Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv);
priority_queue<HitRecord> records;
for (auto g : geometries) {
Optional<float> t = g->intersect(ray);
if (t.hasValue())
records.push(HitRecord(t.value(), ray, g));
}
for (int x = 0; x < width; ++x) {
Vector3f color = Scene::current->backgroundColor();
if (!records.empty()) {
HitRecord hit = records.top();
hit.calcNormal();
calculateColor(hit, y * width + x);
}
if (Scene::current->globalIllum()) {
for (int j = 0; j < gridHeight; ++j)
for (int i = 0; i < gridWidth; ++i) {
color = trace();
}
} else {
Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv);
priority_queue<HitRecord> records;
for (auto g : geometries) {
Optional<float> t = g->intersect(ray);
if (t.hasValue())
records.push(HitRecord(t.value(), ray, g));
}
}
if (!records.empty()) {
HitRecord hit = records.top();
hit.calcNormal();
color = calculateColor(hit, y * width + x);
}
}
writeColor(y * width + x, color);
}
}
std::cout << std::endl;
}

View file

@ -22,7 +22,7 @@ private:
std::vector<Output *> outputs;
void parse();
void calculateColor(const HitRecord &, int);
Vector3f calculateColor(const HitRecord &, int) const;
void render();
};