💿🐜 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.

161 lines
4.9 KiB

  1. /*
  2. * Copyright (C) 2021 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.hpp"
  20. #include "entity/components/transform.hpp"
  21. #include "entity/components/marker.hpp"
  22. #include "event/event-dispatcher.hpp"
  23. #include "resources/resource-manager.hpp"
  24. #include "scene/collection.hpp"
  25. #include "scene/model-instance.hpp"
  26. #include "math/math.hpp"
  27. #include "renderer/material.hpp"
  28. #include "renderer/model.hpp"
  29. #include "utility/fundamental-types.hpp"
  30. #include "entity/commands.hpp"
  31. namespace entity {
  32. namespace system {
  33. tracking::tracking(entity::registry& registry, ::event_dispatcher* event_dispatcher, ::resource_manager* resource_manager):
  34. updatable(registry),
  35. event_dispatcher(event_dispatcher),
  36. resource_manager(resource_manager),
  37. scene_collection(nullptr)
  38. {
  39. registry.on_construct<component::trackable>().connect<&tracking::on_component_construct>(this);
  40. registry.on_destroy<component::trackable>().connect<&tracking::on_component_destroy>(this);
  41. // Load paint ball model
  42. paint_ball_model = resource_manager->load<model>("paint-ball.mdl");
  43. // Load tracker model
  44. tracker_model = resource_manager->load<model>("tracker.mdl");
  45. // Load paint ball materials
  46. paint_ball_materials = new material*[7];
  47. paint_ball_materials[0] = resource_manager->load<material>("paint-ball-yellow.mtl");
  48. paint_ball_materials[1] = resource_manager->load<material>("paint-ball-green.mtl");
  49. paint_ball_materials[2] = resource_manager->load<material>("paint-ball-blue.mtl");
  50. paint_ball_materials[3] = resource_manager->load<material>("paint-ball-purple.mtl");
  51. paint_ball_materials[4] = resource_manager->load<material>("paint-ball-pink.mtl");
  52. paint_ball_materials[5] = resource_manager->load<material>("paint-ball-red.mtl");
  53. paint_ball_materials[6] = resource_manager->load<material>("paint-ball-orange.mtl");
  54. event_dispatcher->subscribe<tool_pressed_event>(this);
  55. event_dispatcher->subscribe<tool_released_event>(this);
  56. }
  57. tracking::~tracking()
  58. {
  59. event_dispatcher->unsubscribe<tool_pressed_event>(this);
  60. event_dispatcher->unsubscribe<tool_released_event>(this);
  61. for (auto it = trackers.begin(); it != trackers.end(); ++it)
  62. {
  63. delete it->second;
  64. }
  65. delete[] paint_ball_materials;
  66. }
  67. void tracking::update(double t, double dt)
  68. {
  69. for (auto it = trackers.begin(); it != trackers.end(); ++it)
  70. {
  71. const component::transform& transform = registry.get<component::transform>(it->first);
  72. // Project world coordinates to screen coordinates
  73. // Update billboard position
  74. it->second->set_translation(transform.world.translation);
  75. if (transform.warp)
  76. {
  77. it->second->update_tweens();
  78. }
  79. }
  80. }
  81. void tracking::set_scene(scene::collection* collection)
  82. {
  83. this->scene_collection = collection;
  84. }
  85. void tracking::on_component_construct(entity::registry& registry, entity::id entity_id, component::trackable& component)
  86. {
  87. }
  88. void tracking::on_component_destroy(entity::registry& registry, entity::id entity_id)
  89. {
  90. if (auto it = trackers.find(entity_id); it != trackers.end())
  91. {
  92. // Remove model instance from all layers
  93. /*
  94. for (std::size_t i = 0; i < layers.size(); ++i)
  95. {
  96. layers[i]->remove_object(it->second);
  97. }
  98. */
  99. delete it->second;
  100. trackers.erase(it);
  101. }
  102. }
  103. void tracking::handle_event(const tool_pressed_event& event)
  104. {
  105. if (registry.has<component::marker>(event.entity_id))
  106. {
  107. math::transform<float> transform = command::get_world_transform(registry, event.entity_id);
  108. int marker_index = registry.get<component::marker>(event.entity_id).color;
  109. if (marker_index > 0)
  110. {
  111. const float tracker_scale = 1.0f;
  112. // Create tracker model instance
  113. scene::model_instance* instance = new scene::model_instance();
  114. instance->set_model(tracker_model);
  115. instance->set_translation(transform.translation);
  116. instance->set_scale(float3{tracker_scale, tracker_scale, tracker_scale});
  117. // Set tracker paint ball material
  118. const model_group* paint_ball_model_group = tracker_model->get_group("paint-ball");
  119. if (paint_ball_model_group != nullptr)
  120. {
  121. instance->set_material(paint_ball_model_group->get_index(), paint_ball_materials[marker_index - 1]);
  122. }
  123. instance->update_tweens();
  124. scene_collection->add_object(instance);
  125. }
  126. }
  127. }
  128. void tracking::handle_event(const tool_released_event& event)
  129. {
  130. }
  131. } // namespace system
  132. } // namespace entity