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

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