mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-07 23:12:00 +00:00
bugfix:
This commit is contained in:
parent
f7e2ad664b
commit
271f213969
3 changed files with 26 additions and 35 deletions
|
@ -39,8 +39,8 @@ Scene *Parser::getScene(const nlohmann::json &j) {
|
||||||
sc->setAntialiasing(j.value("antialiasing", false));
|
sc->setAntialiasing(j.value("antialiasing", false));
|
||||||
sc->setTwoSideRender(j.value("twosiderender", false));
|
sc->setTwoSideRender(j.value("twosiderender", false));
|
||||||
sc->setGlobalIllum(j.value("globalillum", false));
|
sc->setGlobalIllum(j.value("globalillum", false));
|
||||||
sc->setMaxBounce(j.value("maxbounce", 3));
|
sc->setMaxBounce(j.value("maxbounces", 3));
|
||||||
sc->setProbTerminate(j.value("probTerminate", 0.33f));
|
sc->setProbTerminate(j.value("probterminate", 0.33f));
|
||||||
if (j.contains("raysperpixel"))
|
if (j.contains("raysperpixel"))
|
||||||
sc->setRaysPerPixel(getRpp(j["raysperpixel"]));
|
sc->setRaysPerPixel(getRpp(j["raysperpixel"]));
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ using Eigen::VectorXi;
|
||||||
using std::priority_queue;
|
using std::priority_queue;
|
||||||
|
|
||||||
// help function declarations
|
// help function declarations
|
||||||
Ray getRay(int, int);
|
|
||||||
Ray getRay(int, int, int, int);
|
Ray getRay(int, int, int, int);
|
||||||
void writeColor(int, const Vector3f &);
|
void writeColor(int, const Vector3f &);
|
||||||
utils::Optional<Vector3f> trace(Ray r);
|
utils::Optional<Vector3f> trace(Ray r);
|
||||||
|
@ -87,11 +86,6 @@ 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);
|
||||||
|
|
||||||
|
@ -102,10 +96,9 @@ void RayTracer::render() {
|
||||||
Vector3f accumulate = Vector3f::Zero();
|
Vector3f accumulate = Vector3f::Zero();
|
||||||
for (int j = 0; j < gridHeight; ++j)
|
for (int j = 0; j < gridHeight; ++j)
|
||||||
for (int i = 0; i < gridWidth; ++i) {
|
for (int i = 0; i < gridWidth; ++i) {
|
||||||
Ray ray = getRay(x, y, i, j);
|
|
||||||
for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) {
|
for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) {
|
||||||
Optional<Vector3f> result =
|
Ray ray = getRay(x, y, i, j);
|
||||||
globalIllum ? trace(ray) : trace(ray, x, y);
|
Optional<Vector3f> result = trace(ray);
|
||||||
if (result.hasValue()) {
|
if (result.hasValue()) {
|
||||||
accumulate += result.value();
|
accumulate += result.value();
|
||||||
success++;
|
success++;
|
||||||
|
@ -169,14 +162,22 @@ Light *RayTracer::singleLightSource() const {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ray getRay(int x, int y) {
|
// This should generate a higher quality random number
|
||||||
using namespace camera;
|
float getRandomNumber() {
|
||||||
return Ray(pos, pxUpperLeft + x * du + y * dv - pos);
|
static std::uniform_real_distribution<float> distribution(0.0, 1.0);
|
||||||
|
static std::mt19937 generator;
|
||||||
|
return distribution(generator);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ray getRay(int x, int y, int i, int j) {
|
Ray getRay(int x, int y, int i, int j) {
|
||||||
using namespace camera;
|
using namespace camera;
|
||||||
return Ray(pos, vpUpperLeft + x * du + i * gdu + y * dv + j * gdv - pos);
|
Vector3f offset = Vector3f::Zero();
|
||||||
|
|
||||||
|
if (globalIllum || antiAliasing)
|
||||||
|
offset = getRandomNumber() * gdu + getRandomNumber() * gdv;
|
||||||
|
|
||||||
|
return Ray(pos,
|
||||||
|
vpUpperLeft + x * du + i * gdu + y * dv + j * gdv + offset - pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3f clamp(const Vector3f &color) {
|
Vector3f clamp(const Vector3f &color) {
|
||||||
|
@ -189,13 +190,6 @@ void writeColor(int i, const Vector3f &color) {
|
||||||
Output::current->b(i, color.z());
|
Output::current->b(i, color.z());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should generate a higher quality random number
|
|
||||||
float getRandomNumber() {
|
|
||||||
static std::uniform_real_distribution<float> distribution(0.0, 1.0);
|
|
||||||
static std::mt19937 generator;
|
|
||||||
return distribution(generator);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a randon point on a unit hemisphere
|
// Generate a randon point on a unit hemisphere
|
||||||
Vector3f getRandomDirection() {
|
Vector3f getRandomDirection() {
|
||||||
RETRY_RANDOM:
|
RETRY_RANDOM:
|
||||||
|
@ -264,22 +258,20 @@ RETRY_TRACING:
|
||||||
std::max(0.0f, hit.normal().dot(direction));
|
std::max(0.0f, hit.normal().dot(direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Vector3f> RayTracer::trace(Ray r, int x, int y) const {
|
|
||||||
Optional<HitRecord> hitRecord = getHitRecord(r);
|
|
||||||
if (hitRecord.hasValue())
|
|
||||||
return Optional<Vector3f>(calculateColor(hitRecord.value()));
|
|
||||||
|
|
||||||
return Optional<Vector3f>::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<Vector3f> RayTracer::trace(Ray r) const {
|
Optional<Vector3f> RayTracer::trace(Ray r) const {
|
||||||
Optional<HitRecord> hitRecord = getHitRecord(r);
|
Optional<HitRecord> hitRecord = getHitRecord(r);
|
||||||
if (hitRecord.hasValue()) {
|
if (!camera::globalIllum) {
|
||||||
Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(),
|
if (hitRecord.hasValue())
|
||||||
Scene::current->probTerminate());
|
return Optional<Vector3f>(calculateColor(hitRecord.value()));
|
||||||
|
else if (camera::antiAliasing)
|
||||||
|
return Optional<Vector3f>(Scene::current->backgroundColor());
|
||||||
|
} else {
|
||||||
|
if (hitRecord.hasValue()) {
|
||||||
|
Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(),
|
||||||
|
Scene::current->probTerminate());
|
||||||
|
|
||||||
if (color != Vector3f::Zero())
|
|
||||||
return Optional<Vector3f>(color);
|
return Optional<Vector3f>(color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional<Vector3f>::nullopt;
|
return Optional<Vector3f>::nullopt;
|
||||||
|
|
|
@ -30,7 +30,6 @@ private:
|
||||||
Optional<HitRecord> getHitRecord(Ray) const;
|
Optional<HitRecord> getHitRecord(Ray) const;
|
||||||
Vector3f calculateColor(const HitRecord &) const;
|
Vector3f calculateColor(const HitRecord &) const;
|
||||||
Light *singleLightSource() const;
|
Light *singleLightSource() const;
|
||||||
Optional<Vector3f> trace(Ray, int, int) const;
|
|
||||||
Optional<Vector3f> trace(Ray) const;
|
Optional<Vector3f> trace(Ray) const;
|
||||||
Vector3f trace(HitRecord, int, float) const;
|
Vector3f trace(HitRecord, int, float) const;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue