Commit 81835268 authored by unknown's avatar unknown
Browse files

Nearly working resizing. Only GBuffer is bugging out

parent bae66b8e
......@@ -7,38 +7,6 @@
<item name="msaa" value="4"/>
</group>
<group name="splash">
<item name="logo" value="/textures/splash.png"/>
<item name="sound" value="/audio/swoosh.wav"/>
<item name="color" value="#0277BD"/>
<item name="duration" value="200"/>
</group>
<group name="pathtracer">
<group name="bounces">
<item name="diffuse" value="4"/>
<item name="translucent" value="4"/>
<item name="reflect" value="5"/>
<item name="refract" value="8"/>
<item name="transparent" value="9"/>
<item name="emissive" value="1"/>
</group>
<group name="ls-thresholds">
<item name="diffuse" value="4"/>
<item name="translucent" value="4"/>
<item name="reflect" value="5"/>
<item name="refract" value="8"/>
<item name="transparent" value="9"/>
<item name="emissive" value="1"/>
<item name="accuracy" value="0.4"/>
<item name="shadow" value="0.04"/>
</group>
<item name="clamp-direct" value="8"/>
<item name="clamp-indirect" value="8"/>
</group>
<group name="linespace">
<item name="subdivisions" value="24"/>
</group>
......
<pathtracer version="1.0">
<group name="bounces">
<item name="diffuse" value="4"/>
<item name="translucent" value="4"/>
<item name="reflect" value="5"/>
<item name="refract" value="8"/>
<item name="transparent" value="9"/>
<item name="emissive" value="1"/>
</group>
<group name="ls-thresholds">
<item name="diffuse" value="4"/>
<item name="translucent" value="4"/>
<item name="reflect" value="5"/>
<item name="refract" value="8"/>
<item name="transparent" value="9"/>
<item name="emissive" value="1"/>
<item name="accuracy" value="0.4"/>
<item name="shadow" value="0.04"/>
<item name="distance" value="12"/>
</group>
<item name="clamp-direct" value="8"/>
<item name="clamp-indirect" value="8"/>
</pathtracer>
\ No newline at end of file
<splash version="1.0">
<item name="logo" value="/textures/splash.png"/>
<item name="sound" value="/audio/swoosh.wav"/>
<item name="color" value="#0277BD"/>
<item name="duration" value="200"/>
</splash>
\ No newline at end of file
assets/screenshots/render.png

1.64 MB | W: | H:

assets/screenshots/render.png

1.79 MB | W: | H:

