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

886 lines
43 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. // Get material parameters
  87. // Build exoskeleton material
  88. render::material* exoskeleton_material = build_exoskeleton_material(*breed.pigmentation, *breed.sculpturing);
  89. // Get worker body part models
  90. render::model* antennae_model = breed.antennae->model;
  91. render::model* eyes_model = breed.eyes->model;
  92. render::model* gaster_model = breed.gaster->model;
  93. render::model* head_model = breed.head->model;
  94. render::model* legs_model = breed.legs->model;
  95. render::model* mandibles_model = breed.mandibles->model;
  96. render::model* mesosoma_model = breed.mesosoma->model;
  97. render::model* sting_model = breed.sting->model;
  98. render::model* waist_model = breed.waist->model;
  99. //render::model* lateral_ocelli_model = breed.ocelli->lateral_ocelli_model;
  100. //render::model* median_ocellus_model = breed.ocelli->median_ocellus_model;
  101. // Build worker model
  102. render::model* model = build_model
  103. (
  104. exoskeleton_material,
  105. antennae_model,
  106. eyes_model,
  107. nullptr, // forewings
  108. gaster_model,
  109. head_model,
  110. nullptr, // hindwings
  111. legs_model,
  112. mandibles_model,
  113. mesosoma_model,
  114. nullptr, // lateral ocelli
  115. nullptr, // median ocellus
  116. sting_model,
  117. waist_model
  118. );
  119. return model;
  120. }
  121. render::model* generate_soldier(const ant::breed& breed)
  122. {
  123. return nullptr;
  124. }
  125. render::model* generate_male(const ant::breed& breed)
  126. {
  127. return nullptr;
  128. }
  129. render::material* build_exoskeleton_material
  130. (
  131. const ant::trait::pigmentation& pigmentation,
  132. const ant::trait::sculpturing& sculpturing
  133. )
  134. {
  135. // Allocate copy of pigmentation material
  136. render::material* exoskeleton_material = new render::material(*pigmentation.material);
  137. // Adjust roughness parameter
  138. if (render::material_property_base* property = exoskeleton_material->get_property("roughness"); property != nullptr)
  139. {
  140. static_cast<render::material_property<float>*>(property)->set_value(sculpturing.roughness);
  141. }
  142. else
  143. {
  144. exoskeleton_material->add_property<float>("roughness")->set_value(sculpturing.roughness);
  145. }
  146. // Adjust normal map parameter
  147. if (render::material_property_base* property = exoskeleton_material->get_property("normal_map"); property != nullptr)
  148. {
  149. static_cast<render::material_property<const gl::texture_2d*>*>(property)->set_value(sculpturing.normal_map);
  150. }
  151. else
  152. {
  153. exoskeleton_material->add_property<const gl::texture_2d*>("normal_map")->set_value(sculpturing.normal_map);
  154. }
  155. return exoskeleton_material;
  156. }
  157. render::model* build_model
  158. (
  159. render::material* exoskeleton_material,
  160. const render::model* antennae,
  161. const render::model* eyes,
  162. const render::model* forewings,
  163. const render::model* gaster,
  164. const render::model* head,
  165. const render::model* hindwings,
  166. const render::model* legs,
  167. const render::model* mandibles,
  168. const render::model* mesosoma,
  169. const render::model* lateral_ocelli,
  170. const render::model* median_ocellus,
  171. const render::model* sting,
  172. const render::model* waist
  173. )
  174. {
  175. // Get vertex buffers of required body parts
  176. const gl::vertex_buffer* mesosoma_vbo = mesosoma->get_vertex_buffer();
  177. const gl::vertex_buffer* legs_vbo = legs->get_vertex_buffer();
  178. const gl::vertex_buffer* head_vbo = head->get_vertex_buffer();
  179. const gl::vertex_buffer* mandibles_vbo = mandibles->get_vertex_buffer();
  180. const gl::vertex_buffer* antennae_vbo = antennae->get_vertex_buffer();
  181. const gl::vertex_buffer* waist_vbo = waist->get_vertex_buffer();
  182. const gl::vertex_buffer* gaster_vbo = gaster->get_vertex_buffer();
  183. // Get vertex buffers of optional body parts
  184. const gl::vertex_buffer* sting_vbo = (sting) ? sting->get_vertex_buffer() : nullptr;
  185. const gl::vertex_buffer* eyes_vbo = (eyes) ? eyes->get_vertex_buffer() : nullptr;
  186. const gl::vertex_buffer* lateral_ocelli_vbo = (lateral_ocelli) ? lateral_ocelli->get_vertex_buffer() : nullptr;
  187. const gl::vertex_buffer* median_ocellus_vbo = (median_ocellus) ? median_ocellus->get_vertex_buffer() : nullptr;
  188. const gl::vertex_buffer* forewings_vbo = (forewings) ? forewings->get_vertex_buffer() : nullptr;
  189. const gl::vertex_buffer* hindwings_vbo = (hindwings) ? hindwings->get_vertex_buffer() : nullptr;
  190. // Determine combined size of vertex buffers and save offsets
  191. std::size_t vertex_buffer_size = 0;
  192. std::size_t mesosoma_vbo_offset = vertex_buffer_size;
  193. vertex_buffer_size += mesosoma_vbo->get_size();
  194. std::size_t legs_vbo_offset = vertex_buffer_size;
  195. vertex_buffer_size += legs_vbo->get_size();
  196. std::size_t head_vbo_offset = vertex_buffer_size;
  197. vertex_buffer_size += head_vbo->get_size();
  198. std::size_t mandibles_vbo_offset = vertex_buffer_size;
  199. vertex_buffer_size += mandibles_vbo->get_size();
  200. std::size_t antennae_vbo_offset = vertex_buffer_size;
  201. vertex_buffer_size += antennae_vbo->get_size();
  202. std::size_t waist_vbo_offset = vertex_buffer_size;
  203. vertex_buffer_size += waist_vbo->get_size();
  204. std::size_t gaster_vbo_offset = vertex_buffer_size;
  205. vertex_buffer_size += gaster_vbo->get_size();
  206. std::size_t sting_vbo_offset = vertex_buffer_size;
  207. if (sting)
  208. vertex_buffer_size += sting_vbo->get_size();
  209. std::size_t eyes_vbo_offset = vertex_buffer_size;
  210. if (eyes)
  211. vertex_buffer_size += eyes_vbo->get_size();
  212. std::size_t lateral_ocelli_vbo_offset = vertex_buffer_size;
  213. if (lateral_ocelli)
  214. vertex_buffer_size += lateral_ocelli_vbo->get_size();
  215. std::size_t median_ocellus_vbo_offset = vertex_buffer_size;
  216. if (median_ocellus)
  217. vertex_buffer_size += median_ocellus_vbo->get_size();
  218. std::size_t forewings_vbo_offset = vertex_buffer_size;
  219. if (forewings)
  220. vertex_buffer_size += forewings_vbo->get_size();
  221. std::size_t hindwings_vbo_offset = vertex_buffer_size;
  222. if (hindwings)
  223. vertex_buffer_size += hindwings_vbo->get_size();
  224. // Allocate combined vertex buffer data
  225. std::uint8_t* vertex_buffer_data = new std::uint8_t[vertex_buffer_size];
  226. // Read body part vertex buffer data into combined vertex buffer data
  227. mesosoma_vbo->read(0, mesosoma_vbo->get_size(), vertex_buffer_data + mesosoma_vbo_offset);
  228. legs_vbo->read(0, legs_vbo->get_size(), vertex_buffer_data + legs_vbo_offset);
  229. head_vbo->read(0, head_vbo->get_size(), vertex_buffer_data + head_vbo_offset);
  230. mandibles_vbo->read(0, mandibles_vbo->get_size(), vertex_buffer_data + mandibles_vbo_offset);
  231. antennae_vbo->read(0, antennae_vbo->get_size(), vertex_buffer_data + antennae_vbo_offset);
  232. waist_vbo->read(0, waist_vbo->get_size(), vertex_buffer_data + waist_vbo_offset);
  233. gaster_vbo->read(0, gaster_vbo->get_size(), vertex_buffer_data + gaster_vbo_offset);
  234. if (sting)
  235. sting_vbo->read(0, sting_vbo->get_size(), vertex_buffer_data + sting_vbo_offset);
  236. if (eyes)
  237. eyes_vbo->read(0, eyes_vbo->get_size(), vertex_buffer_data + eyes_vbo_offset);
  238. if (lateral_ocelli)
  239. lateral_ocelli_vbo->read(0, lateral_ocelli_vbo->get_size(), vertex_buffer_data + lateral_ocelli_vbo_offset);
  240. if (median_ocellus)
  241. median_ocellus_vbo->read(0, median_ocellus_vbo->get_size(), vertex_buffer_data + median_ocellus_vbo_offset);
  242. if (forewings)
  243. forewings_vbo->read(0, forewings_vbo->get_size(), vertex_buffer_data + forewings_vbo_offset);
  244. if (hindwings)
  245. hindwings_vbo->read(0, hindwings_vbo->get_size(), vertex_buffer_data + hindwings_vbo_offset);
  246. // Allocate model
  247. render::model* model = new render::model();
  248. // Setup model VAO
  249. gl::vertex_array* model_vao = model->get_vertex_array();
  250. for (auto [location, attribute]: mesosoma->get_vertex_array()->get_attributes())
  251. {
  252. attribute.buffer = model->get_vertex_buffer();
  253. model_vao->bind(location, attribute);
  254. }
  255. // Get vertex attributes
  256. const gl::vertex_attribute* position_attribute = nullptr;
  257. const gl::vertex_attribute* normal_attribute = nullptr;
  258. const gl::vertex_attribute* tangent_attribute = nullptr;
  259. const gl::vertex_attribute* bone_index_attribute = nullptr;
  260. const auto& vertex_attribute_map = model_vao->get_attributes();
  261. if (auto it = vertex_attribute_map.find(render::vertex_attribute::position); it != vertex_attribute_map.end())
  262. position_attribute = &it->second;
  263. if (auto it = vertex_attribute_map.find(render::vertex_attribute::normal); it != vertex_attribute_map.end())
  264. normal_attribute = &it->second;
  265. if (auto it = vertex_attribute_map.find(render::vertex_attribute::tangent); it != vertex_attribute_map.end())
  266. tangent_attribute = &it->second;
  267. if (auto it = vertex_attribute_map.find(render::vertex_attribute::bone_index); it != vertex_attribute_map.end())
  268. bone_index_attribute = &it->second;
  269. // Get body part skeletons
  270. const ::skeleton& mesosoma_skeleton = mesosoma->get_skeleton();
  271. const ::skeleton& legs_skeleton = legs->get_skeleton();
  272. const ::skeleton& head_skeleton = head->get_skeleton();
  273. const ::skeleton& mandibles_skeleton = mandibles->get_skeleton();
  274. const ::skeleton& antennae_skeleton = antennae->get_skeleton();
  275. const ::skeleton& waist_skeleton = waist->get_skeleton();
  276. const ::skeleton& gaster_skeleton = gaster->get_skeleton();
  277. const ::skeleton* sting_skeleton = (sting) ? &sting->get_skeleton() : nullptr;
  278. const ::skeleton* eyes_skeleton = (eyes) ? &eyes->get_skeleton() : nullptr;
  279. const ::skeleton* lateral_ocelli_skeleton = (lateral_ocelli) ? &lateral_ocelli->get_skeleton() : nullptr;
  280. const ::skeleton* median_ocellus_skeleton = (median_ocellus) ? &median_ocellus->get_skeleton() : nullptr;
  281. bool postpetiole = (waist_skeleton.bone_map.find("postpetiole") != waist_skeleton.bone_map.end());
  282. // Allocate skeleton bones
  283. ::skeleton& skeleton = model->get_skeleton();
  284. std::size_t bone_count = 34;
  285. if (postpetiole)
  286. bone_count += 1;
  287. if (sting)
  288. bone_count += 1;
  289. if (forewings)
  290. bone_count += 2;
  291. if (hindwings)
  292. bone_count += 2;
  293. // Assign bone indices
  294. std::uint8_t bone_index = 0;
  295. std::uint8_t mesosoma_bone_index = bone_index++;
  296. std::uint8_t foreleg_coxa_l_bone_index = bone_index++;
  297. std::uint8_t foreleg_coxa_r_bone_index = bone_index++;
  298. std::uint8_t foreleg_femur_l_bone_index = bone_index++;
  299. std::uint8_t foreleg_femur_r_bone_index = bone_index++;
  300. std::uint8_t foreleg_tibia_l_bone_index = bone_index++;
  301. std::uint8_t foreleg_tibia_r_bone_index = bone_index++;
  302. std::uint8_t foreleg_tarsus_l_bone_index = bone_index++;
  303. std::uint8_t foreleg_tarsus_r_bone_index = bone_index++;
  304. std::uint8_t midleg_coxa_l_bone_index = bone_index++;
  305. std::uint8_t midleg_coxa_r_bone_index = bone_index++;
  306. std::uint8_t midleg_femur_l_bone_index = bone_index++;
  307. std::uint8_t midleg_femur_r_bone_index = bone_index++;
  308. std::uint8_t midleg_tibia_l_bone_index = bone_index++;
  309. std::uint8_t midleg_tibia_r_bone_index = bone_index++;
  310. std::uint8_t midleg_tarsus_l_bone_index = bone_index++;
  311. std::uint8_t midleg_tarsus_r_bone_index = bone_index++;
  312. std::uint8_t hindleg_coxa_l_bone_index = bone_index++;
  313. std::uint8_t hindleg_coxa_r_bone_index = bone_index++;
  314. std::uint8_t hindleg_femur_l_bone_index = bone_index++;
  315. std::uint8_t hindleg_femur_r_bone_index = bone_index++;
  316. std::uint8_t hindleg_tibia_l_bone_index = bone_index++;
  317. std::uint8_t hindleg_tibia_r_bone_index = bone_index++;
  318. std::uint8_t hindleg_tarsus_l_bone_index = bone_index++;
  319. std::uint8_t hindleg_tarsus_r_bone_index = bone_index++;
  320. std::uint8_t head_bone_index = bone_index++;
  321. std::uint8_t mandible_l_bone_index = bone_index++;
  322. std::uint8_t mandible_r_bone_index = bone_index++;
  323. std::uint8_t scape_l_bone_index = bone_index++;
  324. std::uint8_t scape_r_bone_index = bone_index++;
  325. std::uint8_t pedicel_l_bone_index = bone_index++;
  326. std::uint8_t pedicel_r_bone_index = bone_index++;
  327. std::uint8_t petiole_bone_index = bone_index++;
  328. std::uint8_t postpetiole_bone_index = (postpetiole) ? bone_index++ : static_cast<std::uint8_t>(bone_count);
  329. std::uint8_t gaster_bone_index = bone_index++;
  330. std::uint8_t sting_bone_index = (sting) ? bone_index++ : static_cast<std::uint8_t>(bone_count);
  331. // Construct bone identifiers
  332. ::bone mesosoma_bone = make_bone(mesosoma_bone_index);
  333. ::bone foreleg_coxa_l_bone = make_bone(foreleg_coxa_l_bone_index);
  334. ::bone foreleg_coxa_r_bone = make_bone(foreleg_coxa_r_bone_index);
  335. ::bone foreleg_femur_l_bone = make_bone(foreleg_femur_l_bone_index, foreleg_coxa_l_bone_index);
  336. ::bone foreleg_femur_r_bone = make_bone(foreleg_femur_r_bone_index, foreleg_coxa_r_bone_index);
  337. ::bone foreleg_tibia_l_bone = make_bone(foreleg_tibia_l_bone_index, foreleg_femur_l_bone_index);
  338. ::bone foreleg_tibia_r_bone = make_bone(foreleg_tibia_r_bone_index, foreleg_femur_r_bone_index);
  339. ::bone foreleg_tarsus_l_bone = make_bone(foreleg_tarsus_l_bone_index, foreleg_tibia_l_bone_index);
  340. ::bone foreleg_tarsus_r_bone = make_bone(foreleg_tarsus_r_bone_index, foreleg_tibia_r_bone_index);
  341. ::bone midleg_coxa_l_bone = make_bone(midleg_coxa_l_bone_index);
  342. ::bone midleg_coxa_r_bone = make_bone(midleg_coxa_r_bone_index);
  343. ::bone midleg_femur_l_bone = make_bone(midleg_femur_l_bone_index, midleg_coxa_l_bone_index);
  344. ::bone midleg_femur_r_bone = make_bone(midleg_femur_r_bone_index, midleg_coxa_r_bone_index);
  345. ::bone midleg_tibia_l_bone = make_bone(midleg_tibia_l_bone_index, midleg_femur_l_bone_index);
  346. ::bone midleg_tibia_r_bone = make_bone(midleg_tibia_r_bone_index, midleg_femur_r_bone_index);
  347. ::bone midleg_tarsus_l_bone = make_bone(midleg_tarsus_l_bone_index, midleg_tibia_l_bone_index);
  348. ::bone midleg_tarsus_r_bone = make_bone(midleg_tarsus_r_bone_index, midleg_tibia_r_bone_index);
  349. ::bone hindleg_coxa_l_bone = make_bone(hindleg_coxa_l_bone_index);
  350. ::bone hindleg_coxa_r_bone = make_bone(hindleg_coxa_r_bone_index);
  351. ::bone hindleg_femur_l_bone = make_bone(hindleg_femur_l_bone_index, hindleg_coxa_l_bone_index);
  352. ::bone hindleg_femur_r_bone = make_bone(hindleg_femur_r_bone_index, hindleg_coxa_r_bone_index);
  353. ::bone hindleg_tibia_l_bone = make_bone(hindleg_tibia_l_bone_index, hindleg_femur_l_bone_index);
  354. ::bone hindleg_tibia_r_bone = make_bone(hindleg_tibia_r_bone_index, hindleg_femur_r_bone_index);
  355. ::bone hindleg_tarsus_l_bone = make_bone(hindleg_tarsus_l_bone_index, hindleg_tibia_l_bone_index);
  356. ::bone hindleg_tarsus_r_bone = make_bone(hindleg_tarsus_r_bone_index, hindleg_tibia_r_bone_index);
  357. ::bone head_bone = make_bone(head_bone_index, mesosoma_bone_index);
  358. ::bone mandible_l_bone = make_bone(mandible_l_bone_index, head_bone_index);
  359. ::bone mandible_r_bone = make_bone(mandible_r_bone_index, head_bone_index);
  360. ::bone scape_l_bone = make_bone(scape_l_bone_index, head_bone_index);
  361. ::bone scape_r_bone = make_bone(scape_r_bone_index, head_bone_index);
  362. ::bone pedicel_l_bone = make_bone(pedicel_l_bone_index, scape_l_bone_index);
  363. ::bone pedicel_r_bone = make_bone(pedicel_r_bone_index, scape_r_bone_index);
  364. ::bone petiole_bone = make_bone(petiole_bone_index, mesosoma_bone_index);
  365. ::bone postpetiole_bone = make_bone(postpetiole_bone_index, petiole_bone_index);
  366. ::bone gaster_bone = make_bone(gaster_bone_index, (postpetiole) ? postpetiole_bone_index : petiole_bone_index);
  367. ::bone sting_bone = make_bone(sting_bone_index, gaster_bone_index);
  368. // Map bone names to bones
  369. skeleton.bone_map["mesosoma"] = mesosoma_bone;
  370. skeleton.bone_map["foreleg_coxa_l"] = foreleg_coxa_l_bone;
  371. skeleton.bone_map["foreleg_coxa_r"] = foreleg_coxa_r_bone;
  372. skeleton.bone_map["foreleg_femur_l"] = foreleg_femur_l_bone;
  373. skeleton.bone_map["foreleg_femur_r"] = foreleg_femur_r_bone;
  374. skeleton.bone_map["foreleg_tibia_l"] = foreleg_tibia_l_bone;
  375. skeleton.bone_map["foreleg_tibia_r"] = foreleg_tibia_r_bone;
  376. skeleton.bone_map["foreleg_tarsus_l"] = foreleg_tarsus_l_bone;
  377. skeleton.bone_map["foreleg_tarsus_r"] = foreleg_tarsus_r_bone;
  378. skeleton.bone_map["midleg_coxa_l"] = midleg_coxa_l_bone;
  379. skeleton.bone_map["midleg_coxa_r"] = midleg_coxa_r_bone;
  380. skeleton.bone_map["midleg_femur_l"] = midleg_femur_l_bone;
  381. skeleton.bone_map["midleg_femur_r"] = midleg_femur_r_bone;
  382. skeleton.bone_map["midleg_tibia_l"] = midleg_tibia_l_bone;
  383. skeleton.bone_map["midleg_tibia_r"] = midleg_tibia_r_bone;
  384. skeleton.bone_map["midleg_tarsus_l"] = midleg_tarsus_l_bone;
  385. skeleton.bone_map["midleg_tarsus_r"] = midleg_tarsus_r_bone;
  386. skeleton.bone_map["hindleg_coxa_l"] = hindleg_coxa_l_bone;
  387. skeleton.bone_map["hindleg_coxa_r"] = hindleg_coxa_r_bone;
  388. skeleton.bone_map["hindleg_femur_l"] = hindleg_femur_l_bone;
  389. skeleton.bone_map["hindleg_femur_r"] = hindleg_femur_r_bone;
  390. skeleton.bone_map["hindleg_tibia_l"] = hindleg_tibia_l_bone;
  391. skeleton.bone_map["hindleg_tibia_r"] = hindleg_tibia_r_bone;
  392. skeleton.bone_map["hindleg_tarsus_l"] = hindleg_tarsus_l_bone;
  393. skeleton.bone_map["hindleg_tarsus_r"] = hindleg_tarsus_r_bone;
  394. skeleton.bone_map["head"] = head_bone;
  395. skeleton.bone_map["mandible_l"] = mandible_l_bone;
  396. skeleton.bone_map["mandible_r"] = mandible_r_bone;
  397. skeleton.bone_map["scape_l"] = scape_l_bone;
  398. skeleton.bone_map["scape_r"] = scape_r_bone;
  399. skeleton.bone_map["pedicel_l"] = pedicel_l_bone;
  400. skeleton.bone_map["pedicel_r"] = pedicel_r_bone;
  401. skeleton.bone_map["petiole"] = petiole_bone;
  402. if (postpetiole)
  403. skeleton.bone_map["postpetiole"] = postpetiole_bone;
  404. skeleton.bone_map["gaster"] = gaster_bone;
  405. if (sting)
  406. skeleton.bone_map["sting"] = sting_bone;
  407. // Get reference to skeleton bind pose
  408. pose& bind_pose = skeleton.bind_pose;
  409. // Skeleton mesosoma pose
  410. if (auto it = mesosoma_skeleton.bone_map.find("mesosoma"); it != mesosoma_skeleton.bone_map.end())
  411. bind_pose[mesosoma_bone] = mesosoma_skeleton.bind_pose.at(it->second);
  412. // Skeleton forelegs pose
  413. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_l"); it != legs_skeleton.bone_map.end())
  414. bind_pose[foreleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  415. if (auto it = legs_skeleton.bone_map.find("foreleg_coxa_r"); it != legs_skeleton.bone_map.end())
  416. bind_pose[foreleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  417. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_l"); it != legs_skeleton.bone_map.end())
  418. bind_pose[foreleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  419. if (auto it = legs_skeleton.bone_map.find("foreleg_femur_r"); it != legs_skeleton.bone_map.end())
  420. bind_pose[foreleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  421. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_l"); it != legs_skeleton.bone_map.end())
  422. bind_pose[foreleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  423. if (auto it = legs_skeleton.bone_map.find("foreleg_tibia_r"); it != legs_skeleton.bone_map.end())
  424. bind_pose[foreleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  425. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus_l"); it != legs_skeleton.bone_map.end())
  426. bind_pose[foreleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  427. if (auto it = legs_skeleton.bone_map.find("foreleg_tarsus_r"); it != legs_skeleton.bone_map.end())
  428. bind_pose[foreleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  429. // Skeleton midlegs pose
  430. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_l"); it != legs_skeleton.bone_map.end())
  431. bind_pose[midleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  432. if (auto it = legs_skeleton.bone_map.find("midleg_coxa_r"); it != legs_skeleton.bone_map.end())
  433. bind_pose[midleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  434. if (auto it = legs_skeleton.bone_map.find("midleg_femur_l"); it != legs_skeleton.bone_map.end())
  435. bind_pose[midleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  436. if (auto it = legs_skeleton.bone_map.find("midleg_femur_r"); it != legs_skeleton.bone_map.end())
  437. bind_pose[midleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  438. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_l"); it != legs_skeleton.bone_map.end())
  439. bind_pose[midleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  440. if (auto it = legs_skeleton.bone_map.find("midleg_tibia_r"); it != legs_skeleton.bone_map.end())
  441. bind_pose[midleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  442. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus_l"); it != legs_skeleton.bone_map.end())
  443. bind_pose[midleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  444. if (auto it = legs_skeleton.bone_map.find("midleg_tarsus_r"); it != legs_skeleton.bone_map.end())
  445. bind_pose[midleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  446. // Skeleton hindlegs pose
  447. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_l"); it != legs_skeleton.bone_map.end())
  448. bind_pose[hindleg_coxa_l_bone] = legs_skeleton.bind_pose.at(it->second);
  449. if (auto it = legs_skeleton.bone_map.find("hindleg_coxa_r"); it != legs_skeleton.bone_map.end())
  450. bind_pose[hindleg_coxa_r_bone] = legs_skeleton.bind_pose.at(it->second);
  451. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_l"); it != legs_skeleton.bone_map.end())
  452. bind_pose[hindleg_femur_l_bone] = legs_skeleton.bind_pose.at(it->second);
  453. if (auto it = legs_skeleton.bone_map.find("hindleg_femur_r"); it != legs_skeleton.bone_map.end())
  454. bind_pose[hindleg_femur_r_bone] = legs_skeleton.bind_pose.at(it->second);
  455. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_l"); it != legs_skeleton.bone_map.end())
  456. bind_pose[hindleg_tibia_l_bone] = legs_skeleton.bind_pose.at(it->second);
  457. if (auto it = legs_skeleton.bone_map.find("hindleg_tibia_r"); it != legs_skeleton.bone_map.end())
  458. bind_pose[hindleg_tibia_r_bone] = legs_skeleton.bind_pose.at(it->second);
  459. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus_l"); it != legs_skeleton.bone_map.end())
  460. bind_pose[hindleg_tarsus_l_bone] = legs_skeleton.bind_pose.at(it->second);
  461. if (auto it = legs_skeleton.bone_map.find("hindleg_tarsus_r"); it != legs_skeleton.bone_map.end())
  462. bind_pose[hindleg_tarsus_r_bone] = legs_skeleton.bind_pose.at(it->second);
  463. // Skeleton head pose
  464. 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"));
  465. // Skeleton mandibles pose
  466. 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"));
  467. 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"));
  468. // Skeleton antennae pose
  469. 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"));
  470. 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"));
  471. bind_pose[pedicel_l_bone] = antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("pedicel_l"));
  472. bind_pose[pedicel_r_bone] = antennae_skeleton.bind_pose.at(antennae_skeleton.bone_map.at("pedicel_r"));
  473. // Skeleton waist pose
  474. 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"));
  475. if (postpetiole)
  476. {
  477. bind_pose[postpetiole_bone] = waist_skeleton.bind_pose.at(waist_skeleton.bone_map.at("postpetiole"));
  478. }
  479. // Skeleton gaster pose
  480. if (postpetiole)
  481. {
  482. 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"));
  483. }
  484. else
  485. {
  486. 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"));
  487. }
  488. // Skeleton sting pose
  489. if (sting)
  490. {
  491. 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"));
  492. }
  493. // Calculate the skeleton-space bind pose
  494. pose bind_pose_ss;
  495. concatenate(bind_pose, bind_pose_ss);
  496. // Get number of vertex indices for each body part
  497. std::size_t mesosoma_index_count = (*mesosoma->get_groups())[0]->get_index_count();
  498. std::size_t legs_index_count = (*legs->get_groups())[0]->get_index_count();
  499. std::size_t head_index_count = (*head->get_groups())[0]->get_index_count();
  500. std::size_t mandibles_index_count = (*mandibles->get_groups())[0]->get_index_count();
  501. std::size_t antennae_index_count = (*antennae->get_groups())[0]->get_index_count();
  502. std::size_t waist_index_count = (*waist->get_groups())[0]->get_index_count();
  503. std::size_t gaster_index_count = (*gaster->get_groups())[0]->get_index_count();
  504. std::size_t sting_index_count = (sting) ? (*sting->get_groups())[0]->get_index_count() : 0;
  505. std::size_t eyes_index_count = (eyes) ? (*eyes->get_groups())[0]->get_index_count() : 0;
  506. std::size_t lateral_ocelli_index_count = (lateral_ocelli) ? (*lateral_ocelli->get_groups())[0]->get_index_count() : 0;
  507. std::size_t median_ocellus_index_count = (median_ocellus) ? (*median_ocellus->get_groups())[0]->get_index_count() : 0;
  508. std::size_t forewings_index_count = (forewings) ? (*forewings->get_groups())[0]->get_index_count() : 0;
  509. std::size_t hindwings_index_count = (hindwings) ? (*hindwings->get_groups())[0]->get_index_count() : 0;
  510. std::size_t exoskeleton_index_count =
  511. mesosoma_index_count
  512. + legs_index_count
  513. + head_index_count
  514. + mandibles_index_count
  515. + antennae_index_count
  516. + waist_index_count
  517. + gaster_index_count
  518. + sting_index_count;
  519. // Calculate transform from head space to body space
  520. math::transform<float> head_to_body = bind_pose_ss.at(mesosoma_bone) * mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("head"));
  521. // Reskin head bone
  522. std::unordered_set<std::uint8_t> old_head_bone_indices;
  523. if (auto it = head_skeleton.bone_map.find("head"); it != head_skeleton.bone_map.end())
  524. old_head_bone_indices.emplace(it->second);
  525. 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);
  526. // Calculate transforms from mandibles space to body space
  527. 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"));
  528. 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"));
  529. // Reskin mandible bones
  530. std::unordered_set<std::uint8_t> old_head_mandible_l_bone_indices;
  531. if (auto it = mandibles_skeleton.bone_map.find("mandible_l"); it != mandibles_skeleton.bone_map.end())
  532. old_head_mandible_l_bone_indices.emplace(it->second);
  533. 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);
  534. std::unordered_set<std::uint8_t> old_head_mandible_r_bone_indices;
  535. if (auto it = mandibles_skeleton.bone_map.find("mandible_r"); it != mandibles_skeleton.bone_map.end())
  536. old_head_mandible_r_bone_indices.emplace(it->second);
  537. 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);
  538. // Calculate transforms from antennae space to body space
  539. 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"));
  540. 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"));
  541. // Reskin scape bones
  542. std::unordered_set<std::uint8_t> old_scape_l_indices;
  543. if (auto it = antennae_skeleton.bone_map.find("scape_l"); it != antennae_skeleton.bone_map.end())
  544. old_scape_l_indices.emplace(it->second);
  545. 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);
  546. std::unordered_set<std::uint8_t> old_scape_r_indices;
  547. if (auto it = antennae_skeleton.bone_map.find("scape_r"); it != antennae_skeleton.bone_map.end())
  548. old_scape_r_indices.emplace(it->second);
  549. 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);
  550. // Reskin pedicel bones
  551. const std::vector<std::string> pedicel_bone_names =
  552. {
  553. "pedicel",
  554. "flagellomere_1",
  555. "flagellomere_2",
  556. "flagellomere_3",
  557. "flagellomere_4",
  558. "flagellomere_5",
  559. "flagellomere_6",
  560. "flagellomere_7",
  561. "flagellomere_8",
  562. "flagellomere_9",
  563. "flagellomere_10",
  564. "flagellomere_11",
  565. "flagellomere_12"
  566. };
  567. std::unordered_set<std::uint8_t> old_pedicel_l_indices;
  568. for (const std::string& bone_name: pedicel_bone_names)
  569. if (auto it = antennae_skeleton.bone_map.find(bone_name + "_l"); it != antennae_skeleton.bone_map.end())
  570. old_pedicel_l_indices.emplace(it->second);
  571. 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);
  572. std::unordered_set<std::uint8_t> old_pedicel_r_indices;
  573. for (const std::string& bone_name: pedicel_bone_names)
  574. if (auto it = antennae_skeleton.bone_map.find(bone_name + "_r"); it != antennae_skeleton.bone_map.end())
  575. old_pedicel_r_indices.emplace(it->second);
  576. 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);
  577. // Calculate transform from waist space to body space
  578. math::transform<float> waist_to_body = bind_pose_ss.at(mesosoma_bone) * mesosoma_skeleton.bind_pose.at(mesosoma_skeleton.bone_map.at("petiole"));
  579. // Reskin waist bones
  580. std::unordered_set<std::uint8_t> old_petiole_bone_indices;
  581. if (auto it = waist_skeleton.bone_map.find("petiole"); it != waist_skeleton.bone_map.end())
  582. old_petiole_bone_indices.emplace(it->second);
  583. 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);
  584. if (postpetiole)
  585. {
  586. std::unordered_set<std::uint8_t> old_postpetiole_bone_indices;
  587. if (auto it = waist_skeleton.bone_map.find("postpetiole"); it != waist_skeleton.bone_map.end())
  588. old_postpetiole_bone_indices.emplace(it->second);
  589. 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);
  590. }
  591. // Calculate transform from gaster space to body space
  592. 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"));
  593. // Reskin gaster bones
  594. std::unordered_set<std::uint8_t> old_gaster_bone_indices;
  595. if (auto it = gaster_skeleton.bone_map.find("gaster"); it != gaster_skeleton.bone_map.end())
  596. old_gaster_bone_indices.emplace(it->second);
  597. 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);
  598. if (sting)
  599. {
  600. // Calculate transform from sting space to body space
  601. math::transform<float> sting_to_body = bind_pose_ss.at(gaster_bone) * gaster_skeleton.bind_pose.at(gaster_skeleton.bone_map.at("sting"));
  602. // Reskin sting bones
  603. std::unordered_set<std::uint8_t> old_sting_bone_indices;
  604. if (auto it = sting_skeleton->bone_map.find("sting"); it != sting_skeleton->bone_map.end())
  605. old_sting_bone_indices.emplace(it->second);
  606. 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);
  607. }
  608. if (eyes)
  609. {
  610. // Calculate transforms from eyes space to body space
  611. 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"));
  612. 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"));
  613. // Reskin eye bones
  614. std::unordered_set<std::uint8_t> old_eye_l_bone_indices;
  615. if (auto it = eyes_skeleton->bone_map.find("eye_l"); it != eyes_skeleton->bone_map.end())
  616. old_eye_l_bone_indices.emplace(it->second);
  617. 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);
  618. std::unordered_set<std::uint8_t> old_eye_r_bone_indices;
  619. if (auto it = eyes_skeleton->bone_map.find("eye_r"); it != eyes_skeleton->bone_map.end())
  620. old_eye_r_bone_indices.emplace(it->second);
  621. 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);
  622. }
  623. if (lateral_ocelli)
  624. {
  625. // Calculate transforms from lateral ocelli space to body space
  626. 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"));
  627. 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"));
  628. // Reskin lateral ocelli bones
  629. std::unordered_set<std::uint8_t> old_ocellus_l_bone_indices;
  630. if (auto it = lateral_ocelli_skeleton->bone_map.find("ocellus_l"); it != lateral_ocelli_skeleton->bone_map.end())
  631. old_ocellus_l_bone_indices.emplace(it->second);
  632. 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);
  633. std::unordered_set<std::uint8_t> old_ocellus_r_bone_indices;
  634. if (auto it = lateral_ocelli_skeleton->bone_map.find("ocellus_r"); it != lateral_ocelli_skeleton->bone_map.end())
  635. old_ocellus_r_bone_indices.emplace(it->second);
  636. 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);
  637. }
  638. if (median_ocellus)
  639. {
  640. // Calculate transforms from lateral ocelli space to body space
  641. 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"));
  642. // Reskin lateral ocelli bones
  643. std::unordered_set<std::uint8_t> old_ocellus_m_bone_indices;
  644. if (auto it = median_ocellus_skeleton->bone_map.find("ocellus_m"); it != median_ocellus_skeleton->bone_map.end())
  645. old_ocellus_m_bone_indices.emplace(it->second);
  646. 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);
  647. }
  648. // Upload vertex buffer data to model VBO
  649. model->get_vertex_buffer()->repurpose(gl::buffer_usage::static_draw, vertex_buffer_size, vertex_buffer_data);
  650. // Construct exoskeleton model group
  651. render::model_group* exoskeleton_group = model->add_group("exoskeleton");
  652. exoskeleton_group->set_material(exoskeleton_material);
  653. exoskeleton_group->set_drawing_mode(gl::drawing_mode::triangles);
  654. exoskeleton_group->set_start_index(0);
  655. exoskeleton_group->set_index_count(exoskeleton_index_count);
  656. std::size_t index_offset = exoskeleton_index_count;
  657. if (eyes)
  658. {
  659. // Construct eyes model group
  660. render::model_group* eyes_group = model->add_group("eyes");
  661. eyes_group->set_material((*eyes->get_groups())[0]->get_material());
  662. eyes_group->set_drawing_mode(gl::drawing_mode::triangles);
  663. eyes_group->set_start_index(index_offset);
  664. eyes_group->set_index_count(eyes_index_count);
  665. index_offset += eyes_index_count;
  666. }
  667. if (lateral_ocelli || median_ocellus)
  668. {
  669. // Construct ocelli model group
  670. render::model_group* ocelli_group = model->add_group("ocelli");
  671. ocelli_group->set_drawing_mode(gl::drawing_mode::triangles);
  672. ocelli_group->set_start_index(index_offset);
  673. std::size_t index_count = 0;
  674. if (lateral_ocelli)
  675. {
  676. index_count += lateral_ocelli_index_count;
  677. index_offset += lateral_ocelli_index_count;
  678. ocelli_group->set_material((*lateral_ocelli->get_groups())[0]->get_material());
  679. }
  680. if (median_ocellus)
  681. {
  682. index_count += median_ocellus_index_count;
  683. index_offset += median_ocellus_index_count;
  684. if (!lateral_ocelli)
  685. ocelli_group->set_material((*median_ocellus->get_groups())[0]->get_material());
  686. }
  687. ocelli_group->set_index_count(index_count);
  688. }
  689. if (forewings)
  690. {
  691. // Construct forewings model group
  692. render::model_group* forewings_group = model->add_group("forewings");
  693. forewings_group->set_material((*forewings->get_groups())[0]->get_material());
  694. forewings_group->set_drawing_mode(gl::drawing_mode::triangles);
  695. forewings_group->set_start_index(index_offset);
  696. forewings_group->set_index_count(forewings_index_count);
  697. index_offset += forewings_index_count;
  698. }
  699. if (hindwings)
  700. {
  701. // Construct hindwings model group
  702. render::model_group* hindwings_group = model->add_group("hindwings");
  703. hindwings_group->set_material((*hindwings->get_groups())[0]->get_material());
  704. hindwings_group->set_drawing_mode(gl::drawing_mode::triangles);
  705. hindwings_group->set_start_index(index_offset);
  706. hindwings_group->set_index_count(hindwings_index_count);
  707. index_offset += hindwings_index_count;
  708. }
  709. // Calculate model bounding box
  710. geom::aabb<float> bounds = calculate_bounds(vertex_buffer_data, index_offset, *position_attribute);
  711. model->set_bounds(bounds);
  712. // Free vertex buffer data
  713. delete[] vertex_buffer_data;
  714. return model;
  715. }
  716. void reskin_vertices
  717. (
  718. std::uint8_t* vertex_data,
  719. std::size_t index_count,
  720. const gl::vertex_attribute& position_attribute,
  721. const gl::vertex_attribute& normal_attribute,
  722. const gl::vertex_attribute& tangent_attribute,
  723. const gl::vertex_attribute& bone_index_attribute,
  724. const std::unordered_set<std::uint8_t>& old_bone_indices,
  725. std::uint8_t new_bone_index,
  726. const math::transform<float>& transform
  727. )
  728. {
  729. std::uint8_t* position_data = vertex_data + position_attribute.offset;
  730. std::uint8_t* normal_data = vertex_data + normal_attribute.offset;
  731. std::uint8_t* tangent_data = vertex_data + tangent_attribute.offset;
  732. std::uint8_t* bone_index_data = vertex_data + bone_index_attribute.offset;
  733. for (std::size_t i = 0; i < index_count; ++i)
  734. {
  735. // Get bone index of current vertex
  736. float* bone_index = reinterpret_cast<float*>(bone_index_data + bone_index_attribute.stride * i);
  737. // Skip irrelevant bones
  738. if (!old_bone_indices.count(static_cast<std::uint8_t>(*bone_index + 0.5f)))
  739. continue;
  740. // Get vertex position
  741. float* x = reinterpret_cast<float*>(position_data + position_attribute.stride * i);
  742. float* y = x + 1;
  743. float* z = y + 1;
  744. // Get vertex normal
  745. float* nx = reinterpret_cast<float*>(normal_data + normal_attribute.stride * i);
  746. float* ny = nx + 1;
  747. float* nz = ny + 1;
  748. // Get vertex tangent
  749. float* tx = reinterpret_cast<float*>(tangent_data + tangent_attribute.stride * i);
  750. float* ty = tx + 1;
  751. float* tz = ty + 1;
  752. //float* bts = tz + 1;
  753. // Transform vertex attributes
  754. float3 position = transform * float3{*x, *y, *z};
  755. float3 normal = math::normalize(transform.rotation * float3{*nx, *ny, *nz});
  756. float3 tangent = transform.rotation * float3{*tx, *ty, *tz};
  757. // Update vertex data
  758. *x = position.x;
  759. *y = position.y;
  760. *z = position.z;
  761. *nx = normal.x;
  762. *ny = normal.y;
  763. *nz = normal.z;
  764. *tx = tangent.x;
  765. *ty = tangent.y;
  766. *tz = tangent.z;
  767. //*bts = ...
  768. *bone_index = static_cast<float>(new_bone_index);
  769. }
  770. }
  771. geom::aabb<float> calculate_bounds(std::uint8_t* vertex_data, std::size_t index_count, const gl::vertex_attribute& position_attribute)
  772. {
  773. std::uint8_t* position_data = vertex_data + position_attribute.offset;
  774. geom::aabb<float> bounds;
  775. bounds.min_point.x = std::numeric_limits<float>::infinity();
  776. bounds.min_point.y = std::numeric_limits<float>::infinity();
  777. bounds.min_point.z = std::numeric_limits<float>::infinity();
  778. bounds.max_point.x = -std::numeric_limits<float>::infinity();
  779. bounds.max_point.y = -std::numeric_limits<float>::infinity();
  780. bounds.max_point.z = -std::numeric_limits<float>::infinity();
  781. for (std::size_t i = 0; i < index_count; ++i)
  782. {
  783. // Get vertex position
  784. float* x = reinterpret_cast<float*>(position_data + position_attribute.stride * i);
  785. float* y = x + 1;
  786. float* z = y + 1;
  787. bounds.min_point.x = std::min<float>(*x, bounds.min_point.x);
  788. bounds.min_point.y = std::min<float>(*y, bounds.min_point.y);
  789. bounds.min_point.z = std::min<float>(*z, bounds.min_point.z);
  790. bounds.max_point.x = std::max<float>(*x, bounds.max_point.x);
  791. bounds.max_point.y = std::max<float>(*y, bounds.max_point.y);
  792. bounds.max_point.z = std::max<float>(*z, bounds.max_point.z);
  793. }
  794. return bounds;
  795. }
  796. } // namespace ant
  797. } // namespace game