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

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