Commit 3d843167 authored by Johannes Braun's avatar Johannes Braun
Browse files

Renamed some methods

parent 8b8fc5fd
build_VS
.vs
.idea
assets/screenshots/*
assets/meshes/*
assets/textures/*
......
#include "some_tree.h"
\ No newline at end of file
#ifndef INCLUDE_SOME_TREE_H
#define INCLUDE_SOME_TREE_H
#include <functional>
#include <memory>
#include <vector>
#include <core/numeric/geometry.h>
template<typename T>
struct some_locality_tree
{
using converter = std::function<math::Bounds(const T& src)>;
enum class node_type : uint32_t
{
eRoot = 0,
eLeaf,
eInner
};
struct node
{
int32_t parent = -1;
node_type type;
int32_t left;
int32_t right;
int32_t data_index;
int32_t bounds_index;
uint32_t cost;
};
some_locality_tree(converter bounds_from_data)
: m_converter(bounds_from_data), m_depth(0)
{
}
void put(const T& data)
{
auto index = m_data_storage.size();
m_data_storage.push_back(data);
if (m_node_storage.empty())
{
node n;
n.type = node_type::eRoot;
n.data_index = 0;
n.cost = 0;
m_node_storage.emplace_back();
n.bounds_index = m_bounds_storage.size();
m_bounds_storage.push_back(m_converter(data));
m_depth = 1;
return;
}
// when adding a node... check if inside the whole bb
const auto bounds = m_converter(data);
m_bounds_storage.push_back(bounds);
auto current_id = 0;
unsigned depth = 0;
while (m_node_storage[current_id].type != node_type::eLeaf && m_node_storage.size() > 1)
{
// decide which child is being overlapped the most
auto cv_l = m_converter(get_bounds_from_dyn(m_node_storage[m_node_storage[current_id].left]));
auto cv_r = m_converter(get_bounds_from_dyn(m_node_storage[m_node_storage[current_id].right]));
auto cost_l = m_node_storage[m_node_storage[current_id].left].cost;
auto cost_r = m_node_storage[m_node_storage[current_id].right].cost;
auto l = cv_l.overlap(bounds);
auto r = cv_r.overlap(bounds);
bool pred = (l != 0 && r != 0) ? (l / float(cost_l) > r / float(cost_r)) : ((l == 0 && r != 0) ? false : ((l != 0 && r == 0) ? true : (cost_l < cost_r)));
if (pred)
{
++(m_node_storage[m_node_storage[current_id].left].cost);
current_id = m_node_storage[current_id].left;
}
else
{
++(m_node_storage[m_node_storage[current_id].right].cost);
current_id = m_node_storage[current_id].right;
}
++depth;
}
if (depth >= m_depth)
m_depth = depth + 1;
//copy old node!
node old_node = m_node_storage[current_id];
auto old_parent = old_node.parent;
old_node.parent = current_id;
old_node.type = node_type::eLeaf;
node new_node;
new_node.type = node_type::eLeaf;
new_node.data_index = index;
new_node.parent = current_id;
new_node.bounds_index = m_bounds_storage.size() - 1;
// now being at a leaf node.
// create a parent, attach the leaf to it.
auto old_bounds = m_converter(get_from(old_node));
int sizing = 0;
auto center_old = old_bounds.center();
auto center_new = bounds.center();
for (auto i = 0; i < 3; ++i)
{
if (center_old[i] < center_new[i])
++sizing;
}
auto node_id_left = m_node_storage.size() + (sizing >= 2 ? 0 : 1);
auto node_id_right = m_node_storage.size() + (sizing >= 2 ? 1 : 0);
m_node_storage.push_back(sizing >= 2 ? old_node : new_node);
m_node_storage.push_back(sizing >= 2 ? new_node : old_node);
node new_parent;
new_parent.parent = old_parent;
new_parent.left = node_id_left;
new_parent.right = node_id_right;
new_parent.type = old_parent == -1 ? node_type::eRoot : node_type::eInner;
old_bounds.expand(bounds);
math::Bounds new_bounds = old_bounds;
new_parent.bounds_index = m_bounds_storage.size();
m_bounds_storage.push_back(new_bounds);
if (new_parent.type != node_type::eRoot)
{
auto c_id = new_parent.parent;
while(true) {
auto&& data = get_bounds_from_dyn(m_node_storage[c_id]);
data.expand(old_bounds);
if (m_node_storage[c_id].type == node_type::eRoot)
break;
c_id = m_node_storage[c_id].parent;
}
}
m_node_storage[current_id] = new_parent;
}
const T& get_from(const node& n)
{
return m_data_storage[n.data_index];
}
unsigned depth() const
{
return m_depth;
}
private:
math::Bounds& get_bounds_from_dyn(const node& n)
{
return m_bounds_storage[n.bounds_index];
}
unsigned m_depth;
converter m_converter;
std::vector<node> m_node_storage;
std::vector<math::Bounds> m_bounds_storage;
std::vector<T> m_data_storage;
};
#endif // !INCLUDE_SOME_TREE_H
......@@ -2,42 +2,10 @@
#include <random>
#include <util/log.h>
#include <core/time.h>
#include "dat/some_tree.h"
int main()
{
some_locality_tree<math::Bounds> loctree([](const auto& x) { return x; });
const static auto rnd_bounds = []() {
std::random_device dev;
std::mt19937 gen(dev());
std::uniform_real_distribution<float> dst(-100.f, 100.f);
auto v1 = glm::vec4(dst(gen), dst(gen), dst(gen), 1);
auto v2 = glm::vec4(dst(gen), dst(gen), dst(gen), 1);
return math::Bounds{ glm::min(v1, v2), glm::max(v1, v2) };
};
constexpr auto x = 4000;
std::array<math::Bounds, x> bounds;
std::vector<math::Bounds> test;
for (auto i = 0; i < x; ++i)
{
bounds[i] = rnd_bounds();
}
glare::core::ClockNS clocky;
clocky.restart();
//test.reserve(10000);
for (int i = 0; i < x; ++i)
{
test.insert(test.begin(), bounds[i]);
}
Log_Info << "Took " << clocky.time();
for(auto&& b : bounds)
loctree.put(b);
Log_Info << sizeof(int*);
bool b = true;
......
......@@ -8,8 +8,6 @@
#include <core/state.h>
#include <core/audio/sound_source.h>
#include <core/objects/skybox.h>
#include <core/rendering/gbuffer.h>
#include <core/rendering/batch_render_list.h>
#include <core/res/res.h>
#include <raytrace/tracer/pathtracer.h>
......@@ -31,7 +29,7 @@ enum class RenderMode
// Default paths for easy change access
const fs::path engine_settings_path = files::asset("/preferences/default.xml");
const fs::path pathtracer_settings_path = files::asset("/preferences/pathtracer_default.xml");
const fs::path skybox_files_path = files::asset("/textures/lycksele2/");
const fs::path skybox_files_path = files::asset("/textures/ryfjallet/");
const fs::path env_audio_path = files::asset("/audio/env.wav");
const fs::path startup_scene_path = files::asset("/meshes/scenery/TY_Plane.dae");
......@@ -187,7 +185,7 @@ void drawSceneWindow()
case 1:
selected_node->iterateComponentsStable([](core::GraphNodeComponent& component)
{
component.gui();
component.onGui();
});
break;
}
......
......@@ -7,7 +7,7 @@
namespace glare::component
{
void FramerateCounter::update()
void FramerateCounter::onUpdate()
{
frame++;
time += core::Time::deltaTime();
......
......@@ -8,7 +8,7 @@ namespace glare::component
class FramerateCounter : public core::GraphNodeComponent
{
public:
void update() override;
void onUpdate() override;
private:
float fps = 0;
......
......@@ -77,7 +77,7 @@ namespace glare::component
}
}
void PlayerController::gui()
void PlayerController::onGui()
{
if (isPossessed())
{
......@@ -90,7 +90,7 @@ namespace glare::component
}
}
void PlayerController::update()
void PlayerController::onUpdate()
{
if (!isPossessed()) return;
......
......@@ -19,9 +19,9 @@ namespace glare::component
void onKeyAction(controls::Key key, controls::ButtonAction action, controls::KeyMods mods);
void onMouseButtonAction(controls::MouseButton button, controls::ButtonAction action, controls::KeyMods mods);
void update() override;
void onUpdate() override;
void gui() override;
void onGui() override;
bool isPossessed() const;
......
......@@ -13,7 +13,7 @@ namespace glare::component
}
void Rotator::update()
void Rotator::onUpdate()
{
owner()->transform.rotate(glm::vec3(0, glm::radians(80.f), 0) * static_cast<float>(core::Time::deltaTime()));
}
......
......@@ -11,7 +11,7 @@ namespace glare::component
Rotator();
~Rotator();
void update() override;
void onUpdate() override;
};
}
......
......@@ -17,7 +17,7 @@ namespace glare::core
setBuffer(buffer);
}
void SoundSource::update()
void SoundSource::onUpdate()
{
glm::vec3 position = owner()->worldTransform().position;
......
......@@ -18,7 +18,7 @@ namespace glare::core
/**
* \brief Important for local sound sources: Updates the position and velocity of this sound source.
*/
void update() override;
void onUpdate() override;
void play() const;
void pause() const;
......
......@@ -23,16 +23,14 @@ namespace glare::core
/**
* \brief Will get called when the parent node transformation has changed.
*/
virtual void onTransformChanged();
virtual void onTransform();
/**
* \brief Can be used to draw the component settings via ImGui.
*/
virtual void gui();
virtual void update();
virtual void draw(DrawMode mode);
virtual void onGui();
virtual void onUpdate();
virtual void onDraw(DrawMode mode);
template <typename T>
std::shared_ptr<T> detachAs();
......@@ -48,19 +46,19 @@ namespace glare::core
{
}
inline void GraphNodeComponent::onTransformChanged()
inline void GraphNodeComponent::onTransform()
{
}
inline void GraphNodeComponent::gui()
inline void GraphNodeComponent::onGui()
{
}
inline void GraphNodeComponent::update()
inline void GraphNodeComponent::onUpdate()
{
}
inline void GraphNodeComponent::draw(DrawMode mode)
inline void GraphNodeComponent::onDraw(DrawMode mode)
{
}
......
#ifndef INCLUDE_INTERACTABLE_H
#define INCLUDE_INTERACTABLE_H
namespace glare::core
{
enum class DrawMode;
class Interactable
{
public:
void onGui();
void onUpdate();
void onDraw(DrawMode mode);
/**
* \brief Will get called when this node's transformation has changed.
*/
void onTransfform();
};
}
#endif //!INCLUDE_INTERACTABLE_H
\ No newline at end of file
......@@ -26,29 +26,29 @@ namespace glare::core
ImGui::PopID();
}
void GraphNode::update(const glm::mat4 &parent_transform)
void GraphNode::update()
{
const glm::mat4 absolute_transform = parent_transform * transform.matrix();
const glm::mat4 absolute_transform = parent() ? parent()->worldTransform().matrix() * transform.matrix() : glm::mat4(1.0f);
m_world_transform.fromMatrix(absolute_transform);
callAndTraverse(&GraphNodeComponent::update, &GraphNode::update, absolute_transform);
callAndTraverse(&GraphNodeComponent::onUpdate, &GraphNode::update);
if (m_last_transform != transform)
{
m_last_transform = transform;
messaging::Handler::getInstance().submit(tags::graph_transform, transform);
onAbsoluteTransformChanged();
notifyTransformChanged();
}
}
void GraphNode::draw(DrawMode mode)
{
callAndTraverse(&GraphNodeComponent::draw, &GraphNode::draw, mode);
callAndTraverse(&GraphNodeComponent::onDraw, &GraphNode::draw, mode);
}
void GraphNode::onAbsoluteTransformChanged()
void GraphNode::notifyTransformChanged()
{
callAndTraverse(&GraphNodeComponent::onTransformChanged, &GraphNode::onAbsoluteTransformChanged);
callAndTraverse(&GraphNodeComponent::onTransform, &GraphNode::notifyTransformChanged);
}
const math::Transform& GraphNode::worldTransform() const
......
......@@ -25,13 +25,13 @@ namespace glare::core
*/
void gui();
void update(const glm::mat4 &parent_transform = glm::mat4(1));
void update();
void draw(DrawMode mode = DrawMode::eShaded);
/**
* \brief Will get called when this node's transformation has changed.
*/
void onAbsoluteTransformChanged();
void notifyTransformChanged();
/**
* \brief The world transformation will be updated on every update call.
......
......@@ -22,7 +22,7 @@ namespace glare::core
{
}
void Camera::gui()
void Camera::onGui()
{
if (ImGui::ComponentHeader("Camera", owner()->name().c_str(), id()))
{
......@@ -51,7 +51,7 @@ namespace glare::core
return m_dof_size;
}
void Camera::update()
void Camera::onUpdate()
{
if (m_adapt_to_screen)
{
......@@ -84,7 +84,7 @@ namespace glare::core
}
}
void Camera::onTransformChanged()
void Camera::onTransform()
{
messaging::Handler::getInstance().submit(tags::camera, 1);
}
......
......@@ -35,9 +35,9 @@ namespace glare::core
/**
* \brief Updates all camera matrices.
*/
void update() override;
void gui() override;
void onTransformChanged() override;
void onUpdate() override;
void onGui() override;
void onTransform() override;
void resize(unsigned width, unsigned height);
......
......@@ -32,7 +32,7 @@ namespace glare::core
return m_id;
}
void LightComponent::update()
void LightComponent::onUpdate()
{
if (!m_shadow_map_renderer && owner() && m_data.light_type == LightType::eDirectional) {
m_shadow_map_renderer = std::make_unique<ShadowMap>(4096, 8, owner());
......@@ -105,13 +105,13 @@ namespace glare::core
return{ buildLight(), shadow_map };
}
void LightComponent::onTransformChanged()
void LightComponent::onTransform()
{
// Light just has changed
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, this });
}
void LightComponent::gui()
void LightComponent::onGui()
{
if (ImGui::ComponentHeader("Light", owner()->name().c_str(), id()))
{
......@@ -180,7 +180,7 @@ namespace glare::core
}
}
void LightComponent::draw(DrawMode mode)
void LightComponent::onDraw(DrawMode mode)
{
switch (mode) {
case DrawMode::eShaded:
......
Markdown is supported
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