primitives.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #include "primitives.h"
  2. #include "gl/mesh.h"
  3. #include <QVector3D>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <memory>
  7. #include <numbers>
  8. #include <qvectornd.h>
  9. #include <vector>
  10. namespace Render::GL {
  11. namespace {
  12. constexpr float k_pi = std::numbers::pi_v<float>;
  13. constexpr float k_two_pi = 6.28318530718F;
  14. constexpr float k_half_scalar = 0.5F;
  15. constexpr float k_unit_radius = 1.0F;
  16. constexpr float k_micro_noise_frequency = 12.9898F;
  17. constexpr float k_micro_noise_scale = 43758.5453F;
  18. constexpr float k_uv_center = 0.5F;
  19. constexpr float k_uv_scale = 0.5F;
  20. constexpr int k_indices_per_quad = 6;
  21. auto create_unit_cylinder_mesh(int radial_segments) -> std::unique_ptr<Mesh> {
  22. const float radius = k_unit_radius;
  23. const float half_h = k_half_scalar;
  24. std::vector<Vertex> v;
  25. std::vector<unsigned int> idx;
  26. for (int y = 0; y <= 1; ++y) {
  27. float py = (y != 0) ? half_h : -half_h;
  28. auto v_coord = float(y);
  29. for (int i = 0; i <= radial_segments; ++i) {
  30. float u = float(i) / float(radial_segments);
  31. float const ang = u * k_two_pi;
  32. float px = radius * std::cos(ang);
  33. float pz = radius * std::sin(ang);
  34. QVector3D n(px, 0.0F, pz);
  35. n.normalize();
  36. v.push_back({{px, py, pz}, {n.x(), n.y(), n.z()}, {u, v_coord}});
  37. }
  38. }
  39. int const row = radial_segments + 1;
  40. for (int i = 0; i < radial_segments; ++i) {
  41. int a = 0 * row + i;
  42. int b = 0 * row + i + 1;
  43. int c = 1 * row + i + 1;
  44. int d = 1 * row + i;
  45. idx.push_back(a);
  46. idx.push_back(b);
  47. idx.push_back(c);
  48. idx.push_back(c);
  49. idx.push_back(d);
  50. idx.push_back(a);
  51. }
  52. int base_top = (int)v.size();
  53. v.push_back(
  54. {{0.0F, half_h, 0.0F}, {0.0F, 1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  55. for (int i = 0; i <= radial_segments; ++i) {
  56. float const u = float(i) / float(radial_segments);
  57. float const ang = u * k_two_pi;
  58. float px = radius * std::cos(ang);
  59. float pz = radius * std::sin(ang);
  60. v.push_back({{px, half_h, pz},
  61. {0.0F, 1.0F, 0.0F},
  62. {k_uv_center + k_uv_scale * std::cos(ang),
  63. k_uv_center + k_uv_scale * std::sin(ang)}});
  64. }
  65. for (int i = 1; i <= radial_segments; ++i) {
  66. idx.push_back(base_top);
  67. idx.push_back(base_top + i);
  68. idx.push_back(base_top + i + 1);
  69. }
  70. int base_bot = (int)v.size();
  71. v.push_back(
  72. {{0.0F, -half_h, 0.0F}, {0.0F, -1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  73. for (int i = 0; i <= radial_segments; ++i) {
  74. float const u = float(i) / float(radial_segments);
  75. float const ang = u * k_two_pi;
  76. float px = radius * std::cos(ang);
  77. float pz = radius * std::sin(ang);
  78. v.push_back({{px, -half_h, pz},
  79. {0.0F, -1.0F, 0.0F},
  80. {k_uv_center + k_uv_scale * std::cos(ang),
  81. k_uv_center + k_uv_scale * std::sin(ang)}});
  82. }
  83. for (int i = 1; i <= radial_segments; ++i) {
  84. idx.push_back(base_bot);
  85. idx.push_back(base_bot + i + 1);
  86. idx.push_back(base_bot + i);
  87. }
  88. return std::make_unique<Mesh>(v, idx);
  89. }
  90. auto create_unit_sphere_mesh(int lat_segments,
  91. int lon_segments) -> std::unique_ptr<Mesh> {
  92. const float r = k_unit_radius;
  93. std::vector<Vertex> v;
  94. std::vector<unsigned int> idx;
  95. for (int y = 0; y <= lat_segments; ++y) {
  96. float vy = float(y) / float(lat_segments);
  97. float const phi = vy * k_pi;
  98. float py = r * std::cos(phi);
  99. float const pr = r * std::sin(phi);
  100. for (int x = 0; x <= lon_segments; ++x) {
  101. float vx = float(x) / float(lon_segments);
  102. float const theta = vx * k_two_pi;
  103. float px = pr * std::cos(theta);
  104. float pz = pr * std::sin(theta);
  105. QVector3D n(px, py, pz);
  106. n.normalize();
  107. v.push_back({{px, py, pz}, {n.x(), n.y(), n.z()}, {vx, vy}});
  108. }
  109. }
  110. int const row = lon_segments + 1;
  111. for (int y = 0; y < lat_segments; ++y) {
  112. for (int x = 0; x < lon_segments; ++x) {
  113. int a = y * row + x;
  114. int b = a + 1;
  115. int c = (y + 1) * row + x + 1;
  116. int d = (y + 1) * row + x;
  117. idx.push_back(a);
  118. idx.push_back(b);
  119. idx.push_back(c);
  120. idx.push_back(c);
  121. idx.push_back(d);
  122. idx.push_back(a);
  123. }
  124. }
  125. return std::make_unique<Mesh>(v, idx);
  126. }
  127. auto create_unit_cone_mesh(int radial_segments) -> std::unique_ptr<Mesh> {
  128. const float base_r = k_unit_radius;
  129. const float half_h = k_half_scalar;
  130. std::vector<Vertex> v;
  131. std::vector<unsigned int> idx;
  132. int apex_idx = 0;
  133. v.push_back({{0.0F, +half_h, 0.0F}, {0.0F, 1.0F, 0.0F}, {k_uv_center, 1.0F}});
  134. for (int i = 0; i <= radial_segments; ++i) {
  135. float u = float(i) / float(radial_segments);
  136. float const ang = u * k_two_pi;
  137. float px = base_r * std::cos(ang);
  138. float pz = base_r * std::sin(ang);
  139. QVector3D n(px, base_r, pz);
  140. n.normalize();
  141. v.push_back({{px, -half_h, pz}, {n.x(), n.y(), n.z()}, {u, 0.0F}});
  142. }
  143. for (int i = 1; i <= radial_segments; ++i) {
  144. idx.push_back(apex_idx);
  145. idx.push_back(i);
  146. idx.push_back(i + 1);
  147. }
  148. int base_center = (int)v.size();
  149. v.push_back(
  150. {{0.0F, -half_h, 0.0F}, {0.0F, -1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  151. int const base_start = (int)v.size();
  152. for (int i = 0; i <= radial_segments; ++i) {
  153. float const u = float(i) / float(radial_segments);
  154. float const ang = u * k_two_pi;
  155. float px = base_r * std::cos(ang);
  156. float pz = base_r * std::sin(ang);
  157. v.push_back({{px, -half_h, pz},
  158. {0.0F, -1.0F, 0.0F},
  159. {k_uv_center + k_uv_scale * std::cos(ang),
  160. k_uv_center + k_uv_scale * std::sin(ang)}});
  161. }
  162. for (int i = 0; i < radial_segments; ++i) {
  163. idx.push_back(base_center);
  164. idx.push_back(base_start + i + 1);
  165. idx.push_back(base_start + i);
  166. }
  167. return std::make_unique<Mesh>(v, idx);
  168. }
  169. auto create_capsule_mesh(int radial_segments,
  170. int height_segments) -> std::unique_ptr<Mesh> {
  171. constexpr float k_capsule_radius = 0.25F;
  172. const float radius = k_capsule_radius;
  173. const float half_h = k_half_scalar;
  174. std::vector<Vertex> verts;
  175. std::vector<unsigned int> idx;
  176. for (int y = 0; y <= height_segments; ++y) {
  177. float v = float(y) / float(height_segments);
  178. float py = -half_h + v * (2.0F * half_h);
  179. for (int i = 0; i <= radial_segments; ++i) {
  180. float u = float(i) / float(radial_segments);
  181. float const ang = u * k_two_pi;
  182. float px = radius * std::cos(ang);
  183. float pz = radius * std::sin(ang);
  184. QVector3D n(px, 0.0F, pz);
  185. n.normalize();
  186. verts.push_back({{px, py, pz}, {n.x(), n.y(), n.z()}, {u, v}});
  187. }
  188. }
  189. int const row = radial_segments + 1;
  190. for (int y = 0; y < height_segments; ++y) {
  191. for (int i = 0; i < radial_segments; ++i) {
  192. int a = y * row + i;
  193. int b = y * row + i + 1;
  194. int c = (y + 1) * row + i + 1;
  195. int d = (y + 1) * row + i;
  196. idx.push_back(a);
  197. idx.push_back(b);
  198. idx.push_back(c);
  199. idx.push_back(c);
  200. idx.push_back(d);
  201. idx.push_back(a);
  202. }
  203. }
  204. int base_top = (int)verts.size();
  205. verts.push_back(
  206. {{0.0F, half_h, 0.0F}, {0.0F, 1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  207. for (int i = 0; i <= radial_segments; ++i) {
  208. float const u = float(i) / float(radial_segments);
  209. float const ang = u * k_two_pi;
  210. float px = radius * std::cos(ang);
  211. float pz = radius * std::sin(ang);
  212. verts.push_back({{px, half_h, pz},
  213. {0.0F, 1.0F, 0.0F},
  214. {k_uv_center + k_uv_scale * std::cos(ang),
  215. k_uv_center + k_uv_scale * std::sin(ang)}});
  216. }
  217. for (int i = 1; i <= radial_segments; ++i) {
  218. idx.push_back(base_top);
  219. idx.push_back(base_top + i);
  220. idx.push_back(base_top + i + 1);
  221. }
  222. int base_bot = (int)verts.size();
  223. verts.push_back(
  224. {{0.0F, -half_h, 0.0F}, {0.0F, -1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  225. for (int i = 0; i <= radial_segments; ++i) {
  226. float const u = float(i) / float(radial_segments);
  227. float const ang = u * k_two_pi;
  228. float px = radius * std::cos(ang);
  229. float pz = radius * std::sin(ang);
  230. verts.push_back({{px, -half_h, pz},
  231. {0.0F, -1.0F, 0.0F},
  232. {k_uv_center + k_uv_scale * std::cos(ang),
  233. k_uv_center + k_uv_scale * std::sin(ang)}});
  234. }
  235. for (int i = 1; i <= radial_segments; ++i) {
  236. idx.push_back(base_bot);
  237. idx.push_back(base_bot + i + 1);
  238. idx.push_back(base_bot + i);
  239. }
  240. return std::make_unique<Mesh>(verts, idx);
  241. }
  242. auto simple_hash(float seed) -> float {
  243. float const x =
  244. std::sin(seed * k_micro_noise_frequency) * k_micro_noise_scale;
  245. return x - std::floor(x);
  246. }
  247. auto create_unit_torso_mesh(int radial_segments,
  248. int height_segments) -> std::unique_ptr<Mesh> {
  249. const float half_h = k_half_scalar;
  250. constexpr float k_lower_extension = 0.05F;
  251. const float y_min = -half_h;
  252. const float y_max = half_h + k_lower_extension;
  253. const float y_span = y_max - y_min;
  254. const bool invert_profile = true;
  255. constexpr float k_band_epsilon = 1e-6F;
  256. constexpr float k_radius_epsilon = 1e-8F;
  257. constexpr float k_shoulder_dome_t_end = 0.10F;
  258. constexpr float k_shoulder_dome_height = 0.06F;
  259. constexpr float k_shoulder_dome_min_radius_scale = 0.06F;
  260. constexpr float k_xforward_amp = 0.02F;
  261. constexpr float k_xforward_start = 0.6F;
  262. constexpr float k_xforward_end = 0.95F;
  263. constexpr float k_xbackward_amp = -0.01F;
  264. constexpr float k_xbackward_start = 0.0F;
  265. constexpr float k_xbackward_end = 0.2F;
  266. constexpr float k_lordosis_amp = -0.03F;
  267. constexpr float k_lordosis_start = 0.15F;
  268. constexpr float k_lordosis_end = 0.40F;
  269. constexpr float k_chest_forward_amp = 0.035F;
  270. constexpr float k_chest_forward_start = 0.65F;
  271. constexpr float k_chest_forward_end = 0.85F;
  272. constexpr float k_neck_back_amp = -0.015F;
  273. constexpr float k_neck_back_start = 0.90F;
  274. constexpr float k_neck_back_end = 1.0F;
  275. constexpr float k_twist_amplitude = 0.10F;
  276. constexpr float k_twist_start = 0.55F;
  277. constexpr float k_twist_end = 0.95F;
  278. constexpr float k_theta_sin_pos_amp = 0.07F;
  279. constexpr float k_theta_sin_pos_start = 0.68F;
  280. constexpr float k_theta_sin_pos_end = 0.88F;
  281. constexpr float k_theta_sin_neg_amp = -0.03F;
  282. constexpr float k_theta_sin_neg_start = 0.65F;
  283. constexpr float k_theta_sin_neg_end = 0.90F;
  284. constexpr float k_theta_cos_sq_amp = 0.06F;
  285. constexpr float k_theta_cos_sq_start = 0.55F;
  286. constexpr float k_theta_cos_sq_end = 0.75F;
  287. constexpr float k_theta_cos_sq_neg_amp = -0.02F;
  288. constexpr float k_theta_cos_sq_neg_start = 0.40F;
  289. constexpr float k_theta_cos_sq_neg_end = 0.55F;
  290. constexpr float k_theta_cos_amp = 0.015F;
  291. constexpr float k_theta_cos_start = 0.70F;
  292. constexpr float k_theta_cos_end = 0.95F;
  293. constexpr float k_micro_temporal_frequency = 37.0F;
  294. constexpr float k_micro_angular_frequency = 3.0F;
  295. constexpr float k_micro_phase_offset = 1.23F;
  296. constexpr float k_micro_center = 0.5F;
  297. constexpr float k_micro_jitter = 0.004F;
  298. auto clamp_f = [](float x, float a, float b) {
  299. return x < a ? a : (x > b ? b : x);
  300. };
  301. auto smoothstep01 = [&](float x) {
  302. x = clamp_f(x, 0.0F, 1.0F);
  303. return x * x * (3.0F - 2.0F * x);
  304. };
  305. auto smooth_band = [&](float t, float a, float b) {
  306. float const enter = smoothstep01((t - a) / (b - a + k_band_epsilon));
  307. float const exit = smoothstep01((t - b) / (a - b - k_band_epsilon));
  308. float const v = enter < exit ? enter : exit;
  309. return clamp_f(v, 0.0F, 1.0F);
  310. };
  311. struct Axes {
  312. float ax;
  313. float az;
  314. };
  315. struct Key {
  316. float t;
  317. Axes A;
  318. };
  319. const Key keys[] = {
  320. {0.00F, {0.72F, 0.65F}}, {0.08F, {0.88F, 0.82F}}, {0.15F, {1.02F, 0.95F}},
  321. {0.22F, {0.98F, 0.92F}}, {0.45F, {0.76F, 0.70F}}, {0.65F, {1.12F, 1.06F}},
  322. {0.85F, {1.30F, 1.25F}}, {1.02F, {1.48F, 1.20F}}, {1.10F, {1.12F, 0.92F}},
  323. };
  324. constexpr int key_count = sizeof(keys) / sizeof(keys[0]);
  325. constexpr float k_shoulder_bulge_amp = 0.08F;
  326. constexpr float k_shoulder_bulge_start = 0.10F;
  327. constexpr float k_shoulder_bulge_end = 0.22F;
  328. constexpr float k_trap_slope_amp = 0.04F;
  329. constexpr float k_trap_slope_start = 0.00F;
  330. constexpr float k_trap_slope_end = 0.12F;
  331. auto cat_rom = [](float p0, float p1, float p2, float p3, float u) {
  332. return 0.5F * ((2.0F * p1) + (-p0 + p2) * u +
  333. (2.0F * p0 - 5.0F * p1 + 4.0F * p2 - p3) * u * u +
  334. (-p0 + 3.0F * p1 - 3.0F * p2 + p3) * u * u * u);
  335. };
  336. auto sample_profile_axes = [&](float profile_t) -> Axes {
  337. profile_t = clamp_f(profile_t, 0.0F, 1.0F);
  338. int i = 0;
  339. while (i + 1 < key_count && profile_t > keys[i + 1].t) {
  340. ++i;
  341. }
  342. int const i0 = i > 0 ? i - 1 : 0;
  343. int const i1 = i;
  344. int const i2 = (i + 1 < key_count) ? i + 1 : key_count - 1;
  345. int const i3 = (i + 2 < key_count) ? i + 2 : key_count - 1;
  346. float const denom = (keys[i2].t - keys[i1].t);
  347. float u = denom > k_band_epsilon ? (profile_t - keys[i1].t) / denom : 0.0F;
  348. u = clamp_f(u, 0.0F, 1.0F);
  349. float const ax =
  350. cat_rom(keys[i0].A.ax, keys[i1].A.ax, keys[i2].A.ax, keys[i3].A.ax, u);
  351. float const az =
  352. cat_rom(keys[i0].A.az, keys[i1].A.az, keys[i2].A.az, keys[i3].A.az, u);
  353. return {ax, az};
  354. };
  355. auto ellipse_radius = [&](float a, float b, float ang) {
  356. float const c = std::cos(ang);
  357. float const s = std::sin(ang);
  358. float const denom = std::sqrt((b * b * c * c) + (a * a * s * s));
  359. return (a * b) / (denom + k_radius_epsilon);
  360. };
  361. auto x_offset_at = [&](float profile_t) {
  362. float const forward =
  363. k_xforward_amp *
  364. smooth_band(profile_t, k_xforward_start, k_xforward_end);
  365. float const backward =
  366. k_xbackward_amp *
  367. smooth_band(profile_t, k_xbackward_start, k_xbackward_end);
  368. return forward + backward;
  369. };
  370. auto z_offset_at = [&](float profile_t) {
  371. float const lordosis =
  372. k_lordosis_amp *
  373. smooth_band(profile_t, k_lordosis_start, k_lordosis_end);
  374. float const chest_fwd =
  375. k_chest_forward_amp *
  376. smooth_band(profile_t, k_chest_forward_start, k_chest_forward_end);
  377. float const neck_back =
  378. k_neck_back_amp *
  379. smooth_band(profile_t, k_neck_back_start, k_neck_back_end);
  380. return lordosis + chest_fwd + neck_back;
  381. };
  382. auto twist_at = [&](float profile_t) {
  383. return k_twist_amplitude *
  384. smooth_band(profile_t, k_twist_start, k_twist_end);
  385. };
  386. auto theta_scale = [&](float profile_t, float ang) {
  387. float s = 0.0F;
  388. float const sin_a = std::sin(ang);
  389. float const cos_a = std::cos(ang);
  390. float const cos2 = cos_a * cos_a;
  391. s += k_theta_sin_pos_amp *
  392. smooth_band(profile_t, k_theta_sin_pos_start, k_theta_sin_pos_end) *
  393. std::max(0.0F, sin_a);
  394. s += k_theta_sin_neg_amp *
  395. smooth_band(profile_t, k_theta_sin_neg_start, k_theta_sin_neg_end) *
  396. std::max(0.0F, -sin_a);
  397. s += k_theta_cos_sq_amp *
  398. smooth_band(profile_t, k_theta_cos_sq_start, k_theta_cos_sq_end) *
  399. cos2;
  400. s += k_theta_cos_sq_neg_amp *
  401. smooth_band(profile_t, k_theta_cos_sq_neg_start,
  402. k_theta_cos_sq_neg_end) *
  403. cos2;
  404. s += k_theta_cos_amp *
  405. smooth_band(profile_t, k_theta_cos_start, k_theta_cos_end) * cos_a;
  406. float const shoulder_band =
  407. smooth_band(profile_t, k_shoulder_bulge_start, k_shoulder_bulge_end);
  408. float const lateral_factor = std::abs(sin_a);
  409. s += k_shoulder_bulge_amp * shoulder_band * lateral_factor;
  410. float const trap_band =
  411. smooth_band(profile_t, k_trap_slope_start, k_trap_slope_end);
  412. float const trap_factor = (1.0F - std::abs(sin_a)) * 0.7F + 0.3F;
  413. s += k_trap_slope_amp * trap_band * trap_factor;
  414. return 1.0F + s;
  415. };
  416. auto micro = [&](float s) {
  417. float const f = std::sin(s * k_micro_noise_frequency) * k_micro_noise_scale;
  418. return f - std::floor(f);
  419. };
  420. auto sample_pos = [&](float t, float ang) -> QVector3D {
  421. float const profile_t = invert_profile ? (1.0F - t) : t;
  422. Axes const a = sample_profile_axes(profile_t);
  423. float const twist = twist_at(profile_t);
  424. float const th = ang + twist;
  425. float const radius = ellipse_radius(a.ax, a.az, th);
  426. float const s = theta_scale(profile_t, th);
  427. float r = radius * s;
  428. float py = y_min + t * y_span;
  429. if (t < k_shoulder_dome_t_end) {
  430. float const u = clamp_f(t / k_shoulder_dome_t_end, 0.0F, 1.0F);
  431. float const us = smoothstep01(u);
  432. float const sphere = std::sqrt(std::max(0.0F, (2.0F * us) - (us * us)));
  433. float const dome_radius_scale =
  434. std::max(sphere, k_shoulder_dome_min_radius_scale);
  435. r *= dome_radius_scale;
  436. py -= k_shoulder_dome_height * (1.0F - us);
  437. }
  438. float px = r * std::cos(th);
  439. float pz = r * std::sin(th);
  440. px += x_offset_at(profile_t);
  441. pz += z_offset_at(profile_t);
  442. float const s_value =
  443. (t * k_micro_temporal_frequency) + (ang * k_micro_angular_frequency);
  444. px += (micro(s_value) - k_micro_center) * k_micro_jitter;
  445. pz += (micro(s_value + k_micro_phase_offset) - k_micro_center) *
  446. k_micro_jitter;
  447. return {px, py, pz};
  448. };
  449. std::vector<Vertex> v;
  450. std::vector<unsigned int> idx;
  451. v.reserve((radial_segments + 1) * (height_segments + 1) +
  452. (radial_segments + 1) * 2 + 2);
  453. idx.reserve(radial_segments * height_segments * k_indices_per_quad +
  454. radial_segments * k_indices_per_quad);
  455. for (int y = 0; y <= height_segments; ++y) {
  456. float const t = float(y) / float(height_segments);
  457. float const dt = 1.0F / float(height_segments);
  458. float v_coord = t;
  459. for (int i = 0; i <= radial_segments; ++i) {
  460. float u = float(i) / float(radial_segments);
  461. float const ang = u * k_two_pi;
  462. float const da = k_two_pi / float(radial_segments);
  463. QVector3D const p = sample_pos(t, ang);
  464. QVector3D const pu = sample_pos(t, ang + da);
  465. QVector3D const pv = sample_pos(clamp_f(t + dt, 0.0F, 1.0F), ang);
  466. QVector3D const du = pu - p;
  467. QVector3D const dv = pv - p;
  468. QVector3D n = QVector3D::crossProduct(du, dv);
  469. if (n.lengthSquared() > 0.0F) {
  470. n.normalize();
  471. }
  472. v.push_back({{p.x(), p.y(), p.z()}, {n.x(), n.y(), n.z()}, {u, v_coord}});
  473. }
  474. }
  475. int const row = radial_segments + 1;
  476. for (int y = 0; y < height_segments; ++y) {
  477. for (int i = 0; i < radial_segments; ++i) {
  478. int a = y * row + i;
  479. int b = y * row + i + 1;
  480. int c = (y + 1) * row + i + 1;
  481. int d = (y + 1) * row + i;
  482. idx.push_back(a);
  483. idx.push_back(b);
  484. idx.push_back(c);
  485. idx.push_back(c);
  486. idx.push_back(d);
  487. idx.push_back(a);
  488. }
  489. }
  490. {
  491. int base_top = (int)v.size();
  492. float const t_top = 1.0F;
  493. float const t_top_s = invert_profile ? (1.0F - t_top) : t_top;
  494. QVector3D const c_top(x_offset_at(t_top_s), y_max, z_offset_at(t_top_s));
  495. v.push_back({{c_top.x(), c_top.y(), c_top.z()},
  496. {0, 1, 0},
  497. {k_uv_center, k_uv_center}});
  498. for (int i = 0; i <= radial_segments; ++i) {
  499. float const u = float(i) / float(radial_segments);
  500. float const ang = u * k_two_pi;
  501. QVector3D const p = sample_pos(t_top, ang);
  502. v.push_back({{p.x(), p.y(), p.z()},
  503. {0, 1, 0},
  504. {k_uv_center + k_uv_scale * std::cos(ang),
  505. k_uv_center + k_uv_scale * std::sin(ang)}});
  506. }
  507. for (int i = 1; i <= radial_segments; ++i) {
  508. idx.push_back(base_top);
  509. idx.push_back(base_top + i);
  510. idx.push_back(base_top + i + 1);
  511. }
  512. }
  513. {
  514. float const t_apex = 0.0F;
  515. float const ts_apex = invert_profile ? (1.0F - t_apex) : t_apex;
  516. float const apex_y = y_min - k_shoulder_dome_height;
  517. int const apex_idx = (int)v.size();
  518. QVector3D const apex(x_offset_at(ts_apex), apex_y, z_offset_at(ts_apex));
  519. v.push_back({{apex.x(), apex.y(), apex.z()},
  520. {0, -1, 0},
  521. {k_uv_center, k_uv_center}});
  522. for (int i = 0; i < radial_segments; ++i) {
  523. idx.push_back(apex_idx);
  524. idx.push_back(i + 1);
  525. idx.push_back(i);
  526. }
  527. }
  528. return std::make_unique<Mesh>(v, idx);
  529. }
  530. } // namespace
  531. auto get_unit_cylinder(int radial_segments) -> Mesh * {
  532. static std::unique_ptr<Mesh> const s_mesh(
  533. create_unit_cylinder_mesh(radial_segments));
  534. return s_mesh.get();
  535. }
  536. auto get_unit_cube() -> Mesh * {
  537. static std::unique_ptr<Mesh> const s_mesh(create_cube_mesh());
  538. return s_mesh.get();
  539. }
  540. auto get_unit_sphere(int lat_segments, int lon_segments) -> Mesh * {
  541. static std::unique_ptr<Mesh> const s_mesh(
  542. create_unit_sphere_mesh(lat_segments, lon_segments));
  543. return s_mesh.get();
  544. }
  545. auto get_unit_cone(int radial_segments) -> Mesh * {
  546. static std::unique_ptr<Mesh> const s_mesh(
  547. create_unit_cone_mesh(radial_segments));
  548. return s_mesh.get();
  549. }
  550. auto get_unit_capsule(int radial_segments, int height_segments) -> Mesh * {
  551. static std::unique_ptr<Mesh> const s_mesh(
  552. create_capsule_mesh(radial_segments, height_segments));
  553. return s_mesh.get();
  554. }
  555. auto get_unit_torso(int radial_segments, int height_segments) -> Mesh * {
  556. static std::unique_ptr<Mesh> const s_mesh(
  557. create_unit_torso_mesh(radial_segments, height_segments));
  558. return s_mesh.get();
  559. }
  560. } // namespace Render::GL