From ed47dc2b9b4acaaa6f7405d4f2ff9c31cccc029e Mon Sep 17 00:00:00 2001 From: "m.gaedke" Date: Sun, 29 Jan 2023 12:10:57 +0100 Subject: [PATCH] some beautiful scenes, dont render DOFScene it takes 8hours ahaha we need gpu acceleration --- CMakeLists.txt | 10 +++ DOFScene.cpp | 123 +++++++++++++++++++++++++++++ beautifulScene.cpp | 125 +++++++++++++++++++++++++++++- common/texture.cpp | 2 +- common/texture.h | 2 +- homeworkMains/ex2.cpp | 2 +- homeworkMains/ex4.cpp | 2 - renderer/depthoffieldrenderer.cpp | 8 -- shader/brdfshader.cpp | 60 +++++++------- 9 files changed, 287 insertions(+), 47 deletions(-) create mode 100644 DOFScene.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 86a86cf..8d2d87c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,13 @@ if(NOT WIN32) find_package(X11 REQUIRED) endif() +find_path(EIGEN_DIR NAMES signature_of_eigen3_matrix_library + PATHS + C:/Program Files/Eigen3 + PATH_SUFFIXES eigen3 eigen + ) +include_directories(${EIGEN_DIR}) + # This directory include_directories(${CMAKE_CURRENT_SOURCE_DIR}) if(APPLE) @@ -78,6 +85,9 @@ endif() add_executable(fancy1 fancy1.cpp) target_link_libraries(fancy1 tracey) +add_executable(DOFScene DOFScene.cpp) +target_link_libraries(DOFScene tracey) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0") diff --git a/DOFScene.cpp b/DOFScene.cpp new file mode 100644 index 0000000..dff3683 --- /dev/null +++ b/DOFScene.cpp @@ -0,0 +1,123 @@ +#include +#include +#include + +#include "camera/perspectivecamera.h" +#include "renderer/simplerenderer.h" +#include "scene/simplescene.h" + +#include "primitive/box.h" +#include "primitive/infiniteplane.h" +#include "primitive/objmodel.h" +#include "primitive/sphere.h" + +#include "shader/brdfshader.h" +#include "shader/lambertshader.h" +#include "shader/mirrorshader.h" +#include "shader/phongshader.h" +#include "shader/cooktorranceshader.h" +#include "renderer/depthoffieldrenderer.h" + +#include "light/ambientlight.h" +#include "light/pointlight.h" +#include "light/spotlight.h" + +#include "post_processing/bloom.h" + +int main() { + // Let's create a simple scene... + SimpleScene scene; + + // Set up the camera + PerspectiveCamera camera; + camera.setFovAngle(90.0f); + camera.setPosition(Vector3d(0.0f, 0.0f, -10.0f)); + camera.setForwardDirection(Vector3d(0.0f, 0.0f, 1.0f)); + camera.setUpDirection(Vector3d(0.0f, 1.0f, 0.0f)); + + // Add shaders + auto mirror = std::make_shared(); + auto white = std::make_shared(Color(0.9f, 0.9f, 0.9f)); + auto red = std::make_shared(Color(1.0f, 0.3f, 0.2f)); + auto blue = std::make_shared(Color(0.2f, 0.3f, 1.0f)); + auto orange = std::make_shared(Color(1.0f, 0.64f, 0.0f), 1.0f, Color(1.0f, 1.0f, 1.0f), 1.0f, 25.0f); + auto gold= std::make_shared(Color(0.83f, 0.69f, 0.22f), Color(1.0f, 1.0f, 0.0f), 1.2f, 0.2f); + auto blueMetallic = std::make_shared("../data/blue-metallic-paint.binary", Color(7.0f, 7.0f, 7.0f)); + auto darkRed = std::make_shared("../data/dark-red-paint.binary", Color(7.0f, 7.0f, 7.0f)); + auto glass = std::make_shared(1.31f, 1.0f); + + + + + // Set up the walls + // --------------------------------------------------------------------------- + + scene.add(std::make_shared(Vector3d(0.0f, 0.0f, +5.0f), Vector3d(0.0f, 0.0f, -1.0f), mirror)); + + scene.add(std::make_shared(Vector3d(0.0f, 0.0f, -5.0f), Vector3d(0.0f, 0.0f, +1.0f), mirror)); + scene.add(std::make_shared(Vector3d(0.0f, +5.0f, 0.0f), Vector3d(0.0f, -1.0f, 0.0f), white)); + scene.add(std::make_shared(Vector3d(0.0f, -5.0f, 0.0f), Vector3d(0.0f, +1.0f, 0.0f), white)); + scene.add(std::make_shared(Vector3d(+5.0f, 0.0f, 0.0f), Vector3d(-1.0f, 0.0f, 0.0f), blue)); + scene.add(std::make_shared(Vector3d(-5.0f, 0.0f, 0.0f), Vector3d(+1.0f, 0.0f, 0.0f), red)); + + scene.add(std::make_shared(Vector3d(-5.0f, 0.0f, 0.0f), 1.0f, blueMetallic)); + scene.add(std::make_shared(Vector3d(-2.0f, 5.0f, 0.0f), 1.0f, orange)); + scene.add(std::make_shared(Vector3d(2.0f, 4.0f, 0.0f), 1.0f, orange)); + scene.add(std::make_shared(Vector3d(3.0f, 4.0f, -2.0f), 1.0f, glass)); + scene.add(std::make_shared(Vector3d(2.0f, 3.0f, 4.0f), 1.0f, orange)); + scene.add(std::make_shared(Vector3d(-3.0f, 3.0f, 2.0f), 1.0f, orange)); + scene.add(std::make_shared(Vector3d(2.0f, 1.0f, 0.0f), 1.0f, mirror)); + scene.add(std::make_shared(Vector3d(3.0f, -3.0f, 0.0f), 1.0f, darkRed)); + scene.add(std::make_shared(Vector3d(5.0f, 0.0f, 0.0f), 1.0f, darkRed)); + scene.add(std::make_shared(Vector3d(-5.0f, -3.3f, -4.0f), 1.0f, mirror)); + + // Add the teapot + auto ship = std::make_shared(gold); + ship->loadObj("../data/NewObjects/Random/Ship.obj", Vector3d(0.07f, 0.07f, 0.07f), Vector3d(-1.0f, -5.0f, 0.0f)); + scene.add(ship); + + // Add ambient light + scene.add(std::make_shared(0.15f)); + scene.add(std::make_shared(Vector3d(0.0f, 4.0f, -4.0f), 7.0f)); + scene.add(std::make_shared(Vector3d(0.0f, 2.5f, -4.0f), 7.0f)); + + + + // Render the scene + // SimpleRenderer renderer; + // renderer.renderImage(scene, camera, 1024, 1024).save("result.png"); + + + // initialize renderer: aperture = lens thickness, secondaryRayCount = how many rays per pixel are created + // focalLength = the area which is in focus + DOFRenderer renderer(0.2, 100, 10.0f); + + // Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times + int width = 1920; + Texture image = renderer.renderImage(scene, camera, width, width / 16 * 9); + + // Use post-processing Bloom effect + // Bloom bloomEffect = Bloom(image.getImage()); + // Texture imageWithBloom = image; + // imageWithBloom.setTexture(bloomEffect.bloom(0.55f, 5, 10.0f, 0.06f)); + + // save images + image.save("result.png"); + CImg CImgOfImage = image.getImage(); + CImg img_8bit(image.width(), image.height(), 1, 3); + cimg_forXYC(CImgOfImage,x,y,c) { + img_8bit(x,y,c) = (unsigned char)std::round(CImgOfImage(x, y, c) * 255); + } + + CImgDisplay disp(img_8bit, "My Rendered Image",0, false, false); + while (!disp.is_closed()) { + disp.wait(); + disp.display(img_8bit); + if (disp.is_resized()) { + disp.resize(); + } + } + // image.save("resultWithBloom"); + + return 0; +} diff --git a/beautifulScene.cpp b/beautifulScene.cpp index f06af40..bf561b5 100644 --- a/beautifulScene.cpp +++ b/beautifulScene.cpp @@ -1,20 +1,137 @@ #include #include - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include int main() { SimpleScene scene; + scene.setEnvironmentMap(std::make_shared("../data/clear_blue_sky.jpg")); + // scene.setEnvironmentMap(std::make_shared("../data/TychoSkymapII.t5_04096x02048.png")); + scene.setBackgroundColor(Color(0.1,0.1,0.1)); // Set up the camera PerspectiveCamera camera; camera.setFovAngle(90.0f); - camera.setPosition(Vector3d(0.0f, 0.0f, -10.0f)); - camera.setForwardDirection(Vector3d(0.0f, 0.0f, 1.0f)); + camera.setPosition(Vector3d(0.0f, -2.0f, -5.0f)); + camera.setForwardDirection(Vector3d(1.0f, 0.1f, 0.2f)); + // Final camera Position + // camera.setPosition(Vector3d(0.0f, -4.9f, 0.0f)); + // camera.setForwardDirection(Vector3d(1.0f, 0.2f, 0.0f)); camera.setUpDirection(Vector3d(0.0f, 1.0f, 0.0f)); + // Shader + auto gold = std::make_shared(Color(0.83f, 0.69f, 0.22f), Color(1.0f, 0.08f, 0.58f), 1.2f, 0.2f); + auto orange = std::make_shared(Color(1.0f, 0.64f, 0.0f), 1.0f, Color(1.0f, 1.0f, 1.0f), 1.0f, 25.0f); + auto white = std::make_shared(Color(0.9f, 0.9f, 0.9f)); + auto fiona_color = std::make_shared(Color(0.4f, 1.0f, 0.f), 1.0f, Color(1.0f, 1.0f, 1.0f), 1.0f, 25.0f); + auto mirror = std::make_shared(); + auto glass = std::make_shared(1.31f, 1.0f); + + + + // BRDF + auto gold_metallic_paint2 = std::make_shared("../data/BRDF/gold-metallic-paint2.binary", Color(7.0f, 7.0f, 7.0f)); + auto alumina_oxide = std::make_shared("../data/BRDF/alumina-oxide.binary", Color(7.0f, 7.0f, 7.0f)); + + + // Make Objects + auto house = std::make_shared(alumina_oxide); + auto temple = std::make_shared(gold_metallic_paint2); + + house->loadObj("../data/NewObjects/house/objBuilding.obj", Vector3d(1.0f, 1.0f, 1.0f), Vector3d(20.0f, -6.0f, 10.0f)); + temple->loadObj("../data/NewObjects/Random/Temple.obj", Vector3d(0.1f, 0.1f, 0.1f), Vector3d(30.0f, -6.0f, -10.0f)); + + + // Setup ground and sky + // Add floor + scene.add(std::make_shared(Vector3d(0.0f, -5.0f, 0.0f), Vector3d(0.0f, 1.0f, 0.0f), white)); + + + + // Add clouds + + auto cloudSettings = CloudSettings(); + cloudSettings.scale = 16.0f; + auto cloudShader = std::make_shared(cloudSettings); + scene.add(std::make_shared(Vector3d(30.0f, 10.0f, 0.0f), Vector3d(75.0f, 10.0f, 75.0f), cloudShader)); + + + + + // Insert Objects + scene.add(house); + scene.add(temple); + scene.add(std::make_shared(Vector3d(3.0f, -2.0f, -5.0f), 0.5f, mirror)); + + + + // Light + auto mainLight = std::make_shared(Vector3d(-10.0f, -0.5f, -1.0f), 2.0f, Color(1,1,1));//Color(1, 0.79f, 0.62f)); + scene.add(mainLight); + scene.add(std::make_shared(0.1f)); + + + // Render + SimpleRenderer rendererTest; + int width = 1920; + Texture imageSceneToTest = rendererTest.renderImage(scene, camera, width, width/16 * 9); + + // initialize renderer: aperture = lens thickness, secondaryRayCount = how many rays per pixel are created + // focalLength = the area which is in focus + // DOFRenderer renderer(0.2, 100, 70.0f); + + // Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times + // Texture image = renderer.renderImage(scene, camera, 1920, 1080); + + // Use post-processing Bloom effect + /* + Bloom bloomEffect = Bloom(image.getImage()); + Texture imageWithBloom = image; + imageWithBloom.setTexture(bloomEffect.bloom(0.55f, 5, 10.0f, 0.06f)); + */ + + // save images + // imageSceneToTest.save("result.png"); + // image.save("result.png"); + // image.save("resultWithBloom"); + + CImg image = imageSceneToTest.getImage(); + CImg img_8bit(image.width(), image.height(), 1, 3); + cimg_forXYC(image,x,y,c) { + img_8bit(x,y,c) = (unsigned char)std::round(image(x, y, c) * 255); + } + + CImgDisplay disp(img_8bit, "My Rendered Image",0, false, false); + while (!disp.is_closed()) { + disp.wait(); + disp.display(img_8bit); + if (disp.is_resized()) { + disp.resize(); + } + } return 0; -} \ No newline at end of file +} diff --git a/common/texture.cpp b/common/texture.cpp index 30a5238..a05be16 100644 --- a/common/texture.cpp +++ b/common/texture.cpp @@ -85,7 +85,7 @@ Color Texture::color(Vector2d const &surfacePosition, bool interpolate) const { return color(surfacePosition.u, surfacePosition.v, interpolate); } -CImg Texture::getImage() { +CImg& Texture::getImage() { return image_; } diff --git a/common/texture.h b/common/texture.h index 4eb21c6..3bbfa81 100644 --- a/common/texture.h +++ b/common/texture.h @@ -33,7 +33,7 @@ public: Color color(float u, float v, bool interpolate = true) const; Color color(Vector2d const &surfacePosition, bool interpolate = true) const; - CImg getImage(); + CImg& getImage(); private: CImg image_; diff --git a/homeworkMains/ex2.cpp b/homeworkMains/ex2.cpp index 53c46c7..236cf09 100644 --- a/homeworkMains/ex2.cpp +++ b/homeworkMains/ex2.cpp @@ -18,7 +18,7 @@ int main() { // Let's create a simple cornell box scene... SimpleScene scene; - scene.setEnvironmentMap(std::make_shared("data/lion_env.png")); + scene.setEnvironmentMap(std::make_shared("../data/lion_env.png")); auto mirror = std::make_shared(); auto glass = std::make_shared(1.31f, 1.0f); diff --git a/homeworkMains/ex4.cpp b/homeworkMains/ex4.cpp index 81ef935..75ac425 100644 --- a/homeworkMains/ex4.cpp +++ b/homeworkMains/ex4.cpp @@ -44,8 +44,6 @@ int main() { auto blueMetallic = std::make_shared("../data/blue-metallic-paint.binary", Color(7.0f, 7.0f, 7.0f)); auto darkRed = std::make_shared("../data/dark-red-paint.binary", Color(7.0f, 7.0f, 7.0f)); - // DOF Shader - bool dofShader = true; // Set up the walls diff --git a/renderer/depthoffieldrenderer.cpp b/renderer/depthoffieldrenderer.cpp index aba7d57..964bce8 100644 --- a/renderer/depthoffieldrenderer.cpp +++ b/renderer/depthoffieldrenderer.cpp @@ -3,7 +3,6 @@ #include #include "depthoffieldrenderer.h" #include -#include "post_processing/bloom.h" DOFRenderer::DOFRenderer(float _aperture, int _secondaryRayCount, float _focalLength) : aperture(_aperture), @@ -112,13 +111,6 @@ Texture DOFRenderer::renderImage(Scene const &scene, Camera const &camera, int w std::cout << "Paths: " << rays << std::endl; std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl; - // Post-processing - // Bloom shader - - image.save("original.png"); - - Bloom bloomEffect = Bloom(image.getImage()); - image.setTexture(bloomEffect.bloom(0.55f, 5, 10.0f, 0.06f)); return image; } \ No newline at end of file diff --git a/shader/brdfshader.cpp b/shader/brdfshader.cpp index 07f054d..cf61cf0 100644 --- a/shader/brdfshader.cpp +++ b/shader/brdfshader.cpp @@ -8,42 +8,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 { - // Calculate theta and phi - float thetaIn = std::acos(dotProduct(-ray.normal, ray.direction)); - float phiIn = 0.0f; + // Calculate theta and phi + float thetaIn = std::acos(dotProduct(-ray.normal, ray.direction)); + float phiIn = 0.0f; - // Derive local coordinate system - Vector3d const x = crossProduct(-ray.direction, ray.normal); - Vector3d const y = crossProduct(ray.normal, x); + // 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); + // 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; + // 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); + // 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)); + // 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)); - } + 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; + // Calculate colors + Color const diffuseColor = scale * color * cosine; + illuminationColor += diffuseColor * illum.color; + } } - } - return illuminationColor; + return illuminationColor; }