Tonemapping, gotta add colour grading

This commit is contained in:
Ben
2019-09-15 22:09:15 +01:00
parent c14ac13292
commit 8ba434e906
10 changed files with 129 additions and 53 deletions

View File

@@ -35,24 +35,24 @@ vn 0.9999 0.0125 0.0022
vn 0.9998 0.0194 0.0055
vn 1.0000 0.0080 0.0000
vn 0.9997 0.0239 0.0077
usemtl rightWall
s 1
f 2//1 4//1 1//1
f 2//1 3//1 4//1
usemtl light
f 6//2 8//2 5//2
f 6//2 7//2 8//2
#usemtl rightWall
#s 1
#f 2//1 4//1 1//1
#f 2//1 3//1 4//1
#usemtl light
#f 6//2 8//2 5//2
#f 6//2 7//2 8//2
usemtl floor
f 10//3 12//3 9//3
f 9//4 16//4 10//4
#f 9//4 16//4 10//4
f 10//3 11//3 12//3
f 9//4 13//4 16//4
usemtl ceiling
f 14//2 16//2 13//2
f 14//2 15//2 16//2
usemtl backWall
f 17//5 19//5 20//5
f 17//5 18//5 19//5
usemtl leftWall
f 21//6 23//7 24//8
f 21//6 22//9 23//7
#usemtl ceiling
#f 14//2 16//2 13//2
#f 14//2 15//2 16//2
#usemtl backWall
#f 17//5 19//5 20//5
#f 17//5 18//5 19//5
#usemtl leftWall
#f 21//6 23//7 24//8
#f 21//6 22//9 23//7

View File

@@ -84,8 +84,8 @@ s 1
f 13//1 14//1 15//1 16//1
usemtl light
f 9//2 10//2 11//2 12//2
usemtl floor
f 5//3 6//3 7//3 8//3
semtl floor
5//3 6//3 7//3 8//3
usemtl ceiling
f 17//2 18//2 19//2 20//2
usemtl backWall

View File

