From 31230ecad9c83e8dcd2f744b195be88d366a4967 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Fri, 13 Sep 2019 17:22:16 +0100 Subject: [PATCH] Texturing and commenting all code --- CMakeLists.txt | 7 ++++-- src/definitions/materials/texture.cpp | 20 ++++++++++-------- src/definitions/primatives/primative.hpp | 3 +++ src/denoise/denoise.cpp | 15 +++++++++++++ src/denoise/denoise.hpp | 4 +++- src/engine/progressiverenderer.hpp | 27 ++++++++++++++++++++++++ test/main.cpp | 11 +++++++--- 7 files changed, 72 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 406d134..360b18f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,15 +18,14 @@ if (UNIX) find_package(SDL2 REQUIRED) #find_package(SDL2_image REQUIRED) #find_package(PNG REQUIRED) - #find_package(JPEG REQUIRED) endif(UNIX) if (WIN32) include_directories(${executable} "C:/dev/glm" "C:/dev/SDL2/include/" - "C:/dev/oidn/include" #"C:/dev/SDL2_image/include/" + "C:/dev/oidn/include" ) endif (WIN32) @@ -44,6 +43,7 @@ if (UNIX) include_directories(${executable} ${SDL2_INCLUDE_DIR} #${SDL2_IMAGE_INCLUDE_DIR} + "/home/ben/dev/oidn/include/" ) endif (UNIX) @@ -82,6 +82,9 @@ if (UNIX) #PNG::PNG #JPEG::JPEG Threads::Threads + "/home/ben/dev/oidn/lib/libOpenImageDenoise.so" + "/home/ben/dev/oidn/lib/libtbb.so.2" + "/home/ben/dev/oidn/lib/libtbbmalloc.so.2" ) endif (UNIX) diff --git a/src/definitions/materials/texture.cpp b/src/definitions/materials/texture.cpp index 60cd15f..77da9d8 100644 --- a/src/definitions/materials/texture.cpp +++ b/src/definitions/materials/texture.cpp @@ -2,7 +2,7 @@ #define STB_IMAGE_IMPLEMENTATION #include "../../util/stb_image.hpp" -#include "../../util/stb_image_Write.hpp" +#include "../../util/stb_image_write.hpp" #include "../../common.hpp" @@ -15,21 +15,23 @@ Texture::Texture(std::string texturePath) { } void Texture::Load(std::string texturePath) { + struct P { + unsigned char r, g, b, a; + }; + Width = 0; Height = 0; int channels = 0; - uint32_t* imageData = (uint32_t*)stbi_load(texturePath.c_str(), &Width, &Height, &channels, 4); + + P* imageData = (P*)stbi_load(texturePath.c_str(), &Width, &Height, &channels, 4); Data = new glm::vec3[Width * Height]; for (int x = 0; x < Width; x++) for (int y = 0; y < Height; y++) { - uint32_t p = imageData[y * Width + x]; - uint8_t r = (uint8_t)(p); // 0x000000FF - uint8_t g = (uint8_t)(p >> 8); // 0x0000FF00 - uint8_t b = (uint8_t)(p >> 16); // 0x00FF0000 - Data[y * Width + x] = {(float)r / 255.0f, - (float)g / 255.0f, - (float)b / 255.0f }; + P p = imageData[y * Width + x]; + Data[y * Width + x] = {(float)p.r / 255.0f, + (float)p.g / 255.0f, + (float)p.b / 255.0f }; } } diff --git a/src/definitions/primatives/primative.hpp b/src/definitions/primatives/primative.hpp index 282f8d4..d215efc 100644 --- a/src/definitions/primatives/primative.hpp +++ b/src/definitions/primatives/primative.hpp @@ -55,6 +55,7 @@ public: virtual bool Intersect(Ray& ray, float& t) = 0; virtual glm::vec3 SurfaceNormal(glm::vec3 hitPoint) = 0; + virtual glm::vec3 SurfaceTangent(glm::vec3 normal) = 0; virtual glm::vec2 TexCoords(glm::vec3 hitPoint) = 0; virtual void Translate(glm::vec3 trans) = 0; @@ -65,6 +66,8 @@ public: return material->NormalTexture->SampleNormal(TexCoords(hitPoint)); // combine with surface normal + + } }; diff --git a/src/denoise/denoise.cpp b/src/denoise/denoise.cpp index 4cafe8b..cb68e9e 100644 --- a/src/denoise/denoise.cpp +++ b/src/denoise/denoise.cpp @@ -1,3 +1,18 @@ #include "denoise.hpp" +Denoiser::Denoiser() { + +} + +void Denoiser::SetAlbedo(glm::vec3* albedoBuffer) { + m_albedoBuffer = albedoBuffer; +} + +void Denoiser::SetNormals(glm::vec3* normalBuffer) { + m_normalBuffer = normalBuffer; +} + +void Denoiser::Denoise(glm::vec3* target, bool hdr) { + m_targetBuffer = target; m_hdr = hdr; +} diff --git a/src/denoise/denoise.hpp b/src/denoise/denoise.hpp index f7ab4f5..70bc1a0 100644 --- a/src/denoise/denoise.hpp +++ b/src/denoise/denoise.hpp @@ -5,14 +5,16 @@ class Denoiser { public: + Denoiser(); void SetAlbedo(glm::vec3* albedoBuffer); void SetNormals(glm::vec3* normalBuffer); void Denoise(glm::vec3* target, bool hdr); -private: + +private: bool m_hdr; glm::vec3* m_albedoBuffer; diff --git a/src/engine/progressiverenderer.hpp b/src/engine/progressiverenderer.hpp index d10d4b1..868120d 100644 --- a/src/engine/progressiverenderer.hpp +++ b/src/engine/progressiverenderer.hpp @@ -17,21 +17,41 @@ class Ray; class ProgressiveRenderer { public: + // Default constructor (unused) ProgressiveRenderer(); + // Initializes the renderer, takes the display interface + // that it will be rendering too, this manages windows and + // that and based on that it will decide if it is going + // to use imgui or not, and based on user preferance void Init(DisplayInterface* interface, Scene* scene); + // Takes program input and updates imgui if it is active + // can manipulate scene based on input but im going to + // give a way for the end user to do that as well as a + // much broader imgui interface void Input(); + + // Starts the rendering process. This function does not return + // until program end / window close and it calls input every frame void Render(); + // Indicates to the threadpool if the progressive renderer is ready + // and will tell them to start the render bool Ready = false; + + // Last 10 frame times to calculate average from std::vector FrameTimes = { }; + // All frame times since program start std::vector AllFrameTimes = { }; + // Average time of the last 10 frames for a more accurate fps float AverageFrameTime = 0.0f; + // If MXAA is enabled bool MXAA = true; public: + // Pointers to RenderThreadPool* m_threadPool = nullptr; DisplayInterface* m_interface = nullptr; RenderEngine* m_engine = nullptr; @@ -41,16 +61,23 @@ public: private: std::mutex m_mutex; + // Calculates the frametime and updates the frametime clocks void m_calculateTimes(std::chrono::high_resolution_clock::time_point& frameStartTime, std::chrono::high_resolution_clock::time_point& frameEndTime); + // ints that map directly to RenderMode and ToneMapMode + // and are used to set them, must be indexed correctly + // according to the enums int m_renderModeSelected = 0; int m_toneMapModeSelected = 0; + int m_framesRendererd = 0; + // Gamma to pass to the framebuffer float m_gamma = 2.3f; + // Image save path to pass to the framebuffer std::string m_imageSavePath = "image.png"; RenderMode m_mode = (RenderMode)m_renderModeSelected; diff --git a/test/main.cpp b/test/main.cpp index 72ecd46..f49799c 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -2,8 +2,8 @@ #include "../src/inferno.hpp" -static const int width = 1010; -static const int height = 1010; +static const int width = 600; +static const int height = 600; int main(int argc, char** argv) { InfernoEngine inferno; @@ -19,14 +19,19 @@ int main(int argc, char** argv) { 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 sphere({ 0.0f, 0.0f, -5.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 })); + sphere.material->NormalTexture = new Texture("/home/ben/programming/inferno/resources/textures/dirt-normal.jpg"); 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); 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.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(new Sphere({ 35.0f, 26.0f, 25.0f }, 15.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.0f)));