Merge remote-tracking branch 'origin/master'

This commit is contained in:
Maximilian Giller 2023-01-29 21:50:08 +01:00
commit 159e9ca843
6 changed files with 91 additions and 47 deletions

View file

@ -84,6 +84,9 @@ endif()
add_executable(fancy1 fancy1.cpp) add_executable(fancy1 fancy1.cpp)
target_link_libraries(fancy1 tracey) target_link_libraries(fancy1 tracey)
add_executable(bloom bloom.cpp)
target_link_libraries(bloom tracey)
add_executable(DOFScene DOFScene.cpp) add_executable(DOFScene DOFScene.cpp)
target_link_libraries(DOFScene tracey) target_link_libraries(DOFScene tracey)

View file

@ -90,10 +90,10 @@ int main() {
// initialize renderer: aperture = lens thickness, secondaryRayCount = how many rays per pixel are created // initialize renderer: aperture = lens thickness, secondaryRayCount = how many rays per pixel are created
// focalLength = the area which is in focus // 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 // 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); Texture image = renderer.renderImage(scene, camera, width, width / 16 * 9);
// Use post-processing Bloom effect // Use post-processing Bloom effect

60
bloom.cpp Normal file
View file

@ -0,0 +1,60 @@
#include <iostream>
#include <string>
#include <post_processing/bloom.h>
#include <shader/phongshader.h>
#include <shader/simpleshadowshader.h>
#include <light/pointlight.h>
#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<SimpleShadowShader>(Color(1.0f, 0.3f, 0.2f));
auto gray = std::make_shared<SimpleShadowShader>(Color(0.78f, 0.78f, 0.78f));
auto blue = std::make_shared<SimpleShadowShader>(Color(0.2f, 0.3f, 1.0f));
auto orange = std::make_shared<PhongShader>(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<Box>(Vector3d(5.0f, -5.0f, 0.0f), Vector3d(4.0f, 4.0f, 4.0f), red));
scene.add(std::make_shared<Box>(Vector3d(-5.0f, -3.0f, 1.0f), Vector3d(1.0f, 6.0f, 1.0f), blue));
scene.add(std::make_shared<Box>(Vector3d(-3.5f, 4.0f, -2.0f), Vector3d(2.0f, 2.0f, 2.0f), orange));
scene.add(std::make_shared<Sphere>(Vector3d(2.0f, 4.0f, 0.0f), 1.0f, orange));
scene.add(std::make_shared<PointLight>(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;
}

View file

@ -86,14 +86,10 @@ int main() {
// Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times // Use DOFRenderer to raytrace !!! careful more pixels lead to insane rendering times
Texture image = renderer.renderImage(scene, camera, 1024, 1024); 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 // save images
image.save("result.png"); image.save("result.png");
image.save("resultWithBloom");
return 0; return 0;
} }

View file

@ -2,36 +2,27 @@
#include <iostream> #include <iostream>
#include "bloom.h" #include "bloom.h"
Bloom::Bloom(CImg<float> image) : image(image) {} Bloom::Bloom(CImg<float> &image, float threshold, Texture& thresholdImg) : image(image), threshold(threshold) {}
CImg<float> Bloom::bloom(float threshold, int kernelSize, float sigma, float intensity) { CImg<float> Bloom::bloom(int kernelSize, float sigma, float intensity) {
// Apply threshold to image
//CImg<float> brightPixels = image_.get_threshold(threshold); CImg<float> brightPixels = image.get_threshold(threshold);
//brightPixels.save("brightpixels.png");
// Apply gaussian blur to bright pixels // Apply gaussian blur to bright pixels
CImg<float> kernel = computeGaussianKernel(kernelSize, sigma); CImg<float> kernel = computeGaussianKernel(kernelSize, sigma);
CImg<float> blurred = convolution(image, kernel); CImg<float> blurred = convolution(image, kernel);
for(int i = 0; i < 3; i++){ blurred *= intensity;
kernel = computeGaussianKernel(kernelSize, sigma);
blurred = convolution(image, kernel);
blurred *= intensity;
}
// Add blurred image back to original image // Add blurred image back to original image
cimg_forXYC(image, x, y, c) { cimg_forXYC(image, x, y, c) {
float value = image(x,y,0,c) + blurred(x,y,0,c); float value = image(x, y, 0, c) + blurred(x, y, 0, c);
image(x,y,0,c) = (value > 1.0f) ? 1.0f : value; image(x, y, 0, c) = (value > 1.0f) ? 1.0f : value;
} }
return image; return image;
} }
void Bloom::gaussianBlur(int kernelSize, float sigma) {
CImg<float> kernel = computeGaussianKernel(kernelSize, sigma);
image = convolution(image, kernel);
}
// Function to compute Gaussian kernel // Function to compute Gaussian kernel
CImg<float> Bloom::computeGaussianKernel(int kernelSize, float sigma) { CImg<float> Bloom::computeGaussianKernel(int kernelSize, float sigma) {
@ -44,7 +35,7 @@ CImg<float> Bloom::computeGaussianKernel(int kernelSize, float sigma) {
for (i = 0; i < kernelSize; i++) { for (i = 0; i < kernelSize; i++) {
for (j = 0; j < kernelSize; j++) { for (j = 0; j < kernelSize; j++) {
kernel(i, j) = exp(-0.5f * (pow((i - kernelSize / 2.f) / sigma, 2.f) + 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); sum += kernel(i, j);
} }
} }
@ -67,22 +58,18 @@ CImg<float> Bloom::convolution(CImg<float> &img, CImg<float> &kernel) {
// Perform convolution // Perform convolution
cimg_forXYC(img, i, j, c) { cimg_forXYC(img, i, j, c) {
sum = 0; sum = 0;
cimg_forY(kernel, m) { cimg_forY(kernel, m) {
cimg_forX(kernel, n) { cimg_forX(kernel, n) {
int x = i + n - kernelRadius; int x = i + n - kernelRadius;
int y = j + m - kernelRadius; int y = j + m - kernelRadius;
if(x >= 0 && x < imgCols && y >= 0 && y < imgRows){ if (x >= 0 && x < imgCols && y >= 0 && y < imgRows) {
sum += img(x, y, 0, c) * kernel(n, m); sum += img(x, y, 0, c) * kernel(n, m);
} }
}
}
result(i, j, 0, c) = sum;
} }
}
result(i, j, 0, c) = sum;
}
return result; return result;
} }
void Bloom::scaleBrightness(float scale) {
image *= scale;
}

View file

@ -8,19 +8,17 @@
class Bloom { class Bloom {
public: public:
Bloom(CImg<float> image); Bloom(CImg<float>& image, float threshold, Texture& thresholdImg);
CImg<float> bloom(float threshold, int kernelSize, float sigma, float intensity); CImg<float> bloom(int kernelSize, float sigma, float intensity);
private: private:
void scaleBrightness(float scale);
void gaussianBlur(int kernelSize, float sigma);
CImg<float> convolution(CImg<float> &img, CImg<float> &kernel); CImg<float> convolution(CImg<float> &img, CImg<float> &kernel);
CImg<float> computeGaussianKernel(int kernelSize, float sigma); CImg<float> computeGaussianKernel(int kernelSize, float sigma);
CImg<float> image; CImg<float> image;
float threshold;
}; };