💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.5 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "tracking-system.hpp"
  20. #include "game/components/transform-component.hpp"
  21. #include "game/components/marker-component.hpp"
  22. #include "event/event-dispatcher.hpp"
  23. #include "resources/resource-manager.hpp"
  24. #include "scene/billboard.hpp"
  25. #include "scene/scene.hpp"
  26. #include "scene/model-instance.hpp"
  27. #include "math/math.hpp"
  28. #include "renderer/material.hpp"
  29. #include "renderer/material-flags.hpp"
  30. #include "renderer/model.hpp"
  31. #include "rasterizer/texture-2d.hpp"
  32. #include "rasterizer/shader-program.hpp"
  33. #include "utility/fundamental-types.hpp"
  34. #include "game/entity-commands.hpp"
  35. #include <iostream>
  36. using namespace ecs;
  37. tracking_system::tracking_system(entt::registry& registry, ::event_dispatcher* event_dispatcher, ::resource_manager* resource_manager):
  38. entity_system(registry),
  39. event_dispatcher(event_dispatcher),
  40. resource_manager(resource_manager),
  41. scene(nullptr)
  42. {
  43. registry.on_construct<trackable_component>().connect<&tracking_system::on_component_construct>(this);
  44. registry.on_destroy<trackable_component>().connect<&tracking_system::on_component_destroy>(this);
  45. // Load tracker material
  46. tracker_material = resource_manager->load<material>("tracker.mtl");
  47. // Load paint ball model
  48. paint_ball_model = resource_manager->load<model>("paint-ball.obj");
  49. // Load paint ball materials
  50. paint_ball_materials = new material*[7];
  51. paint_ball_materials[0] = resource_manager->load<material>("paint-ball-yellow.mtl");
  52. paint_ball_materials[1] = resource_manager->load<material>("paint-ball-green.mtl");
  53. paint_ball_materials[2] = resource_manager->load<material>("paint-ball-blue.mtl");
  54. paint_ball_materials[3] = resource_manager->load<material>("paint-ball-purple.mtl");
  55. paint_ball_materials[4] = resource_manager->load<material>("paint-ball-pink.mtl");
  56. paint_ball_materials[5] = resource_manager->load<material>("paint-ball-red.mtl");
  57. paint_ball_materials[6] = resource_manager->load<material>("paint-ball-orange.mtl");
  58. event_dispatcher->subscribe<tool_pressed_event>(this);
  59. event_dispatcher->subscribe<tool_released_event>(this);
  60. event_dispatcher->subscribe<window_resized_event>(this);
  61. }
  62. tracking_system::~tracking_system()
  63. {
  64. event_dispatcher->unsubscribe<tool_pressed_event>(this);
  65. event_dispatcher->unsubscribe<tool_released_event>(this);
  66. event_dispatcher->unsubscribe<window_resized_event>(this);
  67. for (auto it = billboards.begin(); it != billboards.end(); ++it)
  68. {
  69. delete it->second;
  70. }
  71. delete[] paint_ball_materials;
  72. }
  73. void tracking_system::update(double t, double dt)
  74. {
  75. for (auto it = billboards.begin(); it != billboards.end(); ++it)
  76. {
  77. const transform_component& transform = registry.get<transform_component>(it->first);
  78. // Project world coordinates to screen coordinates
  79. // Update billboard position
  80. it->second->set_translation(transform.world.translation);
  81. if (transform.warp)
  82. {
  83. it->second->update_tweens();
  84. }
  85. }
  86. }
  87. void tracking_system::set_scene(::scene* scene)
  88. {
  89. this->scene = scene;
  90. }
  91. void tracking_system::set_viewport(const float4& viewport)
  92. {
  93. this->viewport = viewport;
  94. }
  95. void tracking_system::on_component_construct(entt::registry& registry, entt::entity entity, trackable_component& component)
  96. {
  97. }
  98. void tracking_system::on_component_destroy(entt::registry& registry, entt::entity entity)
  99. {
  100. if (auto it = billboards.find(entity); it != billboards.end())
  101. {
  102. // Remove model instance from all layers
  103. /*
  104. for (std::size_t i = 0; i < layers.size(); ++i)
  105. {
  106. layers[i]->remove_object(it->second);
  107. }
  108. */
  109. delete it->second;
  110. billboards.erase(it);
  111. }
  112. }
  113. void tracking_system::handle_event(const tool_pressed_event& event)
  114. {
  115. if (registry.has<marker_component>(event.entity))
  116. {
  117. math::transform<float> transform = ec::get_world_transform(registry, event.entity);
  118. int marker_index = registry.get<marker_component>(event.entity).color;
  119. if (marker_index > 0)
  120. {
  121. ::billboard* billboard = new ::billboard();
  122. billboard->set_material(tracker_material);
  123. billboard->set_scale(float3{1, 1, 1});
  124. billboard->set_translation(transform.translation);
  125. billboard->set_billboard_type(billboard_type::spherical);
  126. //billboard->set_alignment_axis({0, 1, 0});
  127. billboard->update_tweens();
  128. const float paint_ball_scale = 0.393f;
  129. model_instance* instance = new model_instance();
  130. instance->set_model(paint_ball_model);
  131. instance->set_material(0, paint_ball_materials[marker_index - 1]);
  132. instance->set_translation(transform.translation);
  133. instance->set_scale(float3{paint_ball_scale, paint_ball_scale, paint_ball_scale});
  134. instance->update_tweens();
  135. scene->add_object(billboard);
  136. scene->add_object(instance);
  137. }
  138. }
  139. }
  140. void tracking_system::handle_event(const tool_released_event& event)
  141. {
  142. }
  143. void tracking_system::handle_event(const window_resized_event& event)
  144. {
  145. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  146. }