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

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