diff --git a/resources/models/box.obj b/resources/models/box.obj index 5cec9fa..64385ad 100644 --- a/resources/models/box.obj +++ b/resources/models/box.obj @@ -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 diff --git a/resources/models/cornell.obj b/resources/models/cornell.obj index beb88af..55802fc 100644 --- a/resources/models/cornell.obj +++ b/resources/models/cornell.obj @@ -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 diff --git a/src/common.hpp b/src/common.hpp index fe05cb8..9bc81d0 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -35,6 +35,8 @@ enum RenderMode { enum ToneMapMode { MODE_TONEMAP_CLAMP, + MODE_TONEMAP_REINHARD, + MODE_TONEMAP_EXP, MODE_TONEMAP_BASIC }; diff --git a/src/definitions/scene.cpp b/src/definitions/scene.cpp index 0c8eb47..09f7e2f 100644 --- a/src/definitions/scene.cpp +++ b/src/definitions/scene.cpp @@ -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); } diff --git a/src/definitions/scene.hpp b/src/definitions/scene.hpp index 7f4e8fb..cbb9c31 100644 --- a/src/definitions/scene.hpp +++ b/src/definitions/scene.hpp @@ -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 objects; + Camera* camera; std::vector meshs; + std::vector objects; - glm::vec3 SampleSky(Ray ray); + Sky* sky; + glm::vec3 SampleSky(Ray& ray); }; #endif diff --git a/src/display/framebuffer.cpp b/src/display/framebuffer.cpp index e345d86..92fb0b3 100644 --- a/src/display/framebuffer.cpp +++ b/src/display/framebuffer.cpp @@ -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++) { diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 46db810..16dfdd5 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -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); diff --git a/src/engine/renderengine.cpp b/src/engine/renderengine.cpp index 2c0979e..04d3164 100644 --- a/src/engine/renderengine.cpp +++ b/src/engine/renderengine.cpp @@ -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); diff --git a/src/util/assetloader.cpp b/src/util/assetloader.cpp index 48e22a1..5d065cc 100644 --- a/src/util/assetloader.cpp +++ b/src/util/assetloader.cpp @@ -70,6 +70,7 @@ std::vector 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]}, diff --git a/test/main.cpp b/test/main.cpp index 17e1265..eab9584 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -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 tris = LoadTrianglesBasic("E://Projects//Inferno//resources//dragon-normals.obj", "E://Projects//Inferno//resources"); //std::vector tris = LoadTrianglesBasic("//home//ben//programming//inferno//resources//dragon-normals.obj", "//home//ben//programming//inferno//resources//resources"); + std::vector 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();