32 lines
1.1 KiB
C++
32 lines
1.1 KiB
C++
#include "light/light.h"
|
|
#include "primitive/triangle.h"
|
|
#include "scene/scene.h"
|
|
#include "shader/shader.h"
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
void Scene::add(const std::shared_ptr<Light> &light) { this->lights_.push_back(light); }
|
|
|
|
void Scene::add(const std::shared_ptr<Primitive> &primitive) {
|
|
assert(primitive->shader() != nullptr);
|
|
this->primitives_.push_back(primitive);
|
|
}
|
|
|
|
Color Scene::traceRay(Ray &ray) const {
|
|
if (this->findIntersection(ray) && ray.remainingBounces-- > 0) {
|
|
// If the ray has hit an object, call the shader ...
|
|
return ray.primitive->shader()->shade(*this, ray);
|
|
} else if (this->environmentMap) {
|
|
// ... otherwise look up the environment map ...
|
|
float const phi = std::acos(ray.direction.y);
|
|
float const rho = std::atan2(ray.direction.z, ray.direction.x) + float(PI);
|
|
return this->environmentMap->color(rho / (2.0f * float(PI)), phi / float(PI));
|
|
} else {
|
|
// ... if all else fails, just return the background color
|
|
return this->backgroundColor;
|
|
}
|
|
}
|