coming together

This commit is contained in:
Ben
2019-08-22 00:28:52 +01:00
parent 81d8759cf6
commit eae59ccf9b
7 changed files with 89 additions and 32 deletions

View File

@@ -13,8 +13,10 @@ Mesh::Mesh(std::vector<Triangle*> tris) {
void Mesh::Optimise(AccelerationMode mode) {
m_accelerator = new Acceleration(mode);
m_accelerator->Construct(Triangles);
if (m_accelerator->Constructed) Optimised = true;
if (m_accelerator->Constructed) Optimised = true;
}
bool Mesh::Intersect(Ray ray, Triangle*& intersect, float& t) {

View File

@@ -2,6 +2,7 @@
#include <iostream>
#include "../common.hpp"
#include "../pixel.hpp"
Display::Display()
@@ -70,33 +71,49 @@ bool Display::InitImGui() {
m_imguiTexture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 100, 100);
if (!m_imguiTexture) return false;
m_imGui = true;
ImGui = true;
return true;
}
void Display::SetPixel(int x, int y, Pixel p) {
m_framebufferMutex.lock();
Framebuffer[y * this->XRes + x] = p.rgb();
m_framebufferMutex.unlock();
}
void Display::SetPixel(int x, int y, uint32_t p) {
m_framebufferMutex.lock();
Framebuffer[y * this->XRes + x] = p;
m_framebufferMutex.unlock();
}
void Display::SetPixelSafe(int x, int y, Pixel p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
m_framebufferMutex.lock();
Framebuffer[y * this->XRes + x] = p.rgb();
}
m_framebufferMutex.unlock();
}
}
void Display::SetPixelSafe(int x, int y, uint32_t p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
m_framebufferMutex.lock();
Framebuffer[y * this->XRes + x] = p;
}
m_framebufferMutex.unlock();
}
}
void Display::SetFramebuffer(uint32_t* fb) {
m_framebufferMutex.lock();
Framebuffer = nullptr;
Framebuffer = fb;
m_framebufferMutex.unlock();
}
void Display::ClearFramebuffer() {
m_framebufferMutex.lock();
memset((void*)Framebuffer, 0, (XRes * YRes) * sizeof(uint32_t));
m_framebufferMutex.unlock();
}
void Display::Update() {
@@ -114,6 +131,7 @@ void Display::Update() {
ImGui::Render();
ImGuiSDL::Render(ImGui::GetDrawData());
SDL_RenderPresent(m_renderer);
}

View File

@@ -1,6 +1,8 @@
#ifndef INFERNO_DISPLAY_DISPLAY_H_
#define INFERNO_DISPLAY_DISPLAY_H_
#include <mutex>
#include "../common.hpp"
#include "displayinterface.hpp"
@@ -19,12 +21,13 @@ public:
void SetPixelSafe(int x, int y, Pixel p) override;
void SetPixelSafe(int x, int y, uint32_t p) override;
void SetFramebuffer(uint32_t* fb);
void SetFramebuffer(uint32_t* fb) override;
void ClearFramebuffer() override;
void Update() override;
void UpdatePartial() override;
void UpdateTitle(std::string title);
void UpdateTitle();
void UpdateTitle(std::string title) override;
void UpdateTitle() override;
void Close() override;
@@ -35,7 +38,7 @@ private:
SDL_Renderer* m_renderer;
SDL_Texture* m_texture;
SDL_Texture* m_imguiTexture;
bool m_imGui = false;
std::mutex m_framebufferMutex;
};
#endif

View File

@@ -10,6 +10,7 @@ public:
DisplayInterface();
bool Active = false;
bool ImGui = false;
int XRes, YRes;
std::string Title;
unsigned int Scale = 1;
@@ -23,6 +24,11 @@ public:
virtual void SetPixelSafe(int x, int y, Pixel p) = 0;
virtual void SetPixelSafe(int x, int y, uint32_t p) = 0;
virtual void SetFramebuffer(uint32_t* fb) = 0;
virtual void ClearFramebuffer() = 0;
virtual void UpdateTitle(std::string title) = 0;
virtual void UpdateTitle() = 0;
virtual void Update() = 0;
virtual void UpdatePartial() = 0;

