diff --git a/CMakeLists.txt b/CMakeLists.txt index d083bf5..44aa2c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,9 @@ endif() add_executable(fancy1 fancy1.cpp) target_link_libraries(fancy1 tracey) +add_executable(bloom bloom.cpp) +target_link_libraries(bloom tracey) + add_executable(DOFScene DOFScene.cpp) target_link_libraries(DOFScene tracey) diff --git a/DOFScene.cpp b/DOFScene.cpp index dff3683..35df12e 100644 --- a/DOFScene.cpp +++ b/DOFScene.cpp @@ -90,10 +90,10 @@ int main() { // 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); + DOFRenderer renderer(0.2, 50, 10.0f); // Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times - int width = 1920; + int width = 600; Texture image = renderer.renderImage(scene, camera, width, width / 16 * 9); // Use post-processing Bloom effect diff --git a/bloom.cpp b/bloom.cpp new file mode 100644 index 0000000..348cf04 --- /dev/null +++ b/bloom.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#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/sphere.h" +#include "primitive/triangle.h" + + +int main() { + SimpleScene scene; + + scene.setBackgroundColor(Color(0, 0, 0)); + + // Add shaders + auto red = std::make_shared(Color(1.0f, 0.3f, 0.2f)); + auto gray = std::make_shared(Color(0.78f, 0.78f, 0.78f)); + 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); + + + scene.add(std::make_shared(Vector3d(5.0f, -5.0f, 0.0f), Vector3d(4.0f, 4.0f, 4.0f), red)); + scene.add(std::make_shared(Vector3d(-5.0f, -3.0f, 1.0f), Vector3d(1.0f, 6.0f, 1.0f), blue)); + scene.add(std::make_shared(Vector3d(-3.5f, 4.0f, -2.0f), Vector3d(2.0f, 2.0f, 2.0f), orange)); + scene.add(std::make_shared(Vector3d(2.0f, 4.0f, 0.0f), 1.0f, orange)); + + scene.add(std::make_shared(Vector3d(0.0f, 0.0f, -11.0f), 100.0f)); + + + + // 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)); + + // Render the scene + SimpleRenderer renderer; + Texture img = renderer.renderImage(scene, camera, 512, 512); + + img.save("beforeBloom.png"); + + Bloom bloom = Bloom(img.getImage(), 0.88f, img); + img.setTexture(bloom.bloom(100, 300.0f, 1.5f)); + + img.save("afterBloom.png"); + + return 0; +} diff --git a/homeworkMains/ex4.cpp b/homeworkMains/ex4.cpp index 75ac425..efd9d8c 100644 --- a/homeworkMains/ex4.cpp +++ b/homeworkMains/ex4.cpp @@ -86,14 +86,10 @@ int main() { // Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times Texture image = renderer.renderImage(scene, camera, 1024, 1024); - // 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"); - image.save("resultWithBloom"); return 0; } diff --git a/post_processing/bloom.cpp b/post_processing/bloom.cpp index 93291fe..02f3ff3 100644 --- a/post_processing/bloom.cpp +++ b/post_processing/bloom.cpp @@ -2,36 +2,27 @@ #include #include "bloom.h" -Bloom::Bloom(CImg image) : image(image) {} +Bloom::Bloom(CImg &image, float threshold, Texture& thresholdImg) : image(image), threshold(threshold) {} -CImg Bloom::bloom(float threshold, int kernelSize, float sigma, float intensity) { - // Apply threshold to image - //CImg brightPixels = image_.get_threshold(threshold); - //brightPixels.save("brightpixels.png"); +CImg Bloom::bloom(int kernelSize, float sigma, float intensity) { + + CImg brightPixels = image.get_threshold(threshold); // Apply gaussian blur to bright pixels CImg kernel = computeGaussianKernel(kernelSize, sigma); CImg blurred = convolution(image, kernel); - for(int i = 0; i < 3; i++){ - kernel = computeGaussianKernel(kernelSize, sigma); - blurred = convolution(image, kernel); - blurred *= intensity; - } + blurred *= intensity; // Add blurred image back to original image cimg_forXYC(image, x, y, c) { - float value = image(x,y,0,c) + blurred(x,y,0,c); - image(x,y,0,c) = (value > 1.0f) ? 1.0f : value; - } + float value = image(x, y, 0, c) + blurred(x, y, 0, c); + image(x, y, 0, c) = (value > 1.0f) ? 1.0f : value; + } return image; } -void Bloom::gaussianBlur(int kernelSize, float sigma) { - CImg kernel = computeGaussianKernel(kernelSize, sigma); - image = convolution(image, kernel); -} // Function to compute Gaussian kernel CImg Bloom::computeGaussianKernel(int kernelSize, float sigma) { @@ -44,7 +35,7 @@ CImg Bloom::computeGaussianKernel(int kernelSize, float sigma) { for (i = 0; i < kernelSize; i++) { for (j = 0; j < kernelSize; j++) { kernel(i, j) = exp(-0.5f * (pow((i - kernelSize / 2.f) / sigma, 2.f) + - pow((j - kernelSize / 2.f) / sigma, 2.f))) / (2 * M_PI * sigma * sigma); + pow((j - kernelSize / 2.f) / sigma, 2.f))) / (2 * M_PI * sigma * sigma); sum += kernel(i, j); } } @@ -67,22 +58,18 @@ CImg Bloom::convolution(CImg &img, CImg &kernel) { // Perform convolution cimg_forXYC(img, i, j, c) { - sum = 0; - cimg_forY(kernel, m) { - cimg_forX(kernel, n) { - int x = i + n - kernelRadius; - int y = j + m - kernelRadius; - if(x >= 0 && x < imgCols && y >= 0 && y < imgRows){ - sum += img(x, y, 0, c) * kernel(n, m); - } + sum = 0; + cimg_forY(kernel, m) { + cimg_forX(kernel, n) { + int x = i + n - kernelRadius; + int y = j + m - kernelRadius; + if (x >= 0 && x < imgCols && y >= 0 && y < imgRows) { + sum += img(x, y, 0, c) * kernel(n, m); + } + } + } + result(i, j, 0, c) = sum; } - } - result(i, j, 0, c) = sum; - } return result; } - -void Bloom::scaleBrightness(float scale) { - image *= scale; -} diff --git a/post_processing/bloom.h b/post_processing/bloom.h index 694b0d8..2e0c961 100644 --- a/post_processing/bloom.h +++ b/post_processing/bloom.h @@ -8,19 +8,17 @@ class Bloom { public: - Bloom(CImg image); - CImg bloom(float threshold, int kernelSize, float sigma, float intensity); + Bloom(CImg& image, float threshold, Texture& thresholdImg); + CImg bloom(int kernelSize, float sigma, float intensity); private: - void scaleBrightness(float scale); - void gaussianBlur(int kernelSize, float sigma); - CImg convolution(CImg &img, CImg &kernel); CImg computeGaussianKernel(int kernelSize, float sigma); - - CImg image; + + float threshold; + };