merge duplicate code

This commit is contained in:
Shuo Feng 2024-03-21 17:53:20 -04:00
parent 342c9fdef2
commit f7e2ad664b
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1

View file

@ -8,6 +8,7 @@
#include "Ray.h" #include "Ray.h"
#include <Eigen/Core> #include <Eigen/Core>
#include <Eigen/src/Core/Matrix.h>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
@ -27,6 +28,7 @@ Vector3f clamp(const Vector3f &);
namespace camera { namespace camera {
int width, height, gridWidth, gridHeight, raysPerPixel; int width, height, gridWidth, gridHeight, raysPerPixel;
Vector3f pos, u, v, du, dv, vpUpperLeft, pxUpperLeft, gdu, gdv; Vector3f pos, u, v, du, dv, vpUpperLeft, pxUpperLeft, gdu, gdv;
bool globalIllum, antiAliasing;
void init(); void init();
} // namespace camera } // namespace camera
@ -85,14 +87,17 @@ void RayTracer::render() {
using namespace camera; using namespace camera;
Output::current = new Output(Scene::current->name(), width, height); Output::current = new Output(Scene::current->name(), width, height);
std::cout << "Global Illumination: " << (globalIllum ? "T" : "F") << std::endl
<< "Anti-aliasing: " << (antiAliasing ? "T" : "F") << std::endl;
std::cout << gridHeight << " " << gridWidth << " " << raysPerPixel
<< std::endl;
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
utils::Progress::of((y + 1.0f) / height); utils::Progress::of((y + 1.0f) / height);
for (int x = 0; x < width; ++x) { for (int x = 0; x < width; ++x) {
Vector3f color = Scene::current->backgroundColor(); Vector3f color = Scene::current->backgroundColor();
bool globalIllum = Scene::current->globalIllum();
if (globalIllum || Scene::current->antiAliasing()) {
int success = 0; int success = 0;
Vector3f accumulate = Vector3f::Zero(); Vector3f accumulate = Vector3f::Zero();
for (int j = 0; j < gridHeight; ++j) for (int j = 0; j < gridHeight; ++j)
@ -114,13 +119,6 @@ void RayTracer::render() {
else else
color = accumulate / success; color = accumulate / success;
} }
} else {
Ray ray = getRay(x, y);
Optional<Vector3f> result = trace(ray, x, y);
if (result.hasValue())
color = result.value();
}
writeColor(y * width + x, clamp(color)); writeColor(y * width + x, clamp(color));
} }
@ -320,7 +318,12 @@ void init() {
vpUpperLeft = pos + lookAt - u / 2.0 - v / 2.0; vpUpperLeft = pos + lookAt - u / 2.0 - v / 2.0;
pxUpperLeft = vpUpperLeft + (du + dv) / 2.0; pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
VectorXi data = Scene::current->raysPerPixel(); globalIllum = Scene::current->globalIllum();
antiAliasing = Scene::current->antiAliasing();
VectorXi data = !(globalIllum || antiAliasing)
? VectorXi(1)
: Scene::current->raysPerPixel();
gridWidth = getGridWidth(data); gridWidth = getGridWidth(data);
gridHeight = getGridHeight(data); gridHeight = getGridHeight(data);
raysPerPixel = getRayNumber(data); raysPerPixel = getRayNumber(data);