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

365 lines
12 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 "geometry/mesh-functions.hpp"
  38. #include <limits>
  39. using namespace ecs;
  40. painting_system::painting_system(entt::registry& registry, ::event_dispatcher* event_dispatcher, ::resource_manager* resource_manager):
  41. entity_system(registry),
  42. event_dispatcher(event_dispatcher),
  43. resource_manager(resource_manager),
  44. scene(nullptr),
  45. painting(false)
  46. {
  47. event_dispatcher->subscribe<tool_pressed_event>(this);
  48. event_dispatcher->subscribe<tool_released_event>(this);
  49. max_miter_angle = math::radians(135.0f);
  50. decal_offset = 0.01f;
  51. stroke_width = 1.5f;
  52. min_stroke_length = 1.0f;
  53. min_stroke_length_squared = min_stroke_length * min_stroke_length;
  54. max_stroke_segments = 4096;
  55. current_stroke_segment = 0;
  56. vertex_size = 15;
  57. vertex_stride = sizeof(float) * vertex_size;
  58. vertex_count = max_stroke_segments * 6;
  59. // Create stroke model
  60. stroke_model = new model();
  61. stroke_model_group = stroke_model->add_group();
  62. stroke_model_group->set_material(resource_manager->load<material>("brushstroke.mtl"));
  63. // Setup stroke vbo and vao
  64. stroke_vbo = stroke_model->get_vertex_buffer();
  65. stroke_vbo->repurpose(sizeof(float) * vertex_size * vertex_count, nullptr, buffer_usage::dynamic_draw);
  66. stroke_model->get_vertex_array()->bind_attribute(VERTEX_POSITION_LOCATION, *stroke_vbo, 4, vertex_attribute_type::float_32, vertex_stride, 0);
  67. stroke_model->get_vertex_array()->bind_attribute(VERTEX_NORMAL_LOCATION, *stroke_vbo, 3, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * 4);
  68. stroke_model->get_vertex_array()->bind_attribute(VERTEX_TEXCOORD_LOCATION, *stroke_vbo, 2, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * 7);
  69. stroke_model->get_vertex_array()->bind_attribute(VERTEX_TANGENT_LOCATION, *stroke_vbo, 3, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * 9);
  70. stroke_model->get_vertex_array()->bind_attribute(VERTEX_BITANGENT_LOCATION, *stroke_vbo, 3, vertex_attribute_type::float_32, vertex_stride, sizeof(float) * 12);
  71. // Create stroke model instance
  72. stroke_model_instance = new model_instance();
  73. stroke_model_instance->set_model(stroke_model);
  74. stroke_model_instance->update_tweens();
  75. stroke_bounds_min.x = std::numeric_limits<float>::infinity();
  76. stroke_bounds_min.y = std::numeric_limits<float>::infinity();
  77. stroke_bounds_min.z = std::numeric_limits<float>::infinity();
  78. stroke_bounds_max.x = -std::numeric_limits<float>::infinity();
  79. stroke_bounds_max.y = -std::numeric_limits<float>::infinity();
  80. stroke_bounds_max.z = -std::numeric_limits<float>::infinity();
  81. midstroke = false;
  82. }
  83. painting_system::~painting_system()
  84. {
  85. event_dispatcher->unsubscribe<tool_pressed_event>(this);
  86. event_dispatcher->unsubscribe<tool_released_event>(this);
  87. }
  88. void painting_system::update(double t, double dt)
  89. {
  90. if (painting)
  91. {
  92. const tool_component& tool = registry.get<tool_component>(brush_entity);
  93. auto cast_result = cast_ray(tool.cursor);
  94. if (cast_result.has_value())
  95. {
  96. stroke_end = std::get<0>(cast_result.value());
  97. float3 surface_normal = std::get<1>(cast_result.value());
  98. float3 segment_difference = stroke_end - stroke_start;
  99. float segment_length_squared = math::dot(segment_difference, segment_difference);
  100. if (segment_length_squared >= min_stroke_length_squared)
  101. {
  102. float segment_length = std::sqrt(segment_length_squared);
  103. float3 segment_forward = segment_difference / segment_length;
  104. float3 segment_right = math::normalize(math::cross(segment_forward, surface_normal));
  105. float3 segment_up = math::cross(segment_right, segment_forward);
  106. float angle = std::acos(math::dot(segment_forward, float3{0, 0, -1}));
  107. float3 cross = math::cross(segment_forward, float3{0, 0, -1});
  108. if (math::dot(surface_normal, cross) < 0.0f)
  109. angle = -angle;
  110. math::quaternion<float> tangent_rotation = math::normalize(math::angle_axis(-angle, surface_normal));
  111. float3 p1 = stroke_start;
  112. float3 p2 = stroke_end;
  113. // Find miter
  114. float3 tangent = math::normalize(math::normalize(p2 - p1) + math::normalize(p1 - p0));
  115. float2 miter = float2{-tangent.z, tangent.x};
  116. float2 normal = float2{segment_right.x, segment_right.z};
  117. float miter_length = stroke_width / math::dot(miter, normal);
  118. float3 a = p0a;
  119. float3 b = p0b;
  120. float3 c = p1 - segment_right * stroke_width * 0.5f + segment_up * decal_offset;
  121. float3 d = p1 + segment_right * stroke_width * 0.5f + segment_up * decal_offset;
  122. float3 e = p2 - segment_right * stroke_width * 0.5f + segment_up * decal_offset;
  123. float3 f = p2 + segment_right * stroke_width * 0.5f + segment_up * decal_offset;
  124. // Adjust c and d
  125. bool mitered = false;
  126. if (midstroke)
  127. {
  128. float angle = std::acos(math::dot(math::normalize(p2 - p1), math::normalize(p1 - p0)));
  129. if (angle < max_miter_angle)
  130. {
  131. mitered = true;
  132. c = p1 - float3{miter.x, 0.0f, miter.y} * miter_length * 0.5f + segment_up * decal_offset;
  133. d = p1 + float3{miter.x, 0.0f, miter.y} * miter_length * 0.5f + segment_up * decal_offset;
  134. }
  135. }
  136. const float3 positions[] =
  137. {
  138. a, b, c,
  139. c, b, d,
  140. c, d, e,
  141. e, d, f
  142. };
  143. const float w = static_cast<float>(t);
  144. float2 texcoords[] =
  145. {
  146. {0, 0}, {1, 0}, {0, 1},
  147. {0, 1}, {1, 0}, {1, 1},
  148. {0, 0}, {1, 0}, {0, 1},
  149. {0, 1}, {1, 0}, {1, 1},
  150. };
  151. float3 tangent_positions[] =
  152. {
  153. {0, 0, 0}, {1, 0, 0}, {0, 0, 1},
  154. {0, 0, 1}, {1, 0, 0}, {1, 0, 1},
  155. {0, 0, 0}, {1, 0, 0}, {0, 0, 1},
  156. {0, 0, 1}, {1, 0, 0}, {1, 0, 1}
  157. };
  158. /// @TODO: smooth normals in middle of segment
  159. float3 tangents[12];
  160. float3 bitangents[12];
  161. for (int i = 0; i < 4; ++i)
  162. {
  163. const float3& a = tangent_positions[i * 3];
  164. const float3& b = tangent_positions[i * 3 + 1];
  165. const float3& c = tangent_positions[i * 3 + 2];
  166. const float2& uva = texcoords[i * 3];
  167. const float2& uvb = texcoords[i * 3 + 1];
  168. const float2& uvc = texcoords[i * 3 + 2];
  169. float3 ba = b - a;
  170. float3 ca = c - a;
  171. float2 uvba = uvb - uva;
  172. float2 uvca = uvc - uva;
  173. float f = 1.0f / (uvba.x * uvca.y - uvca.x * uvba.y);
  174. float3 tangent = math::normalize((ba * uvca.y - ca * uvba.y) * f);
  175. float3 bitangent = math::normalize((ba * -uvca.x + ca * uvba.x) * f);
  176. // Rotate tangent and bitangent according to segment rotation
  177. tangent = math::normalize(tangent_rotation * tangent);
  178. bitangent = math::normalize(tangent_rotation * bitangent);
  179. tangents[i * 3] = tangent;
  180. tangents[i * 3 + 1] = tangent;
  181. tangents[i * 3 + 2] = tangent;
  182. bitangents[i * 3] = bitangent;
  183. bitangents[i * 3 + 1] = bitangent;
  184. bitangents[i * 3 + 2] = bitangent;
  185. }
  186. float vertex_data[15 * 12];
  187. float* v = &vertex_data[0];
  188. for (int i = 0; i < 12; ++i)
  189. {
  190. *(v++) = positions[i].x;
  191. *(v++) = positions[i].y;
  192. *(v++) = positions[i].z;
  193. *(v++) = w;
  194. *(v++) = surface_normal.x;
  195. *(v++) = surface_normal.y;
  196. *(v++) = surface_normal.z;
  197. *(v++) = texcoords[i].x;
  198. *(v++) = texcoords[i].y;
  199. *(v++) = tangents[i].x;
  200. *(v++) = tangents[i].y;
  201. *(v++) = tangents[i].z;
  202. *(v++) = bitangents[i].x;
  203. *(v++) = bitangents[i].y;
  204. *(v++) = bitangents[i].z;
  205. }
  206. std::size_t segment_size = sizeof(float) * vertex_size * 6;
  207. if (mitered)
  208. {
  209. stroke_vbo->update((current_stroke_segment - 1) * segment_size, segment_size * 2, &vertex_data[0]);
  210. }
  211. else
  212. {
  213. stroke_vbo->update(current_stroke_segment * segment_size, segment_size, &vertex_data[vertex_size * 6]);
  214. }
  215. ++current_stroke_segment;
  216. stroke_model_group->set_index_count(current_stroke_segment * 6);
  217. // Update stroke bounds
  218. 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))));
  219. 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))));
  220. 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))));
  221. 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))));
  222. 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))));
  223. 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))));
  224. stroke_model->set_bounds(aabb<float>{stroke_bounds_min, stroke_bounds_max});
  225. stroke_model_instance->update_bounds();
  226. p0 = stroke_start;
  227. p0a = c;
  228. p0b = d;
  229. stroke_start = stroke_end;
  230. midstroke = true;
  231. }
  232. }
  233. }
  234. }
  235. void painting_system::set_scene(::scene* scene)
  236. {
  237. this->scene = scene;
  238. scene->add_object(stroke_model_instance);
  239. }
  240. void painting_system::handle_event(const tool_pressed_event& event)
  241. {
  242. if (registry.has<brush_component>(event.entity))
  243. {
  244. auto cast_result = cast_ray(event.position);
  245. if (cast_result.has_value())
  246. {
  247. brush_entity = event.entity;
  248. painting = true;
  249. stroke_start = std::get<0>(cast_result.value());
  250. stroke_end = stroke_start;
  251. p0 = stroke_start;
  252. p0a = p0;
  253. p0b = p0;
  254. midstroke = false;
  255. }
  256. }
  257. }
  258. void painting_system::handle_event(const tool_released_event& event)
  259. {
  260. if (registry.has<brush_component>(event.entity))
  261. {
  262. auto cast_result = cast_ray(ec::get_world_transform(registry, event.entity).translation);
  263. if (cast_result.has_value())
  264. {
  265. stroke_end = std::get<0>(cast_result.value());
  266. }
  267. brush_entity = entt::null;
  268. painting = false;
  269. }
  270. }
  271. std::optional<std::tuple<float3, float3>> painting_system::cast_ray(const float3& position) const
  272. {
  273. std::optional<std::tuple<float3, float3>> result;
  274. float3 intersection;
  275. float3 surface_normal;
  276. mesh::face* face = nullptr;
  277. ray<float> untransformed_ray = {position + float3{0.0f, 10000.0f, 0.0f}, {0, -1, 0}};
  278. float min_distance = std::numeric_limits<float>::infinity();
  279. registry.view<transform_component, collision_component>().each(
  280. [&](auto entity, auto& collision_transform, auto& collision)
  281. {
  282. // Transform ray into local space of collision component
  283. math::transform<float> inverse_transform = math::inverse(collision_transform.local);
  284. float3 origin = inverse_transform * untransformed_ray.origin;
  285. float3 direction = math::normalize(math::conjugate(collision_transform.local.rotation) * untransformed_ray.direction);
  286. ray<float> transformed_ray = {origin, direction};
  287. // Broad phase AABB test
  288. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  289. if (!std::get<0>(aabb_result))
  290. {
  291. return;
  292. }
  293. // Narrow phase mesh test
  294. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  295. if (mesh_result)
  296. {
  297. if (mesh_result->t < min_distance)
  298. {
  299. min_distance = mesh_result->t;
  300. intersection = untransformed_ray.extrapolate(min_distance);
  301. face = mesh_result->face;
  302. }
  303. }
  304. });
  305. if (face != nullptr)
  306. {
  307. surface_normal = calculate_face_normal(*face);
  308. result = std::make_tuple(intersection, surface_normal);
  309. }
  310. return result;
  311. }