From eefbc0c301bae2fd16eb465efa0984379cbd0613 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Sun, 23 Aug 2020 16:08:48 -0700 Subject: [PATCH] Align lens according to sun position and mouse position --- src/game/states/play-state.cpp | 15 +++++++++- src/game/systems/tool-system.cpp | 41 ++++++++++++++++++++++++++-- src/game/systems/tool-system.hpp | 6 ++++ src/renderer/passes/outline-pass.cpp | 1 + 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/game/states/play-state.cpp b/src/game/states/play-state.cpp index 10391a2..9bc213f 100644 --- a/src/game/states/play-state.cpp +++ b/src/game/states/play-state.cpp @@ -44,9 +44,11 @@ #include "scene/model-instance.hpp" #include "scene/scene.hpp" #include "scene/camera.hpp" +#include "scene/directional-light.hpp" #include "game/systems/control-system.hpp" #include "game/systems/camera-system.hpp" #include "game/systems/render-system.hpp" +#include "game/systems/tool-system.hpp" #include "utility/fundamental-types.hpp" #include "utility/gamma.hpp" @@ -64,6 +66,8 @@ void play_state_enter(game_context* ctx) sky_pass->set_zenith_color(to_linear(float3{7.0f, 134.0f, 206.0f} / 255.0f)); //sky_pass->set_horizon_color(float3{0.002f, 0.158f, 0.250f}); //sky_pass->set_zenith_color(float3{0.002f, 0.158f, 0.250f}); + + ctx->tool_system->set_sun_direction(ctx->sun_direct->get_direction()); resource_manager* resource_manager = ctx->resource_manager; entt::registry& ecs_registry = *ctx->ecs_registry; @@ -80,6 +84,7 @@ void play_state_enter(game_context* ctx) ecs::archetype* pebble_archetype = resource_manager->load("pebble.ent"); ecs::archetype* flashlight_archetype = resource_manager->load("flashlight.ent"); ecs::archetype* flashlight_light_cone_archetype = resource_manager->load("flashlight-light-cone.ent"); + ecs::archetype* lens_light_cone_archetype = resource_manager->load("lens-light-cone.ent"); // Create tools forceps_archetype->assign(ecs_registry, ctx->forceps_entity); @@ -100,9 +105,17 @@ void play_state_enter(game_context* ctx) lens_model_instance->set_culling_mask(&ctx->no_cull); } - // Activate brush tools + // Create lens light cone and bind to lens + auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry); + ec::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity); + + // Activate lens tool auto& active_tool_component = ecs_registry.get(ctx->lens_entity); active_tool_component.active = true; + + // Hide inactive tools + ec::assign_render_layers(ecs_registry, ctx->forceps_entity, 0); + ec::assign_render_layers(ecs_registry, ctx->brush_entity, 0); // Create ant-hill auto ant_hill_entity = ant_hill_archetype->create(ecs_registry); diff --git a/src/game/systems/tool-system.cpp b/src/game/systems/tool-system.cpp index 132df10..de97b48 100644 --- a/src/game/systems/tool-system.cpp +++ b/src/game/systems/tool-system.cpp @@ -37,7 +37,19 @@ tool_system::tool_system(entt::registry& registry): mouse_position{0, 0}, pick_enabled(true), was_pick_enabled(pick_enabled) -{} +{ + hand_angle_spring.z = 1.0f; + hand_angle_spring.w = hz_to_rads(8.0f); + hand_angle_spring.x1 = math::pi; + hand_angle_spring.x0 = hand_angle_spring.x1; + hand_angle_spring.v = 0.0f; + + pick_spring.z = 1.0f; + pick_spring.w = hz_to_rads(30.0f); + pick_spring.x1 = {0.0f, 0.0f, 0.0f}; + pick_spring.x0 = pick_spring.x1; + pick_spring.v = {0.0f, 0.0f, 0.0f}; +} void tool_system::update(double t, double dt) { @@ -79,6 +91,7 @@ void tool_system::update(double t, double dt) { a = mesh_result->t; pick = picking_ray.extrapolate(a); + pick_spring.x1 = pick; } } }); @@ -97,6 +110,13 @@ void tool_system::update(double t, double dt) if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f) pick_angle = -pick_angle; } + + // Determine target hand angle + hand_angle_spring.x1 = math::pi - std::min(0.5f, std::max(-0.5f, ((mouse_position[0] / viewport[2]) - 0.5f) * 3.0f)) * math::pi; + + // Solve springs + solve_numeric_spring(hand_angle_spring, dt); + solve_numeric_spring(pick_spring, dt); // Move active tools to intersection location registry.view().each( @@ -113,7 +133,19 @@ void tool_system::update(double t, double dt) if (intersection) { - transform.transform.translation = pick + float3{0, tool.hover_distance, 0}; + transform.transform.translation = pick_spring.x0 + float3{0, tool.hover_distance, 0}; + } + + if (tool.heliotropic) + { + math::quaternion solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction); + + transform.transform.translation = pick_spring.x0 + solar_rotation * float3{0, tool.hover_distance, 0}; + + // Interpolate between left and right hand + math::quaternion hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0}); + + transform.transform.rotation = solar_rotation * hand_rotation; } //math::quaternion rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0}); @@ -143,6 +175,11 @@ void tool_system::set_pick(bool enabled) pick_enabled = enabled; } +void tool_system::set_sun_direction(const float3& direction) +{ + sun_direction = direction; +} + void tool_system::handle_event(const mouse_moved_event& event) { if (pick_enabled && was_pick_enabled) diff --git a/src/game/systems/tool-system.hpp b/src/game/systems/tool-system.hpp index 6062d59..de60963 100644 --- a/src/game/systems/tool-system.hpp +++ b/src/game/systems/tool-system.hpp @@ -25,6 +25,7 @@ #include "event/input-events.hpp" #include "event/window-events.hpp" #include "utility/fundamental-types.hpp" +#include "animation/spring.hpp" class camera; class orbit_cam; @@ -42,6 +43,7 @@ public: void set_orbit_cam(const orbit_cam* camera); void set_viewport(const float4& viewport); void set_pick(bool enabled); + void set_sun_direction(const float3& direction); private: virtual void handle_event(const mouse_moved_event& event); @@ -53,6 +55,10 @@ private: float2 mouse_position; bool was_pick_enabled; bool pick_enabled; + float3 sun_direction; + + numeric_spring hand_angle_spring; + numeric_spring pick_spring; }; #endif // ANTKEEPER_TOOL_SYSTEM_HPP diff --git a/src/renderer/passes/outline-pass.cpp b/src/renderer/passes/outline-pass.cpp index f7bf10b..e678a4e 100644 --- a/src/renderer/passes/outline-pass.cpp +++ b/src/renderer/passes/outline-pass.cpp @@ -64,6 +64,7 @@ void outline_pass::render(render_context* context) const glDisable(GL_BLEND); } + glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glCullFace(GL_BACK);