View File

@@ -1,8 +1,5 @@
#include "progressiverenderer.hpp"
#include <vector>
#include <chrono>
#include "../common.hpp"
#include "../pixel.hpp"
#include "../display/displayinterface.hpp"
@@ -23,31 +20,45 @@ void ProgressiveRenderer::Init(DisplayInterface* interface, Scene* scene) {
m_scene = scene;
}
void ProgressiveRenderer::Input() {
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) m_interface->Close();
if (!m_interface->ImGui) return;
ImGui::NewFrame();
ImGui::Begin("Debug");
ImGui::Text("Hello, world %d", 123);
if (ImGui::Button("Save")) {}
char* buf = ""; float f;
ImGui::InputText("string", buf, IM_ARRAYSIZE(buf));
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::End();
}
void ProgressiveRenderer::Render() {
int frames = 0;
auto startTime = std::chrono::high_resolution_clock::now();
while (m_interface->Active) {
Input();
m_interface->Update();
}
while (m_interface->Active) {
auto frameStartTime = std::chrono::high_resolution_clock::now();
ImGui::NewFrame();
ImGui::Begin("Thing");
ImGui::Text("Hello, world %d", 123);
if (ImGui::Button("Save")) {}
char* buf = ""; float f;
ImGui::InputText("string", buf, IM_ARRAYSIZE(buf));
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::End();
#pragma omp parallel for schedule(dynamic)
for (int x = 0; x < m_scene->w; x++)
for (int y = 0; y < m_scene->h; y++) {
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) m_interface->Close();
Ray ray = m_scene->camera->CastRay(x, y);
float t;
@@ -71,12 +82,18 @@ void ProgressiveRenderer::Render() {
frames++;
std::cout << "Frame: " << frames << std::endl;
std::cout << "Frame Time: " << std::chrono::duration_cast<std::chrono::seconds>(endTime - frameStartTime).count()
<< ":" << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - frameStartTime).count() << std::endl;
<< ":" << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - frameStartTime).count() << "s" << std::endl;
std::cout << "Avg Frame Time: " << std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count() / frames
<< ":" << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() / frames
<< ":" << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() / frames << "s"
<< std::endl << std::endl;
// Swap framebuffers
m_interface->Update();
m_interface->ClearFramebuffer();
}
}
void ProgressiveRenderer::RenderProgressive() {
}

View File

@@ -1,8 +1,12 @@
#ifndef INFERNO_ENGINE_PROGRESSIVERENDERER_H_
#define INFERNO_ENGINE_PROGRESSIVERENDERER_H_
class DisplayInterface;
#include <vector>
#include <chrono>
#include <thread>
#include <mutex>
class DisplayInterface;
class Scene;
class ProgressiveRenderer {
@@ -11,12 +15,19 @@ public:
void Init(DisplayInterface* interface, Scene* scene);
void Input();
void Render();
void RenderProgressive();
private:
DisplayInterface* m_interface = nullptr;
Scene* m_scene = nullptr;
int m_workerMax = 16;
std::vector<std::thread*> m_workers;
bool m_mxaa = true;
};
#endif

View File

@@ -24,17 +24,17 @@ int main(int argc, char** argv) {
// object->Translate({ 0.0f, -5.0f, -20.0f });
// std::vector<Triangle*> tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/lucy-normals.obj");
// std::vector<Triangle*> tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/lucy-normals.obj");
// for (const auto& object : tris)
// object->Translate({ 0.0f, -3.9f, -10.6f });
//std::vector<Triangle*> tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/lucy-normals.obj");
//for (const auto& object : tris)
// object->Translate({ 0.0f, -3.9f, -10.6f });
// std::vector<Triangle*> tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/cornell.obj");
//std::vector<Triangle*> tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/cornell.obj");
std::vector<Triangle*> tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/cornell.obj");
for (const auto& object : tris)
object->Translate({ 0.0f, -0.9f, -3.0f });
Mesh* mesh = new Mesh(tris);
mesh->Optimise();
//mesh->Optimise();
scene->meshs.push_back(mesh);
inferno.SetScene(scene);