Commit 87fef2e6 authored by Johannes Braun's avatar Johannes Braun
Browse files

Switch light vector for unordered_map; Change window update

parent ec9a482d
This diff is collapsed.
#pragma once
#include "dino\opengl\TextureResource.h"
#include <dino/editor/Blueprints.h>
#include <dino/input/Window.h>
#include <optional>
#include <unordered_set>
#include <vector>
#include "dino\opengl\TextureResource.h"
namespace dino
{
......@@ -56,25 +56,26 @@ private:
void drawLightsTab();
void drawRenderingTab();
void drawPreview();
void processInputsEditor();
void processInputsMain();
void processInputsEditor();
void processInputsMain();
void processInputsFields();
void processInputsLights();
void selectByIndex(uint32_t index);
void selectByIndex(uint32_t index);
void deleteField(uint32_t index);
void saveField(uint32_t index, std::filesystem::path path);
void loadField(const std::filesystem::path path);
void saveScene();
void loadScene();
void saveScene();
void loadScene();
bool editorShown() const;
void prepareTextures();
const dino::Texture* m_stipplingTexture;
const dino::Texture* m_hatchingTexture;
const dino::Texture* m_brickTexture;
dino::TextureResource m_textureResourceStippling;
void prepareTextures();
const dino::Texture* m_stipplingTexture;
const dino::Texture* m_hatchingTexture;
const dino::Texture* m_brickTexture;
dino::TextureResource m_textureResourceStippling;
dino::TextureResource m_textureResourceHatching;
dino::TextureResource m_textureResourceBrick;
......@@ -89,17 +90,17 @@ private:
std::unordered_set<uint32_t> m_selectedLightIndices;
std::vector<uint32_t> m_selectedLightIds;
std::vector<std::optional<uint32_t>> m_allFieldIds;
std::vector<uint32_t> m_allLightIds;
std::optional<uint32_t> m_activeFieldIndex = std::nullopt;
std::optional<uint32_t> m_activeLightIndex = std::nullopt;
std::vector<std::unique_ptr<Blueprints>> m_blueprints;
KeyboardButton m_ctrlC = KeyboardButton(GLFW_KEY_C, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlV = KeyboardButton(GLFW_KEY_V, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlC = KeyboardButton(GLFW_KEY_C, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlV = KeyboardButton(GLFW_KEY_V, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlSEditor = KeyboardButton(GLFW_KEY_S, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlSMain = KeyboardButton(GLFW_KEY_S, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlO = KeyboardButton(GLFW_KEY_O, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlA = KeyboardButton(GLFW_KEY_A, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlSMain = KeyboardButton(GLFW_KEY_S, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlO = KeyboardButton(GLFW_KEY_O, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_ctrlA = KeyboardButton(GLFW_KEY_A, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_shiftTabEditor = KeyboardButton(GLFW_KEY_LEFT_SHIFT, GLFW_KEY_TAB);
KeyboardButton m_shiftTabMain = KeyboardButton(GLFW_KEY_LEFT_SHIFT, GLFW_KEY_TAB);
KeyboardButton m_shiftTabMain = KeyboardButton(GLFW_KEY_LEFT_SHIFT, GLFW_KEY_TAB);
};
} // namespace dino
\ No newline at end of file
......@@ -7,10 +7,8 @@
#include <random>
#include "Editor.hpp"
std::unique_ptr<dino::Texture> idTexture;
std::shared_ptr<dino::RendererDeferred> renderer;
std::shared_ptr<dino::Window> mainWindow;
std::shared_ptr<dino::Window> editorWindow;
bool mainHasFocus = true;
int pixelObjectId = -1;
bool didSelect = false;
......@@ -48,18 +46,10 @@ void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
}
}
// catch window closing and hide it instead
void windowCloseCallback(GLFWwindow* window)
{
glfwSetWindowShouldClose(window, GL_FALSE);
glfwHideWindow(window);
}
void onResize(GLFWwindow* w, int x, int y)
{
x = glm::max(1, x);
y = glm::max(1, y);
renderer->setFramebufferSize({x, y});
}
......@@ -69,9 +59,6 @@ int main()
dino::Window::Hints hints = {{GLFW_SAMPLES, 8}, {GLFW_CONTEXT_VERSION_MINOR, 3}};
mainWindow = std::make_shared<dino::Window>(
windowWidth, windowHeight, "Example 05: Main Window", hints);
hints.insert({GLFW_VISIBLE, 0});
std::string editorWindowName = "Example 05 : Complex Node Editor";
editorWindow = std::make_shared<dino::Window>(1280, 720, editorWindowName, mainWindow, hints);
glfwMakeContextCurrent(*mainWindow);
glClearColor(0.125f, 0.125f, 0.125f, 1.00f);
......@@ -154,23 +141,20 @@ int main()
auto deserialized = dino::Function::deserialize(x)->as<dino::OpBlendPolynomial>();
/**************************************** Callbacks ****************************************/
glfwSetKeyCallback(*mainWindow, [](GLFWwindow*, int key, int sc, int ac, int md) {
/*glfwSetKeyCallback(*mainWindow, [](GLFWwindow*, int key, int sc, int ac, int md) {
if(key == GLFW_KEY_F5 && ac == GLFW_PRESS)
dino::Program::reloadAll();
if(key == GLFW_KEY_ESCAPE && ac == GLFW_PRESS)
mainWindow->close();
});
});*/
glfwSetMouseButtonCallback(*mainWindow, mouseButtonCallback);
glfwSetWindowCloseCallback(*editorWindow, windowCloseCallback);
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);
// renderer =
// std::make_unique<dino::Renderer>(dino::ShaderFile::load("trace_plugin/ggx.glsl"));
std::vector<std::shared_ptr<dino::RenderLayer>> deferredPipeline =
dino::RenderLayer::createRenderPipeline(/*dino::RenderLayer::DF_VISUALIZATION*/ {
dino::RenderLayer::createRenderPipeline({
dino::RenderLayer::SHADOW_MAPPING,
dino::RenderLayer::SOFT_SHADOWS,
dino::RenderLayer::DIFFUSE_LIGHTING,
......@@ -201,12 +185,6 @@ int main()
/********************************************************************************************/
GLFWwindow* wind = *editorWindow;
std::vector<std::unique_ptr<dino::Blueprints>> blueprints;
for(int i = 0; i < renderer->fieldCount(); i++)
blueprints.emplace_back(std::make_unique<dino::Blueprints>(wind))
->load(renderer->getFunctionByID(i));
std::mt19937 gen;
std::uniform_real_distribution<float> dist;
......@@ -217,7 +195,7 @@ int main()
while(!glfwWindowShouldClose(*mainWindow))
{
// Here in this window you can draw the distance fields
if(mainWindow->update())
if(mainWindow->beginContent())
{
double delta = glfwGetTime() - last;
last = glfwGetTime();
......@@ -237,7 +215,8 @@ int main()
didSelect = false;
openBlueprint = false;
}
#if 0
ImGui::Begin("I am a Window");
if(ImGui::Button("Load SkyBox"))
{
......@@ -347,10 +326,10 @@ int main()
}
}
ImGui::End();
#endif
glfwGetFramebufferSize(*mainWindow, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);
/************************************** Animate
......@@ -393,17 +372,13 @@ int main()
* ****************************************/
renderer->render(camera.view(), projection);
// skyBox.render(camera.view(), projection); //not needed in deferred mode
mainWindow->endContent();
}
mainHasFocus = glfwGetWindowAttrib(*mainWindow, GLFW_FOCUSED);
editor.drawBlueprint();
if(!glfwGetWindowAttrib(*editorWindow, GLFW_FOCUSED) &&
!glfwGetWindowAttrib(*mainWindow, GLFW_FOCUSED))
{
glfwPollEvents();
}
mainHasFocus = glfwGetWindowAttrib(*mainWindow, GLFW_FOCUSED);
glfwPollEvents();
}
return 0;
}
......@@ -127,7 +127,7 @@ Window::~Window() { glfwDestroyWindow(m_window); }
Window::operator GLFWwindow*() const { return m_window; }
bool Window::update()
bool Window::beginContent()
{
glfwMakeContextCurrent(m_window);
int sx, sy;
......@@ -138,15 +138,16 @@ bool Window::update()
glfwGetWindowSize(m_window, &sx, &sy);
}
if(m_guiHasFrame)
m_gui.render();
const bool shouldClose = glfwWindowShouldClose(m_window);
if(!shouldClose) m_gui.newFrame();
return !shouldClose;
}
glfwSwapBuffers(m_window);
if(glfwGetWindowAttrib(m_window, GLFW_FOCUSED))
glfwPollEvents();
m_gui.newFrame();
m_guiHasFrame = true;
return !glfwWindowShouldClose(m_window);
void Window::endContent()
{
m_gui.render();
glfwSwapBuffers(m_window);
//if (glfwGetWindowAttrib(m_window, GLFW_FOCUSED))
}
void Window::close() const { glfwSetWindowShouldClose(m_window, true); }
......
......@@ -48,7 +48,9 @@ public:
* glfwPollEvents(), until the window is resized back to a valid size.
* @return true if the window should be kept open, false if there is a hint to close the window.
*/
bool update();
bool beginContent();
void endContent();
/**
* @brief Hints a closing action for the window. Should be closed after update() returns false
......
......@@ -79,16 +79,14 @@ void Renderer::replace(int id, const std::shared_ptr<Operator>& op,
{
remove(getFunctionByID(id));
RenderInfo renderable;
m_distanceFields[id] = std::make_shared<DistanceField>(op);
renderable.bounds = m_distanceFields[id]->bounds();
if(!material)
m_materials[id] = std::make_shared<Material>();
else
m_materials[id] = material;
m_infos.emplace(m_infos.begin() + id, renderable);
m_infos.emplace_back();
m_invalidRenderables = true;
}
......@@ -98,11 +96,8 @@ void Renderer::replace(int id, const std::shared_ptr<Primitive>& op,
if(getFunctionByID(id))
remove(getFunctionByID(id));
//RenderInfo renderable;
m_distanceFields[id] = std::make_shared<DistanceField>(op);
// renderable.bounds = m_distanceFields[id]->bounds();
// TODO: material IDs.
if(!material)
m_materials[id] = std::make_shared<Material>();
else
......@@ -128,7 +123,6 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
m_infos[i].normalPrec = (fieldIter->second)->normalPrec();
m_infos[i].id = fieldIter->first;
// TODO: material IDs
materials[i] = *m_materials[fieldIter->first];
m_dataProxy.insert(m_dataProxy.end(),
(fieldIter->second)->data().begin(),
......@@ -214,8 +208,8 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
m_onTrace});
std::for_each(m_lights.begin(), m_lights.end(), [&](auto& light) {
if(light->shadowType() == ShadowType::SHADOWMAP_SHADOW)
std::static_pointer_cast<ShadowMapLight>(light)->setGeneratedFragmentSource(
if(light.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
std::static_pointer_cast<ShadowMapLight>(light.second)->setGeneratedFragmentSource(
m_generatedShaderSource);
});
......@@ -243,7 +237,7 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
/*auto light = std::holds_alternative<std::shared_ptr<dino::ShadowMap>>(lightPtr)
? std::get<std::shared_ptr<dino::ShadowMap>>(lightPtr)->getLight()
: std::get<std::shared_ptr<dino::Light>>(lightPtr);*/
lights.push_back(static_cast<Light>(*lightPtr));
lights.push_back(static_cast<Light>(*lightPtr.second));
});
m_lightBuffer.assign(lights);
m_lightBuffer.bind(GL_SHADER_STORAGE_BUFFER, LIGHTS_STORAGE_BINDING);
......@@ -256,9 +250,9 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
std::vector<ShadowMapLight::GpuSm> shadowMaps;
for(const auto& l : m_lights)
{
if(l->shadowType() == ShadowType::SHADOWMAP_SHADOW)
if(l.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
const auto& sm = std::static_pointer_cast<ShadowMapLight>(l);
const auto& sm = std::static_pointer_cast<ShadowMapLight>(l.second);
sm->renderShadowMap([&]() {
glDrawArraysInstanced(GL_TRIANGLES,
0,
......@@ -285,40 +279,54 @@ void Renderer::render(const glm::mat4& view, const glm::mat4& projection)
0);
}
void Renderer::addLight(std::shared_ptr<Light> light)
void Renderer::addLightAt(std::shared_ptr<Light> light, uint32_t lightIndex)
{
m_lights.push_back(light);
if(light->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
if(!m_generatedShaderSource.empty())
std::static_pointer_cast<ShadowMapLight>(light)->setGeneratedFragmentSource(
m_generatedShaderSource);
m_lights[lightIndex] = light;
if (light->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
if (!m_generatedShaderSource.empty())
std::static_pointer_cast<ShadowMapLight>(light)->setGeneratedFragmentSource(
m_generatedShaderSource);
int smCount = 0;
for (auto& l : m_lights)
{
if (l.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
smCount++;
}
}
// const auto sm = std::get<std::shared_ptr<ShadowMapLight>>(light);
std::static_pointer_cast<ShadowMapLight>(light)->setShadowMapIndex(smCount - 1);
}
}
int smCount = 0;
for(auto& l : m_lights)
{
if(l->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
smCount++;
}
}
// const auto sm = std::get<std::shared_ptr<ShadowMapLight>>(light);
std::static_pointer_cast<ShadowMapLight>(light)->setShadowMapIndex(smCount - 1);
}
uint32_t Renderer::addLight(std::shared_ptr<Light> light)
{
const auto max_id_iter =
std::max_element(m_lights.begin(),
m_lights.end(),
[](const std::pair<uint32_t, std::shared_ptr<Light>>& item1,
const std::pair<uint32_t, std::shared_ptr<Light>>& item2) {
return item1.first < item2.first;
});
const uint32_t id = max_id_iter == m_lights.end() ? 0 : max_id_iter->first + 1;
addLightAt(std::move(light), id);
return id;
}
const std::vector<std::shared_ptr<Light>>& Renderer::getLights() const { return m_lights; }
const std::unordered_map<uint32_t, std::shared_ptr<Light>>& Renderer::getLights() const { return m_lights; }
void Renderer::removeLight(int lightIndex)
{
m_lights.erase(m_lights.begin() + lightIndex);
m_lights.erase(lightIndex);
int smIndex = 0;
for(auto& l : m_lights)
{
if(l->shadowType() == ShadowType::SHADOWMAP_SHADOW)
if(l.second->shadowType() == ShadowType::SHADOWMAP_SHADOW)
{
auto sm = std::static_pointer_cast<ShadowMapLight>(l);
auto sm = std::static_pointer_cast<ShadowMapLight>(l.second);
sm->setShadowMapIndex(smIndex++);
}
}
......@@ -332,28 +340,28 @@ void Renderer::convertLight(int lightIndex, ShadowType toType)
if(toType == ShadowType::TRACED_SHADOW)
{
auto sm = std::static_pointer_cast<Light>(light);
addLight(std::make_shared<dino::TracedLight>(sm->type(),
addLightAt(std::make_shared<dino::TracedLight>(sm->type(),
sm->position(),
sm->color(),
sm->intensity(),
sm->direction(),
sm->cutoff()));
sm->cutoff()), lightIndex);
}
else if(toType == ShadowType::SHADOWMAP_SHADOW)
{
auto tl = std::static_pointer_cast<Light>(light);
addLight(std::make_shared<dino::ShadowMapLight>(tl->type(),
addLightAt(std::make_shared<dino::ShadowMapLight>(tl->type(),
tl->position(),
tl->color(),
tl->intensity(),
tl->direction(),
tl->cutoff()));
tl->cutoff()), lightIndex);
}
else if(toType == ShadowType::NO_SHADOW)
{
auto l = std::static_pointer_cast<Light>(light);
addLight(std::make_shared<dino::Light>(
l->type(), l->position(), l->color(), l->intensity(), l->direction(), l->cutoff()));
addLightAt(std::make_shared<dino::Light>(
l->type(), l->position(), l->color(), l->intensity(), l->direction(), l->cutoff()), lightIndex);
}
}
......
......@@ -159,13 +159,15 @@ public:
* If it is a ShadowMap, a new layer in the shadow map texture array is created.
* @param light A shared pointer to the light that should be added
*/
virtual void addLight(std::shared_ptr<Light> light);
virtual uint32_t addLight(std::shared_ptr<Light> light);
void addLightAt(std::shared_ptr<Light> light, uint32_t lightIndex);
/**
* @brief Returns a list with all lights (Light or ShadowMap) currently added to the renderer
* @return The light-list
*/
const std::vector<std::shared_ptr<Light>>& getLights() const;
const std::unordered_map<uint32_t, std::shared_ptr<Light>>& getLights() const;
/**
* @brief Removes the light with the given lightIndex from the m_lights vector.
......@@ -214,7 +216,7 @@ protected:
Shader::ShaderSource m_onTrace;
std::string m_generatedShaderSource;
bool m_invalidRenderables = true;
std::vector<std::shared_ptr<Light>> m_lights;
std::unordered_map<uint32_t, std::shared_ptr<Light>> m_lights;
std::unordered_map<uint32_t, std::shared_ptr<Material>> m_materials;
private:
......
......@@ -57,10 +57,10 @@ void RendererDeferred::setFramebufferSize(glm::ivec2 size)
for(auto& l : m_lights)
{
if(l->shadowType() == ShadowType::SHADOWMAP_SHADOW || l->shadowType() ==
if(l.second->shadowType() == ShadowType::SHADOWMAP_SHADOW || l.second->shadowType() ==
ShadowType::TRACED_SHADOW)
{
std::static_pointer_cast<TracedLight>(l)->setSsShadowTextureSize(size);
std::static_pointer_cast<TracedLight>(l.second)->setSsShadowTextureSize(size);
}
}
}
......@@ -70,9 +70,9 @@ std::vector<std::shared_ptr<RenderLayer>> RendererDeferred::getRenderLayers() co
return m_renderLayers;
}
void RendererDeferred::addLight(std::shared_ptr<Light> light)
uint32_t RendererDeferred::addLight(std::shared_ptr<Light> light)
{
Renderer::addLight(light);
const auto id = Renderer::addLight(light);
if(light->shadowType() == ShadowType::SHADOWMAP_SHADOW) // is TracedLight or ShadowMapLight
{
......@@ -84,6 +84,7 @@ void RendererDeferred::addLight(std::shared_ptr<Light> light)
std::static_pointer_cast<TracedLight>(light)->setSsShadowTextureSize(
m_gBufferFbo->getDepthAttachment()->size());
}
return id;
}
void RendererDeferred::addRenderLayer(RenderLayer::Type type)
......
......@@ -70,7 +70,7 @@ public:
* If it is a ShadowMap, a new layer in the shadow map texture array is created.
* @param light A shared pointer to the light that should be added
*/
void addLight(std::shared_ptr<Light> light) override;
uint32_t addLight(std::shared_ptr<Light> light) override;
/**
* @brief Adds a new render layer to the list of render layers
......
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