Skip to content
Snippets Groups Projects
Commit 61a9c9cd authored by Yoel's avatar Yoel
Browse files

Scene now implemented a form of ImportanceSampling

parent 6a1098be
No related branches found
No related tags found
No related merge requests found
#define _USE_MATH_DEFINES
#include "Scene.h"
Scene::Scene(const shapes::Group& group, const shapes::Background& bg,
#include <math.h>
#include <cassert>
Scene::Scene(const shapes::Group& group,
const std::vector<std::shared_ptr<shapes::Light>> lights,
const cam::CamObs& cam, size_t depth)
: group(group), bg(bg), cam(cam), depth(depth) {
: group(group), lights(lights), cam(cam), depth(depth) {
}
util::Vec3 Scene::color(float x, float y) const {
cam::Ray r = cam.create(x, y);
return calculateRadiance(r, depth);
bool l = false;
return calculateRadiance(r, depth, l);
}
util::Vec3 Scene::calculateRadiance(const cam::Ray& r, size_t depth) const {
util::Vec3 Scene::calculateRadiance(const cam::Ray& r, size_t depth,
bool& write) const {
if (depth == 0) {
return util::Vec3(0, 0, 0);
}
std::optional<cam::Hit> h = group.intersect(r);
// To investigate possible bugs
/*if (depth < 19) {
write = true;
std::cout << depth << " " << r << std::endl;
std::cout << *h << std::endl;
// assert(false);
}*/
util::Vec3 result;
if (!h) {
h = bg.intersect(r);
return h->emission();
}
if (h->scatter()) {
result =
h->albedo() * calculateRadiance(h->scattered_ray(r), depth - 1);
cam::Ray scatter_ray = h->scattered_ray(r);
if (h->scatter(scatter_ray.d, h->normal())) {
result = h->emission() +
h->albedo() * calculateRadiance(scatter_ray, depth - 1, write);
// To investigate possible bugs
/*
if (write) {
std::cout << depth << " " << r << " " << r.tmin << std::endl;
std::cout << *h << std::endl;
if (depth == 20) assert(false);
}*/
#if true
for (auto light : lights) {
int nn = 10;
util::Vec3 lightPart;
for (int i = 0; i < nn; i++) {
auto samplePoint = light->sampleLight();
auto shadowRay = cam::Ray(samplePoint.point(),
h->point() - samplePoint.point(),
cam::epsilon, 1, false);
auto lightEmission =
light->calculateLightEmission(samplePoint, shadowRay.d);
auto lightMultiplier = h->calculateLightMultiplier(
shadowRay.d.normalize(), -r.d, h->normal());
util::Vec3 scatterFunction =
lightMultiplier * lightEmission *
std::max<float>(
util::dot(-shadowRay.d.normalize(), h->normal()), 0);
lightPart = lightPart + (scatterFunction);
}
result = result + (h->albedo() * (lightPart / nn));
}
#endif
} else {
result = util::Vec3(0, 0, 0);
result = h->emission();
}
return result;
}
#pragma once
#include <vector>
#include "../camera/CamObs.h"
#include "../shape/Background.h"
#include "../shape/Group.h"
#include "../shape/Light.h"
#include "Sampler.h"
class Scene : public util::Sampler {
public:
Scene(const shapes::Group& group, const shapes::Background& bg,
Scene(const shapes::Group& group,
std::vector<std::shared_ptr<shapes::Light>> lights,
const cam::CamObs& cam, size_t depth);
util::Vec3 color(float x, float y) const override;
util::Vec3 calculateRadiance(const cam::Ray& r, size_t depth) const;
util::Vec3 calculateRadiance(const cam::Ray& r, size_t depth,
bool& write) const;
private:
shapes::Background bg;
shapes::Group group;
std::vector<std::shared_ptr<shapes::Light>> lights;
cam::CamObs cam;
size_t depth;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment