Last active
November 24, 2018 11:41
-
-
Save hi2p-perim/5036d37d534469e7928fd665e2fef991 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Lightmetrica - Copyright (c) 2018 Hisanari Otsu | |
Distributed under MIT license. See LICENSE file for details. | |
*/ | |
#include <pch.h> | |
#include <lm/user.h> | |
#include <lm/renderer.h> | |
#include <lm/scene.h> | |
#include <lm/film.h> | |
#include <lm/parallel.h> | |
#include <lm/json.h> | |
#include <lm/logger.h> | |
LM_NAMESPACE_BEGIN(LM_NAMESPACE) | |
class Renderer_Raycast final : public Renderer { | |
private: | |
Vec3 bgColor_; | |
bool useConstantColor_; | |
Film* film_; | |
public: | |
virtual bool construct(const Json& prop) override { | |
bgColor_ = valueOr(prop, "bg_color", Vec3(1_f)); | |
useConstantColor_ = valueOr(prop, "use_constant_color", false); | |
film_ = comp::cast<lm::Film>(lm::getAsset(prop["output"].get<std::string>())); | |
if (!film_) { | |
return false; | |
} | |
return true; | |
} | |
virtual void render(const Scene* scene) const override { | |
const auto [w, h] = film_->size(); | |
const long long samples = w * h; | |
parallel::foreach(samples, [&](long long index, int threadId) { | |
LM_UNUSED(threadId); | |
const int x = int(index % w); | |
const int y = int(index / w); | |
const auto ray = scene->primaryRay({(x+.5_f)/w, (y+.5_f)/h}); | |
const auto sp = scene->intersect(ray); | |
if (!sp) { | |
film_->setPixel(x, y, bgColor_); | |
return; | |
} | |
const auto R = scene->reflectance(*sp); | |
auto C = R ? *R : Vec3(); | |
if (!useConstantColor_) { | |
C *= glm::abs(glm::dot(sp->n, -ray.d)); | |
} | |
film_->setPixel(x, y, C); | |
}); | |
} | |
}; | |
LM_COMP_REG_IMPL(Renderer_Raycast, "renderer::raycast"); | |
LM_NAMESPACE_END(LM_NAMESPACE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment