Commit ec9a482d authored by Marc Schikowski's avatar Marc Schikowski
Browse files

Merge

parents cb15e52b b2be3f66
......@@ -116,6 +116,8 @@ void Editor::prepareTextures()
void Editor::deleteField(uint32_t index)
{
if(m_allFieldIds.empty())
return;
const auto id = m_allFieldIds[index];
if(id)
m_renderer->remove(m_renderer->getFunctionByID(*id));
......@@ -130,7 +132,7 @@ void Editor::saveField(uint32_t index, std::filesystem::path path)
if(!id)
return;
auto mat = m_renderer->getMaterials()[*id];
auto mat = m_renderer->getMaterials().at(*id);
nlohmann::json json = m_renderer->getFunctionByID(*id)->serialize();
json["material"] = mat->serialize();
{
......@@ -182,7 +184,7 @@ void Editor::drawFieldsTab()
ImGui::PushID((std::string("Field") + std::to_string(field_index)).c_str());
if(ImGui::Button(ICON_MDI_DELETE))
{
deleteField(field_index);
deleteField(0);
m_selectedFieldIndices.clear();
m_activeFieldIndex = std::nullopt;
--field_index;
......@@ -196,7 +198,7 @@ void Editor::drawFieldsTab()
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.2f);
}
if(ImGui::Button(ICON_MDI_DOWNLOAD))
if(ImGui::Button(ICON_MDI_CONTENT_SAVE))
{
const char* fileEnding = "*.json";
const char* filePath =
......@@ -349,7 +351,331 @@ void Editor::drawFieldsTab()
ImGui::EndChild();
} // namespace dino
void Editor::drawLightsTab() { ImGui::Text("Lights."); }
void Editor::drawLightsTab()
{
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::BeginHorizontal("Koffein", ImVec2(ImGui::GetContentRegionAvailWidth(), 0));
if(ImGui::Button(ICON_MDI_PLUS " Add"))
{
ImGui::OpenPopup("Add Light");
}
if(ImGui::BeginPopup("Add Light"))
{
const auto newNodePosition = ImGui::GetMousePosOnOpeningCurrentPopup();
if(ImGui::BeginMenu("Simple Light"))
{
if(ImGui::MenuItem("Point Light"))
{
m_renderer->addLight(std::make_shared<dino::Light>(dino::LightType::POINT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f));
}
if(ImGui::MenuItem("Spot Light"))
{
m_renderer->addLight(std::make_shared<dino::Light>(dino::LightType::SPOT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f,
glm::vec3(0.0f, -1.0f, 0.0f),
0.5f));
}
if(ImGui::MenuItem("Directional Light"))
{
m_renderer->addLight(std::make_shared<dino::Light>(dino::LightType::DIR_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
1.0f,
glm::vec3(-1.0f)));
}
ImGui::EndMenu();
};
if(ImGui::BeginMenu("Shadowmap Lights"))
{
if(ImGui::MenuItem("Point Light"))
{
m_renderer->addLight(
std::make_shared<dino::ShadowMapLight>(dino::LightType::POINT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f));
}
if(ImGui::MenuItem("Spot Light"))
{
m_renderer->addLight(
std::make_shared<dino::ShadowMapLight>(dino::LightType::SPOT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f,
glm::vec3(0.0f, -1.0f, 0.0f),
0.5f));
}
if(ImGui::MenuItem("Directional Light"))
{
m_renderer->addLight(
std::make_shared<dino::ShadowMapLight>(dino::LightType::DIR_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
1.0f,
glm::vec3(-1.0f)));
}
ImGui::EndMenu();
};
if(ImGui::BeginMenu("Traced Lights"))
{
if(ImGui::MenuItem("Point Light"))
{
m_renderer->addLight(
std::make_shared<dino::TracedLight>(dino::LightType::POINT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f));
}
if(ImGui::MenuItem("Spot Light"))
{
m_renderer->addLight(
std::make_shared<dino::TracedLight>(dino::LightType::SPOT_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
10.0f,
glm::vec3(0.0f, -1.0f, 0.0f),
0.5f));
}
if(ImGui::MenuItem("Directional Light"))
{
m_renderer->addLight(
std::make_shared<dino::TracedLight>(dino::LightType::DIR_LIGHT,
glm::vec3(1.0f, 10.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
1.0f,
glm::vec3(-1.0f)));
}
ImGui::EndMenu();
};
ImGui::EndPopup();
}
if(ImGui::Button(ICON_MDI_ARROW_EXPAND_HORIZONTAL " All"))
{
ImGui::OpenPopup("Convert All");
}
if(ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted("Convert all to ..");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
if(ImGui::BeginPopup("Convert All"))
{
if(ImGui::MenuItem("ShadowMap Light"))
{
for(int i = 0; i < m_renderer->getLights().size(); i++)
{
m_renderer->convertLight(0, dino::ShadowType::SHADOWMAP_SHADOW);
}
}
if(ImGui::MenuItem("Traced Light"))
{
for(int i = 0; i < m_renderer->getLights().size(); i++)
{
m_renderer->convertLight(0, dino::ShadowType::TRACED_SHADOW);
}
}
if(ImGui::MenuItem("Simple Light"))
{
for(int i = 0; i < m_renderer->getLights().size(); i++)
{
m_renderer->convertLight(0, dino::ShadowType::NO_SHADOW);
}
}
ImGui::EndPopup();
}
if(ImGui::Button(ICON_MDI_DELETE " All"))
{
m_selectedLightIndices.clear();
m_activeLightIndex = std::nullopt;
while(m_renderer->getLights().size() > 0)
{
m_renderer->removeLight(0);
}
}
ImGui::Spring();
ImGui::EndHorizontal();
ImGui::BeginChild("##content", ImVec2(0, 150));
ImGui::BeginVertical("##ButtonBar");
for(int light_index = 0; light_index < m_renderer->getLights().size(); ++light_index)
{
const auto it = std::find_if(m_selectedLightIndices.begin(),
m_selectedLightIndices.end(),
[&](uint32_t index) { return index == light_index; });
bool sel = it != m_selectedLightIndices.end();
ImGui::PushID((std::string("light") + std::to_string(light_index)).c_str());
std::string lightName = "";
auto currentLight = m_renderer->getLights()[light_index];
if(currentLight->shadowType() == ShadowType::NO_SHADOW)
lightName += "Simple ";
else if(currentLight->shadowType() == ShadowType::SHADOWMAP_SHADOW)
lightName += "Shadowmap ";
else if(currentLight->shadowType() == ShadowType::TRACED_SHADOW)
lightName += "Traced ";
if(currentLight->type() == LightType::POINT_LIGHT)
lightName += "Point ";
else if(currentLight->type() == LightType::DIR_LIGHT)
lightName += "Directional ";
else if(currentLight->type() == LightType::SPOT_LIGHT)
lightName += "Spot ";
if(ImGui::Button(ICON_MDI_DELETE))
{
m_renderer->removeLight(light_index);
m_selectedLightIndices.clear();
m_activeLightIndex = std::nullopt;
--light_index;
}
ImGui::SameLine();
if(ImGui::Button(ICON_MDI_ARROW_EXPAND_HORIZONTAL))
{
ImGui::OpenPopup("Convert");
}
if(ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted("Convert to ..");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
if(ImGui::BeginPopup("Convert"))
{
if(ImGui::MenuItem("ShadowMap Light"))
{
m_renderer->convertLight(*m_activeLightIndex, dino::ShadowType::SHADOWMAP_SHADOW);
}
if(ImGui::MenuItem("Traced Light"))
{
m_renderer->convertLight(*m_activeLightIndex, dino::ShadowType::TRACED_SHADOW);
}
if(ImGui::MenuItem("Simple Light"))
{
m_renderer->convertLight(*m_activeLightIndex, dino::ShadowType::NO_SHADOW);
}
ImGui::EndPopup();
}
ImGui::SameLine();
bool temp_sel = sel;
if(ImGui::Selectable(
(lightName + std::string(" Light [ID: ") + std::to_string(light_index) + "]")
.c_str(),
&temp_sel))
{
if(glfwGetKey(*m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
if(sel)
{
m_selectedLightIndices.erase(it);
}
else
{
m_selectedLightIndices.emplace(light_index);
}
}
else
{
m_selectedLightIndices.clear();
m_selectedLightIndices.emplace(light_index);
m_activeLightIndex = light_index;
}
}
ImGui::PopID();
}
ImGui::PopStyleColor();
ImGui::EndVertical();
ImGui::EndChild();
ImGui::Separator();
ImGui::BeginChild("##settings");
if(m_activeLightIndex)
{
auto light = m_renderer->getLights()[*m_activeLightIndex];
const dino::LightType type = light->type();
float intensity = light->intensity();
glm::vec3 col = light->color();
if(ImGui::ColorEdit3("Color ",
glm::value_ptr(col),
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR))
{
light->setColor(col);
}
if(ImGui::DragFloat("Intensity ", &intensity, 0.01f))
{
light->setIntensity(intensity);
}
if(type == dino::LightType::SPOT_LIGHT || type == dino::LightType::POINT_LIGHT)
{
glm::vec3 pos = light->position();
if(ImGui::DragFloat3("Position", glm::value_ptr(pos), 0.01f))
light->setPosition(pos);
}
if(type == dino::LightType::SPOT_LIGHT || type == dino::LightType::DIR_LIGHT)
{
glm::vec3 dir = light->direction();
if(ImGui::DragFloat3("Direction", glm::value_ptr(dir), 0.01f))
light->setDirection(dir);
}
if(type == dino::LightType::SPOT_LIGHT)
{
float cutoff = light->cutoff();
if(ImGui::DragFloat("Cutoff", &cutoff, 0.01f))
light->setCutoff(cutoff);
}
}
// if(pixelObjectId != -1)
//{
// if(!renderer->getFunctionByID(pixelObjectId))
// {
// if(!m_selectedFieldIndices.empty())
//{
// if(m_selectedFieldIndices.size() == 1)
// {
// const auto opt_id = m_allFieldIds[*m_selectedFieldIndices.begin()];
// if(!opt_id)
// {
// ImGui::BeginHorizontal("FieldInfo", ImVec2(ImGui::GetContentRegionAvailWidth(),
// 0)); ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); ImGui::Text("
// Distance Field [New*]"); ImGui::Spring(); if(ImGui::Button(ICON_MDI_SETTINGS "
// Edit"))
// {
// glfwShowWindow(*m_editorWindow);
// }
// ImGui::PopStyleColor();
// ImGui::EndHorizontal();
// }
// else
// {
// ImGui::BeginHorizontal("FieldInfo", ImVec2(ImGui::GetContentRegionAvailWidth(),
// 0)); ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); ImGui::Text(("
// Distance Field [ID: " + std::to_string(*opt_id) + "]").c_str()); ImGui::Spring();
// if(ImGui::Button(ICON_MDI_SETTINGS " Edit"))
// {
// glfwShowWindow(*m_editorWindow);
// }
// ImGui::PopStyleColor();
// ImGui::EndHorizontal();
// }
// }
//}
ImGui::EndChild();
} // namespace dino
void Editor::drawRenderingTab() { ImGui::Text("Rendering."); }
......@@ -395,7 +721,7 @@ void Editor::drawSettings()
ImGui::Begin("Scene");
{
enum class Tab
{
{
FIELDS,
LIGHTS,
RENDERING
......@@ -428,16 +754,28 @@ void Editor::drawSettings()
}
}
ImGui::End();
processInputsMain();
}
void Editor::selectByIndex(uint32_t index)
{
m_activeFieldIndex = index;
m_selectedFieldIndices.clear();
m_selectedFieldIndices.emplace(index);
m_renderer->setActivePixelObjectId(m_allFieldIds[index] ? *m_allFieldIds[index] : -1);
}
void Editor::select(std::optional<uint32_t> id, bool showBlueprint)
void Editor::select(std::optional<uint32_t> id, bool showBlueprint, bool deselectOther)
{
if(deselectOther)
m_selectedFieldIndices.clear();
if(!id)
{
if(m_activeFieldIndex != std::nullopt)
glfwHideWindow(*m_editorWindow);
m_activeFieldIndex = std::nullopt;
m_selectedFieldIndices.clear();
m_renderer->setActivePixelObjectId(-1);
return;
}
const auto it = std::find_if(m_allFieldIds.begin(), m_allFieldIds.end(), [id](const auto& opt) {
......@@ -473,6 +811,9 @@ void Editor::select(std::optional<uint32_t> id, bool showBlueprint)
}
}
}
const auto opt = m_allFieldIds[*m_activeFieldIndex];
if(opt)
m_renderer->setActivePixelObjectId(*opt);
}
void Editor::drawBlueprint()
......@@ -527,6 +868,95 @@ void Editor::drawBlueprint()
glfwGetFramebufferSize(*m_editorWindow, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);
processInputsEditor();
}
void Editor::processInputsEditor()
{
if(m_ctrlC.pressed(*m_editorWindow))
{
m_blueprints[*m_activeFieldIndex]->copyNodes();
}
if(m_ctrlV.pressed(*m_editorWindow))
{
m_blueprints[*m_activeFieldIndex]->pasteNodes();
}
if(m_ctrlSEditor.pressed(*m_editorWindow))
{
const std::filesystem::path& path = m_blueprints[*m_activeFieldIndex]->getPath();
if(!path.empty())
{
saveField(*m_activeFieldIndex, path);
}
else
{
const char* fileEnding = "*.json";
const char* filePath =
tinyfd_saveFileDialog("Save", nullptr, 1, &fileEnding, "JSON-Files");
if(filePath)
{
saveField(*m_activeFieldIndex, filePath);
m_blueprints[*m_activeFieldIndex]->setPath(filePath);
}
}
}
if(m_shiftTabEditor.pressed(*m_editorWindow))
{
selectByIndex((*m_activeFieldIndex + 1) % m_allFieldIds.size());
}
}
void Editor::processInputsMain()
{
if(glfwGetKey(*m_window, GLFW_KEY_DELETE) == GLFW_PRESS)
{
for(auto index : m_selectedFieldIndices)
{
deleteField(0);
}
m_selectedFieldIndices.clear();
select(std::nullopt, false);
}
if(m_ctrlO.pressed(*m_window))
{
if(glfwGetKey(*m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
const char* fileEnding = "*.json";
const char* filePath =
tinyfd_openFileDialog("Load", nullptr, 1, &fileEnding, "JSON-Files", false);
if(filePath)
{
loadField(filePath);
select(m_allFieldIds.back(), false);
}
}
else
{
loadScene();
}
}
if(m_ctrlSMain.pressed(*m_window))
{
saveScene();
}
if(m_shiftTabMain.pressed(*m_window))
{
selectByIndex((*m_activeFieldIndex + 1) % m_allFieldIds.size());
}
if(m_ctrlA.pressed(*m_window))
{
if(m_selectedFieldIndices.size() == m_allFieldIds.size())
{
m_selectedFieldIndices.clear();
select(std::nullopt, false, true);
}
else if(!m_allFieldIds.empty())
{
m_selectedFieldIndices.clear();
for(size_t i = 0; i < m_allFieldIds.size(); ++i)
m_selectedFieldIndices.emplace(i);
}
}
}
void Editor::processInputs()
......
......@@ -49,19 +49,24 @@ public:
void drawSettings();
void drawBlueprint();
void select(std::optional<uint32_t> id, bool showBlueprint);
void select(std::optional<uint32_t> id, bool showBlueprint, bool deselectOther = true);
private:
void drawFieldsTab();
void drawLightsTab();
void drawRenderingTab();
void drawPreview();
void processInputs();
void processInputsEditor();
void processInputsMain();
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();
bool editorShown() const;
void prepareTextures();
......@@ -81,14 +86,20 @@ private:
std::shared_ptr<RendererDeferred> m_renderer;
std::unique_ptr<Window> m_editorWindow;
std::unordered_set<uint32_t> m_selectedFieldIndices;
std::unordered_set<uint32_t> m_selectedLightIndices;
std::vector<uint32_t> m_selectedLightIds;
std::vector<std::optional<uint32_t>> m_allFieldIds;
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_ctrlS = KeyboardButton(GLFW_KEY_S, GLFW_KEY_LEFT_CONTROL);
KeyboardButton m_shiftTab = KeyboardButton(GLFW_KEY_LEFT_SHIFT, GLFW_KEY_TAB);
std::vector<dino::Node> m_copiedNodes;
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_shiftTabEditor = 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
This diff is collapsed.
......@@ -3043,7 +3043,7 @@ void Blueprints::save(std::filesystem::path path, const std::shared_ptr<dino::Fu
return;
}*/
nlohmann::json json = func->serialize();
auto mat = renderer.getMaterials()[renderer.getFunctionId(func)];
auto mat = renderer.getMaterials().at(renderer.getFunctionId(func));
json["material"] = mat->serialize();
while(path.extension() != ".json")
{
......
......@@ -25,9 +25,9 @@ void Renderer::add(const std::shared_ptr<Operator>& op, const std::shared_ptr<Ma
renderable.bounds = (m_distanceFields[id] = std::make_shared<DistanceField>(op))->bounds();
if(!material)
m_materials.push_back(std::make_shared<Material>());
m_materials[id] = std::make_shared<Material>();
else
m_materials.push_back(material);
m_materials[id] = material;
m_infos.push_back(renderable);
m_invalidRenderables = true;
......@@ -49,9 +49,9 @@ void Renderer::add(const std::shared_ptr<Primitive>& prim,
renderable.bounds = (m_distanceFields[id] = std::make_shared<DistanceField>(prim))->bounds();
if(!material)
m_materials.push_back(std::make_shared<Material>());
m_materials[id] = std::make_shared<Material>();
else
m_materials.push_back(material);
m_materials