From f8f9f627d8c1013df27d6cf1f1dbf40f4f75157d Mon Sep 17 00:00:00 2001 From: jp Date: Tue, 6 Dec 2022 13:48:21 +0100 Subject: [PATCH 1/2] added ex04 solution --- primitive/triangle.cpp | 9 ++++++-- shader/brdfshader.cpp | 38 +++++++++++++++++++++++++++++-- shader/cooktorranceshader.cpp | 43 ++++++++++++++++++++++++++++++++++- shader/lambertshader.cpp | 8 ++++++- shader/phongshader.cpp | 22 +++++++++++++++++- 5 files changed, 113 insertions(+), 7 deletions(-) diff --git a/primitive/triangle.cpp b/primitive/triangle.cpp index 34da50a..3632b6b 100644 --- a/primitive/triangle.cpp +++ b/primitive/triangle.cpp @@ -100,8 +100,13 @@ bool Triangle::intersect(Ray &ray) const { return false; // Calculate the normal - // IMPLEMENT smooth triangles, if available - ray.normal = normalized(crossProduct(edge1, edge2)); + if (length(this->normal[0]) * length(this->normal[1]) * length(this->normal[2]) > EPSILON) + ray.normal = normalized(u * this->normal[1] + v * this->normal[2] + (1 - u - v) * this->normal[0]); + else + ray.normal = normalized(crossProduct(edge1, edge2)); + // calculate the tangent and bitangent vectors as well + ray.tangent = normalized(u * this->tangent[1] + v * this->tangent[2] + (1 - u - v) * this->tangent[0]); + ray.bitangent = normalized(u * this->bitangent[1] + v * this->bitangent[2] + (1 - u - v) * this->bitangent[0]); // Calculate the surface position ray.surface = u * this->surface[1] + v * this->surface[2] + (1 - u - v) * this->surface[0]; diff --git a/shader/brdfshader.cpp b/shader/brdfshader.cpp index ab77d37..0c40a96 100644 --- a/shader/brdfshader.cpp +++ b/shader/brdfshader.cpp @@ -6,8 +6,42 @@ BrdfShader::BrdfShader(char const *fileName, Color const &scale) : scale(scale), brdf(std::make_unique(fileName)) {} Color BrdfShader::shade(Scene const &scene, Ray const &ray) const { - Color illuminationColor; + // Calculate theta and phi + float thetaIn = std::acos(dotProduct(-ray.normal, ray.direction)); + float phiIn = 0.0f; - // IMPLEMENT ME + // Derive local coordinate system + Vector3d const x = crossProduct(-ray.direction, ray.normal); + Vector3d const y = crossProduct(ray.normal, x); + + // Accumulate the light over all light sources + Color illuminationColor; + for (const auto &light : scene.lights()) { + Light::Illumination illum; + illum = light->illuminate(scene, ray); + + // Diffuse term + float const cosine = dotProduct(-illum.direction, ray.normal); + if (cosine > 0) { + Color color; + + // Avoid numeric instability + if (cosine < 1) { + float const thetaOut = std::acos(cosine); + + // Project outgoing vector into local coordinate system + Vector3d const c = crossProduct(-illum.direction, ray.normal); + float const phiOut = std::atan2(dotProduct(c, y), dotProduct(c, x)); + + color = Color(brdf->lookupBrdfValues(thetaIn, phiIn, thetaOut, phiOut)); + } else { + color = Color(brdf->lookupBrdfValues(thetaIn, phiIn, 0, 0)); + } + + // Calculate colors + Color const diffuseColor = scale * color * cosine; + illuminationColor += diffuseColor * illum.color; + } + } return illuminationColor; } diff --git a/shader/cooktorranceshader.cpp b/shader/cooktorranceshader.cpp index 3f9cff5..ccfc01d 100644 --- a/shader/cooktorranceshader.cpp +++ b/shader/cooktorranceshader.cpp @@ -4,10 +4,51 @@ CookTorranceShader::CookTorranceShader(Color const &diffCol, Color const &ctCol, float IOR, float roughness, float diffCoeff, float ctCoeff) : diffuseColor(diffCol * diffCoeff), ctColor(ctCol * ctCoeff), F0(IOR), m(roughness) {} +float CookTorranceShader::D(float NdotH) const { + // Beckmann distribution + float const r2 = m * m; + float const NdotH2 = NdotH * NdotH; + return expf((NdotH2 - 1.0f) / (r2 * NdotH2)) / (4.0f * r2 * powf(NdotH, 4.0f)); +} + +float CookTorranceShader::F(float VdotH) const { + // Schlicks approximation + return F0 + (1.0f - F0) * powf(1.0f - VdotH, 5); +} + +float CookTorranceShader::G(float NdotH, float NdotV, float VdotH, float NdotL) const { return std::min(1.0f, std::min(2.0f * NdotH * NdotV / VdotH, 2.0f * NdotH * NdotL / VdotH)); } + Color CookTorranceShader::shade(Scene const &scene, Ray const &ray) const { Color fragmentColor; - // IMPLEMENT ME + if (m >= 0.0f) { + // Accumulate the light over all light sources + for (const auto &light : scene.lights()) { + Light::Illumination illum; + illum = light->illuminate(scene, ray); + + float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal)); + if (NdotL <= 0.0f) + continue; + + // Diffuse term + Color const diffuse = this->diffuseColor / float(PI); + fragmentColor += diffuse * NdotL * illum.color; + + // Cook-Torrance term + // half angle vector + Vector3d const H = normalized(-illum.direction - ray.direction); + float const NdotH = std::max(0.0f, dotProduct(ray.normal, H)); + float const NdotV = std::max(0.0f, dotProduct(ray.normal, -ray.direction)); + float const VdotH = std::max(0.0f, dotProduct(-ray.direction, H)); + + if (NdotV * NdotL > EPSILON) { + Color const specular = this->ctColor * (F(VdotH) * D(NdotH) * G(NdotH, NdotV, VdotH, NdotL)) / (float(PI) * NdotV * NdotL); + + fragmentColor += specular * NdotL * illum.color; + } + } + } return fragmentColor; } \ No newline at end of file diff --git a/shader/lambertshader.cpp b/shader/lambertshader.cpp index 3aebacd..0e2c4a6 100644 --- a/shader/lambertshader.cpp +++ b/shader/lambertshader.cpp @@ -7,7 +7,13 @@ LambertShader::LambertShader(Color const &diffuseColor) : diffuseColor(diffuseCo Color LambertShader::shade(Scene const &scene, Ray const &ray) const { Color fragmentColor; - // IMPLEMENT ME + // Accumulate the light over all light sources + for (const auto &light : scene.lights()) { + Light::Illumination const illum = light->illuminate(scene, ray); + // Diffuse term + Color const diffuse = this->diffuseColor * std::max(dotProduct(-illum.direction, ray.normal), 0.0f); + fragmentColor += diffuse * illum.color; + } return fragmentColor; } diff --git a/shader/phongshader.cpp b/shader/phongshader.cpp index da6d3e2..ac9c3d2 100644 --- a/shader/phongshader.cpp +++ b/shader/phongshader.cpp @@ -10,7 +10,27 @@ PhongShader::PhongShader(Color const &diffuseColor, float diffuseCoefficient, Co Color PhongShader::shade(Scene const &scene, Ray const &ray) const { Color fragmentColor; - // IMPLEMENT ME + // Calculate the reflection vector + Vector3d const reflection = ray.direction - 2 * dotProduct(ray.normal, ray.direction) * ray.normal; + + // Accumulate the light over all light sources + for (const auto &light : scene.lights()) { + Light::Illumination illum; + illum = light->illuminate(scene, ray); + + // Diffuse term + Color const diffuse = + this->diffuseCoefficient * this->diffuseColor * std::max(dotProduct(-illum.direction, ray.normal), 0.0f); + fragmentColor += diffuse * illum.color; + + // Specular term + float const cosine = dotProduct(-illum.direction, reflection); + if (cosine > 0) { + Color const specular = this->specularCoefficient * this->specularColor // highlight + * powf(cosine, this->shininessExponent); // shininess factor + fragmentColor += specular * illum.color; + } + } return fragmentColor; } From fe68572fae9b0f6defcf39a1a3d4d5593503b32d Mon Sep 17 00:00:00 2001 From: jp Date: Tue, 6 Dec 2022 13:49:37 +0100 Subject: [PATCH 2/2] added task 05 --- CMakeLists.txt | 3 ++ ex5.cpp | 54 +++++++++++++++++++++++++++ renderer/kdtreerenderer.cpp | 18 +++++++++ renderer/kdtreerenderer.h | 22 +++++++++++ scene/fastscene.cpp | 73 +++++++++++++++++++++++++++++++++++++ scene/fastscene.h | 46 +++++++++++++++++++++++ 6 files changed, 216 insertions(+) create mode 100644 ex5.cpp create mode 100644 renderer/kdtreerenderer.cpp create mode 100644 renderer/kdtreerenderer.h create mode 100644 scene/fastscene.cpp create mode 100644 scene/fastscene.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 48a0af5..c13aa74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,9 @@ add_executable(tracey_ex4 ex4.cpp) target_link_libraries(tracey_ex4 tracey) +add_executable(tracey_ex5 ex5.cpp) +target_link_libraries(tracey_ex5 tracey) + diff --git a/ex5.cpp b/ex5.cpp new file mode 100644 index 0000000..a2ede32 --- /dev/null +++ b/ex5.cpp @@ -0,0 +1,54 @@ +#include +#include + +#include "camera/perspectivecamera.h" + +#include "scene/fastscene.h" + +#include "renderer/kdtreerenderer.h" +#include "renderer/simplerenderer.h" + +#include "shader/cooktorranceshader.h" + +#include "light/ambientlight.h" +#include "light/pointlight.h" +#include "light/spotlight.h" + +#include "primitive/objmodel.h" + +int main() { + FastScene scene; + scene.setEnvironmentMap(std::make_shared("data/space.png")); + + // Set up the camera + PerspectiveCamera camera; + camera.setFovAngle(90.0f); + camera.setPosition(Vector3d(0.0f, -2.5f, 10.0f)); + camera.setForwardDirection(Vector3d(0.0f, 0.6f, -1.0f)); + camera.setUpDirection(Vector3d(0.0f, 1.0f, 0.0f)); + + // Add shaders + auto redCook = std::make_shared(Color(1.0f, 0.0f, 0.0f), Color(1.0f, 1.0f, 1.0f), 1.0f, 0.3f); + auto goldCook = std::make_shared(Color(0.83f, 0.69f, 0.22f), Color(1.0f, 1.0f, 0.0f), 1.2f, 0.2f); + + scene.addObj("data/teapot_stadium.obj", Vector3d(1.0f, 1.0f, 1.0f) * 40.0f, Vector3d(-0.25f, -0.5f, -2.5f), goldCook); + scene.addObj("data/stadium.obj", Vector3d(1.0f, 1.0f, 1.0f) * 70.0f, Vector3d(-0.5f, -1.0f, -3.0f), redCook); + + // Add lights + scene.add(std::make_shared(0.1f)); + scene.add(std::make_shared(Vector3d(-0.25f, 20.0f, -3.0f), 60.0f)); + scene.add( + std::make_shared(Vector3d(-0.25f, 7.0f, -4.0f), Vector3d(0.0f, -1.0f, 0.0f), 10.0f, 30.0f, 25.0f)); + + // build the tree + scene.buildTree(); + + // Render the scene + SimpleRenderer renderer; + renderer.renderImage(scene, camera, 1920, 1080).save("result.png"); + + KDTreeRenderer kd_renderer; + kd_renderer.renderKDTree(scene, camera, 1920, 1080).save("result_kd.png"); + + return 0; +} diff --git a/renderer/kdtreerenderer.cpp b/renderer/kdtreerenderer.cpp new file mode 100644 index 0000000..b4ae141 --- /dev/null +++ b/renderer/kdtreerenderer.cpp @@ -0,0 +1,18 @@ +#include "camera/camera.h" +#include "renderer/kdtreerenderer.h" +#include "scene/fastscene.h" +#include + +Texture KDTreeRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) { + Texture image(width, height); + std::cout << "KD Tree renderer will only output KD Tree visualization" << std::endl; + return image; +} + +Texture KDTreeRenderer::renderKDTree(FastScene const &scene, Camera const &camera, int width, int height) { + Texture image(width, height); + + // IMPLEMENT ME + + return image; +} \ No newline at end of file diff --git a/renderer/kdtreerenderer.h b/renderer/kdtreerenderer.h new file mode 100644 index 0000000..9a1a15c --- /dev/null +++ b/renderer/kdtreerenderer.h @@ -0,0 +1,22 @@ +#ifndef KDTREERENDERER_H +#define KDTREERENDERER_H + +#include "renderer/renderer.h" + +// Forward Declaration +class FastScene; + +class KDTreeRenderer : public Renderer { + +public: + // Constructor / Destructor + KDTreeRenderer() = default; + ~KDTreeRenderer() override = default; + + // Render functions + Texture renderImage(Scene const &scene, Camera const &camera, int width, int height) override; + + Texture renderKDTree(FastScene const &scene, Camera const &camera, int width, int height); +}; + +#endif \ No newline at end of file diff --git a/scene/fastscene.cpp b/scene/fastscene.cpp new file mode 100644 index 0000000..158ee4c --- /dev/null +++ b/scene/fastscene.cpp @@ -0,0 +1,73 @@ +#include "primitive/primitive.h" +#include "scene/fastscene.h" +#include "shader/shader.h" +#include +#include + +int Node::countNodeIntersections(const Ray &ray, float t0, float t1) const { + // IMPLEMENT ME + return 0; +} + +bool Node::findIntersection(Ray &ray, float t0, float t1) const { + // IMPLEMENT ME + // If this is a leaf node, we intersect with all the primitives... + // ... otherwise we continue through the branches + // Determine the order in which we intersect the child nodes + // Traverse the necessary children + return false; +} + +bool Node::findOcclusion(Ray &ray, float t0, float t1) const { + // IMPLEMENT ME + return false; +} + +int FastScene::countNodeIntersections(const Ray &ray) const { + // IMPLEMENT ME + return false; +} + +bool FastScene::findIntersection(Ray &ray) const { + // IMPLEMENT ME + return false; +} + +bool FastScene::findOcclusion(Ray &ray) const { + // IMPLEMENT ME + return false; +} + +void FastScene::buildTree(int maximumDepth, int minimumNumberOfPrimitives) { + // IMPLEMENT ME + // Set the new depth and number of primitives + + // Determine the bounding box of the kD-Tree + + // Recursively build the kD-Tree +} + +std::unique_ptr FastScene::build(Vector3d const &minimumBounds, Vector3d const &maximumBounds, const std::vector> &primitives, int depth) { + // IMPLEMENT ME + // Determine the diameter of the bounding box + + // Test whether we have reached a leaf node... + + // ... otherwise create a new inner node by splitting through the widest + // dimension + + // Determine the split position + // Note: Use the median of the minimum bounds of the primitives + + // Divide primitives into the left and right lists + // Remember: A primitive can be in both lists! + // Also remember: You split exactly at the minimum of a primitive, + // make sure *that* primitive does *not* appear in both lists! + + // Print out the number of primitives in the left and right child node + + // Set the left and right split vectors + + // Recursively build the tree + return nullptr; +} diff --git a/scene/fastscene.h b/scene/fastscene.h new file mode 100644 index 0000000..fd59324 --- /dev/null +++ b/scene/fastscene.h @@ -0,0 +1,46 @@ +#ifndef FASTSCENE_H +#define FASTSCENE_H + +#include "scene/scene.h" + +struct Node { + // Constructor / Destructor + Node() : dimension(0), split(0) {} + + // Traversal function + bool findIntersection(Ray &ray, float t0, float t1) const; + bool findOcclusion(Ray &ray, float t0, float t1) const; + int countNodeIntersections(const Ray &ray, float t0, float t1) const; + inline bool isLeaf() const { return (!this->primitives.empty() || (!this->child[0] && !this->child[1])); } + + // Branch split + std::unique_ptr child[2]; + int dimension; + float split; + + // Leaf primitives + std::vector> primitives; +}; + +class FastScene : public Scene { + +public: + // Raytracing functions + bool findIntersection(Ray &ray) const override; + bool findOcclusion(Ray &ray) const override; + int countNodeIntersections(const Ray &ray) const; + + // Setup functions + void buildTree(int maximumDepth = 10, int minimumNumberOfPrimitives = 2); + +private: + std::unique_ptr build(Vector3d const &minimumBounds, Vector3d const &maximumBounds, + const std::vector> &primitives, int depth); + + std::unique_ptr root; + int maximumDepth; + int minimumNumberOfPrimitives; + Vector3d absoluteMinimum, absoluteMaximum; +}; + +#endif