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

1040 lines
57 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 "game/ant/morphogenesis.hpp"
  20. #include "render/material.hpp"
  21. #include "render/vertex-attribute.hpp"
  22. #include <unordered_set>
  23. #include <iostream>
  24. namespace game {
  25. namespace ant {
  26. static render::model* generate_queen(const ant::breed& breed);
  27. static render::model* generate_worker(const ant::breed& breed);
  28. static render::model* generate_soldier(const ant::breed& breed);
  29. static render::model* generate_male(const ant::breed& breed);
  30. static render::material* build_exoskeleton_material
  31. (
  32. const ant::trait::pigmentation& pigmentation,
  33. const ant::trait::sculpturing& sculpturing
  34. );
  35. static void reskin_vertices
  36. (
  37. std::uint8_t* vertex_data,
  38. std::size_t index_count,
  39. const gl::vertex_attribute& position_attribute,
  40. const gl::vertex_attribute& normal_attribute,
  41. const gl::vertex_attribute& tangent_attribute,
  42. const gl::vertex_attribute& bone_index_attribute,
  43. const std::unordered_set<std::uint8_t>& old_bone_indices,
  44. std::uint8_t new_bone_index,
  45. const math::transform<float>& transform
  46. );
  47. static geom::aabb<float> calculate_bounds(std::uint8_t* vertex_data, std::size_t index_count, const gl::vertex_attribute& position_attribute);
  48. static render::model* build_model
  49. (
  50. render::material* material,
  51. const render::model* antennae,
  52. const render::model* eyes,
  53. const render::model* forewings,
  54. const render::model* gaster,
  55. const render::model* head,
  56. const render::model* hindwings,
  57. const render::model* legs,
  58. const render::model* mandibles,
  59. const render::model* mesosoma,
  60. const render::model* lateral_ocelli,
  61. const render::model* median_ocellus,
  62. const render::model* sting,
  63. const render::model* waist
  64. );
  65. render::model* morphogenesis(const ant::breed& breed, ant::caste caste)
  66. {
  67. switch (caste)
  68. {
  69. case ant::caste::queen:
  70. return generate_queen(breed);
  71. case ant::caste::worker:
  72. return generate_worker(breed);
  73. case ant::caste::soldier:
  74. return generate_soldier(breed);
  75. case ant::caste::male:
  76. return generate_male(breed);
  77. }
  78. return nullptr;
  79. }
  80. render::model* generate_queen(const ant::breed& breed)
  81. {
  82. return nullptr;
  83. }
  84. render::model* generate_worker(const ant::breed& breed)
  85. {
  86. // Build exoskeleton material
  87. render::material* exoskeleton_material = build_exoskeleton_material(*breed.pigmentation, *breed.sculpturing);
  88. // Get worker body part models
  89. render::model* antennae_model = breed.antennae->model;
  90. render::model* eyes_model = breed.eyes->model;
  91. render::model* gaster_model = breed.gaster->model;
  92. render::model* head_model = breed.head->model;
  93. render::model* legs_model = breed.legs->model;
  94. render::model* mandibles_model = breed.mandibles->model;
  95. render::model* mesosoma_model = breed.mesosoma->model;
  96. render::model* sting_model = breed.sting->model;
  97. render::model* waist_model = breed.waist->model;
  98. //render::model* lateral_ocelli_model = breed.ocelli->lateral_ocelli_model;
  99. //render::model* median_ocellus_model = breed.ocelli->median_ocellus_model;
  100. // Build worker model
  101. render::model* model = build_model
  102. (
  103. exoskeleton_material,
  104. antennae_model,
  105. eyes_model,
  106. nullptr, // forewings
  107. gaster_model,
  108. head_model,
  109. nullptr, // hindwings
  110. legs_model,
  111. mandibles_model,
  112. mesosoma_model,
  113. nullptr, // lateral ocelli
  114. nullptr, // median ocellus
  115. sting_model,
  116. waist_model
  117. );
  118. return model;
  119. }
  120. render::model* generate_soldier(const ant::breed& breed)
  121. {
  122. return nullptr;
  123. }
  124. render::model* generate_male(const ant::breed& breed)
  125. {
  126. return nullptr;
  127. }
  128. render::material* build_exoskeleton_material
  129. (
  130. const ant::trait::pigmentation& pigmentation,
  131. const ant::trait::sculpturing& sculpturing
  132. )
  133. {
  134. // Allocate copy of pigmentation material
  135. render::material* exoskeleton_material = new render::material(*pigmentation.material);
  136. // Adjust roughness parameter
  137. if (render::material_property_base* property = exoskeleton_material->get_property("roughness"); property != nullptr)
  138. {
  139. static_cast<render::material_property<float>*>(property)->set_value(sculpturing.roughness);
  140. }
  141. else
  142. {
  143. exoskeleton_material->add_property<float>("roughness")->set_value(sculpturing.roughness);
  144. }
  145. // Adjust normal map parameter
  146. if (render::material_property_base* property = exoskeleton_material->get_property("normal_map"); property != nullptr)
  147. {
  148. static_cast<render::material_property<const gl::texture_2d*>*>(property)->set_value(sculpturing.normal_map);
  149. }
  150. else
  151. {
  152. exoskeleton_material->add_property<const gl::texture_2d*>("normal_map")->set_value(sculpturing.normal_map);
  153. }
  154. return exoskeleton_material;
  155. }
  156. render::model* build_model
  157. (
  158. render::material* exoskeleton_material,
  159. const render::model* antennae,
  160. const render::model* eyes,
  161. const render::model* forewings,
  162. const render::model* gaster,
  163. const render::model* head,
  164. const render::model* hindwings,
  165. const render::model* legs,
  166. const render::model* mandibles,
  167. const render::model* mesosoma,
  168. const render::model* lateral_ocelli,
  169. const render::model* median_ocellus,
  170. const render::model* sting,
  171. const render::model* waist
  172. )
  173. {
  174. // Get vertex buffers of required body parts
  175. const gl::vertex_buffer* mesosoma_vbo = mesosoma->get_vertex_buffer();
  176. const gl::vertex_buffer* legs_vbo = legs->get_vertex_buffer();
  177. const gl::vertex_buffer* head_vbo = head->get_vertex_buffer();
  178. const gl::vertex_buffer* mandibles_vbo = mandibles->get_vertex_buffer();
  179. const gl::vertex_buffer* antennae_vbo = antennae->get_vertex_buffer();
  180. const gl::vertex_buffer* waist_vbo = waist->get_vertex_buffer();
  181. const gl::vertex_buffer* gaster_vbo = gaster->get_vertex_buffer();
  182. // Get vertex buffers of optional body parts
  183. const gl::vertex_buffer* sting_vbo = (sting) ? sting->get_vertex_buffer() : nullptr;
  184. const gl::vertex_buffer* eyes_vbo = (eyes) ? eyes->get_vertex_buffer() : nullptr;
  185. const gl::vertex_buffer* lateral_ocelli_vbo = (lateral_ocelli) ? lateral_ocelli->get_vertex_buffer() : nullptr;
  186. const gl::vertex_buffer* median_ocellus_vbo = (median_ocellus) ? median_ocellus->get_vertex_buffer() : nullptr;
  187. const gl::vertex_buffer* forewings_vbo = (forewings) ? forewings->get_vertex_buffer() : nullptr;
  188. const gl::vertex_buffer* hindwings_vbo = (hindwings) ? hindwings->get_vertex_buffer() : nullptr;
  189. // Determine combined size of vertex buffers and save offsets
  190. std::size_t vertex_buffer_size = 0;
  191. std::size_t mesosoma_vbo_offset = vertex_buffer_size;
  192. vertex_buffer_size += mesosoma_vbo->get_size();
  193. std::size_t legs_vbo_offset = vertex_buffer_size;
  194. vertex_buffer_size += legs_vbo->get_size();
  195. std::size_t head_vbo_offset = vertex_buffer_size;
  196. vertex_buffer_size += head_vbo->get_size();
  197. std::size_t mandibles_vbo_offset = vertex_buffer_size;
  198. vertex_buffer_size += mandibles_vbo->get_size();
  199. std::size_t antennae_vbo_offset = vertex_buffer_size;
  200. vertex_buffer_size += antennae_vbo->get_size();
  201. std::size_t waist_vbo_offset = vertex_buffer_size;
  202. vertex_buffer_size += waist_vbo->get_size();
  203. std::size_t gaster_vbo_offset = vertex_buffer_size;
  204. vertex_buffer_size += gaster_vbo->get_size();
  205. std::size_t sting_vbo_offset = vertex_buffer_size;
  206. if (sting)
  207. vertex_buffer_size += sting_vbo->get_size();
  208. std::size_t eyes_vbo_offset = vertex_buffer_size;
  209. if (eyes)
  210. vertex_buffer_size += eyes_vbo->get_size();
  211. std::size_t lateral_ocelli_vbo_offset = vertex_buffer_size;
  212. if (lateral_ocelli)
  213. vertex_buffer_size += lateral_ocelli_vbo->get_size();
  214. std::size_t median_ocellus_vbo_offset = vertex_buffer_size;
  215. if (median_ocellus)
  216. vertex_buffer_size += median_ocellus_vbo->get_size();
  217. std::size_t forewings_vbo_offset = vertex_buffer_size;
  218. if (forewings)
  219. vertex_buffer_size += forewings_vbo->get_size();
  220. std::size_t hindwings_vbo_offset = vertex_buffer_size;
  221. if (hindwings)
  222. vertex_buffer_size += hindwings_vbo->get_size();
  223. // Allocate combined vertex buffer data
  224. std::uint8_t* vertex_buffer_data = new std::uint8_t[vertex_buffer_size];
  225. // Read body part vertex buffer data into combined vertex buffer data
  226. mesosoma_vbo->read(0, mesosoma_vbo->get_size(), vertex_buffer_data + mesosoma_vbo_offset);
  227. legs_vbo->read(0, legs_vbo->get_size(), vertex_buffer_data + legs_vbo_offset);
  228. head_vbo->read(0, head_vbo->get_size(), vertex_buffer_data + head_vbo_offset);
  229. mandibles_vbo->read(0, mandibles_vbo->get_size(), vertex_buffer_data + mandibles_vbo_offset);
  230. antennae_vbo->read(0, antennae_vbo->get_size(), vertex_buffer_data + antennae_vbo_offset);
  231. waist_vbo->read(0, waist_vbo->get_size(), vertex_buffer_data + waist_vbo_offset);
  232. gaster_vbo->read(0, gaster_vbo->get_size(), vertex_buffer_data + gaster_vbo_offset);
  233. if (sting)
  234. sting_vbo->read(0, sting_vbo->get_size(), vertex_buffer_data + sting_vbo_offset);
  235. if (eyes)
  236. eyes_vbo->read(0, eyes_vbo->get_size(), vertex_buffer_data + eyes_vbo_offset);
  237. if (lateral_ocelli)
  238. lateral_ocelli_vbo->read(0, lateral_ocelli_vbo->get_size(), vertex_buffer_data + lateral_ocelli_vbo_offset);
  239. if (median_ocellus)
  240. median_ocellus_vbo->read(0, median_ocellus_vbo->get_size(), vertex_buffer_data + median_ocellus_vbo_offset);
  241. if (forewings)
  242. forewings_vbo->read(0, forewings_vbo->get_size(), vertex_buffer_data + forewings_vbo_offset);
  243. if (hindwings)
  244. hindwings_vbo->read(0, hindwings_vbo->get_size(), vertex_buffer_data + hindwings_vbo_offset);
  245. // Allocate model
  246. render::model* model = new render::model();
  247. // Setup model VAO
  248. gl::vertex_array* model_vao = model->get_vertex_array();
  249. for (auto [location, attribute]: mesosoma->get_vertex_array()->get_attributes())
  250. {
  251. attribute.buffer = model->get_vertex_buffer();
  252. model_vao->bind(location, attribute);
  253. }
  254. // Get vertex attributes
  255. const gl::vertex_attribute* position_attribute = nullptr;
  256. const gl::vertex_attribute* normal_attribute = nullptr;
  257. const gl::vertex_attribute* tangent_attribute = nullptr;
  258. const gl::vertex_attribute* bone_index_attribute = nullptr;
  259. const auto& vertex_attribute_map = model_vao->get_attributes();
  260. if (auto it = vertex_attribute_map.find(render::vertex_attribute::position); it != vertex_attribute_map.end())
  261. position_attribute = &it->second;
  262. if (auto it = vertex_attribute_map.find(render::vertex_attribute::normal); it != vertex_attribute_map.end())
  263. normal_attribute = &it->second;
  264. if (auto it = vertex_attribute_map.find(render::vertex_attribute::tangent); it != vertex_attribute_map.end())
  265. tangent_attribute = &it->second;
  266. if (auto it = vertex_attribute_map.find(render::vertex_attribute::bone_index); it != vertex_attribute_map.end())
  267. bone_index_attribute = &it->second;
  268. // Get body part skeletons
  269. const ::skeleton& mesosoma_skeleton = mesosoma->get_skeleton();
  270. const ::skeleton& legs_skeleton = legs->get_skeleton();
  271. const ::skeleton& head_skeleton = head->get_skeleton();
  272. const ::skeleton& mandibles_skeleton = mandibles->get_skeleton();
  273. const ::skeleton& antennae_skeleton = antennae->get_skeleton();
  274. const ::skeleton& waist_skeleton = waist->get_skeleton();
  275. const ::skeleton& gaster_skeleton = gaster->get_skeleton();
  276. const ::skeleton* sting_skeleton = (sting) ? &sting->get_skeleton() : nullptr;
  277. const ::skeleton* eyes_skeleton = (eyes) ? &eyes->get_skeleton() : nullptr;
  278. const ::skeleton* lateral_ocelli_skeleton = (lateral_ocelli) ? &lateral_ocelli->get_skeleton() : nullptr;
  279. const ::skeleton* median_ocellus_skeleton = (median_ocellus) ? &median_ocellus->get_skeleton() : nullptr;
  280. bool postpetiole = (waist_skeleton.bone_map.find("postpetiole") != waist_skeleton.bone_map.end());
  281. // Allocate skeleton bones
  282. ::skeleton& skeleton = model->get_skeleton();
  283. std::size_t bone_count = 34;
  284. if (postpetiole)
  285. bone_count += 1;
  286. if (sting)
  287. bone_count += 1;
  288. if (forewings)
  289. bone_count += 2;
  290. if (hindwings)
  291. bone_count += 2;
  292. // Assign bone indices
  293. std::uint8_t bone_index = 0;
  294. std::uint8_t mesosoma_bone_index = bone_index++;
  295. std::uint8_t foreleg_coxa_l_bone_index = bone_index++;
  296. std::uint8_t foreleg_coxa_r_bone_index = bone_index++;
  297. std::uint8_t foreleg_femur_l_bone_index = bone_index++;
  298. std::uint8_t foreleg_femur_r_bone_index = bone_index++;
  299. std::uint8_t foreleg_tibia_l_bone_index = bone_index++;
  300. std::uint8_t foreleg_tibia_r_bone_index = bone_index++;
  301. std::uint8_t foreleg_tarsus_l_bone_index = bone_index++;
  302. std::uint8_t foreleg_tarsus_r_bone_index = bone_index++;
  303. std::uint8_t midleg_coxa_l_bone_index = bone_index++;
  304. std::uint8_t midleg_coxa_r_bone_index = bone_index++;
  305. std::uint8_t midleg_femur_l_bone_index = bone_index++;
  306. std::uint8_t midleg_femur_r_bone_index = bone_index++;
  307. std::uint8_t midleg_tibia_l_bone_index = bone_index++;
  308. std::uint8_t midleg_tibia_r_bone_index = bone_index++;
  309. std::uint8_t midleg_tarsus_l_bone_index = bone_index++;
  310. std::uint8_t midleg_tarsus_r_bone_index = bone_index++;
  311. std::uint8_t hindleg_coxa_l_bone_index = bone_index++;
  312. std::uint8_t hindleg_coxa_r_bone_index = bone_index++;
  313. std::uint8_t hindleg_femur_l_bone_index = bone_index++;
  314. std::uint8_t hindleg_femur_r_bone_index = bone_index++;
  315. std::uint8_t hindleg_tibia_l_bone_index = bone_index++;
  316. std::uint8_t hindleg_tibia_r_bone_index = bone_index++;
  317. std::uint8_t hindleg_tarsus_l_bone_index = bone_index++;
  318. std::uint8_t hindleg_tarsus_r_bone_index = bone_index++;
  319. std::uint8_t head_bone_index = bone_index++;
  320. std::uint8_t mandible_l_bone_index = bone_index++;
  321. std::uint8_t mandible_r_bone_index = bone_index++;
  322. std::uint8_t scape_l_bone_index = bone_index++;
  323. std::uint8_t scape_r_bone_index = bone_index++;
  324. std::uint8_t pedicel_l_bone_index = bone_index++;
  325. std::uint8_t pedicel_r_bone_index = bone_index++;
  326. std::uint8_t petiole_bone_index = bone_index++;
  327. std::uint8_t postpetiole_bone_index = (postpetiole) ? bone_index++ : static_cast<std::uint8_t>(bone_count);
  328. std::uint8_t gaster_bone_index = bone_index++;
  329. std::uint8_t sting_bone_index = (sting) ? bone_index++ : static_cast<std::uint8_t>(bone_count);
  330. // Construct bone identifiers
  331. ::bone mesosoma_bone = make_bone(mesosoma_bone_index);
  332. ::bone foreleg_coxa_l_bone = make_bone(foreleg_coxa_l_bone_index, mesosoma_bone_index);
  333. ::bone foreleg_coxa_r_bone = make_bone(foreleg_coxa_r_bone_index, mesosoma_bone_index);
  334. ::bone foreleg_femur_l_bone = make_bone(foreleg_femur_l_bone_index, foreleg_coxa_l_bone_index);
  335. ::bone foreleg_femur_r_bone = make_bone(foreleg_femur_r_bone_index, foreleg_coxa_r_bone_index);
  336. ::bone foreleg_tibia_l_bone = make_bone(foreleg_tibia_l_bone_index, foreleg_femur_l_bone_index);
  337. ::bone foreleg_tibia_r_bone = make_bone(foreleg_tibia_r_bone_index, foreleg_femur_r_bone_index);
  338. ::bone foreleg_tarsus_l_bone = make_bone(foreleg_tarsus_l_bone_index, foreleg_tibia_l_bone_index);
  339. ::bone foreleg_tarsus_r_bone = make_bone(foreleg_tarsus_r_bone_index, foreleg_tibia_r_bone_index);
  340. ::bone midleg_coxa_l_bone = make_bone(midleg_coxa_l_bone_index, mesosoma_bone_index);
  341. ::bone midleg_coxa_r_bone = make_bone(midleg_coxa_r_bone_index, mesosoma_bone_index);
  342. ::bone midleg_femur_l_bone = make_bone(midleg_femur_l_bone_index, midleg_coxa_l_bone_index);
  343. ::bone midleg_femur_r_bone = make_bone(midleg_femur_r_bone_index, midleg_coxa_r_bone_index);
  344. ::bone midleg_tibia_l_bone = make_bone(midleg_tibia_l_bone_index, midleg_femur_l_bone_index);
  345. ::bone midleg_tibia_r_bone = make_bone(midleg_tibia_r_bone_index, midleg_femur_r_bone_index);
  346. ::bone midleg_tarsus_l_bone = make_bone(midleg_tarsus_l_bone_index, midleg_tibia_l_bone_index);
  347. ::bone midleg_tarsus_r_bone = make_bone(midleg_tarsus_r_bone_index, midleg_tibia_r_bone_index);
  348. ::bone hindleg_coxa_l_bone = make_bone(hindleg_coxa_l_bone_index, mesosoma_bone_index);
  349. ::bone hindleg_coxa_r_bone = make_bone(hindleg_coxa_r_bone_index, mesosoma_bone_index);
  350. ::bone hindleg_femur_l_bone = make_bone(hindleg_femur_l_bone_index, hindleg_coxa_l_bone_index);
  351. ::bone hindleg_femur_r_bone = make_bone(hindleg_femur_r_bone_index, hindleg_coxa_r_bone_index);
  352. ::bone hindleg_tibia_l_bone = make_bone(hindleg_tibia_l_bone_index, hindleg_femur_l_bone_index);
  353. ::bone hindleg_tibia_r_bone = make_bone(hindleg_tibia_r_bone_index, hindleg_femur_r_bone_index);
  354. ::bone hindleg_tarsus_l_bone = make_bone(hindleg_tarsus_l_bone_index, hindleg_tibia_l_bone_index);
  355. ::bone hindleg_tarsus_r_bone = make_bone(hindleg_tarsus_r_bone_index, hindleg_tibia_r_bone_index);
  356. ::bone head_bone = make_bone(head_bone_index, mesosoma_bone_index);
  357. ::bone mandible_l_bone = make_bone(mandible_l_bone_index, head_bone_index);
  358. ::bone mandible_r_bone = make_bone(mandible_r_bone_index, head_bone_index);
  359. ::bone scape_l_bone = make_bone(scape_l_bone_index, head_bone_index);
  360. ::bone scape_r_bone = make_bone(scape_r_bone_index, head_bone_index);
  361. ::bone pedicel_l_bone = make_bone(pedicel_l_bone_index, scape_l_bone_index);
  362. ::bone pedicel_r_bone = make_bone(pedicel_r_bone_index, scape_r_bone_index);
  363. ::bone petiole_bone = make_bone(petiole_bone_index, mesosoma_bone_index);
  364. ::bone postpetiole_bone = make_bone(postpetiole_bone_index, petiole_bone_index);
  365. ::bone gaster_bone = make_bone(gaster_bone_index, (postpetiole) ? postpetiole_bone_index : petiole_bone_index);
  366. ::bone sting_bone = make_bone(sting_bone_index, gaster_bone_index);
  367. // Map bone names to bones
  368. skeleton.bone_map["mesosoma"] = mesosoma_bone;
  369. skeleton.bone_map["foreleg_coxa_l"] = foreleg_coxa_l_bone;
  370. skeleton.bone_map["foreleg_coxa_r"] = foreleg_coxa_r_bone;
  371. skeleton.bone_map["foreleg_femur_l"] = foreleg_femur_l_bone;
  372. skeleton.bone_map["foreleg_femur_r"] = foreleg_femur_r_bone;
  373. skeleton.bone_map["foreleg_tibia_l"] = foreleg_tibia_l_bone;
  374. skeleton.bone_map["foreleg_tibia_r"] = foreleg_tibia_r_bone;
  375. skeleton.bone_map["foreleg_tarsus_l"] = foreleg_tarsus_l_bone;
  376. skeleton.bone_map["foreleg_tarsus_r"] = foreleg_tarsus_r_bone;
  377. skeleton.bone_map["midleg_coxa_l"] = midleg_coxa_l_bone;
  378. skeleton.bone_map["midleg_coxa_r"] = midleg_coxa_r_bone;
  379. skeleton.bone_map["midleg_femur_l"] = midleg_femur_l_bone;
  380. skeleton.bone_map["midleg_femur_r"] = midleg_femur_r_bone;
  381. skeleton.bone_map["midleg_tibia_l"] = midleg_tibia_l_bone;
  382. skeleton.bone_map["midleg_tibia_r"] = midleg_tibia_r_bone;
  383. skeleton.bone_map["midleg_tarsus_l"] = midleg_tarsus_l_bone;
  384. skeleton.bone_map["midleg_tarsus_r"] = midleg_tarsus_r_bone;
  385. skeleton.bone_map["hindleg_coxa_l"] = hindleg_coxa_l_bone;
  386. skeleton.bone_map["hindleg_coxa_r"] = hindleg_coxa_r_bone;
  387. skeleton.bone_map["hindleg_femur_l"] = hindleg_femur_l_bone;
  388. skeleton.bone_map["hindleg_femur_r"] = hindleg_femur_r_bone;
  389. skeleton.bone_map["hindleg_tibia_l"] = hindleg_tibia_l_bone;
  390. skeleton.bone_map["hindleg_tibia_r"] = hindleg_tibia_r_bone;
  391. skeleton.bone_map["hindleg_tarsus_l"] = hindleg_tarsus_l_bone;
  392. skeleton.bone_map["hindleg_tarsus_r"] = hindleg_tarsus_r_bone;
  393. skeleton.bone_map["head"] = head_bone;
  394. skeleton.bone_map["mandible_l"] = mandible_l_bone;
  395. skeleton.bone_map["mandible_r"] = mandible_r_bone;
  396. skeleton.bone_map["scape_l"] = scape_l_bone;
  397. skeleton.bone_map["scape_r"] = scape_r_bone;
  398. skeleton.bone_map["pedicel_l"] = pedicel_l_bone;
  399. skeleton.bone_map["pedicel_r"] = pedicel_r_bone;
  400. skeleton.bone_map["petiole"] = petiole_bone;
  401. if (postpetiole)
  402. skeleton.bone_map["postpetiole"] = postpetiole_bone;
  403. skeleton.bone_map["gaster"] = gaster_bone;
  404. if (sting)
  405. skeleton.bone_map["sting"] = sting_bone;
  406. // Get reference to skeleton bind pose
  407. pose& bind_pose = skeleton.bind_pose;
  408. // Skeleton mesosoma pose
  409. if (auto it = mesosoma_skeleton.bone_map.find("mesosoma"); it != mesosoma_skeleton.bone_map.end())
  410. bind_pose[mesosoma_bone] = mesosoma_skeleton.bind_pose.at(it->second);
  411. // Skeleton forelegs pose
  412. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_l"); it != legs_skeleton.bone_map.end())
  413. bind_pose[foreleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  414. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_r"); it != legs_skeleton.bone_map.end())
  415. bind_pose[foreleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  416. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_l"); it != legs_skeleton.bone_map.end())
  417. bind_pose[foreleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  418. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_r"); it != legs_skeleton.bone_map.end())
  419. bind_pose[foreleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  420. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_l"); it != legs_skeleton.bone_map.end())
  421. bind_pose[foreleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  422. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_r"); it != legs_skeleton.bone_map.end())
  423. bind_pose[foreleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  424. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  425. bind_pose[foreleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  426. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  427. bind_pose[foreleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  428. // Skeleton midlegs pose
  429. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_l"); it != legs_skeleton.bone_map.end())
  430. bind_pose[midleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  431. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_r"); it != legs_skeleton.bone_map.end())
  432. bind_pose[midleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  433. if (auto it = legs_skeleton.bone_map.find("midleg_femur_l"); it != legs_skeleton.bone_map.end())
  434. bind_pose[midleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  435. if (auto it = legs_skeleton.bone_map.find("midleg_femur_r"); it != legs_skeleton.bone_map.end())
  436. bind_pose[midleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  437. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_l"); it != legs_skeleton.bone_map.end())
  438. bind_pose[midleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  439. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_r"); it != legs_skeleton.bone_map.end())
  440. bind_pose[midleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  441. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  442. bind_pose[midleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  443. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  444. bind_pose[midleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  445. // Skeleton hindlegs pose
  446. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_l"); it != legs_skeleton.bone_map.end())
  447. bind_pose[hindleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  448. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_r"); it != legs_skeleton.bone_map.end())
  449. bind_pose[hindleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  450. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_l"); it != legs_skeleton.bone_map.end())
  451. bind_pose[hindleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  452. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_r"); it != legs_skeleton.bone_map.end())
  453. bind_pose[hindleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  454. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_l"); it != legs_skeleton.bone_map.end())
  455. bind_pose[hindleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  456. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_r"); it != legs_skeleton.bone_map.end())
  457. bind_pose[hindleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  458. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  459. bind_pose[hindleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  460. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  461. bind_pose[hindleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  462. // Skeleton head pose
  463. bind_pose[head_bone] = mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("head")) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("head"));
  464. // Skeleton mandibles pose
  465. bind_pose[mandible_l_bone] = head_skeleton.bind_pose.at(head_skeleton.bone_map.at("mandible_l")) * mandibles_skeleton.bind_pose.at(mandibles_skeleton.bone_map.at("mandible_l"));
  466. bind_pose[mandible_r_bone] = head_skeleton.bind_pose.at(head_skeleton.bone_map.at("mandible_r")) * mandibles_skeleton.bind_pose.at(mandibles_skeleton.bone_map.at("mandible_r"));
  467. // Skeleton antennae pose
  468. bind_pose[scape_l_bone] = head_skeleton.bind_pose.at(head_skeleton.bone_map.at("scape_l")) * antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("scape_l"));
  469. bind_pose[scape_r_bone] = head_skeleton.bind_pose.at(head_skeleton.bone_map.at("scape_r")) * antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("scape_r"));
  470. bind_pose[pedicel_l_bone] = antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("pedicel_l"));
  471. bind_pose[pedicel_r_bone] = antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("pedicel_r"));
  472. // Skeleton waist pose
  473. bind_pose[petiole_bone] = mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("petiole")) * waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("petiole"));
  474. if (postpetiole)
  475. {
  476. bind_pose[postpetiole_bone] = waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("postpetiole"));
  477. }
  478. // Skeleton gaster pose
  479. if (postpetiole)
  480. {
  481. bind_pose[gaster_bone] = waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("postpetiole")) * gaster_skeleton.bind_pose.at(gaster_skeleton.bone_map.at("gaster"));
  482. }
  483. else
  484. {
  485. bind_pose[gaster_bone] = waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("petiole")) * gaster_skeleton.bind_pose.at(gaster_skeleton.bone_map.at("gaster"));
  486. }
  487. // Skeleton sting pose
  488. if (sting)
  489. {
  490. bind_pose[sting_bone] = gaster_skeleton.bind_pose.at(gaster_skeleton.bone_map.at("sting")) * sting_skeleton->bind_pose.at(sting_skeleton->bone_map.at("sting"));
  491. }
  492. // Calculate the skeleton-space bind pose
  493. pose bind_pose_ss;
  494. ::concatenate(bind_pose, bind_pose_ss);
  495. // Calculate inverse skeleton-space bind pose
  496. ::inverse(bind_pose_ss, skeleton.inverse_bind_pose);
  497. // Get number of vertex indices for each body part
  498. std::size_t mesosoma_index_count = (*mesosoma->get_groups())[0]->get_index_count();
  499. std::size_t legs_index_count = (*legs->get_groups())[0]->get_index_count();
  500. std::size_t head_index_count = (*head->get_groups())[0]->get_index_count();
  501. std::size_t mandibles_index_count = (*mandibles->get_groups())[0]->get_index_count();
  502. std::size_t antennae_index_count = (*antennae->get_groups())[0]->get_index_count();
  503. std::size_t waist_index_count = (*waist->get_groups())[0]->get_index_count();
  504. std::size_t gaster_index_count = (*gaster->get_groups())[0]->get_index_count();
  505. std::size_t sting_index_count = (sting) ? (*sting->get_groups())[0]->get_index_count() : 0;
  506. std::size_t eyes_index_count = (eyes) ? (*eyes->get_groups())[0]->get_index_count() : 0;
  507. std::size_t lateral_ocelli_index_count = (lateral_ocelli) ? (*lateral_ocelli->get_groups())[0]->get_index_count() : 0;
  508. std::size_t median_ocellus_index_count = (median_ocellus) ? (*median_ocellus->get_groups())[0]->get_index_count() : 0;
  509. std::size_t forewings_index_count = (forewings) ? (*forewings->get_groups())[0]->get_index_count() : 0;
  510. std::size_t hindwings_index_count = (hindwings) ? (*hindwings->get_groups())[0]->get_index_count() : 0;
  511. std::size_t exoskeleton_index_count =
  512. mesosoma_index_count
  513. + legs_index_count
  514. + head_index_count
  515. + mandibles_index_count
  516. + antennae_index_count
  517. + waist_index_count
  518. + gaster_index_count
  519. + sting_index_count;
  520. // Calculate transform from legs space to body space
  521. const math::transform<float>& legs_to_body = math::identity_transform<float>;
  522. // Reskin leg bones
  523. std::unordered_set<std::uint8_t> old_foreleg_coxa_l_indices;
  524. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_l"); it != legs_skeleton.bone_map.end())
  525. old_foreleg_coxa_l_indices.emplace(it->second);
  526. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_coxa_l_indices, foreleg_coxa_l_bone_index, legs_to_body);
  527. std::unordered_set<std::uint8_t> old_foreleg_femur_l_indices;
  528. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_l"); it != legs_skeleton.bone_map.end())
  529. old_foreleg_femur_l_indices.emplace(it->second);
  530. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_femur_l_indices, foreleg_femur_l_bone_index, legs_to_body);
  531. std::unordered_set<std::uint8_t> old_foreleg_tibia_l_indices;
  532. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_l"); it != legs_skeleton.bone_map.end())
  533. old_foreleg_tibia_l_indices.emplace(it->second);
  534. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_tibia_l_indices, foreleg_tibia_l_bone_index, legs_to_body);
  535. std::unordered_set<std::uint8_t> old_foreleg_tarsus_l_indices;
  536. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  537. old_foreleg_tarsus_l_indices.emplace(it->second);
  538. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus2_l"); it != legs_skeleton.bone_map.end())
  539. old_foreleg_tarsus_l_indices.emplace(it->second);
  540. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus3_l"); it != legs_skeleton.bone_map.end())
  541. old_foreleg_tarsus_l_indices.emplace(it->second);
  542. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus4_l"); it != legs_skeleton.bone_map.end())
  543. old_foreleg_tarsus_l_indices.emplace(it->second);
  544. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus5_l"); it != legs_skeleton.bone_map.end())
  545. old_foreleg_tarsus_l_indices.emplace(it->second);
  546. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_tarsus_l_indices, foreleg_tarsus_l_bone_index, legs_to_body);
  547. std::unordered_set<std::uint8_t> old_foreleg_coxa_r_indices;
  548. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_r"); it != legs_skeleton.bone_map.end())
  549. old_foreleg_coxa_r_indices.emplace(it->second);
  550. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_coxa_r_indices, foreleg_coxa_r_bone_index, legs_to_body);
  551. std::unordered_set<std::uint8_t> old_foreleg_femur_r_indices;
  552. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_r"); it != legs_skeleton.bone_map.end())
  553. old_foreleg_femur_r_indices.emplace(it->second);
  554. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_femur_r_indices, foreleg_femur_r_bone_index, legs_to_body);
  555. std::unordered_set<std::uint8_t> old_foreleg_tibia_r_indices;
  556. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_r"); it != legs_skeleton.bone_map.end())
  557. old_foreleg_tibia_r_indices.emplace(it->second);
  558. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_tibia_r_indices, foreleg_tibia_r_bone_index, legs_to_body);
  559. std::unordered_set<std::uint8_t> old_foreleg_tarsus_r_indices;
  560. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  561. old_foreleg_tarsus_r_indices.emplace(it->second);
  562. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus2_r"); it != legs_skeleton.bone_map.end())
  563. old_foreleg_tarsus_r_indices.emplace(it->second);
  564. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus3_r"); it != legs_skeleton.bone_map.end())
  565. old_foreleg_tarsus_r_indices.emplace(it->second);
  566. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus4_r"); it != legs_skeleton.bone_map.end())
  567. old_foreleg_tarsus_r_indices.emplace(it->second);
  568. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus5_r"); it != legs_skeleton.bone_map.end())
  569. old_foreleg_tarsus_r_indices.emplace(it->second);
  570. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_foreleg_tarsus_r_indices, foreleg_tarsus_r_bone_index, legs_to_body);
  571. std::unordered_set<std::uint8_t> old_midleg_coxa_l_indices;
  572. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_l"); it != legs_skeleton.bone_map.end())
  573. old_midleg_coxa_l_indices.emplace(it->second);
  574. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_coxa_l_indices, midleg_coxa_l_bone_index, legs_to_body);
  575. std::unordered_set<std::uint8_t> old_midleg_femur_l_indices;
  576. if (auto it = legs_skeleton.bone_map.find("midleg_femur_l"); it != legs_skeleton.bone_map.end())
  577. old_midleg_femur_l_indices.emplace(it->second);
  578. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_femur_l_indices, midleg_femur_l_bone_index, legs_to_body);
  579. std::unordered_set<std::uint8_t> old_midleg_tibia_l_indices;
  580. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_l"); it != legs_skeleton.bone_map.end())
  581. old_midleg_tibia_l_indices.emplace(it->second);
  582. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_tibia_l_indices, midleg_tibia_l_bone_index, legs_to_body);
  583. std::unordered_set<std::uint8_t> old_midleg_tarsus_l_indices;
  584. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  585. old_midleg_tarsus_l_indices.emplace(it->second);
  586. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus2_l"); it != legs_skeleton.bone_map.end())
  587. old_midleg_tarsus_l_indices.emplace(it->second);
  588. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus3_l"); it != legs_skeleton.bone_map.end())
  589. old_midleg_tarsus_l_indices.emplace(it->second);
  590. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus4_l"); it != legs_skeleton.bone_map.end())
  591. old_midleg_tarsus_l_indices.emplace(it->second);
  592. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus5_l"); it != legs_skeleton.bone_map.end())
  593. old_midleg_tarsus_l_indices.emplace(it->second);
  594. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_tarsus_l_indices, midleg_tarsus_l_bone_index, legs_to_body);
  595. std::unordered_set<std::uint8_t> old_midleg_coxa_r_indices;
  596. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_r"); it != legs_skeleton.bone_map.end())
  597. old_midleg_coxa_r_indices.emplace(it->second);
  598. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_coxa_r_indices, midleg_coxa_r_bone_index, legs_to_body);
  599. std::unordered_set<std::uint8_t> old_midleg_femur_r_indices;
  600. if (auto it = legs_skeleton.bone_map.find("midleg_femur_r"); it != legs_skeleton.bone_map.end())
  601. old_midleg_femur_r_indices.emplace(it->second);
  602. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_femur_r_indices, midleg_femur_r_bone_index, legs_to_body);
  603. std::unordered_set<std::uint8_t> old_midleg_tibia_r_indices;
  604. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_r"); it != legs_skeleton.bone_map.end())
  605. old_midleg_tibia_r_indices.emplace(it->second);
  606. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_tibia_r_indices, midleg_tibia_r_bone_index, legs_to_body);
  607. std::unordered_set<std::uint8_t> old_midleg_tarsus_r_indices;
  608. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  609. old_midleg_tarsus_r_indices.emplace(it->second);
  610. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus2_r"); it != legs_skeleton.bone_map.end())
  611. old_midleg_tarsus_r_indices.emplace(it->second);
  612. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus3_r"); it != legs_skeleton.bone_map.end())
  613. old_midleg_tarsus_r_indices.emplace(it->second);
  614. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus4_r"); it != legs_skeleton.bone_map.end())
  615. old_midleg_tarsus_r_indices.emplace(it->second);
  616. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus5_r"); it != legs_skeleton.bone_map.end())
  617. old_midleg_tarsus_r_indices.emplace(it->second);
  618. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_midleg_tarsus_r_indices, midleg_tarsus_r_bone_index, legs_to_body);
  619. std::unordered_set<std::uint8_t> old_hindleg_coxa_l_indices;
  620. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_l"); it != legs_skeleton.bone_map.end())
  621. old_hindleg_coxa_l_indices.emplace(it->second);
  622. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_coxa_l_indices, hindleg_coxa_l_bone_index, legs_to_body);
  623. std::unordered_set<std::uint8_t> old_hindleg_femur_l_indices;
  624. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_l"); it != legs_skeleton.bone_map.end())
  625. old_hindleg_femur_l_indices.emplace(it->second);
  626. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_femur_l_indices, hindleg_femur_l_bone_index, legs_to_body);
  627. std::unordered_set<std::uint8_t> old_hindleg_tibia_l_indices;
  628. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_l"); it != legs_skeleton.bone_map.end())
  629. old_hindleg_tibia_l_indices.emplace(it->second);
  630. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_tibia_l_indices, hindleg_tibia_l_bone_index, legs_to_body);
  631. std::unordered_set<std::uint8_t> old_hindleg_tarsus_l_indices;
  632. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus1_l"); it != legs_skeleton.bone_map.end())
  633. old_hindleg_tarsus_l_indices.emplace(it->second);
  634. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus2_l"); it != legs_skeleton.bone_map.end())
  635. old_hindleg_tarsus_l_indices.emplace(it->second);
  636. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus3_l"); it != legs_skeleton.bone_map.end())
  637. old_hindleg_tarsus_l_indices.emplace(it->second);
  638. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus4_l"); it != legs_skeleton.bone_map.end())
  639. old_hindleg_tarsus_l_indices.emplace(it->second);
  640. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus5_l"); it != legs_skeleton.bone_map.end())
  641. old_hindleg_tarsus_l_indices.emplace(it->second);
  642. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_tarsus_l_indices, hindleg_tarsus_l_bone_index, legs_to_body);
  643. std::unordered_set<std::uint8_t> old_hindleg_coxa_r_indices;
  644. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_r"); it != legs_skeleton.bone_map.end())
  645. old_hindleg_coxa_r_indices.emplace(it->second);
  646. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_coxa_r_indices, hindleg_coxa_r_bone_index, legs_to_body);
  647. std::unordered_set<std::uint8_t> old_hindleg_femur_r_indices;
  648. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_r"); it != legs_skeleton.bone_map.end())
  649. old_hindleg_femur_r_indices.emplace(it->second);
  650. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_femur_r_indices, hindleg_femur_r_bone_index, legs_to_body);
  651. std::unordered_set<std::uint8_t> old_hindleg_tibia_r_indices;
  652. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_r"); it != legs_skeleton.bone_map.end())
  653. old_hindleg_tibia_r_indices.emplace(it->second);
  654. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_tibia_r_indices, hindleg_tibia_r_bone_index, legs_to_body);
  655. std::unordered_set<std::uint8_t> old_hindleg_tarsus_r_indices;
  656. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus1_r"); it != legs_skeleton.bone_map.end())
  657. old_hindleg_tarsus_r_indices.emplace(it->second);
  658. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus2_r"); it != legs_skeleton.bone_map.end())
  659. old_hindleg_tarsus_r_indices.emplace(it->second);
  660. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus3_r"); it != legs_skeleton.bone_map.end())
  661. old_hindleg_tarsus_r_indices.emplace(it->second);
  662. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus4_r"); it != legs_skeleton.bone_map.end())
  663. old_hindleg_tarsus_r_indices.emplace(it->second);
  664. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus5_r"); it != legs_skeleton.bone_map.end())
  665. old_hindleg_tarsus_r_indices.emplace(it->second);
  666. reskin_vertices(vertex_buffer_data + legs_vbo_offset, legs_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_hindleg_tarsus_r_indices, hindleg_tarsus_r_bone_index, legs_to_body);
  667. // Calculate transform from head space to body space
  668. math::transform<float> head_to_body = bind_pose_ss.at(mesosoma_bone) * mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("head"));
  669. // Reskin head bone
  670. std::unordered_set<std::uint8_t> old_head_bone_indices;
  671. if (auto it = head_skeleton.bone_map.find("head"); it != head_skeleton.bone_map.end())
  672. old_head_bone_indices.emplace(it->second);
  673. reskin_vertices(vertex_buffer_data + head_vbo_offset, head_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_head_bone_indices, head_bone_index, head_to_body);
  674. // Calculate transforms from mandibles space to body space
  675. math::transform<float> mandible_l_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("mandible_l"));
  676. math::transform<float> mandible_r_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("mandible_r"));
  677. // Reskin mandible bones
  678. std::unordered_set<std::uint8_t> old_head_mandible_l_bone_indices;
  679. if (auto it = mandibles_skeleton.bone_map.find("mandible_l"); it != mandibles_skeleton.bone_map.end())
  680. old_head_mandible_l_bone_indices.emplace(it->second);
  681. reskin_vertices(vertex_buffer_data + mandibles_vbo_offset, mandibles_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_head_mandible_l_bone_indices, mandible_l_bone_index, mandible_l_to_body);
  682. std::unordered_set<std::uint8_t> old_head_mandible_r_bone_indices;
  683. if (auto it = mandibles_skeleton.bone_map.find("mandible_r"); it != mandibles_skeleton.bone_map.end())
  684. old_head_mandible_r_bone_indices.emplace(it->second);
  685. reskin_vertices(vertex_buffer_data + mandibles_vbo_offset, mandibles_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_head_mandible_r_bone_indices, mandible_r_bone_index, mandible_r_to_body);
  686. // Calculate transforms from antennae space to body space
  687. math::transform<float> antenna_l_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("scape_l"));
  688. math::transform<float> antenna_r_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("scape_r"));
  689. // Reskin scape bones
  690. std::unordered_set<std::uint8_t> old_scape_l_indices;
  691. if (auto it = antennae_skeleton.bone_map.find("scape_l"); it != antennae_skeleton.bone_map.end())
  692. old_scape_l_indices.emplace(it->second);
  693. reskin_vertices(vertex_buffer_data + antennae_vbo_offset, antennae_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_scape_l_indices, scape_l_bone_index, antenna_l_to_body);
  694. std::unordered_set<std::uint8_t> old_scape_r_indices;
  695. if (auto it = antennae_skeleton.bone_map.find("scape_r"); it != antennae_skeleton.bone_map.end())
  696. old_scape_r_indices.emplace(it->second);
  697. reskin_vertices(vertex_buffer_data + antennae_vbo_offset, antennae_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_scape_r_indices, scape_r_bone_index, antenna_r_to_body);
  698. // Reskin pedicel bones
  699. const std::vector<std::string> pedicel_bone_names =
  700. {
  701. "pedicel",
  702. "flagellomere_1",
  703. "flagellomere_2",
  704. "flagellomere_3",
  705. "flagellomere_4",
  706. "flagellomere_5",
  707. "flagellomere_6",
  708. "flagellomere_7",
  709. "flagellomere_8",
  710. "flagellomere_9",
  711. "flagellomere_10",
  712. "flagellomere_11",
  713. "flagellomere_12"
  714. };
  715. std::unordered_set<std::uint8_t> old_pedicel_l_indices;
  716. for (const std::string& bone_name: pedicel_bone_names)
  717. if (auto it = antennae_skeleton.bone_map.find(bone_name + "_l"); it != antennae_skeleton.bone_map.end())
  718. old_pedicel_l_indices.emplace(it->second);
  719. reskin_vertices(vertex_buffer_data + antennae_vbo_offset, antennae_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_pedicel_l_indices, pedicel_l_bone_index, antenna_l_to_body);
  720. std::unordered_set<std::uint8_t> old_pedicel_r_indices;
  721. for (const std::string& bone_name: pedicel_bone_names)
  722. if (auto it = antennae_skeleton.bone_map.find(bone_name + "_r"); it != antennae_skeleton.bone_map.end())
  723. old_pedicel_r_indices.emplace(it->second);
  724. reskin_vertices(vertex_buffer_data + antennae_vbo_offset, antennae_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_pedicel_r_indices, pedicel_r_bone_index, antenna_r_to_body);
  725. // Calculate transform from waist space to body space
  726. math::transform<float> waist_to_body = bind_pose_ss.at(mesosoma_bone) * mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("petiole"));
  727. // Reskin waist bones
  728. std::unordered_set<std::uint8_t> old_petiole_bone_indices;
  729. if (auto it = waist_skeleton.bone_map.find("petiole"); it != waist_skeleton.bone_map.end())
  730. old_petiole_bone_indices.emplace(it->second);
  731. reskin_vertices(vertex_buffer_data + waist_vbo_offset, waist_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_petiole_bone_indices, petiole_bone_index, waist_to_body);
  732. if (postpetiole)
  733. {
  734. std::unordered_set<std::uint8_t> old_postpetiole_bone_indices;
  735. if (auto it = waist_skeleton.bone_map.find("postpetiole"); it != waist_skeleton.bone_map.end())
  736. old_postpetiole_bone_indices.emplace(it->second);
  737. reskin_vertices(vertex_buffer_data + waist_vbo_offset, waist_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_postpetiole_bone_indices, postpetiole_bone_index, waist_to_body);
  738. }
  739. // Calculate transform from gaster space to body space
  740. math::transform<float> gaster_to_body = bind_pose_ss.at(bone_parent_index(gaster_bone)) * waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("gaster"));
  741. // Reskin gaster bones
  742. std::unordered_set<std::uint8_t> old_gaster_bone_indices;
  743. if (auto it = gaster_skeleton.bone_map.find("gaster"); it != gaster_skeleton.bone_map.end())
  744. old_gaster_bone_indices.emplace(it->second);
  745. reskin_vertices(vertex_buffer_data + gaster_vbo_offset, gaster_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_gaster_bone_indices, gaster_bone_index, gaster_to_body);
  746. if (sting)
  747. {
  748. // Calculate transform from sting space to body space
  749. math::transform<float> sting_to_body = bind_pose_ss.at(gaster_bone) * gaster_skeleton.bind_pose.at(gaster_skeleton.bone_map.at("sting"));
  750. // Reskin sting bones
  751. std::unordered_set<std::uint8_t> old_sting_bone_indices;
  752. if (auto it = sting_skeleton->bone_map.find("sting"); it != sting_skeleton->bone_map.end())
  753. old_sting_bone_indices.emplace(it->second);
  754. reskin_vertices(vertex_buffer_data + sting_vbo_offset, sting_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_sting_bone_indices, sting_bone_index, sting_to_body);
  755. }
  756. if (eyes)
  757. {
  758. // Calculate transforms from eyes space to body space
  759. math::transform<float> eye_l_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("eye_l"));
  760. math::transform<float> eye_r_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("eye_r"));
  761. // Reskin eye bones
  762. std::unordered_set<std::uint8_t> old_eye_l_bone_indices;
  763. if (auto it = eyes_skeleton->bone_map.find("eye_l"); it != eyes_skeleton->bone_map.end())
  764. old_eye_l_bone_indices.emplace(it->second);
  765. reskin_vertices(vertex_buffer_data + eyes_vbo_offset, eyes_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_eye_l_bone_indices, head_bone_index, eye_l_to_body);
  766. std::unordered_set<std::uint8_t> old_eye_r_bone_indices;
  767. if (auto it = eyes_skeleton->bone_map.find("eye_r"); it != eyes_skeleton->bone_map.end())
  768. old_eye_r_bone_indices.emplace(it->second);
  769. reskin_vertices(vertex_buffer_data + eyes_vbo_offset, eyes_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_eye_r_bone_indices, head_bone_index, eye_r_to_body);
  770. }
  771. if (lateral_ocelli)
  772. {
  773. // Calculate transforms from lateral ocelli space to body space
  774. math::transform<float> ocellus_l_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("ocellus_l"));
  775. math::transform<float> ocellus_r_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("ocellus_r"));
  776. // Reskin lateral ocelli bones
  777. std::unordered_set<std::uint8_t> old_ocellus_l_bone_indices;
  778. if (auto it = lateral_ocelli_skeleton->bone_map.find("ocellus_l"); it != lateral_ocelli_skeleton->bone_map.end())
  779. old_ocellus_l_bone_indices.emplace(it->second);
  780. reskin_vertices(vertex_buffer_data + lateral_ocelli_vbo_offset, lateral_ocelli_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_ocellus_l_bone_indices, head_bone_index, ocellus_l_to_body);
  781. std::unordered_set<std::uint8_t> old_ocellus_r_bone_indices;
  782. if (auto it = lateral_ocelli_skeleton->bone_map.find("ocellus_r"); it != lateral_ocelli_skeleton->bone_map.end())
  783. old_ocellus_r_bone_indices.emplace(it->second);
  784. reskin_vertices(vertex_buffer_data + lateral_ocelli_vbo_offset, lateral_ocelli_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_ocellus_r_bone_indices, head_bone_index, ocellus_r_to_body);
  785. }
  786. if (median_ocellus)
  787. {
  788. // Calculate transforms from lateral ocelli space to body space
  789. math::transform<float> ocellus_m_to_body = bind_pose_ss.at(head_bone) * head_skeleton.bind_pose.at(head_skeleton.bone_map.at("ocellus_m"));
  790. // Reskin lateral ocelli bones
  791. std::unordered_set<std::uint8_t> old_ocellus_m_bone_indices;
  792. if (auto it = median_ocellus_skeleton->bone_map.find("ocellus_m"); it != median_ocellus_skeleton->bone_map.end())
  793. old_ocellus_m_bone_indices.emplace(it->second);
  794. reskin_vertices(vertex_buffer_data + median_ocellus_vbo_offset, median_ocellus_index_count, *position_attribute, *normal_attribute, *tangent_attribute, *bone_index_attribute, old_ocellus_m_bone_indices, head_bone_index, ocellus_m_to_body);
  795. }
  796. // Upload vertex buffer data to model VBO
  797. model->get_vertex_buffer()->repurpose(gl::buffer_usage::static_draw, vertex_buffer_size, vertex_buffer_data);
  798. // Construct exoskeleton model group
  799. render::model_group* exoskeleton_group = model->add_group("exoskeleton");
  800. exoskeleton_group->set_material(exoskeleton_material);
  801. exoskeleton_group->set_drawing_mode(gl::drawing_mode::triangles);
  802. exoskeleton_group->set_start_index(0);
  803. exoskeleton_group->set_index_count(exoskeleton_index_count);
  804. std::size_t index_offset = exoskeleton_index_count;
  805. if (eyes)
  806. {
  807. // Construct eyes model group
  808. render::model_group* eyes_group = model->add_group("eyes");
  809. eyes_group->set_material((*eyes->get_groups())[0]->get_material());
  810. eyes_group->set_drawing_mode(gl::drawing_mode::triangles);
  811. eyes_group->set_start_index(index_offset);
  812. eyes_group->set_index_count(eyes_index_count);
  813. index_offset += eyes_index_count;
  814. }
  815. if (lateral_ocelli || median_ocellus)
  816. {
  817. // Construct ocelli model group
  818. render::model_group* ocelli_group = model->add_group("ocelli");
  819. ocelli_group->set_drawing_mode(gl::drawing_mode::triangles);
  820. ocelli_group->set_start_index(index_offset);
  821. std::size_t index_count = 0;
  822. if (lateral_ocelli)
  823. {
  824. index_count += lateral_ocelli_index_count;
  825. index_offset += lateral_ocelli_index_count;
  826. ocelli_group->set_material((*lateral_ocelli->get_groups())[0]->get_material());
  827. }
  828. if (median_ocellus)
  829. {
  830. index_count += median_ocellus_index_count;
  831. index_offset += median_ocellus_index_count;
  832. if (!lateral_ocelli)
  833. ocelli_group->set_material((*median_ocellus->get_groups())[0]->get_material());
  834. }
  835. ocelli_group->set_index_count(index_count);
  836. }
  837. if (forewings)
  838. {
  839. // Construct forewings model group
  840. render::model_group* forewings_group = model->add_group("forewings");
  841. forewings_group->set_material((*forewings->get_groups())[0]->get_material());
  842. forewings_group->set_drawing_mode(gl::drawing_mode::triangles);
  843. forewings_group->set_start_index(index_offset);
  844. forewings_group->set_index_count(forewings_index_count);
  845. index_offset += forewings_index_count;
  846. }
  847. if (hindwings)
  848. {
  849. // Construct hindwings model group
  850. render::model_group* hindwings_group = model->add_group("hindwings");
  851. hindwings_group->set_material((*hindwings->get_groups())[0]->get_material());
  852. hindwings_group->set_drawing_mode(gl::drawing_mode::triangles);
  853. hindwings_group->set_start_index(index_offset);
  854. hindwings_group->set_index_count(hindwings_index_count);
  855. index_offset += hindwings_index_count;
  856. }
  857. // Calculate model bounding box
  858. geom::aabb<float> bounds = calculate_bounds(vertex_buffer_data, index_offset, *position_attribute);
  859. model->set_bounds(bounds);
  860. // Free vertex buffer data
  861. delete[] vertex_buffer_data;
  862. return model;
  863. }
  864. void reskin_vertices
  865. (
  866. std::uint8_t* vertex_data,
  867. std::size_t index_count,
  868. const gl::vertex_attribute& position_attribute,
  869. const gl::vertex_attribute& normal_attribute,
  870. const gl::vertex_attribute& tangent_attribute,
  871. const gl::vertex_attribute& bone_index_attribute,
  872. const std::unordered_set<std::uint8_t>& old_bone_indices,
  873. std::uint8_t new_bone_index,
  874. const math::transform<float>& transform
  875. )
  876. {
  877. std::uint8_t* position_data = vertex_data + position_attribute.offset;
  878. std::uint8_t* normal_data = vertex_data + normal_attribute.offset;
  879. std::uint8_t* tangent_data = vertex_data + tangent_attribute.offset;
  880. std::uint8_t* bone_index_data = vertex_data + bone_index_attribute.offset;
  881. for (std::size_t i = 0; i < index_count; ++i)
  882. {
  883. // Get bone index of current vertex
  884. float* bone_index = reinterpret_cast<float*>(bone_index_data + bone_index_attribute.stride * i);
  885. // Skip irrelevant bones
  886. if (!old_bone_indices.count(static_cast<std::uint8_t>(*bone_index + 0.5f)))
  887. continue;
  888. // Get vertex position
  889. float* x = reinterpret_cast<float*>(position_data + position_attribute.stride * i);
  890. float* y = x + 1;
  891. float* z = y + 1;
  892. // Get vertex normal
  893. float* nx = reinterpret_cast<float*>(normal_data + normal_attribute.stride * i);
  894. float* ny = nx + 1;
  895. float* nz = ny + 1;
  896. // Get vertex tangent
  897. float* tx = reinterpret_cast<float*>(tangent_data + tangent_attribute.stride * i);
  898. float* ty = tx + 1;
  899. float* tz = ty + 1;
  900. //float* bts = tz + 1;
  901. // Transform vertex attributes
  902. float3 position = transform * float3{*x, *y, *z};
  903. float3 normal = math::normalize(transform.rotation * float3{*nx, *ny, *nz});
  904. float3 tangent = transform.rotation * float3{*tx, *ty, *tz};
  905. // Update vertex data
  906. *x = position.x;
  907. *y = position.y;
  908. *z = position.z;
  909. *nx = normal.x;
  910. *ny = normal.y;
  911. *nz = normal.z;
  912. *tx = tangent.x;
  913. *ty = tangent.y;
  914. *tz = tangent.z;
  915. //*bts = ...
  916. *bone_index = static_cast<float>(new_bone_index);
  917. }
  918. }
  919. geom::aabb<float> calculate_bounds(std::uint8_t* vertex_data, std::size_t index_count, const gl::vertex_attribute& position_attribute)
  920. {
  921. std::uint8_t* position_data = vertex_data + position_attribute.offset;
  922. geom::aabb<float> bounds;
  923. bounds.min_point.x = std::numeric_limits<float>::infinity();
  924. bounds.min_point.y = std::numeric_limits<float>::infinity();
  925. bounds.min_point.z = std::numeric_limits<float>::infinity();
  926. bounds.max_point.x = -std::numeric_limits<float>::infinity();
  927. bounds.max_point.y = -std::numeric_limits<float>::infinity();
  928. bounds.max_point.z = -std::numeric_limits<float>::infinity();
  929. for (std::size_t i = 0; i < index_count; ++i)
  930. {
  931. // Get vertex position
  932. float* x = reinterpret_cast<float*>(position_data + position_attribute.stride * i);
  933. float* y = x + 1;
  934. float* z = y + 1;
  935. bounds.min_point.x = std::min<float>(*x, bounds.min_point.x);
  936. bounds.min_point.y = std::min<float>(*y, bounds.min_point.y);
  937. bounds.min_point.z = std::min<float>(*z, bounds.min_point.z);
  938. bounds.max_point.x = std::max<float>(*x, bounds.max_point.x);
  939. bounds.max_point.y = std::max<float>(*y, bounds.max_point.y);
  940. bounds.max_point.z = std::max<float>(*z, bounds.max_point.z);
  941. }
  942. return bounds;
  943. }
  944. } // namespace ant
  945. } // namespace game