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

272 lines
9.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 "painting-system.hpp"
  20. #include "game/components/transform-component.hpp"
  21. #include "game/components/brush-component.hpp"
  22. #include "event/event-dispatcher.hpp"
  23. #include "resources/resource-manager.hpp"
  24. #include "scene/scene.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 "game/entity-commands.hpp"
  31. #include "game/components/collision-component.hpp"
  32. #include "game/components/transform-component.hpp"
  33. #include "rasterizer/vertex-buffer.hpp"
  34. #include "rasterizer/vertex-attribute-type.hpp"
  35. #include "renderer/vertex-attributes.hpp"
  36. #include <limits>
  37. using namespace ecs;
  38. painting_system::painting_system(entt::registry& registry, ::event_dispatcher* event_dispatcher, ::resource_manager* resource_manager):
  39. entity_system(registry),
  40. event_dispatcher(event_dispatcher),
  41. resource_manager(resource_manager),
  42. scene(nullptr),
  43. painting(false)
  44. {
  45. event_dispatcher->subscribe<tool_pressed_event>(this);
  46. event_dispatcher->subscribe<tool_released_event>(this);
  47. max_miter_angle = math::radians(135.0f);
  48. stroke_width = 1.0f;
  49. min_stroke_length = 1.0f;
  50. min_stroke_length_squared = min_stroke_length * min_stroke_length;
  51. max_stroke_segments = 4096;
  52. current_stroke_segment = 0;
  53. std::size_t vertex_size = 4;
  54. std::size_t vertex_stride = sizeof(float) * vertex_size;
  55. std::size_t vertex_count = max_stroke_segments * 6;
  56. // Create stroke model
  57. stroke_model = new model();
  58. stroke_model_group = stroke_model->add_group();
  59. stroke_model_group->set_material(resource_manager->load<material>("brushstroke.mtl"));
  60. // Setup stroke vbo and vao
  61. stroke_vbo = stroke_model->get_vertex_buffer();
  62. stroke_vbo->repurpose(sizeof(float) * vertex_size * vertex_count, nullptr, buffer_usage::dynamic_draw);
  63. stroke_model->get_vertex_array()->bind_attribute(VERTEX_POSITION_LOCATION, *stroke_vbo, 4, vertex_attribute_type::float_32, vertex_stride, 0);
  64. // Create stroke model instance
  65. stroke_model_instance = new model_instance();
  66. stroke_model_instance->set_model(stroke_model);
  67. stroke_model_instance->update_tweens();
  68. stroke_bounds_min.x = std::numeric_limits<float>::infinity();
  69. stroke_bounds_min.y = std::numeric_limits<float>::infinity();
  70. stroke_bounds_min.z = std::numeric_limits<float>::infinity();
  71. stroke_bounds_max.x = -std::numeric_limits<float>::infinity();
  72. stroke_bounds_max.y = -std::numeric_limits<float>::infinity();
  73. stroke_bounds_max.z = -std::numeric_limits<float>::infinity();
  74. midstroke = false;
  75. }
  76. painting_system::~painting_system()
  77. {
  78. event_dispatcher->unsubscribe<tool_pressed_event>(this);
  79. event_dispatcher->unsubscribe<tool_released_event>(this);
  80. }
  81. void painting_system::update(double t, double dt)
  82. {
  83. if (painting)
  84. {
  85. auto cast_result = cast_ray(ec::get_world_transform(registry, brush_entity).translation);
  86. if (cast_result.has_value())
  87. {
  88. float3 p2 =
  89. stroke_end = cast_result.value();
  90. float3 segment_difference = stroke_end - stroke_start;
  91. float segment_length_squared = math::dot(segment_difference, segment_difference);
  92. if (segment_length_squared >= min_stroke_length_squared)
  93. {
  94. float segment_length = std::sqrt(segment_length_squared);
  95. float3 segment_forward = segment_difference / segment_length;
  96. float3 segment_right = math::normalize(math::cross(segment_forward, float3{0, 1, 0}));
  97. float3 segment_up = math::cross(segment_right, segment_forward);
  98. float3 segment_center = (stroke_start + stroke_end) * 0.5f;
  99. float3 p1 = stroke_start;
  100. float3 p2 = stroke_end;
  101. // Find miter
  102. float3 tangent = math::normalize(math::normalize(p2 - p1) + math::normalize(p1 - p0));
  103. float2 miter = float2{-tangent.z, tangent.x};
  104. float2 normal = float2{segment_right.x, segment_right.z};
  105. float miter_length = stroke_width / math::dot(miter, normal);
  106. float3 a = p0a;
  107. float3 b = p0b;
  108. float3 c = p1 - segment_right * stroke_width * 0.5f;
  109. float3 d = p1 + segment_right * stroke_width * 0.5f;
  110. float3 e = p2 - segment_right * stroke_width * 0.5f;
  111. float3 f = p2 + segment_right * stroke_width * 0.5f;
  112. // Adjust c and d
  113. bool mitered = false;
  114. if (midstroke)
  115. {
  116. float angle = std::acos(math::dot(math::normalize(p2 - p1), math::normalize(p1 - p0)));
  117. if (angle < max_miter_angle)
  118. {
  119. mitered = true;
  120. c = p1 - float3{miter.x, 0.0f, miter.y} * miter_length * 0.5f;
  121. d = p1 + float3{miter.x, 0.0f, miter.y} * miter_length * 0.5f;
  122. }
  123. }
  124. float4 segment_vertices[12];
  125. float w = static_cast<float>(t);
  126. segment_vertices[0] = {a.x, a.y, a.z, w};
  127. segment_vertices[1] = {b.x, b.y, b.z, w};
  128. segment_vertices[2] = {c.x, c.y, c.z, w};
  129. segment_vertices[3] = {c.x, c.y, c.z, w};
  130. segment_vertices[4] = {b.x, b.y, b.z, w};
  131. segment_vertices[5] = {d.x, d.y, d.z, w};
  132. segment_vertices[6] = {c.x, c.y, c.z, w};
  133. segment_vertices[7] = {d.x, d.y, d.z, w};
  134. segment_vertices[8] = {e.x, e.y, e.z, w};
  135. segment_vertices[9] = {e.x, e.y, e.z, w};
  136. segment_vertices[10] = {d.x, d.y, d.z, w};
  137. segment_vertices[11] = {f.x, f.y, f.z, w};
  138. std::size_t segment_size = sizeof(float) * 4 * 6;
  139. if (mitered)
  140. {
  141. stroke_vbo->update((current_stroke_segment - 1) * segment_size, segment_size * 2, &segment_vertices[0][0]);
  142. }
  143. else
  144. {
  145. stroke_vbo->update(current_stroke_segment * segment_size, segment_size, &segment_vertices[6][0]);
  146. }
  147. ++current_stroke_segment;
  148. stroke_model_group->set_index_count(current_stroke_segment * 6);
  149. // Update stroke bounds
  150. stroke_bounds_min.x = std::min<float>(stroke_bounds_min.x, std::min<float>(c.x, std::min<float>(d.x, std::min<float>(e.x, f.x))));
  151. stroke_bounds_min.y = std::min<float>(stroke_bounds_min.y, std::min<float>(c.y, std::min<float>(d.y, std::min<float>(e.y, f.y))));
  152. stroke_bounds_min.z = std::min<float>(stroke_bounds_min.z, std::min<float>(c.z, std::min<float>(d.z, std::min<float>(e.z, f.z))));
  153. stroke_bounds_max.x = std::max<float>(stroke_bounds_max.x, std::max<float>(c.x, std::max<float>(d.x, std::max<float>(e.x, f.x))));
  154. stroke_bounds_max.y = std::max<float>(stroke_bounds_max.y, std::max<float>(c.y, std::max<float>(d.y, std::max<float>(e.y, f.y))));
  155. stroke_bounds_max.z = std::max<float>(stroke_bounds_max.z, std::max<float>(c.z, std::max<float>(d.z, std::max<float>(e.z, f.z))));
  156. stroke_model->set_bounds(aabb<float>{stroke_bounds_min, stroke_bounds_max});
  157. stroke_model_instance->update_bounds();
  158. p0 = stroke_start;
  159. p0a = c;
  160. p0b = d;
  161. stroke_start = stroke_end;
  162. midstroke = true;
  163. }
  164. }
  165. }
  166. }
  167. void painting_system::set_scene(::scene* scene)
  168. {
  169. this->scene = scene;
  170. scene->add_object(stroke_model_instance);
  171. }
  172. void painting_system::handle_event(const tool_pressed_event& event)
  173. {
  174. if (registry.has<brush_component>(event.entity))
  175. {
  176. auto cast_result = cast_ray(ec::get_world_transform(registry, event.entity).translation);
  177. if (cast_result.has_value())
  178. {
  179. brush_entity = event.entity;
  180. painting = true;
  181. stroke_start = cast_result.value();
  182. stroke_end = stroke_start;
  183. p0 = stroke_start;
  184. p0a = p0;
  185. p0b = p0;
  186. midstroke = false;
  187. }
  188. }
  189. }
  190. void painting_system::handle_event(const tool_released_event& event)
  191. {
  192. if (registry.has<brush_component>(event.entity))
  193. {
  194. auto cast_result = cast_ray(ec::get_world_transform(registry, event.entity).translation);
  195. if (cast_result.has_value())
  196. {
  197. stroke_end = cast_result.value();
  198. }
  199. brush_entity = entt::null;
  200. painting = false;
  201. }
  202. }
  203. std::optional<float3> painting_system::cast_ray(const float3& position) const
  204. {
  205. std::optional<float3> result;
  206. ray<float> untransformed_ray = {position + float3{0.0f, 10000.0f, 0.0f}, {0, -1, 0}};
  207. float min_distance = std::numeric_limits<float>::infinity();
  208. registry.view<transform_component, collision_component>().each(
  209. [&](auto entity, auto& collision_transform, auto& collision)
  210. {
  211. // Transform ray into local space of collision component
  212. math::transform<float> inverse_transform = math::inverse(collision_transform.local);
  213. float3 origin = inverse_transform * untransformed_ray.origin;
  214. float3 direction = math::normalize(math::conjugate(collision_transform.local.rotation) * untransformed_ray.direction);
  215. ray<float> transformed_ray = {origin, direction};
  216. // Broad phase AABB test
  217. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  218. if (!std::get<0>(aabb_result))
  219. {
  220. return;
  221. }
  222. // Narrow phase mesh test
  223. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  224. if (mesh_result)
  225. {
  226. if (mesh_result->t < min_distance)
  227. {
  228. min_distance = mesh_result->t;
  229. result = untransformed_ray.extrapolate(min_distance);
  230. }
  231. }
  232. });
  233. return result;
  234. }