added task 06 solution
This commit is contained in:
parent
227335f869
commit
fc608346c0
4 changed files with 179 additions and 199 deletions
|
@ -55,22 +55,27 @@ Color Texture::color(float u, float v, bool interpolate) const {
|
|||
if (!interpolate) {
|
||||
color = this->getPixelAt(int(roundf(u * this->width())), int(roundf(v * this->height())));
|
||||
} else {
|
||||
// IMPLEMENT bilinear interpolation
|
||||
float x = u * this->width();
|
||||
float x1 = std::floor(x);
|
||||
float x2 = std::ceil(x);
|
||||
// bilinear interpolation
|
||||
// adjacent pixel coordinates
|
||||
int left = int(floorf(u * this->width()));
|
||||
int right = int(ceilf(u * this->width()));
|
||||
int top = int(floorf(v * this->height()));
|
||||
int bottom = int(ceilf(v * this->height()));
|
||||
|
||||
float y = v * this->height();
|
||||
float y1 = std::floor(y);
|
||||
float y2 = std::ceil(y);
|
||||
// weights
|
||||
float w[4];
|
||||
w[0] = right - u * this->width();
|
||||
w[1] = 1 - w[0];
|
||||
w[2] = bottom - v * this->height();
|
||||
w[3] = 1 - w[2];
|
||||
|
||||
Color x1y1 = this->getPixelAt(static_cast<int>(x1), static_cast<int>(y1));
|
||||
Color x2y1 = this->getPixelAt(static_cast<int>(x2), static_cast<int>(y1));
|
||||
Color x1y2 = this->getPixelAt(static_cast<int>(x1), static_cast<int>(y2));
|
||||
Color x2y2 = this->getPixelAt(static_cast<int>(x2), static_cast<int>(y2));
|
||||
Color fxy1 = (x2 - x) * x1y1 + (x - x1) * x2y1;
|
||||
Color fxy2 = (x2 - x) * x1y2 + (x - x1) * x2y2;
|
||||
color = (y2 - y) * fxy1 + (y - y1) * fxy2;
|
||||
// get color values and interpolate
|
||||
Color val[4];
|
||||
val[0] = this->getPixelAt(left, top);
|
||||
val[1] = this->getPixelAt(right, top);
|
||||
val[2] = this->getPixelAt(left, bottom);
|
||||
val[3] = this->getPixelAt(right, bottom);
|
||||
color = w[2] * w[0] * val[0] + w[2] * w[1] * val[1] + w[3] * w[0] * val[2] + w[3] * w[1] * val[3];
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
|
|
@ -1,27 +1,29 @@
|
|||
//
|
||||
// Created by arvids on 13.12.22.
|
||||
//
|
||||
|
||||
#include "superrenderer.h"
|
||||
#include "common/ray.h"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include "renderer/superrenderer.h"
|
||||
#include "camera/camera.h"
|
||||
#include "scene/scene.h"
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
void SuperRenderer::renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep,
|
||||
int widthOffset, int height, int heightStep, int heightOffset, std::atomic<int> *k,
|
||||
int const stepSize, int superSamplingFactor) {
|
||||
void SuperRenderer::renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep, int widthOffset, int height, int heightStep, int heightOffset, std::atomic<int> *k, int const stepSize,
|
||||
int superSamplingFactor) {
|
||||
int const sampleCount = superSamplingFactor * superSamplingFactor;
|
||||
float const samplingStep = 1.0f / superSamplingFactor;
|
||||
float const aspectRatio = static_cast<float>(height) / width;
|
||||
for (int y = heightOffset; y < height; y += heightStep) {
|
||||
for (int x = widthOffset; x < width; x += widthStep) {
|
||||
Color fragmentColor = {};
|
||||
fragmentColor = calcSuperColor(scene, camera, width, height, superSamplingFactor, aspectRatio, y, x,
|
||||
fragmentColor);
|
||||
fragmentColor /= static_cast<float>(superSamplingFactor * superSamplingFactor);
|
||||
image->setPixelAt(x, y, clamped(fragmentColor));
|
||||
|
||||
for (int y = heightOffset; y < image->height(); y += heightStep) {
|
||||
for (int x = widthOffset; x < image->width(); x += widthStep) {
|
||||
// The fragment color is averaged over all sub-pixel rays
|
||||
Color fragmentColor;
|
||||
for (int xs = 0; xs < superSamplingFactor; ++xs) {
|
||||
for (int ys = 0; ys < superSamplingFactor; ++ys) {
|
||||
Ray ray = camera->createRay(((xs * samplingStep + x) / width * 2 - 1), -((ys * samplingStep + y) / height * 2 - 1) * aspectRatio);
|
||||
fragmentColor += scene->traceRay(ray);
|
||||
}
|
||||
}
|
||||
image->setPixelAt(x, y, clamped(fragmentColor / float(sampleCount)));
|
||||
|
||||
// Super hacky progress bar!
|
||||
if (++*k % stepSize == 0) {
|
||||
std::cout << "=" << std::flush;
|
||||
|
@ -30,31 +32,17 @@ void SuperRenderer::renderThread(const Scene *scene, Camera const *camera, Textu
|
|||
}
|
||||
}
|
||||
|
||||
Color &
|
||||
SuperRenderer::calcSuperColor(const Scene *scene, const Camera *camera, int width, int height, int superSamplingFactor,
|
||||
const float aspectRatio, int y, int x, Color &fragmentColor) {
|
||||
for (int x1 = 0; x1 < superSamplingFactor; x1++) {
|
||||
for (int y1 = 0; y1 < superSamplingFactor; y1++) {
|
||||
float offsetX = (-0.5f + static_cast<float>(x1) / static_cast<float>(superSamplingFactor - 1));
|
||||
float offsetY = (-0.5f + static_cast<float>(y1) / static_cast<float>(superSamplingFactor - 1));
|
||||
|
||||
Ray ray = camera->createRay(((static_cast<float>(x) + offsetX) / static_cast<float>(width) * 2.0f - 1),
|
||||
-((static_cast<float>(y) + offsetY) / static_cast<float>(height) * 2.0f - 1) * aspectRatio);
|
||||
fragmentColor += scene->traceRay(ray);
|
||||
}
|
||||
}
|
||||
return fragmentColor;
|
||||
}
|
||||
|
||||
Texture SuperRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) {
|
||||
Texture image(width, height);
|
||||
|
||||
// Setup timer
|
||||
std::chrono::steady_clock::time_point start, stop;
|
||||
|
||||
// ICGVARIANT ray_counting
|
||||
// Reset Ray counting
|
||||
Ray::resetRayCount();
|
||||
|
||||
// ENDVARIANT ray_counting
|
||||
// Super-hacky progress bar!
|
||||
std::cout << "(SuperRenderer): Begin rendering..." << std::endl;
|
||||
std::cout << "| 0%";
|
||||
|
@ -65,19 +53,18 @@ Texture SuperRenderer::renderImage(Scene const &scene, Camera const &camera, int
|
|||
std::cout << "100% |" << std::endl << "|";
|
||||
std::atomic<int> k(0);
|
||||
|
||||
/* Start timer */ start = std::chrono::steady_clock::now();
|
||||
// Start timer
|
||||
start = std::chrono::steady_clock::now();
|
||||
|
||||
// Spawn a thread for every logical processor -1, calling the renderThread function
|
||||
int const nThreads = std::thread::hardware_concurrency();
|
||||
std::vector<std::thread> threads;
|
||||
for (int t = 0; t < nThreads - 1; ++t) {
|
||||
threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize,
|
||||
this->superSamplingFactor);
|
||||
threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize, this->superSamplingFactor_);
|
||||
}
|
||||
|
||||
// Call the renderThread function yourself
|
||||
renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize,
|
||||
this->superSamplingFactor);
|
||||
renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize, this->superSamplingFactor_);
|
||||
|
||||
// Rejoin the threads
|
||||
for (int t = 0; t < nThreads - 1; ++t) {
|
||||
|
@ -94,17 +81,14 @@ Texture SuperRenderer::renderImage(Scene const &scene, Camera const &camera, int
|
|||
|
||||
std::cout << "Time: " << seconds << "s" << std::endl;
|
||||
|
||||
// ICGVARIANT ray_counting
|
||||
// Get the number of seconds per ray
|
||||
int rays = Ray::getRayCount();
|
||||
|
||||
std::cout << "Paths: " << rays << std::endl;
|
||||
std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl;
|
||||
|
||||
// ENDVARIANT ray_counting
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
void SuperRenderer::setSuperSamplingFactor(int superSamplingFactor) {
|
||||
SuperRenderer::superSamplingFactor = superSamplingFactor;
|
||||
}
|
||||
|
|
|
@ -1,33 +1,30 @@
|
|||
//
|
||||
// Created by arvids on 13.12.22.
|
||||
//
|
||||
#ifndef SUPERRENDERER_H
|
||||
#define SUPERRENDERER_H
|
||||
|
||||
#ifndef CG1_TRACER_SUPERRENDERER_H
|
||||
#define CG1_TRACER_SUPERRENDERER_H
|
||||
|
||||
#include "renderer.h"
|
||||
#include <thread>
|
||||
#include "renderer/renderer.h"
|
||||
#include <atomic>
|
||||
|
||||
class SuperRenderer : public Renderer {
|
||||
public:
|
||||
static void
|
||||
renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep,
|
||||
static void renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep,
|
||||
int widthOffset, int height, int heightStep, int heightOffset, std::atomic<int> *k,
|
||||
int const stepSize, int superSamplingFactor);
|
||||
|
||||
Texture renderImage(const Scene &scene, const Camera &camera, int width, int height) override;
|
||||
void setSuperSamplingFactor(int superSamplingFactor);
|
||||
public:
|
||||
// Constructor / Destructor
|
||||
SuperRenderer() = default;
|
||||
~SuperRenderer() override = default;
|
||||
|
||||
// Get
|
||||
int superSamplingFactor() { return this->superSamplingFactor_; }
|
||||
|
||||
// Set
|
||||
void setSuperSamplingFactor(int factor) { this->superSamplingFactor_ = factor; }
|
||||
|
||||
// Render functions
|
||||
Texture renderImage(Scene const &scene, Camera const &camera, int width, int height) override;
|
||||
|
||||
private:
|
||||
private:
|
||||
|
||||
int superSamplingFactor;
|
||||
|
||||
static Color &
|
||||
calcSuperColor(const Scene *scene, const Camera *camera, int width, int height, int superSamplingFactor,
|
||||
const float aspectRatio, int y, int x, Color &fragmentColor);
|
||||
int superSamplingFactor_ = 2;
|
||||
};
|
||||
|
||||
|
||||
#endif //CG1_TRACER_SUPERRENDERER_H
|
||||
#endif
|
||||
|
|
|
@ -6,64 +6,88 @@
|
|||
|
||||
Vector3d
|
||||
tangentToWorldSpace(const Vector3d &surfaceNormal, const Vector3d &surfaceTangent, const Vector3d &surfaceBitangent,
|
||||
const Vector3d &textureNormal)
|
||||
{
|
||||
const Vector3d &textureNormal) {
|
||||
return textureNormal.x * surfaceTangent + textureNormal.y * surfaceBitangent + textureNormal.z * surfaceNormal;
|
||||
}
|
||||
|
||||
MaterialShader::MaterialShader() : opacity(1.0f), normalCoefficient(1.0f), diffuseCoefficient(0.5f), reflectance(0.0f),
|
||||
specularCoefficient(0.5f), shininessExponent(8)
|
||||
{}
|
||||
specularCoefficient(0.5f), shininessExponent(8) {}
|
||||
|
||||
Color MaterialShader::shade(Scene const &scene, Ray const &ray) const
|
||||
{
|
||||
Color MaterialShader::shade(Scene const &scene, Ray const &ray) const {
|
||||
Color fragmentColor;
|
||||
// IMPLEMENT ME
|
||||
|
||||
// (Normal Map) Calculate the new normal vector
|
||||
Vector3d surfaceNormal = ray.normal;
|
||||
if (this->normalMap != nullptr)
|
||||
{
|
||||
auto surfaceNormalMapColor = this->normalMap->color(ray.surface, true);
|
||||
Vector3d textureNormal = {surfaceNormalMapColor.r, surfaceNormalMapColor.g, surfaceNormalMapColor.b};
|
||||
textureNormal = textureNormal * 2.0f - Vector3d{1, 1, 1};
|
||||
surfaceNormal = ray.normal * normalCoefficient +
|
||||
(1 - normalCoefficient) *
|
||||
tangentToWorldSpace(ray.normal, ray.tangent, ray.bitangent, textureNormal);
|
||||
Vector3d normal = ray.normal;
|
||||
if (this->normalMap) {
|
||||
Color const normalColor = this->normalMap->color(ray.surface);
|
||||
Vector3d const textureNormal =
|
||||
Vector3d(2.0f * normalColor.r, 2.0f * normalColor.g, 2.0f * normalColor.b) - Vector3d(1, 1, 1);
|
||||
normal = normalized(tangentToWorldSpace(normal, ray.tangent, ray.bitangent, normalized(textureNormal)) *
|
||||
this->normalCoefficient + (1.0f - this->normalCoefficient) * normal);
|
||||
}
|
||||
|
||||
// Calculate the reflection vector
|
||||
Vector3d const reflection = normalized(ray.direction - 2 * dotProduct(normal, ray.direction) * normal);
|
||||
|
||||
// (Diffuse-/Specular Map) Accumulate the light over all light sources
|
||||
Color surfaceDiffuseColor(0, 0, 0);
|
||||
if (this->diffuseMap != nullptr)
|
||||
{
|
||||
surfaceDiffuseColor = this->diffuseMap->color(ray.surface, true);
|
||||
for (const auto &light: scene.lights()) {
|
||||
|
||||
// Retrieve an illumination object
|
||||
Light::Illumination illum = light->illuminate(scene, ray);
|
||||
|
||||
// Diffuse term
|
||||
Color const diffuse =
|
||||
this->diffuseCoefficient * illum.color * std::max(dotProduct(-illum.direction, normal), 0.0f);
|
||||
if (this->diffuseMap)
|
||||
fragmentColor += diffuse * this->diffuseMap->color(ray.surface);
|
||||
else
|
||||
fragmentColor += diffuse;
|
||||
|
||||
// Specular term
|
||||
float const cosine = dotProduct(-illum.direction, reflection);
|
||||
if (cosine > 0) {
|
||||
Color const specular = this->specularCoefficient * illum.color * std::pow(cosine, shininessExponent);
|
||||
if (this->specularMap)
|
||||
fragmentColor += specular * this->specularMap->color(ray.surface);
|
||||
else
|
||||
fragmentColor += specular;
|
||||
}
|
||||
}
|
||||
|
||||
Color surfaceSpecularColor(0, 0, 0);
|
||||
if (this->specularMap != nullptr)
|
||||
{
|
||||
surfaceSpecularColor = this->specularMap->color(ray.surface, true);
|
||||
}
|
||||
|
||||
|
||||
// (Reflection Map) Calculate the reflectance, create a reflection ray
|
||||
Vector3d const reflection = ray.direction - 2 * dotProduct(surfaceNormal, ray.direction) * ray.normal;
|
||||
float surfaceReflectanceCoefficient = this->reflectance;
|
||||
if (this->reflectionMap != nullptr)
|
||||
{
|
||||
auto surfaceReflectiveMapColor = this->reflectionMap->color(ray.surface, true);
|
||||
surfaceReflectanceCoefficient = surfaceReflectiveMapColor.r;
|
||||
}
|
||||
// (Reflection Map) Calculate the reflectance
|
||||
float reflectance = this->reflectance;
|
||||
if (this->reflectionMap)
|
||||
reflectance *= this->reflectionMap->color(ray.surface).r;
|
||||
if (reflectance > 0.0f) {
|
||||
// Create a new reflection ray
|
||||
Ray reflectionRay = ray;
|
||||
reflectionRay.origin = ray.origin + (ray.length - REFR_EPS) * ray.direction;
|
||||
reflectionRay.direction = normalized(reflection);
|
||||
reflectionRay.origin = ray.origin + (ray.length - EPSILON) * ray.direction;
|
||||
reflectionRay.direction = reflection;
|
||||
reflectionRay.length = INFINITY;
|
||||
reflectionRay.primitive = nullptr;
|
||||
// Mix the object and the reflected image
|
||||
Color const reflectionColor = scene.traceRay(reflectionRay);
|
||||
fragmentColor = (1 - reflectance) * fragmentColor + reflectance * reflectionColor;
|
||||
}
|
||||
|
||||
// (Alpha Map) Calculate the opacity
|
||||
float alpha = this->opacity;
|
||||
if (this->alphaMap)
|
||||
alpha *= this->alphaMap->color(ray.surface).r;
|
||||
if (alpha < 1) {
|
||||
// Create a new alpha ray
|
||||
Ray alphaRay = ray;
|
||||
alphaRay.origin = ray.origin + (ray.length + EPSILON) * ray.direction;
|
||||
alphaRay.length = INFINITY;
|
||||
alphaRay.primitive = nullptr;
|
||||
// Mix the foreground and background colors
|
||||
Color const backgroundColor = scene.traceRay(alphaRay);
|
||||
fragmentColor = alpha * fragmentColor + (1 - alpha) * backgroundColor;
|
||||
}
|
||||
|
||||
// (Alpha Map) Calculate the opacity, create a background ray
|
||||
float surfaceAlphaCoefficient(1);
|
||||
if (this->alphaMap != nullptr)
|
||||
{
|
||||
if (this->alphaMap != nullptr) {
|
||||
auto surfaceAlphaMapColor = this->alphaMap->color(ray.surface, true);
|
||||
surfaceAlphaCoefficient = surfaceAlphaMapColor.r;
|
||||
}
|
||||
|
@ -73,37 +97,8 @@ Color MaterialShader::shade(Scene const &scene, Ray const &ray) const
|
|||
propagatedRay.length = INFINITY;
|
||||
propagatedRay.primitive = nullptr;
|
||||
|
||||
// Iterate over light sources
|
||||
for (const auto &light: scene.lights())
|
||||
{
|
||||
Light::Illumination const illum = light->illuminate(scene, ray);
|
||||
|
||||
// Diffuse term (lambertian)
|
||||
Color const diffuse = this->diffuseCoefficient * surfaceDiffuseColor *
|
||||
std::max(dotProduct(-illum.direction, ray.normal), 0.0f);
|
||||
fragmentColor += diffuse * illum.color;
|
||||
|
||||
// Specular term (phong)
|
||||
float const cosine = dotProduct(-illum.direction, reflection);
|
||||
if (cosine > 0)
|
||||
{
|
||||
Color const specular = this->specularCoefficient * surfaceSpecularColor // highlight
|
||||
* powf(cosine, this->shininessExponent); // shininess factor
|
||||
fragmentColor += specular * illum.color;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// Reflected ray
|
||||
if (surfaceReflectanceCoefficient > 0)
|
||||
{
|
||||
Color const reflectionColor = scene.traceRay(reflectionRay);
|
||||
fragmentColor += surfaceReflectanceCoefficient * reflectionColor * reflectance;
|
||||
}
|
||||
|
||||
// Opacity
|
||||
if (surfaceAlphaCoefficient < 1)
|
||||
{
|
||||
if (surfaceAlphaCoefficient < 1) {
|
||||
Color const background = scene.traceRay(propagatedRay);
|
||||
fragmentColor = (1 - surfaceAlphaCoefficient) * background + surfaceAlphaCoefficient * fragmentColor;
|
||||
}
|
||||
|
@ -111,5 +106,4 @@ Color MaterialShader::shade(Scene const &scene, Ray const &ray) const
|
|||
return fragmentColor;
|
||||
}
|
||||
|
||||
bool MaterialShader::isTransparent() const
|
||||
{ return this->opacity < 1.0f || this->alphaMap; }
|
||||
bool MaterialShader::isTransparent() const { return this->opacity < 1.0f || this->alphaMap; }
|
||||
|
|
Loading…
Reference in a new issue