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

260 lines
6.7 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 "render-system.hpp"
  20. #include "ecs/components/transform-component.hpp"
  21. #include "renderer/renderer.hpp"
  22. #include "scene/point-light.hpp"
  23. #include "scene/directional-light.hpp"
  24. #include "scene/ambient-light.hpp"
  25. #include "scene/spot-light.hpp"
  26. #include <iostream>
  27. namespace ecs {
  28. render_system::render_system(ecs::registry& registry):
  29. entity_system(registry),
  30. renderer(nullptr)
  31. {
  32. registry.on_construct<model_component>().connect<&render_system::on_model_construct>(this);
  33. registry.on_replace<model_component>().connect<&render_system::on_model_replace>(this);
  34. registry.on_destroy<model_component>().connect<&render_system::on_model_destroy>(this);
  35. registry.on_construct<light_component>().connect<&render_system::on_light_construct>(this);
  36. registry.on_replace<light_component>().connect<&render_system::on_light_replace>(this);
  37. registry.on_destroy<light_component>().connect<&render_system::on_light_destroy>(this);
  38. }
  39. void render_system::update(double t, double dt)
  40. {
  41. // Update model instance transforms
  42. registry.view<transform_component, model_component>().each
  43. (
  44. [this](ecs::entity entity, auto& transform, auto& model)
  45. {
  46. scene::model_instance* instance = model_instances[entity];
  47. instance->set_transform(transform.world);
  48. if (transform.warp)
  49. {
  50. instance->get_transform_tween().update();
  51. instance->update_tweens();
  52. transform.warp = false;
  53. }
  54. }
  55. );
  56. // Update light transforms
  57. registry.view<transform_component, light_component>().each
  58. (
  59. [this](ecs::entity entity, auto& transform, auto& light)
  60. {
  61. scene::light* light_object = lights[entity];
  62. light_object->set_transform(transform.world);
  63. if (transform.warp)
  64. {
  65. light_object->get_transform_tween().update();
  66. light_object->update_tweens();
  67. transform.warp = false;
  68. }
  69. }
  70. );
  71. }
  72. void render_system::render(double alpha)
  73. {
  74. if (renderer)
  75. {
  76. for (const scene::collection* collection: layers)
  77. {
  78. renderer->render(alpha, *collection);
  79. }
  80. }
  81. }
  82. void render_system::add_layer(scene::collection* layer)
  83. {
  84. layers.push_back(layer);
  85. }
  86. void render_system::remove_layers()
  87. {
  88. layers.clear();
  89. }
  90. void render_system::set_renderer(::renderer* renderer)
  91. {
  92. this->renderer = renderer;
  93. }
  94. scene::model_instance* render_system::get_model_instance(ecs::entity entity)
  95. {
  96. if (auto it = model_instances.find(entity); it != model_instances.end())
  97. return it->second;
  98. return nullptr;
  99. }
  100. scene::light* render_system::get_light(ecs::entity entity)
  101. {
  102. if (auto it = lights.find(entity); it != lights.end())
  103. return it->second;
  104. return nullptr;
  105. }
  106. void render_system::update_model_and_materials(ecs::entity entity, model_component& model)
  107. {
  108. if (auto model_it = model_instances.find(entity); model_it != model_instances.end())
  109. {
  110. model_it->second->set_model(model.model);
  111. model_it->second->set_instanced((model.instance_count > 0), model.instance_count);
  112. for (auto material_it = model.materials.begin(); material_it != model.materials.end(); ++material_it)
  113. {
  114. model_it->second->set_material(material_it->first, material_it->second);
  115. }
  116. // Add model instance to its specified layers
  117. for (std::size_t i = 0; i < std::min<std::size_t>(layers.size(), (sizeof(model.layers) << 3)); ++i)
  118. {
  119. layers[i]->remove_object(model_it->second);
  120. if ((model.layers >> i) & 1)
  121. {
  122. layers[i]->add_object(model_it->second);
  123. }
  124. }
  125. }
  126. }
  127. void render_system::update_light(ecs::entity entity, ecs::light_component& component)
  128. {
  129. if (auto light_it = lights.find(entity); light_it != lights.end())
  130. {
  131. scene::light* light = light_it->second;
  132. light->set_color(component.color);
  133. light->set_intensity(component.intensity);
  134. switch (light->get_light_type())
  135. {
  136. case scene::light_type::point:
  137. {
  138. scene::point_light* point = static_cast<scene::point_light*>(light);
  139. point->set_attenuation(component.attenuation);
  140. break;
  141. }
  142. case scene::light_type::spot:
  143. {
  144. scene::spot_light* spot = static_cast<scene::spot_light*>(light);
  145. spot->set_attenuation(component.attenuation);
  146. spot->set_cutoff(component.cutoff);
  147. break;
  148. }
  149. default:
  150. break;
  151. }
  152. }
  153. }
  154. void render_system::on_model_construct(ecs::registry& registry, ecs::entity entity, model_component& model)
  155. {
  156. scene::model_instance* model_instance = new scene::model_instance();
  157. model_instances[entity] = model_instance;
  158. update_model_and_materials(entity, model);
  159. }
  160. void render_system::on_model_replace(ecs::registry& registry, ecs::entity entity, model_component& model)
  161. {
  162. update_model_and_materials(entity, model);
  163. }
  164. void render_system::on_model_destroy(ecs::registry& registry, ecs::entity entity)
  165. {
  166. if (auto it = model_instances.find(entity); it != model_instances.end())
  167. {
  168. scene::model_instance* model_instance = it->second;
  169. // Remove model instance from all layers
  170. for (scene::collection* layer: layers)
  171. layer->remove_object(model_instance);
  172. model_instances.erase(it);
  173. delete model_instance;
  174. }
  175. }
  176. void render_system::on_light_construct(ecs::registry& registry, ecs::entity entity, light_component& component)
  177. {
  178. scene::light* light = nullptr;
  179. switch (component.type)
  180. {
  181. case scene::light_type::ambient:
  182. light = new scene::ambient_light();
  183. break;
  184. case scene::light_type::directional:
  185. light = new scene::directional_light();
  186. break;
  187. case scene::light_type::point:
  188. light = new scene::point_light();
  189. break;
  190. case scene::light_type::spot:
  191. light = new scene::spot_light();
  192. break;
  193. default:
  194. break;
  195. }
  196. if (light)
  197. {
  198. lights[entity] = light;
  199. for (scene::collection* layer: layers)
  200. layer->add_object(light);
  201. update_light(entity, component);
  202. }
  203. }
  204. void render_system::on_light_replace(ecs::registry& registry, ecs::entity entity, light_component& light)
  205. {
  206. update_light(entity, light);
  207. }
  208. void render_system::on_light_destroy(ecs::registry& registry, ecs::entity entity)
  209. {
  210. if (auto it = lights.find(entity); it != lights.end())
  211. {
  212. scene::light* light = it->second;
  213. for (scene::collection* layer: layers)
  214. layer->remove_object(light);
  215. lights.erase(it);
  216. delete light;
  217. }
  218. }
  219. } // namespace ecs