@@ -35,6 +35,8 @@ enum RenderMode {
enum ToneMapMode {
MODE_TONEMAP_CLAMP,
MODE_TONEMAP_REINHARD,
MODE_TONEMAP_EXP,
MODE_TONEMAP_BASIC
};

View File

@@ -2,12 +2,30 @@
#include "ray.hpp"
SolidSky::SolidSky(glm::vec3 col, float intensity) {
m_colour = col; m_intensity = intensity;
}
glm::vec3 SolidSky::Sample(Ray& ray) {
return m_colour * m_intensity;
}
GradientSky::GradientSky(glm::vec3 up, glm::vec3 horizon, float intensity) {
m_up = up; m_horizon = horizon; m_intensity = intensity;
}
glm::vec3 GradientSky::Sample(Ray& ray) {
float fraction = abs(ray.direction.y) * 2.0f + 0.16f;
fraction = clamp(fraction, 0.0f, 1.0f);
return ((m_up - m_horizon) * fraction + m_horizon) * m_intensity;
}
Scene::Scene(int width, int height) {
w = width;
h = height;
}
glm::vec3 Scene::SampleSky(Ray ray) {
// return { 0.9f, 0.9f, 0.9f };
return { 0.0f, 0.0f, 0.0f };
glm::vec3 Scene::SampleSky(Ray& ray) {
return sky->Sample(ray);
}

View File

@@ -10,16 +10,47 @@ class Camera;
class Mesh;
class Ray;
enum SkyType {
//TYPE_
};
class Sky {
public:
virtual glm::vec3 Sample(Ray& ray) = 0;
};
class SolidSky : public Sky {
public:
SolidSky(glm::vec3 col, float intensity = 1.0f);
glm::vec3 Sample(Ray& ray) override;
private:
glm::vec3 m_colour;
float m_intensity;
};
class GradientSky : public Sky {
public:
GradientSky(glm::vec3 up, glm::vec3 horizon, float intensity = 1.0f);
glm::vec3 Sample(Ray& ray) override;
private:
glm::vec3 m_up;
glm::vec3 m_horizon;
float m_intensity;
};
class Scene {
public:
Scene(int width, int height);
int w, h;
Camera* camera;
float gamma;
std::vector<Primative*> objects;
Camera* camera;
std::vector<Mesh*> meshs;
std::vector<Primative*> objects;
glm::vec3 SampleSky(Ray ray);
Sky* sky;
glm::vec3 SampleSky(Ray& ray);
};
#endif

View File

@@ -62,6 +62,7 @@ void FrameBuffer::PostProcess(int& spp, ToneMapMode mode, RenderMode rendermode)
void FrameBuffer::PostProcess(ToneMapMode mode) {
memset((void*)m_swapBuffer, 0, (XRes * YRes) * sizeof(glm::vec3));
// reinhard
if (mode == MODE_TONEMAP_BASIC) {
float max = 0.0f;
@@ -85,6 +86,20 @@ void FrameBuffer::PostProcess(ToneMapMode mode) {
m_swapBuffer[y * this->XRes + x] = Clamp(RenderPostProcess[y * this->XRes + x], 1.0f, 0.0f);
}
} else if (mode == MODE_TONEMAP_REINHARD) {
for (int x = 0; x < XRes; x++)
for (int y = 0; y < YRes; y++) {
m_swapBuffer[y * this->XRes + x] = RenderPostProcess[y * this->XRes + x] / (RenderPostProcess[y * this->XRes + x] + 1.0f);
}
} else if (mode == MODE_TONEMAP_EXP) {
for (int x = 0; x < XRes; x++)
for (int y = 0; y < YRes; y++) {
m_swapBuffer[y * this->XRes + x] = 1.0f - exp(RenderPostProcess[y * this->XRes + x] * 1.0f);
}
} else {
for (int x = 0; x < XRes; x++)
for (int y = 0; y < YRes; y++) {

View File

@@ -84,7 +84,7 @@ void ProgressiveRenderer::Input() {
ImGui::Combo("Render Mode", &m_renderModeSelected, renderItems, IM_ARRAYSIZE(renderItems));
m_mode = (RenderMode)m_renderModeSelected;
const char* toneMapItems[] = { "Clamp", "Basic Tonemap" };
const char* toneMapItems[] = { "Clamp", "Reinhard Tonamap", "Exponential Tonemap", "Basic Tonemap" };
ImGui::Combo("ToneMap Mode", &m_toneMapModeSelected, toneMapItems, IM_ARRAYSIZE(toneMapItems));
ImGui::SliderFloat("Gamma", &m_gamma, 1.0f, 4.0f);

View File

@@ -77,7 +77,12 @@ glm::vec3 RenderEngine::GetColour(Ray ray, int& depth) {
float t = INFINITY; Primative* hit = nullptr;
bool didhit = TraceRayScene(ray, m_scene, t, hit);
if (!didhit) return m_scene->SampleSky(ray);
if (!didhit) {
if (depth > 0) {
return m_scene->SampleSky(ray) * 0.2f;
}
return m_scene->SampleSky(ray);
}
glm::vec3 hitPoint = ray.origin + ray.direction * t;
glm::vec3 normal = hit->Normal(hitPoint);

View File

@@ -70,6 +70,7 @@ std::vector<Triangle*> LoadTrianglesBasic(std::string path, std::string basePath
// Material* mat = new Material({ material.diffuse[0], material.diffuse[1], material.diffuse[2] }, 0.6f, material.illum);
Material* mat = new Material({ 0.717f, 0.792f, 0.474 }, 0.5f);
//Material* mat = new Material({ 0.8, 0.8f, 0.8f });
// glm::vec3 normal = getNormal(
// {avx[0], avy[0], avz[0]},

View File

@@ -6,47 +6,51 @@ static const int width = 1000;
static const int height = 1000;
int main(int argc, char** argv) {
InfernoEngine inferno;
InfernoEngine inferno;
inferno.SetMode(MODE_OPERATION_PROGRESSIVE_GUI);
inferno.SetMode(MODE_OPERATION_PROGRESSIVE_GUI);
bool status = inferno.InitWindow(width, height);
bool status = inferno.InitWindow(width, height);
if (!status) {
std::cout << "Error initializing window: " << inferno.LastError() << std::endl;
}
if (!status) {
std::cout << "Error initializing window: " << inferno.LastError() << std::endl;
}
Scene* scene = new Scene(width, height);
Sky* sky = new GradientSky({ 35.0f / 255.0f, 148.0f / 255.0f, 235.0f / 255.0f }, { 1.0f, 1.0f, 1.0f }, 5.0f);
scene->sky = sky;
Scene* scene = new Scene(width, height);
scene->camera = new Camera(width, height);
Sphere sphere1({ 1.3f, -0.8f, -5.0f }, 0.2f, new Material({ 0.817f, 0.374, 0.574 }));
scene->objects.push_back(&sphere1);
//Sphere sphere1({ 1.3f, -0.8f, -5.0f }, 0.2f, new Material({ 0.817f, 0.374, 0.574 }));
//scene->objects.push_back(&sphere1);
Sphere sphere({ 0.0f, 0.0f, -5.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }));
sphere.material->NormalTexture = new Texture("E:/Projects/Inferno/resources/textures/dirt-normal.jpg");
//Sphere sphere({ 0.0f, 0.0f, -5.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }));
// sphere.material->NormalTexture = new Texture("E:/Projects/Inferno/resources/textures/dirt-normal.jpg");
// sphere.material->NormalTexture = new Texture("/home/ben/programming/inferno/resources/textures/dirt-normal.jpg");
scene->objects.push_back(&sphere);
//scene->objects.push_back(&sphere);
Sphere light({ 35.0f, 56.0f, 25.0f }, 35.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.0f));
scene->objects.push_back(&light);
//Sphere light({ 35.0f, 50.0f, 25.0f }, 25.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 200.0f));
//scene->objects.push_back(&light);
Plane plane({ 0.0f,-1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.9f, 0.9f, 0.9f }, 0.2f));
plane.material->NormalTexture = new Texture("E:/Projects/Inferno/resources/textures/normals.png");
//Plane plane({ 0.0f,-1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.2f, 0.2f, 0.2f }));
// plane.material->NormalTexture = new Texture("E:/Projects/Inferno/resources/textures/normals.png");
// plane.material->NormalTexture = new Texture("/home/ben/programming/inferno/resources/textures/normals.png");
scene->objects.push_back(&plane);
//scene->objects.push_back(&plane);
//scene->objects.push_back(new Sphere({ 35.0f, 26.0f, 25.0f }, 15.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.0f)));
//scene->objects.push_back(new Sphere({-0.457001f, 0.19f, -3.53899f}, 0.02f, new Material({ 1.0f, 0.9f, 0.8f }, 0.0f, 500.0f)));
//scene->objects.push_back(new Plane( { 0.0f, -1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.847f, 0.792f, 0.658f }, 0.2f)));
scene->objects.push_back(new Sphere({ 35.0f, 26.0f, 25.0f }, 15.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 50.0f)));
scene->objects.push_back(new Sphere({-0.457001f, 0.19f, -3.53899f}, 0.02f, new Material({ 1.0f, 0.9f, 0.8f }, 0.0f, 5000.0f)));
scene->objects.push_back(new Plane( { 0.0f, -1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.847f, 0.792f, 0.658f }, 0.2f)));
//std::vector<Triangle*> tris = LoadTrianglesBasic("E://Projects//Inferno//resources//dragon-normals.obj", "E://Projects//Inferno//resources");
//std::vector<Triangle*> tris = LoadTrianglesBasic("//home//ben//programming//inferno//resources//dragon-normals.obj", "//home//ben//programming//inferno//resources//resources");
std::vector<Triangle*> tris = LoadTrianglesBasic("E://Projects//Inferno//resources//models//dragon-normals.obj", "E://Projects//Inferno//resources//models");
//Mesh* mesh1 = new Mesh(tris);
//mesh1->Translate({ 0.2f, -1.04, -3.8f });
//mesh1->Optimise();
//scene->meshs.push_back(mesh1);
Mesh* mesh = new Mesh(tris);
mesh->Translate({ 0.2f, -1.04, -3.8f });
//mesh->Translate({ 0.0f, -1.0, -4.0f });
mesh->Optimise();
scene->meshs.push_back(mesh);
inferno.SetScene(scene);
inferno.Ready();