diff --git a/resources/box.mtl b/resources/box.mtl deleted file mode 100644 index 7fd9845..0000000 --- a/resources/box.mtl +++ /dev/null @@ -1,62 +0,0 @@ -# Blender MTL File: 'None' -# Material Count: 6 - -newmtl backWall -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.725000 0.710000 0.680000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.000000 -d 1.000000 -illum 1 - -newmtl ceiling -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.725000 0.710000 0.680000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.000000 -d 1.000000 -illum 1 - -newmtl floor -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.725000 0.710000 0.680000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.000000 -d 1.000000 -illum 1 - -newmtl leftWall -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.750000 0.250000 0.250000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.500000 -d 1.000000 -illum 1 - -newmtl light -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.780000 0.780000 0.780000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.000000 -d 1.000000 -illum 1 - -newmtl rightWall -Ns 9.803921 -Ka 1.000000 1.000000 1.000000 -Kd 0.250000 0.250000 0.750000 -Ks 0.000000 0.000000 0.000000 -Ke 0.0 0.0 0.0 -Ni 1.500000 -d 1.000000 -illum 1 diff --git a/src/common.hpp b/src/common.hpp index c55180e..fa28c34 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -17,15 +17,6 @@ enum OperationMode { MODE_OPERATION_PROGRESSIVE_GUI, MODE_OPERATION_PROGRESSIVE_IMG, MODE_OPERATION_SAMPLES_IMG, - MODE_OPERATION_DEFAULT -}; - -enum RenderMode { - MODE_RENDER_PATHTRACE, - MODE_RENDER_NORMALS, - MODE_RENDER_PATH_BOUNCES, - MODE_RENDER_PATH_LENGTH, - MODE_RENDER_DEFAULT }; enum AccelerationMode { @@ -33,7 +24,19 @@ enum AccelerationMode { MODE_ACCELERATION_KD, MODE_ACCELERATION_KD_SLOW, MODE_ACCELERATION_BVH, - MODE_ACCELERATION_DEFAULT }; +enum RenderMode { + MODE_RENDER_PATHTRACE, + MODE_RENDER_NORMALS, + MODE_RENDER_PATH_BOUNCES, + MODE_RENDER_PATH_LENGTH +}; + +enum ToneMapMode { + MODE_TONEMAP_BASIC, + MODE_TONEMAP_CLAMP +}; + + #endif diff --git a/src/display/framebuffer.cpp b/src/display/framebuffer.cpp index d7c311a..c23af7d 100644 --- a/src/display/framebuffer.cpp +++ b/src/display/framebuffer.cpp @@ -5,11 +5,6 @@ #include "../pixel.hpp" -FrameBuffer::FrameBuffer(int xres, int yres) { - XRes = xres; YRes = yres; - Data = (uint32_t*)malloc((xres * yres) * sizeof(uint32_t)); - memset((void*)Data, 0, (xres * yres) * sizeof(uint32_t)); -} void FrameBuffer::SetPixel(int x, int y, glm::vec3 p) { Pixel pixel{ (uint8_t)(pow(p.r, Gamma) * 255.0f), (uint8_t)(pow(p.g, Gamma) * 255.0f), (uint8_t)(pow(p.b, Gamma) * 255.0f) }; @@ -23,6 +18,13 @@ void FrameBuffer::SetPixelSafe(int x, int y, glm::vec3 p) { } } +void FrameBuffer::SetPixelSafe(int x, int y, glm::vec3 p) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + Pixel pixel{ (uint8_t)(pow(p.r, Gamma) * 255.0f), (uint8_t)(pow(p.g, Gamma) * 255.0f), (uint8_t)(pow(p.b, Gamma) * 255.0f) }; + Data[y * this->XRes + x] = pixel.rgb(); + } +} + void FrameBuffer::DumpToFile(std::string path) { // int stbi_write_png(char const* filename, int w, int h, int comp, const void* data, int stride_in_bytes); stbi_write_png(path.c_str(), this->XRes, this->YRes, sizeof(uint32_t), this->Data, sizeof(uint32_t) * this->XRes); @@ -40,3 +42,70 @@ void FrameBuffer::ClearFramebuffer() { FrameBuffer::~FrameBuffer() { free(Data); } + + + +FrameBuffer::FrameBuffer(int xres, int yres) { + XRes = xres; YRes = yres; + RenderData = (uint32_t*)malloc((xres * yres) * sizeof(uint32_t)); + memset((void*)RenderData, 0, (xres * yres) * sizeof(uint32_t)); + + RenderNormalsTarget = (glm::vec3*)malloc((xres * yres) * sizeof(glm::vec3)); + memset((void*)RenderNormalsTarget, 0, (xres * yres) * sizeof(glm::vec3)); + + RenderAlbedoTarget = (glm::vec3*)malloc((xres * yres) * sizeof(glm::vec3)); + memset((void*)RenderAlbedoTarget, 0, (xres * yres) * sizeof(glm::vec3)); + + RenderTarget = (glm::vec3*)malloc((xres * yres) * sizeof(glm::vec3)); + memset((void*)RenderTarget, 0, (xres * yres) * sizeof(glm::vec3)); + + RenderPostProcess = (glm::vec3*)malloc((xres * yres) * sizeof(glm::vec3)); + memset((void*)RenderPostProcess, 0, (xres * yres) * sizeof(glm::vec3)); +} + +void FrameBuffer::SetPixelSafeNormal(int x, int y, glm::vec3 p) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + RenderNormalsTarget[y * this->XRes + x] = p; + } +} + +void FrameBuffer::SetPixelSafeAlbedo(int x, int y, glm::vec3 p) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + RenderAlbedoTarget[y * this->XRes + x] = p; + } +} + +void FrameBuffer::SetPixelSafe(int x, int y, glm::vec3 p, int mode = 0) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); } + m_lastActiveFrameBufferMode = mode; + RenderTarget[y * this->XRes + x] = p; + } +} + +void FrameBuffer::AddPixelSafe(int x, int y, glm::vec3 p, int mode = 0) { + if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); } + m_lastActiveFrameBufferMode = mode; + RenderTarget[y * this->XRes + x] += p; + } +} + +// PostProcesses based on previous input, the tonemap mode and +// the sample count for additive frames to average +void PostProcess(int spp, ToneMapMode mode = MODE_TONEMAP_CLAMP); + +// Saves the RenderData to a file, data must first be processed +// by the render engine / the engine manager based on mode +void DumpToFile(std::string path); + +// Copys the active render data to the target framebuffer, +// relies on the framebuffers to be of equal size and not +// not that +void CopyData(uint32_t* dest); + +// Clears the RenderTarget and the RenderData +void ClearFramebuffer(); + +~FrameBuffer(); + diff --git a/src/display/framebuffer.hpp b/src/display/framebuffer.hpp index 55b2867..6ea0de8 100644 --- a/src/display/framebuffer.hpp +++ b/src/display/framebuffer.hpp @@ -12,19 +12,54 @@ class FrameBuffer { public: FrameBuffer(int xres, int yres); - void SetPixel(int x, int y, glm::vec3 p); - void SetPixelSafe(int x, int y, glm::vec3 p); + // Normals are expected to be mapped as -1 to 1 on the RGB + // channels, 0 to 1 is not acceptable. For maximum detail + // the accumilation of normal maps should be preserved + void SetPixelSafeNormal(int x, int y, glm::vec3 p); + // Albedo values are expected to be the basic colour of the + // first hit surface. Mapped between 0 and 1 for intensity on + // the RGB channels + void SetPixelSafeAlbedo(int x, int y, glm::vec3 p); + + // Sets main targeted render. Usually copied to from the thread + // pools internal buffers + void SetPixelSafe(int x, int y, glm::vec3 p, int mode = 0); + + // Add to the render target + void AddPixelSafe(int x, int y, glm::vec3 p, int mode = 0); + + // PostProcesses based on previous input, the tonemap mode and + // the sample count for additive frames to average + void PostProcess(int spp, ToneMapMode mode = MODE_TONEMAP_CLAMP); + + // Saves the RenderData to a file, data must first be processed + // by the render engine / the engine manager based on mode void DumpToFile(std::string path); - void SetFramebuffer(uint32_t* fb); + // Copys the active render data to the target framebuffer, + // relies on the framebuffers to be of equal size and not + // not that + void CopyData(uint32_t* dest); + + // Clears the RenderTarget, Post Processed target + // and the RenderData void ClearFramebuffer(); - uint32_t* Data; + + // Render targets + glm::vec3* RenderNormalsTarget; + glm::vec3* RenderAlbedoTarget; + glm::vec3* RenderTarget; + glm::vec3* RenderPostProcess; + + uint32_t* RenderData; int XRes, YRes; float Gamma = 1.0f / 2.3f; - - + ~FrameBuffer(); + +private: + int m_lastActiveFrameBufferMode; }; #endif diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 18c1ac7..4d82dec 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -42,10 +42,13 @@ void ProgressiveRenderer::Input() { const Uint8* state = SDL_GetKeyboardState(NULL); - //if (state[SDL_SCANCODE_W]) m_scene->objects[0]->center.y += 0.01f; - //if (state[SDL_SCANCODE_S]) m_scene->objects[0]->center.y -= 0.01f; - //if (state[SDL_SCANCODE_D]) m_scene->objects[0]->center.x += 0.01f; - //if (state[SDL_SCANCODE_A]) m_scene->objects[0]->center.x -= 0.01f; + //if (state[SDL_SCANCODE_W]) m_scene->objects[1]->center.y += 0.001f; + //if (state[SDL_SCANCODE_S]) m_scene->objects[1]->center.y -= 0.001f; + //if (state[SDL_SCANCODE_D]) m_scene->objects[1]->center.x += 0.001f; + //if (state[SDL_SCANCODE_A]) m_scene->objects[1]->center.x -= 0.001f; + //if (state[SDL_SCANCODE_R]) m_scene->objects[1]->center.z += 0.001f; + //if (state[SDL_SCANCODE_F]) m_scene->objects[1]->center.z -= 0.001f; + //std::cout << m_scene->objects[1]->center.x << " " << m_scene->objects[1]->center.y << " " << m_scene->objects[1]->center.z << std::endl; if (!m_interface->ImGui) return; diff --git a/src/util/threadpool.cpp b/src/util/threadpool.cpp index 3b230d9..15f4230 100644 --- a/src/util/threadpool.cpp +++ b/src/util/threadpool.cpp @@ -18,7 +18,6 @@ RenderThreadPool::RenderThreadPool() { }; void RenderThreadPool::SetJobs(ProgressiveRenderer* renderer, int w, int h) { - MappedThreadFrameBuffer = new ToneMapFrameBuffer(w, h); ThreadFrameBuffer = new FrameBuffer(w, h); for (int i = 0; i < ThreadCount; i++) { if (i == ThreadCount - 1) { diff --git a/src/util/threadpool.hpp b/src/util/threadpool.hpp index 8bd62c6..77a92e2 100644 --- a/src/util/threadpool.hpp +++ b/src/util/threadpool.hpp @@ -32,7 +32,6 @@ public: void MergeBuffers(uint32_t* framebuffer, int w, int h); // std::vector> RenderRegions; // offest, size - ToneMapFrameBuffer* MappedThreadFrameBuffer; FrameBuffer* ThreadFrameBuffer; }; diff --git a/test/main.cpp b/test/main.cpp index 89e3467..6577ad7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,13 +17,10 @@ int main(int argc, char** argv) { Scene* scene = new Scene(width, height); scene->camera = new Camera(width, height); - - // scene->objects.push_back(new Sphere({ 0.0f, 0.0f, -8.0f }, 1.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.7f))); - // scene->objects.push_back(new Sphere({ 2.0f, 0.0f, -6.0f }, 1.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f))); - // scene->objects.push_back(new Sphere({ -2.0f, 0.0f, -9.0f }, 1.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f))); - scene->objects.push_back(new Sphere({ 25.0f, 26.0f, 25.0f }, 15.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.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.3f))); - // scene->objects.push_back(new Plane( { 0.0f, 10.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, new Material({ 1.0f, 0.9f, 0.9f }, 0.0f, 1.0f))); + + 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))); 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"); @@ -36,11 +33,11 @@ int main(int argc, char** argv) { //mesh->Optimise(); //scene->meshs.push_back(mesh); - //std::vector tris = LoadTrianglesBasic("E://Projects//Inferno//resources//cornell-box.obj", "E://Projects//Inferno//resources"); - // std::vector tris = LoadTrianglesBasic("//home//ben//programming//inferno//resources//cornell.obj", "//home//ben//programming//inferno//resources//resources"); + //std::vector tris = LoadTrianglesBasic("E://Projects//Inferno//resources//box.obj", "E://Projects//Inferno//resources"); + //std::vector tris = LoadTrianglesBasic("//home//ben//programming//inferno//resources//cornell.obj", "//home//ben//programming//inferno//resources//resources"); Mesh* mesh1 = new Mesh(tris); - mesh1->Translate({ 0.0f, -1.0f, -3.8f }); + mesh1->Translate({ 0.2f, -1.04, -3.8f }); mesh1->Optimise(); scene->meshs.push_back(mesh1);