assets/screenshots/render.png
assets/screenshots/render.png
assets/screenshots/render.png
assets/screenshots/render.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -158,8 +158,7 @@ struct TraceProperties
float shadow_importance;
uint bounce_amount;
//Just to give the struct a good padding
uint prop_unused_02;
float travelled_distance;
};
struct Trace
......
......@@ -48,4 +48,5 @@ void resetImportance(int id)
b_traces[id].properties.accuracy_importance = 1.f;
b_traces[id].properties.shadow_importance = 1.f;
b_traces[id].properties.bounce_amount = 0;
b_traces[id].properties.travelled_distance = 0;
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ void resetImportance(uint id)
b_traces[id].properties.accuracy_importance = 1.f;
b_traces[id].properties.shadow_importance = 1.f;
b_traces[id].properties.bounce_amount = 0;
b_traces[id].properties.travelled_distance = 0;
}
void main()
......
......@@ -36,6 +36,7 @@ uniform struct LinespaceProperties
float accuracy_quality;
float shadow_quality;
uint bounce_thresholds;
float distance_threshold;
} u_linespace_properties;
......@@ -101,6 +102,8 @@ bool shade(int id, inout vec3 radiance, uint bounce, out uint bsdf_id)
Material material;
Vertex vertex = hit.getVertex(material);
vec4 diffuse = material.getDiffuse(vertex.texcoord);
b_traces[id].properties.travelled_distance += b_traces[id].hit.valid() ? (distance(b_traces[id].ray.origin, vertex.position.xyz)) : FLT_MAX;
//As the px and py values of the ray are floats instead of integers,
//we can extract the random sample by extracting the subpixel offset.
......@@ -138,7 +141,8 @@ bool shade(int id, inout vec3 radiance, uint bounce, out uint bsdf_id)
Ray shadow_test = light.makeShadowRay(vertex, random_sample, light_distance);
bool use_bvh = (trace.properties.shadow_importance > (1-u_linespace_properties.shadow_quality))
&& (b_traces[id].getBounceAmount(bsdf_id) < getLSBounceThreshold(bsdf_id));
&& (b_traces[id].getBounceAmount(bsdf_id) < getLSBounceThreshold(bsdf_id))
&& b_traces[id].properties.travelled_distance < u_linespace_properties.distance_threshold;
if (!shadow_test.intersectsAny(light_distance, use_bvh))
{
float angle = angle(shadow_test, vertex);
......@@ -174,7 +178,8 @@ bool trace(int id, uint last_bsdf_id)
//Use a BVH if accuracy is important
bool use_bvh = (b_traces[id].properties.accuracy_importance > (1-u_linespace_properties.accuracy_quality))
&& (b_traces[id].getBounceAmount(last_bsdf_id) < getLSBounceThreshold(last_bsdf_id));
&& (b_traces[id].getBounceAmount(last_bsdf_id) < getLSBounceThreshold(last_bsdf_id))
&& b_traces[id].properties.travelled_distance < u_linespace_properties.distance_threshold;
bool intersects = b_traces[id].ray.nearestIntersection(b_traces[id].hit, use_bvh);
return intersects;
}
......@@ -195,7 +200,7 @@ void main()
bool success = shade(id, radiance, 0, last_bsdf_id);
//Then shade all valid hits as long as they bounce around happily in the scene.
for (int bounce = 0; bounce <= u_render_config.num_bounces && success; ++bounce) {
for (int bounce = 0; bounce < u_render_config.num_bounces && success; ++bounce) {
success = trace(id, last_bsdf_id);
success = bool(uint(success) & uint(shade(id, radiance, bounce + 1, last_bsdf_id)));
}
......
......@@ -31,6 +31,20 @@ namespace glare
}
}
void Application::onFramebufferSize(unsigned width, unsigned height)
{
Log_Info << "Resized: " << width << ", " << height;
core::EngineState::opengl_core->updateSize(width, height);
core::EngineState::main_camera->update();
m_gbuffer->updateSize(width, height);
m_config.window_width = width;
m_config.window_height = height;
m_collector->setDirty(glare::advanced::SceneCollector::DirtyFlags::eCamera);
m_collector->collect();
gl::viewport(0, 0, width, height);
}
Application::Application(int argc, char* argv[])
{
m_arguments = std::make_unique<io::Arguments>(argc, argv);
......@@ -39,50 +53,18 @@ namespace glare
preferences_file.load_file(core::asset("/preferences/default.xml").c_str());
// BASIC CONFIG
const auto config_child = preferences_file.child("settings").find_child_by_attribute("group", "name", "config");
m_config.window_width = config_child.find_child_by_attribute("item", "name", "window_size_x").attribute("value").as_uint(1280);
m_config.window_height = config_child.find_child_by_attribute("item", "name", "window_size_y").attribute("value").as_uint(720);
m_config.window_title = config_child.find_child_by_attribute("item", "name", "window_title").attribute("value").as_string("GLARE");
m_config.vsync = VSyncMode(config_child.find_child_by_attribute("item", "name", "vsync").attribute("value").as_uint(0));
m_config.sample_count = config_child.find_child_by_attribute("item", "name", "msaa").attribute("value").as_uint(4);
// SPLASH SCREEN
const auto splash_child = preferences_file.child("settings").find_child_by_attribute("group", "name", "splash");
const auto splash_image = core::asset(splash_child.find_child_by_attribute("item", "name", "logo").attribute("value").as_string());
const auto splash_sound = core::asset(splash_child.find_child_by_attribute("item", "name", "sound").attribute("value").as_string());
const auto splash_background = color::hex<color::rgba32f>(splash_child.find_child_by_attribute("item", "name", "color").attribute("value").as_string("#f"));
const auto splash_duration = splash_child.find_child_by_attribute("item", "name", "duration").attribute("value").as_uint();
//Pathtracer
const auto pt_child = preferences_file.child("settings").find_child_by_attribute("group", "name", "pathtracer");
//bounces
const auto bounces_child = pt_child.find_child_by_attribute("group", "name", "bounces");
uint8_t effect_bounces_diffuse = bounces_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
uint8_t effect_bounces_translucent = bounces_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
uint8_t effect_bounces_reflect = bounces_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
uint8_t effect_bounces_refract = bounces_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
uint8_t effect_bounces_transparent = bounces_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
uint8_t effect_bounces_emissive = bounces_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);
//ls thresholds
const auto ls_thresholds_child = pt_child.find_child_by_attribute("group", "name", "ls-thresholds");
uint8_t effect_thresh_ls_bounces_diffuse = ls_thresholds_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
uint8_t effect_thresh_ls_bounces_translucent = ls_thresholds_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
uint8_t effect_thresh_ls_bounces_reflect = ls_thresholds_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
uint8_t effect_thresh_ls_bounces_refract = ls_thresholds_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
uint8_t effect_thresh_ls_bounces_transparent = ls_thresholds_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
uint8_t effect_thresh_ls_bounces_emissive = ls_thresholds_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);
float accuracy_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "accuracy").attribute("value").as_float(0.5f);
float shadow_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "shadow").attribute("value").as_float(0.5f);
float clamp_direct = pt_child.find_child_by_attribute("item", "name", "clamp-direct").attribute("value").as_float(8);
float clamp_indirect = pt_child.find_child_by_attribute("item", "name", "clamp-indirect").attribute("value").as_float(8);
const auto config_child = preferences_file.child("settings").find_child_by_attribute("group", "name", "config");
m_config.window_width = config_child.find_child_by_attribute("item", "name", "window_size_x").attribute("value").as_uint(1280);
m_config.window_height = config_child.find_child_by_attribute("item", "name", "window_size_y").attribute("value").as_uint(720);
m_config.window_title = config_child.find_child_by_attribute("item", "name", "window_title").attribute("value").as_string("GLARE");
m_config.vsync = VSyncMode(config_child.find_child_by_attribute("item", "name", "vsync").attribute("value").as_uint(0));
m_config.sample_count = config_child.find_child_by_attribute("item", "name", "msaa").attribute("value").as_uint(4);
core::EngineState::initialize(m_config.window_width, m_config.window_height, m_config.window_title);
core::Callbacks::addKeyDownCallback("main_application", std::bind(&Application::onKeyDown, this, std::placeholders::_1, std::placeholders::_2));
core::SplashScreen splash_screen(splash_image, splash_sound, splash_background, splash_duration);
core::Callbacks::addFramebufferSizeCallback("main_application", std::bind(&Application::onFramebufferSize, this, std::placeholders::_1, std::placeholders::_2));
core::SplashScreen splash_screen(core::asset("/preferences/splash_01.xml"));
splash_screen.run();
m_current_scene_root = core::asset("/meshes/scenery/");
......@@ -95,33 +77,16 @@ namespace glare
//quitPromptDefault(0);
//initializeScene(m_current_scene_root / "artifact_suzanne_x2_glass_0r.dae", 1.f);
initializeScene(m_current_scene_root / "artifact_suzanne_x2_glass_0r.dae", 1.f);
//initializeScene(m_current_scene_root / "artifact_suzanne_x2_mirror_0r.dae", 1.f);
//initializeScene(m_current_scene_root / "artifact_suzanne_x2_transparent.dae", 1.f);
initializeScene(m_current_scene_root / "benchmark_stfd_bunny_diff.dae", 1.f);
//initializeScene(m_current_scene_root / "benchmark_stfd_bunny_diff.dae", 1.f);
initializeRenderRequirements();
initializeAdvanced();
initializeGUI();
m_raytracer->setEffectBounces(advanced::EffectType::eDiffuse, effect_bounces_diffuse);
m_raytracer->setEffectBounces(advanced::EffectType::eTranslucent, effect_bounces_translucent);
m_raytracer->setEffectBounces(advanced::EffectType::eReflection, effect_bounces_reflect);
m_raytracer->setEffectBounces(advanced::EffectType::eRefraction, effect_bounces_refract);
m_raytracer->setEffectBounces(advanced::EffectType::eTransparent, effect_bounces_transparent);
m_raytracer->setEffectBounces(advanced::EffectType::eEmissive, effect_bounces_emissive);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eDiffuse, effect_thresh_ls_bounces_diffuse);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eTranslucent, effect_thresh_ls_bounces_translucent);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eReflection, effect_thresh_ls_bounces_reflect);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eRefraction, effect_thresh_ls_bounces_refract);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eTransparent, effect_thresh_ls_bounces_transparent);
m_raytracer->setLinespaceBounceThresholds(advanced::EffectType::eEmissive, effect_thresh_ls_bounces_emissive);
m_raytracer->setAccuracyThreshold(accuracy_threshold);
m_raytracer->setShadowThreshold(shadow_threshold);
m_raytracer->setClampDirect(clamp_direct);
m_raytracer->setClampIndirect(clamp_indirect);
m_raytracer->loadSettings(core::asset("/preferences/pathtracer_default.xml"));
gl::setEnabled(gl::EnableParameter::eMultisample, true);
......@@ -326,12 +291,12 @@ namespace glare
ImGui::Spacing();
int max_samples = m_raytracer->maxSamples();
ImGui::DragInt("Maximum", &max_samples, 1, 1, 50000);
ImGui::DragInt("Maximum", &max_samples, 10, 1, 50000);
if (max_samples != m_raytracer->maxSamples())
m_raytracer->setMaxSamples(max_samples);
int spfr = m_raytracer->samplesPerFrame();
ImGui::DragInt("per frame", &spfr, 1, 1, 10);
ImGui::DragInt("per frame", &spfr, 0.1f, 1, 10);
if (spfr != m_raytracer->samplesPerFrame())
m_raytracer->setSamplesPerFrame(spfr);
ImGui::PopID();
......@@ -342,7 +307,7 @@ namespace glare
ImGui::TextWrapped("The global bounce limit clamps down all effect-dependant bounce limits to a unified maximum value.");
int bounces = m_raytracer->bounces();
ImGui::DragInt("Global Limit", &bounces, 1, 0, 15);
ImGui::DragInt("Global Limit", &bounces, 0.1f, 0, 15);
if (bounces != m_raytracer->bounces())
m_raytracer->setBounces(bounces);
......@@ -497,6 +462,15 @@ namespace glare
m_raytracer->setShadowThreshold(shd);
}
ImGui::Title("Distance Threshold");
ImGui::TextWrapped("Works like a level of detail slider. This is the maximum path length with which the BVH should be used.");
{
float dt = m_raytracer->getDistanceThreshold();
ImGui::DragFloat("Distance", &dt, 0.1f, 0.0f, 10000.f);
if (dt != m_raytracer->getDistanceThreshold())
m_raytracer->setDistanceThreshold(dt);
}
ImGui::Title("Bounce Thresholds");
ImGui::TextWrapped("The following threshold values will determine as of which bounce the Line Space should be used instead of the BVH. This can vary for every kind of effect.");
{
......
......@@ -41,6 +41,8 @@ namespace glare
void updateAdvanced();
void onKeyDown(controls::Key key, controls::KeyMods mods);
void onFramebufferSize(unsigned width, unsigned height);
void drawControlItem(std::string title, std::string description);
void drawDebugWindow();
void drawSceneSelector();
......
......@@ -18,8 +18,8 @@ namespace glare
float shadow_importance;
//Padding properties
glm::uint prop_unused_01;
glm::uint prop_unused_02;
glm::uint bounce_amount;
float travelled_distance;
};
struct Trace
......
......@@ -2,6 +2,7 @@
#include <engine/Messaging.h>
#include <engine/Time.h>
#include <random>
#include <pugixml/pugixml.hpp>
namespace glare
{
......@@ -39,7 +40,7 @@ namespace glare
m_ls_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = 8;
m_ls_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = 0;
m_linespace_config.distance_threshold = 8.f;
m_render_config.bounces = 16;
m_render_shader = core::ShaderProgram::makeUnique("/raytracer/pathtracer.compute");
m_ray_generator = ray_generator;
......@@ -54,6 +55,40 @@ namespace glare
}
void Raytracer::loadSettings(const fs::path &xml_file)
{
pugi::xml_document preferences_file;
Log_Info << "Pathtracer settings file load status: " << preferences_file.load_file(xml_file.c_str()).description();
//Pathtracer
const auto pt_child = preferences_file.child("pathtracer");
//bounces
const auto bounces_child = pt_child.find_child_by_attribute("group", "name", "bounces");
m_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = bounces_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
m_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = bounces_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
m_bounce_count_thresholds[unsigned(EffectType::eReflection)] = bounces_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
m_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = bounces_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
m_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = bounces_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
m_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = bounces_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);
//ls thresholds
const auto ls_thresholds_child = pt_child.find_child_by_attribute("group", "name", "ls-thresholds");
m_ls_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = ls_thresholds_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
m_ls_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = ls_thresholds_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
m_ls_bounce_count_thresholds[unsigned(EffectType::eReflection)] = ls_thresholds_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
m_ls_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = ls_thresholds_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
m_ls_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = ls_thresholds_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
m_ls_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = ls_thresholds_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);
m_linespace_config.accuracy_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "accuracy").attribute("value").as_float(0.5f);
m_linespace_config.shadow_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "shadow").attribute("value").as_float(0.5f);
m_linespace_config.distance_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "distance").attribute("value").as_float(10.f);
m_render_config.clamp_direct = pt_child.find_child_by_attribute("item", "name", "clamp-direct").attribute("value").as_float(8);
m_render_config.clamp_indirect = pt_child.find_child_by_attribute("item", "name", "clamp-indirect").attribute("value").as_float(8);
}
void Raytracer::setEffectBounces(EffectType type, uint8_t bounces)
{
uint8_t clamped = bounces & 0xf;
......@@ -102,6 +137,9 @@ namespace glare
if (width != m_last_width || height != m_last_height) {
m_last_width = width;
m_last_height = height;
Log_Info << "RESIZED: " << width << ", " << height;
m_trace_buffer->setData(sizeof(Trace) * width * height, gl::BufferUsage::eDynamicCopy);
m_render_target = std::make_unique<core::TextureRGBA_32F>(width, height);
m_color_store = std::make_unique<core::TextureRGBA_32F>(width, height);
......@@ -149,6 +187,7 @@ namespace glare
m_render_shader->updateUniformFloat("u_linespace_properties.accuracy_quality", m_linespace_config.accuracy_threshold);
m_render_shader->updateUniformFloat("u_linespace_properties.shadow_quality", m_linespace_config.shadow_threshold);
m_render_shader->updateUniformFloat("u_linespace_properties.distance_threshold", m_linespace_config.distance_threshold);
unsigned bounce_thresholds = 0;
for (int i = 0; i < 8; ++i)
......
......@@ -36,6 +36,8 @@ namespace glare
Raytracer(std::shared_ptr<SceneCollector> collector, std::shared_ptr<RayGeneratorBase> ray_generator);
~Raytracer();
void loadSettings(const fs::path &xml_file);
const core::TextureRGBA_32F &render(uint32_t width, uint32_t height);
void saveRender(fs::path target) const;
......@@ -109,11 +111,14 @@ namespace glare
void setClampIndirect(float clamp_indirect) { m_render_config.clamp_indirect = clamp_indirect; reset(); }
float getAccuracyThreshold() { return m_linespace_config.accuracy_threshold; }
float getShadowThreshold() { return m_linespace_config.shadow_threshold; }
void setAccuracyThreshold(float accuracy_threshold) { m_linespace_config.accuracy_threshold = accuracy_threshold; reset(); }
float getShadowThreshold() { return m_linespace_config.shadow_threshold; }
void setShadowThreshold(float shadow_threshold) { m_linespace_config.shadow_threshold = shadow_threshold; reset(); }
float getDistanceThreshold() { return m_linespace_config.distance_threshold; }
void setDistanceThreshold(float distance_threshold) { m_linespace_config.distance_threshold = distance_threshold; reset(); }
private:
uint32_t m_last_width = 0;
uint32_t m_last_height = 0;
......@@ -131,6 +136,7 @@ namespace glare
float accuracy_threshold;
float shadow_threshold;
uint32_t bounce_thresholds;
float distance_threshold;
} m_linespace_config;
std::array<uint8_t, 8> m_bounce_count_thresholds;
......
......@@ -34,12 +34,13 @@ namespace glare
opengl_core->closeWindow();
}
std::map<std::string, Callbacks::KeyActionCallback> Callbacks::m_onkey_action;
std::map<std::string, Callbacks::KeyDownCallback> Callbacks::m_onkey_down;
std::map<std::string, Callbacks::KeyUpCallback> Callbacks::m_onkey_up;
std::map<std::string, Callbacks::CharCallback> Callbacks::m_onchar;
std::map<std::string, Callbacks::ScrollCallback> Callbacks::m_onscroll;
std::map<std::string, Callbacks::MouseButtonCallback> Callbacks::m_on_mousebutton;
std::map<std::string, callback::key_action> Callbacks::m_onkey_action;
std::map<std::string, callback::key_down> Callbacks::m_onkey_down;
std::map<std::string, callback::key_up> Callbacks::m_onkey_up;
std::map<std::string, callback::character> Callbacks::m_onchar;
std::map<std::string, callback::mouse_button> Callbacks::m_on_mousebutton;
std::map<std::string, callback::scroll> Callbacks::m_onscroll;
std::map<std::string, callback::framebuffer_size> Callbacks::m_on_framebuffer_size;
void Callbacks::initialize(GLFWwindow* window)
{
......@@ -47,38 +48,44 @@ namespace glare
glfwSetCharCallback(window, glfwCharCallback);
glfwSetMouseButtonCallback(window, glfwMouseButtonCallback);
glfwSetScrollCallback(window, glfwScrollCallback);
glfwSetWindowSizeCallback(window, glfwFramebufferSizeCallback);
}
void Callbacks::addKeyActionCallback(std::string name, KeyActionCallback onkey_action)
void Callbacks::addKeyActionCallback(std::string name, callback::key_action onkey_action)
{
m_onkey_action.emplace(name, onkey_action);
}
void Callbacks::addKeyDownCallback(std::string name, KeyDownCallback onkey_down)
void Callbacks::addKeyDownCallback(std::string name, callback::key_down onkey_down)
{
m_onkey_down.emplace(name, onkey_down);
}
void Callbacks::addKeyUpCallback(std::string name, KeyUpCallback onkey_up)
void Callbacks::addKeyUpCallback(std::string name, callback::key_up onkey_up)
{
m_onkey_up.emplace(name, onkey_up);
}
void Callbacks::addCharCallback(std::string name, CharCallback onchar)
void Callbacks::addCharCallback(std::string name, callback::character onchar)
{
m_onchar.emplace(name, onchar);
}
void Callbacks::addMouseButtonCallback(std::string name, MouseButtonCallback onmousebutton)
void Callbacks::addMouseButtonCallback(std::string name, callback::mouse_button onmousebutton)
{
m_on_mousebutton.emplace(name, onmousebutton);
}
void Callbacks::addScrollCallback(std::string name, ScrollCallback onscroll)
void Callbacks::addScrollCallback(std::string name, callback::scroll onscroll)
{
m_onscroll.emplace(name, onscroll);
}
void Callbacks::addFramebufferSizeCallback(std::string name, callback::framebuffer_size on_fb_size)
{
m_on_framebuffer_size.emplace(name, on_fb_size);
}
void Callbacks::removeKeyActionCallback(std::string name)
{
m_onkey_action.erase(name);
......@@ -109,6 +116,11 @@ namespace glare
m_onscroll.erase(name);
}
void Callbacks::removeFramebufferSizeCallback(std::string name)
{
m_on_framebuffer_size.erase(name);
}
void Callbacks::glfwMouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
ImGuiIO &io = ImGui::GetIO();
......@@ -175,5 +187,11 @@ namespace glare
up.second(controls::Key(key), controls::KeyMods(mods));
}
}
void Callbacks::glfwFramebufferSizeCallback(GLFWwindow *window, int width, int height)
{
for (auto &&call : m_on_framebuffer_size)
call.second(width, height);
}
}
}
......@@ -38,25 +38,30 @@ namespace glare
static std::shared_ptr<GraphNode> graph_root;
};
namespace callback
{
using key_down = std::function<void(controls::Key key, controls::KeyMods mod)>;
using key_up = std::function<void(controls::Key key, controls::KeyMods mod)>;
using key_action = controls::key_callback;
using character = std::function<void(unsigned character)>;
using mouse_button = controls::mouse_button_callback;
using scroll = std::function<void(double xoffset, double yoffset)>;
using framebuffer_size = std::function<void(unsigned w, unsigned h)>;
}
class Callbacks
{
public:
//Types
using KeyDownCallback = std::function<void(controls::Key key, controls::KeyMods mod)>;
using KeyUpCallback = std::function<void(controls::Key key, controls::KeyMods mod)>;
using KeyActionCallback = controls::key_callback;
using CharCallback = std::function<void(unsigned character)>;
using MouseButtonCallback = controls::mouse_button_callback;
using ScrollCallback = std::function<void(double xoffset, double yoffset)>;
static void initialize(GLFWwindow* window);
static void addKeyActionCallback(std::string name, KeyActionCallback onkey_action);
static void addKeyDownCallback(std::string name, KeyDownCallback onkey_down);
static void addKeyUpCallback(std::string name, KeyUpCallback onkey_up);
static void addCharCallback(std::string name, CharCallback onchar);
static void addMouseButtonCallback(std::string name, MouseButtonCallback onmousebutton);
static void addScrollCallback(std::string name, ScrollCallback onscroll);
static void addKeyActionCallback(std::string name, callback::key_action onkey_action);
static void addKeyDownCallback(std::string name, callback::key_down onkey_down);
static void addKeyUpCallback(std::string name, callback::key_up onkey_up);
static void addCharCallback(std::string name, callback::character onchar);
static void addMouseButtonCallback(std::string name, callback::mouse_button onmousebutton);
static void addScrollCallback(std::string name, callback::scroll onscroll);
static void addFramebufferSizeCallback(std::string name, callback::framebuffer_size on_fb_size);
static void removeKeyActionCallback(std::string name);
static void removeKeyDownCallback(std::string name);
......@@ -64,19 +69,22 @@ namespace glare
static void removeCharCallback(std::string name);
static void removeMouseButtonCallback(std::string name);
static void removeScrollCallback(std::string name);
static void removeFramebufferSizeCallback(std::string name);
private:
static void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);