Commit 640229ef authored by Johannes Braun's avatar Johannes Braun
Browse files

Move resizing to the renderer

parent 29ebabea
#include "Editor.hpp"
#include <dino/Dino.h>
#include <dino/editor/iconfont/IconsMaterialDesignIcons.h>
#include <fstream>
#include <functional>
#include <imgui/imgui_internal.h>
#include <iostream>
#include <random>
#include "Editor.hpp"
std::shared_ptr<dino::Renderer> renderer;
std::shared_ptr<dino::Window> mainWindow;
void onResize(GLFWwindow* w, int x, int y)
{
x = glm::max(1, x);
y = glm::max(1, y);
renderer->setFramebufferSize({x, y});
}
std::shared_ptr<dino::Window> mainWindow;
int main()
{
......@@ -104,9 +96,6 @@ int main()
auto x = blendPoly1->serialize();
auto deserialized = dino::Function::deserialize(x)->as<dino::OpBlendPolynomial>();
/**************************************** Callbacks ****************************************/
glfwSetFramebufferSizeCallback(*mainWindow, onResize);
/************************************** Build Buffers **************************************/
dino::Camera camera(glm::vec3(0, 0, 10));
glm::mat4 projection = glm::perspectiveFov(glm::radians(70.f), 1280.f, 720.f, 0.01f, 1000.f);
......@@ -139,195 +128,31 @@ int main()
glm::vec3(0.6f, 1.f, 0.65f),
15.f));
/********************************************************************************************/
std::mt19937 gen;
std::uniform_real_distribution<float> dist;
double last = glfwGetTime();
dino::Editor editor(mainWindow, renderer);
dino::Editor editor(mainWindow, renderer);
while(!glfwWindowShouldClose(*mainWindow))
{
// Here in this window you can draw the distance fields
if(mainWindow->beginContent())
{
double delta = glfwGetTime() - last;
last = glfwGetTime();
/*TODO:
- Main Menu Bar
- Rendering panel
*/
editor.drawSettings();
#if 0
ImGui::Begin("I am a Window");
if(ImGui::Button("Load SkyBox"))
{
const std::filesystem::path p = "./../resources/textures/";
auto native = absolute(p).make_preferred();
const char* folderPath =
tinyfd_selectFolderDialog("Load Skybox", native.string().c_str());
if(folderPath)
{
auto filename = std::filesystem::path(
*(std::filesystem::directory_iterator(folderPath)));
if(renderer->getSkyBox().generateCubeMap(folderPath, filename.extension().string()))
{
renderer->getSkyBox().getTexture().bind(5);
renderer->getSkyBox().setFolderPath(folderPath);
}
else
{
tinyfd_notifyPopup("Load SkyBox",
"Selected files cannot be parsed as a valid SkyBox.",
"error");
}
}
}
ImGui::End();
ImGui::Begin("Render Layers");
const static std::vector<const char*> layerNames = {"RENDERLAYER_NONE ",
"NORMALS ",
"TRACE_T ",
"OBJECT_ID ",
"STEP_COUNT ",
"MIN_DISTANCE ",
"GRADIENT_X ",
"GRADIENT_Y ",
"MATERIAL_COLOR ",
"AMBIENT_LIGHTING ",
"DIFFUSE_LIGHTING ",
"GGX_LIGHTING ",
"AMBIENT_OCCLUSION ",
"SHADOW_MAPPING ",
"SOFT_SHADOWS ",
"SKYBOX ",
"SKYBOX_REFLECTIONS",
"SILHOUETTE ",
"TONE_MAPPING ",
"FXAA "};
std::vector<const char*> activeLayers;
for(const auto& rl : renderer->getRenderLayers())
{
activeLayers.push_back(layerNames[static_cast<int>(rl->getType())]);
}
if(ImGui::BeginCombo("##addrenderlayer", "Add Render Layer"))
{
for(size_t i = 0; i < layerNames.size(); i++)
{
auto layerType = static_cast<dino::RenderLayer::Type>(i);
bool foundRenderLayer = false;
for(auto& rl : renderer->getRenderLayers())
{
if(rl->getType() == layerType)
{
foundRenderLayer = true;
break;
}
}
if(!foundRenderLayer && ImGui::Selectable(layerNames[i]))
{
renderer->addRenderLayer(layerType);
}
}
ImGui::EndCombo();
}
// Render + dragging
for(int n = 0; n < renderer->getRenderLayers().size(); n++)
{
if(renderer->getRenderLayers()[n]->getType() != dino::RenderLayer::SILHOUETTE)
{
if(ImGui::Button(
(std::string(ICON_MDI_DELETE "##") + std::to_string(n)).c_str()))
{
renderer->removeRenderLayer(n);
}
ImGui::SameLine();
if(ImGui::Button((std::string(ICON_MDI_ARROW_UP_BOLD "##") + std::to_string(n))
.c_str()) &&
n > 0)
{
renderer->swapLayers(n, n - 1);
}
ImGui::SameLine();
if(ImGui::Button(
(std::string(ICON_MDI_ARROW_DOWN_BOLD "##") + std::to_string(n))
.c_str()) &&
n < renderer->getRenderLayers().size() - 1)
{
renderer->swapLayers(n, n + 1);
}
ImGui::SameLine();
ImGui::Selectable(activeLayers[n]);
}
}
ImGui::End();
#endif
glfwGetFramebufferSize(*mainWindow, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);
/************************************** Animate
* ****************************************/
if(true)
{
/* globalRadius->set(glm::sin(static_cast<float>(glfwGetTime())) + 3.f);
torus->tubeRadius = 0.2f * glm::cos(static_cast<float>(glfwGetTime())) +
0.5f;*/
torusTransform->matrix = glm::rotate(glm::radians(90.f * static_cast<float>(delta)),
glm::vec3(0, 1, 0)) *
torusTransform->matrix.get();
fractalTransform->matrix =
glm::rotate(glm::radians(-30.f * static_cast<float>(delta)),
normalize(glm::vec3(1, 1, 0))) *
fractalTransform->matrix.get();
/*outerCubeTransform->matrix =
glm::translate(glm::vec3(0, glm::cos(3.f * glfwGetTime()), 0));
outerTransform->matrix = glm::translate(
8.f * glm::vec3(0, glm::sin(glfwGetTime()),
glm::cos(glfwGetTime())));*/
}
/************************************** Update
* ****************************************/
editor.drawSettings();
camera.update(*mainWindow);
projection = glm::perspectiveFov(glm::radians(70.f),
static_cast<float>(glm::max(windowWidth, 1)),
static_cast<float>(glm::max(windowHeight, 1)),
0.01f,
1000.f);
/************************************** Draw
* ****************************************/
renderer->render(camera.view(), projection);
mainWindow->endContent();
mainWindow->endContent();
}
//mainHasFocus = glfwGetWindowAttrib(*mainWindow, GLFW_FOCUSED);
editor.drawBlueprint();
editor.drawBlueprint();
glfwPollEvents();
glfwPollEvents();
}
return 0;
}
......@@ -3,6 +3,8 @@
namespace dino
{
Renderer::Renderer(const Shader::ShaderSource& onTrace)
: m_onTrace(onTrace)
{
......@@ -27,6 +29,7 @@ Renderer::Renderer(std::vector<RenderLayer::Type> renderLayers, glm::ivec2 windo
void Renderer::init(glm::ivec2 windowSize)
{
m_lastWindowSize = windowSize;
// gbuffer
m_gBufferFbo = std::make_shared<FrameBuffer>(windowSize);
......@@ -156,10 +159,117 @@ void Renderer::replace(int id, const std::shared_ptr<Primitive>& op,
m_invalidRenderables = true;
}
void Renderer::rebuildRenderables()
{
// construct distance field shader code
std::stringstream shader;
shader << "#pragma optionNV(inline all)\n"; // What the fuck, let's do like Donald Duck
shader << "\nlayout(std430, binding=" << DATA_STORAGE_BINDING
<< ") restrict readonly buffer FloatData { float in_data[]; "
"};\n\n#define DINO_SHADER_BUILD\n\n";
shader << "\nstruct RenderInfo {\n"
"vec4 bounds[2];\n"
"uint dataOffset;\n"
"uint dataSize;\n"
"float normalPrec;\n"
"uint id;\n"
"};\n";
shader << "\nlayout(std430, binding=" << OBJECT_STORAGE_BINDING
<< ") restrict readonly buffer ObjectData { RenderInfo in_renderInfos[]; "
"};\n\n";
shader << dino::Param::conversionFunctions();
for(const auto& field : m_distanceFields)
shader << field.second->distanceFunctions();
shader << "float map(int in_id, vec3 in_position) {\n float distance = "
<< std::numeric_limits<float>::max() << ";\nswitch(in_id) {\n";
auto dfIter = m_distanceFields.begin();
for(int i = 0; i < m_distanceFields.size(); ++i)
{
shader << "case " << i << ": { uint in_dataOffset = " << m_infos[i].dataOffset
<< "; return " << dfIter->second->mapFunction() << ";}\n ";
++dfIter;
}
shader << "default: return 1.f/0.f;\n}\n}\n\n";
shader << "float fullMap(vec3 in_position) {\n float distance = "
<< std::numeric_limits<float>::max() << ";\nuint in_dataOffset = 0;\n";
for(const auto& field : m_distanceFields)
shader << "distance = min(distance, " << field.second->mapFunction()
<< ");\nin_dataOffset+=" << field.second->data().size() << ";\n";
shader << "return distance;\n}\n";
m_generatedShaderSource = shader.str();
m_program = Program();
m_program.attachNew(GL_VERTEX_SHADER, dino::ShaderFile::load("box.vert"));
m_program.attachNew(GL_FRAGMENT_SHADER,
{"#version 430 core\n",
"#extension GL_ARB_bindless_texture : require\n",
dino::ShaderFile::load("implicit/ad_functions.glsl"),
m_generatedShaderSource,
dino::ShaderFile::load("common/light.glsl"),
dino::ShaderFile::load("trace.glsl"),
dino::ShaderFile::load("basicTracing.frag"),
m_onTrace});
std::for_each(m_lights.begin(), m_lights.end(), [&](auto& light) {
if(light.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
std::static_pointer_cast<ShadowMapLight>(light.second)
->setGeneratedFragmentSource(m_generatedShaderSource);
});
// construct common deferred shader code
const std::vector<Shader::ShaderSource> sharedSource = {
dino::ShaderFile::load("post_process/commonDeferred.glsl"),
dino::ShaderFile::load("implicit/ad_functions.glsl"),
m_generatedShaderSource,
dino::ShaderFile::load("common/light.glsl"),
dino::ShaderFile::load("common/material.glsl"),
dino::ShaderFile::load("trace.glsl"),
};
// updatede deferred shader programs
for(size_t i = 0; i < m_deferredPrograms.size() - 1; ++i)
{
std::vector<Shader::ShaderSource> src = sharedSource;
src.push_back(m_renderLayers[i]->getShaderSource());
m_deferredPrograms[i] = Program();
m_deferredPrograms[i].attachNew(GL_VERTEX_SHADER,
dino::ShaderFile::load("screenFilling.vert"));
m_deferredPrograms[i].attachNew(GL_FRAGMENT_SHADER, src);
m_deferredPrograms[i].link();
}
// update silhouette shader
std::vector<Shader::ShaderSource> src = sharedSource;
src.push_back(dino::ShaderFile::load("post_process/silhouette.glsl"));
m_deferredPrograms[m_deferredPrograms.size() - 1].attachNew(GL_FRAGMENT_SHADER, src);
m_deferredPrograms[m_deferredPrograms.size() - 1].use();
m_activeDfIdUniform = glGetUniformLocation(
m_deferredPrograms[m_deferredPrograms.size() - 1].id(), "activeDfId");
m_invalidRenderables = false;
}
void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
{
static Cube cube;
GLFWwindow* const currentWindow = glfwGetCurrentContext();
glm::ivec2 windowSize;
glfwGetFramebufferSize(currentWindow, &windowSize.x, &windowSize.y);
glViewport(0, 0, windowSize.x, windowSize.y);
if(m_lastWindowSize != windowSize)
setFramebufferSize(windowSize);
glClear(GL_COLOR_BUFFER_BIT);
// render to gbuffer
m_gBufferFbo->bind();
......@@ -226,99 +336,7 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
if(m_invalidRenderables)
{
// construct distance field shader code
std::stringstream shader;
shader << "#pragma optionNV(inline all)\n"; // What the fuck, let's do like Donald Duck
shader << "\nlayout(std430, binding=" << DATA_STORAGE_BINDING
<< ") restrict readonly buffer FloatData { float in_data[]; "
"};\n\n#define DINO_SHADER_BUILD\n\n";
shader << "\nstruct RenderInfo {\n"
"vec4 bounds[2];\n"
"uint dataOffset;\n"
"uint dataSize;\n"
"float normalPrec;\n"
"uint id;\n"
"};\n";
shader << "\nlayout(std430, binding=" << OBJECT_STORAGE_BINDING
<< ") restrict readonly buffer ObjectData { RenderInfo in_renderInfos[]; "
"};\n\n";
shader << dino::Param::conversionFunctions();
for(const auto& field : m_distanceFields)
shader << field.second->distanceFunctions();
shader << "float map(int in_id, vec3 in_position) {\n float distance = "
<< std::numeric_limits<float>::max() << ";\nswitch(in_id) {\n";
auto dfIter = m_distanceFields.begin();
for(int i = 0; i < m_distanceFields.size(); ++i)
{
shader << "case " << i << ": { uint in_dataOffset = " << m_infos[i].dataOffset
<< "; return " << dfIter->second->mapFunction() << ";}\n ";
++dfIter;
}
shader << "default: return 1.f/0.f;\n}\n}\n\n";
shader << "float fullMap(vec3 in_position) {\n float distance = "
<< std::numeric_limits<float>::max() << ";\nuint in_dataOffset = 0;\n";
for(const auto& field : m_distanceFields)
shader << "distance = min(distance, " << field.second->mapFunction()
<< ");\nin_dataOffset+=" << field.second->data().size() << ";\n";
shader << "return distance;\n}\n";
m_generatedShaderSource = shader.str();
m_program = Program();
m_program.attachNew(GL_VERTEX_SHADER, dino::ShaderFile::load("box.vert"));
m_program.attachNew(GL_FRAGMENT_SHADER,
{"#version 430 core\n",
"#extension GL_ARB_bindless_texture : require\n",
dino::ShaderFile::load("implicit/ad_functions.glsl"),
m_generatedShaderSource,
dino::ShaderFile::load("common/light.glsl"),
dino::ShaderFile::load("trace.glsl"),
dino::ShaderFile::load("basicTracing.frag"),
m_onTrace});
std::for_each(m_lights.begin(), m_lights.end(), [&](auto& light) {
if(light.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
std::static_pointer_cast<ShadowMapLight>(light.second)
->setGeneratedFragmentSource(m_generatedShaderSource);
});
// construct common deferred shader code
const std::vector<Shader::ShaderSource> sharedSource = {
dino::ShaderFile::load("post_process/commonDeferred.glsl"),
dino::ShaderFile::load("implicit/ad_functions.glsl"),
m_generatedShaderSource,
dino::ShaderFile::load("common/light.glsl"),
dino::ShaderFile::load("common/material.glsl"),
dino::ShaderFile::load("trace.glsl"),
};
// updatede deferred shader programs
for(size_t i = 0; i < m_deferredPrograms.size() - 1; ++i)
{
std::vector<Shader::ShaderSource> src = sharedSource;
src.push_back(m_renderLayers[i]->getShaderSource());
m_deferredPrograms[i] = Program();
m_deferredPrograms[i].attachNew(GL_VERTEX_SHADER,
dino::ShaderFile::load("screenFilling.vert"));
m_deferredPrograms[i].attachNew(GL_FRAGMENT_SHADER, src);
m_deferredPrograms[i].link();
}
// update silhouette shader
std::vector<Shader::ShaderSource> src = sharedSource;
src.push_back(dino::ShaderFile::load("post_process/silhouette.glsl"));
m_deferredPrograms[m_deferredPrograms.size() - 1].attachNew(GL_FRAGMENT_SHADER, src);
m_deferredPrograms[m_deferredPrograms.size() - 1].use();
m_activeDfIdUniform = glGetUniformLocation(
m_deferredPrograms[m_deferredPrograms.size() - 1].id(), "activeDfId");
m_invalidRenderables = false;
rebuildRenderables();
}
// bind buffers
......@@ -607,6 +625,7 @@ void Renderer::setActivePixelObjectId(int id) const
void Renderer::setFramebufferSize(glm::ivec2 size)
{
m_lastWindowSize = size;
m_gBufferFbo->resize(size);
m_postProcessFbo[0].second->resize(size);
m_postProcessFbo[1].second->resize(size);
......
......@@ -295,6 +295,9 @@ public:
virtual ~Renderer() = default;
private:
void init(glm::ivec2 windowSize);
void rebuildRenderables();
Program m_program;
Shader::ShaderSource m_onTrace;
std::string m_generatedShaderSource;
......@@ -339,7 +342,6 @@ private:
VertexArray m_vao;
std::vector<Program> m_deferredPrograms;
std::vector<std::shared_ptr<RenderLayer>> m_renderLayers;
void init(glm::ivec2 windowSize);
glm::ivec2 m_lastWindowSize{ 0, 0 };
};
} // namespace dino
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment