primitives.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 createUnitCylinderMesh(int radialSegments) -> 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 <= radialSegments; ++i) {
  30. float u = float(i) / float(radialSegments);
  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 = radialSegments + 1;
  40. for (int i = 0; i < radialSegments; ++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 <= radialSegments; ++i) {
  56. float const u = float(i) / float(radialSegments);
  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 <= radialSegments; ++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 <= radialSegments; ++i) {
  74. float const u = float(i) / float(radialSegments);
  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 <= radialSegments; ++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 new Mesh(v, idx);
  89. }
  90. auto createUnitSphereMesh(int latSegments, int lonSegments) -> Mesh * {
  91. const float r = k_unit_radius;
  92. std::vector<Vertex> v;
  93. std::vector<unsigned int> idx;
  94. for (int y = 0; y <= latSegments; ++y) {
  95. float vy = float(y) / float(latSegments);
  96. float const phi = vy * k_pi;
  97. float py = r * std::cos(phi);
  98. float const pr = r * std::sin(phi);
  99. for (int x = 0; x <= lonSegments; ++x) {
  100. float vx = float(x) / float(lonSegments);
  101. float const theta = vx * k_two_pi;
  102. float px = pr * std::cos(theta);
  103. float pz = pr * std::sin(theta);
  104. QVector3D n(px, py, pz);
  105. n.normalize();
  106. v.push_back({{px, py, pz}, {n.x(), n.y(), n.z()}, {vx, vy}});
  107. }
  108. }
  109. int const row = lonSegments + 1;
  110. for (int y = 0; y < latSegments; ++y) {
  111. for (int x = 0; x < lonSegments; ++x) {
  112. int a = y * row + x;
  113. int b = a + 1;
  114. int c = (y + 1) * row + x + 1;
  115. int d = (y + 1) * row + x;
  116. idx.push_back(a);
  117. idx.push_back(b);
  118. idx.push_back(c);
  119. idx.push_back(c);
  120. idx.push_back(d);
  121. idx.push_back(a);
  122. }
  123. }
  124. return new Mesh(v, idx);
  125. }
  126. auto createUnitConeMesh(int radialSegments) -> Mesh * {
  127. const float base_r = k_unit_radius;
  128. const float half_h = k_half_scalar;
  129. std::vector<Vertex> v;
  130. std::vector<unsigned int> idx;
  131. int apex_idx = 0;
  132. v.push_back({{0.0F, +half_h, 0.0F}, {0.0F, 1.0F, 0.0F}, {k_uv_center, 1.0F}});
  133. for (int i = 0; i <= radialSegments; ++i) {
  134. float u = float(i) / float(radialSegments);
  135. float const ang = u * k_two_pi;
  136. float px = base_r * std::cos(ang);
  137. float pz = base_r * std::sin(ang);
  138. QVector3D n(px, base_r, pz);
  139. n.normalize();
  140. v.push_back({{px, -half_h, pz}, {n.x(), n.y(), n.z()}, {u, 0.0F}});
  141. }
  142. for (int i = 1; i <= radialSegments; ++i) {
  143. idx.push_back(apex_idx);
  144. idx.push_back(i);
  145. idx.push_back(i + 1);
  146. }
  147. int base_center = (int)v.size();
  148. v.push_back(
  149. {{0.0F, -half_h, 0.0F}, {0.0F, -1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  150. int const base_start = (int)v.size();
  151. for (int i = 0; i <= radialSegments; ++i) {
  152. float const u = float(i) / float(radialSegments);
  153. float const ang = u * k_two_pi;
  154. float px = base_r * std::cos(ang);
  155. float pz = base_r * std::sin(ang);
  156. v.push_back({{px, -half_h, pz},
  157. {0.0F, -1.0F, 0.0F},
  158. {k_uv_center + k_uv_scale * std::cos(ang),
  159. k_uv_center + k_uv_scale * std::sin(ang)}});
  160. }
  161. for (int i = 0; i < radialSegments; ++i) {
  162. idx.push_back(base_center);
  163. idx.push_back(base_start + i + 1);
  164. idx.push_back(base_start + i);
  165. }
  166. return new Mesh(v, idx);
  167. }
  168. auto createCapsuleMesh(int radialSegments, int heightSegments) -> Mesh * {
  169. constexpr float k_capsule_radius = 0.25F;
  170. const float radius = k_capsule_radius;
  171. const float half_h = k_half_scalar;
  172. std::vector<Vertex> verts;
  173. std::vector<unsigned int> idx;
  174. for (int y = 0; y <= heightSegments; ++y) {
  175. float v = float(y) / float(heightSegments);
  176. float py = -half_h + v * (2.0F * half_h);
  177. for (int i = 0; i <= radialSegments; ++i) {
  178. float u = float(i) / float(radialSegments);
  179. float const ang = u * k_two_pi;
  180. float px = radius * std::cos(ang);
  181. float pz = radius * std::sin(ang);
  182. QVector3D n(px, 0.0F, pz);
  183. n.normalize();
  184. verts.push_back({{px, py, pz}, {n.x(), n.y(), n.z()}, {u, v}});
  185. }
  186. }
  187. int const row = radialSegments + 1;
  188. for (int y = 0; y < heightSegments; ++y) {
  189. for (int i = 0; i < radialSegments; ++i) {
  190. int a = y * row + i;
  191. int b = y * row + i + 1;
  192. int c = (y + 1) * row + i + 1;
  193. int d = (y + 1) * row + i;
  194. idx.push_back(a);
  195. idx.push_back(b);
  196. idx.push_back(c);
  197. idx.push_back(c);
  198. idx.push_back(d);
  199. idx.push_back(a);
  200. }
  201. }
  202. int base_top = (int)verts.size();
  203. verts.push_back(
  204. {{0.0F, half_h, 0.0F}, {0.0F, 1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  205. for (int i = 0; i <= radialSegments; ++i) {
  206. float const u = float(i) / float(radialSegments);
  207. float const ang = u * k_two_pi;
  208. float px = radius * std::cos(ang);
  209. float pz = radius * std::sin(ang);
  210. verts.push_back({{px, half_h, pz},
  211. {0.0F, 1.0F, 0.0F},
  212. {k_uv_center + k_uv_scale * std::cos(ang),
  213. k_uv_center + k_uv_scale * std::sin(ang)}});
  214. }
  215. for (int i = 1; i <= radialSegments; ++i) {
  216. idx.push_back(base_top);
  217. idx.push_back(base_top + i);
  218. idx.push_back(base_top + i + 1);
  219. }
  220. int base_bot = (int)verts.size();
  221. verts.push_back(
  222. {{0.0F, -half_h, 0.0F}, {0.0F, -1.0F, 0.0F}, {k_uv_center, k_uv_center}});
  223. for (int i = 0; i <= radialSegments; ++i) {
  224. float const u = float(i) / float(radialSegments);
  225. float const ang = u * k_two_pi;
  226. float px = radius * std::cos(ang);
  227. float pz = radius * std::sin(ang);
  228. verts.push_back({{px, -half_h, pz},
  229. {0.0F, -1.0F, 0.0F},
  230. {k_uv_center + k_uv_scale * std::cos(ang),
  231. k_uv_center + k_uv_scale * std::sin(ang)}});
  232. }
  233. for (int i = 1; i <= radialSegments; ++i) {
  234. idx.push_back(base_bot);
  235. idx.push_back(base_bot + i + 1);
  236. idx.push_back(base_bot + i);
  237. }
  238. return new Mesh(verts, idx);
  239. }
  240. auto simpleHash(float seed) -> float {
  241. float const x =
  242. std::sin(seed * k_micro_noise_frequency) * k_micro_noise_scale;
  243. return x - std::floor(x);
  244. }
  245. auto createUnitTorsoMesh(int radialSegments, int heightSegments) -> Mesh * {
  246. const float half_h = k_half_scalar;
  247. const bool invert_profile = true;
  248. constexpr float k_band_epsilon = 1e-6F;
  249. constexpr float k_radius_epsilon = 1e-8F;
  250. constexpr float k_xforward_amp = 0.02F;
  251. constexpr float k_xforward_start = 0.6F;
  252. constexpr float k_xforward_end = 0.95F;
  253. constexpr float k_xbackward_amp = -0.01F;
  254. constexpr float k_xbackward_start = 0.0F;
  255. constexpr float k_xbackward_end = 0.2F;
  256. constexpr float k_lordosis_amp = -0.03F;
  257. constexpr float k_lordosis_start = 0.15F;
  258. constexpr float k_lordosis_end = 0.40F;
  259. constexpr float k_chest_forward_amp = 0.035F;
  260. constexpr float k_chest_forward_start = 0.65F;
  261. constexpr float k_chest_forward_end = 0.85F;
  262. constexpr float k_neck_back_amp = -0.015F;
  263. constexpr float k_neck_back_start = 0.90F;
  264. constexpr float k_neck_back_end = 1.0F;
  265. constexpr float k_twist_amplitude = 0.10F;
  266. constexpr float k_twist_start = 0.55F;
  267. constexpr float k_twist_end = 0.95F;
  268. constexpr float k_theta_sin_pos_amp = 0.07F;
  269. constexpr float k_theta_sin_pos_start = 0.68F;
  270. constexpr float k_theta_sin_pos_end = 0.88F;
  271. constexpr float k_theta_sin_neg_amp = -0.03F;
  272. constexpr float k_theta_sin_neg_start = 0.65F;
  273. constexpr float k_theta_sin_neg_end = 0.90F;
  274. constexpr float k_theta_cos_sq_amp = 0.06F;
  275. constexpr float k_theta_cos_sq_start = 0.55F;
  276. constexpr float k_theta_cos_sq_end = 0.75F;
  277. constexpr float k_theta_cos_sq_neg_amp = -0.02F;
  278. constexpr float k_theta_cos_sq_neg_start = 0.40F;
  279. constexpr float k_theta_cos_sq_neg_end = 0.55F;
  280. constexpr float k_theta_cos_amp = 0.015F;
  281. constexpr float k_theta_cos_start = 0.70F;
  282. constexpr float k_theta_cos_end = 0.95F;
  283. constexpr float k_micro_temporal_frequency = 37.0F;
  284. constexpr float k_micro_angular_frequency = 3.0F;
  285. constexpr float k_micro_phase_offset = 1.23F;
  286. constexpr float k_micro_center = 0.5F;
  287. constexpr float k_micro_jitter = 0.004F;
  288. auto clampf = [](float x, float a, float b) {
  289. return x < a ? a : (x > b ? b : x);
  290. };
  291. auto smoothstep01 = [&](float x) {
  292. x = clampf(x, 0.0F, 1.0F);
  293. return x * x * (3.0F - 2.0F * x);
  294. };
  295. auto smooth_band = [&](float t, float a, float b) {
  296. float const enter = smoothstep01((t - a) / (b - a + k_band_epsilon));
  297. float const exit = smoothstep01((t - b) / (a - b - k_band_epsilon));
  298. float const v = enter < exit ? enter : exit;
  299. return clampf(v, 0.0F, 1.0F);
  300. };
  301. struct Axes {
  302. float ax;
  303. float az;
  304. };
  305. struct Key {
  306. float t;
  307. Axes A;
  308. };
  309. const Key keys[] = {
  310. {0.10F, {0.98F, 0.92F}}, {0.20F, {1.02F, 0.96F}}, {0.45F, {0.82F, 0.78F}},
  311. {0.65F, {1.20F, 1.04F}}, {0.85F, {1.42F, 1.18F}}, {1.02F, {1.60F, 1.06F}},
  312. {1.10F, {1.20F, 0.96F}},
  313. };
  314. constexpr int key_count = sizeof(keys) / sizeof(keys[0]);
  315. auto cat_rom = [](float p0, float p1, float p2, float p3, float u) {
  316. return 0.5F * ((2.0F * p1) + (-p0 + p2) * u +
  317. (2.0F * p0 - 5.0F * p1 + 4.0F * p2 - p3) * u * u +
  318. (-p0 + 3.0F * p1 - 3.0F * p2 + p3) * u * u * u);
  319. };
  320. auto sample_axes = [&](float t) -> Axes {
  321. t = clampf(t, 0.0F, 1.0F);
  322. int i = 0;
  323. while (i + 1 < key_count && t > keys[i + 1].t) {
  324. ++i;
  325. }
  326. int const i0 = i > 0 ? i - 1 : 0;
  327. int const i1 = i;
  328. int const i2 = (i + 1 < key_count) ? i + 1 : key_count - 1;
  329. int const i3 = (i + 2 < key_count) ? i + 2 : key_count - 1;
  330. float const denom = (keys[i2].t - keys[i1].t);
  331. float u = denom > k_band_epsilon ? (t - keys[i1].t) / denom : 0.0F;
  332. u = clampf(u, 0.0F, 1.0F);
  333. float const ax =
  334. cat_rom(keys[i0].A.ax, keys[i1].A.ax, keys[i2].A.ax, keys[i3].A.ax, u);
  335. float const az =
  336. cat_rom(keys[i0].A.az, keys[i1].A.az, keys[i2].A.az, keys[i3].A.az, u);
  337. return {ax, az};
  338. };
  339. auto ellipse_radius = [&](float a, float b, float ang) {
  340. float const c = std::cos(ang);
  341. float const s = std::sin(ang);
  342. float const denom = std::sqrt((b * b * c * c) + (a * a * s * s));
  343. return (a * b) / (denom + k_radius_epsilon);
  344. };
  345. auto x_offset_at = [&](float t) {
  346. float const forward =
  347. k_xforward_amp * smooth_band(t, k_xforward_start, k_xforward_end);
  348. float const backward =
  349. k_xbackward_amp * smooth_band(t, k_xbackward_start, k_xbackward_end);
  350. return forward + backward;
  351. };
  352. auto z_offset_at = [&](float t) {
  353. float const lordosis =
  354. k_lordosis_amp * smooth_band(t, k_lordosis_start, k_lordosis_end);
  355. float const chest_fwd =
  356. k_chest_forward_amp *
  357. smooth_band(t, k_chest_forward_start, k_chest_forward_end);
  358. float const neck_back =
  359. k_neck_back_amp * smooth_band(t, k_neck_back_start, k_neck_back_end);
  360. return lordosis + chest_fwd + neck_back;
  361. };
  362. auto twist_at = [&](float t) {
  363. return k_twist_amplitude * smooth_band(t, k_twist_start, k_twist_end);
  364. };
  365. auto theta_scale = [&](float t, float ang) {
  366. float s = 0.0F;
  367. float const sin_a = std::sin(ang);
  368. float const cos_a = std::cos(ang);
  369. float const cos2 = cos_a * cos_a;
  370. s += k_theta_sin_pos_amp *
  371. smooth_band(t, k_theta_sin_pos_start, k_theta_sin_pos_end) *
  372. std::max(0.0F, sin_a);
  373. s += k_theta_sin_neg_amp *
  374. smooth_band(t, k_theta_sin_neg_start, k_theta_sin_neg_end) *
  375. std::max(0.0F, -sin_a);
  376. s += k_theta_cos_sq_amp *
  377. smooth_band(t, k_theta_cos_sq_start, k_theta_cos_sq_end) * cos2;
  378. s += k_theta_cos_sq_neg_amp *
  379. smooth_band(t, k_theta_cos_sq_neg_start, k_theta_cos_sq_neg_end) *
  380. cos2;
  381. s += k_theta_cos_amp * smooth_band(t, k_theta_cos_start, k_theta_cos_end) *
  382. cos_a;
  383. return 1.0F + s;
  384. };
  385. auto micro = [&](float s) {
  386. float const f = std::sin(s * k_micro_noise_frequency) * k_micro_noise_scale;
  387. return f - std::floor(f);
  388. };
  389. auto sample_pos = [&](float t, float ang) -> QVector3D {
  390. float const ts = invert_profile ? (1.0F - t) : t;
  391. Axes const a = sample_axes(ts);
  392. float const twist = twist_at(ts);
  393. float const th = ang + twist;
  394. float const radius = ellipse_radius(a.ax, a.az, th);
  395. float const s = theta_scale(ts, th);
  396. float const r = radius * s;
  397. float px = r * std::cos(th);
  398. float pz = r * std::sin(th);
  399. px += x_offset_at(ts);
  400. pz += z_offset_at(ts);
  401. float const py = -half_h + t * (2.0F * half_h);
  402. float const s_value =
  403. (t * k_micro_temporal_frequency) + (ang * k_micro_angular_frequency);
  404. px += (micro(s_value) - k_micro_center) * k_micro_jitter;
  405. pz += (micro(s_value + k_micro_phase_offset) - k_micro_center) *
  406. k_micro_jitter;
  407. return {px, py, pz};
  408. };
  409. std::vector<Vertex> v;
  410. std::vector<unsigned int> idx;
  411. v.reserve((radialSegments + 1) * (heightSegments + 1) +
  412. (radialSegments + 1) * 2 + 2);
  413. idx.reserve(radialSegments * heightSegments * k_indices_per_quad +
  414. radialSegments * k_indices_per_quad);
  415. for (int y = 0; y <= heightSegments; ++y) {
  416. float const t = float(y) / float(heightSegments);
  417. float const dt = 1.0F / float(heightSegments);
  418. float v_coord = t;
  419. for (int i = 0; i <= radialSegments; ++i) {
  420. float u = float(i) / float(radialSegments);
  421. float const ang = u * k_two_pi;
  422. float const da = k_two_pi / float(radialSegments);
  423. QVector3D const p = sample_pos(t, ang);
  424. QVector3D const pu = sample_pos(t, ang + da);
  425. QVector3D const pv = sample_pos(clampf(t + dt, 0.0F, 1.0F), ang);
  426. QVector3D const du = pu - p;
  427. QVector3D const dv = pv - p;
  428. QVector3D n = QVector3D::crossProduct(du, dv);
  429. if (n.lengthSquared() > 0.0F) {
  430. n.normalize();
  431. }
  432. v.push_back({{p.x(), p.y(), p.z()}, {n.x(), n.y(), n.z()}, {u, v_coord}});
  433. }
  434. }
  435. int const row = radialSegments + 1;
  436. for (int y = 0; y < heightSegments; ++y) {
  437. for (int i = 0; i < radialSegments; ++i) {
  438. int a = y * row + i;
  439. int b = y * row + i + 1;
  440. int c = (y + 1) * row + i + 1;
  441. int d = (y + 1) * row + i;
  442. idx.push_back(a);
  443. idx.push_back(b);
  444. idx.push_back(c);
  445. idx.push_back(c);
  446. idx.push_back(d);
  447. idx.push_back(a);
  448. }
  449. }
  450. {
  451. int base_top = (int)v.size();
  452. float const t_top = 1.0F;
  453. float const t_top_s = invert_profile ? (1.0F - t_top) : t_top;
  454. QVector3D const c_top(x_offset_at(t_top_s), half_h, z_offset_at(t_top_s));
  455. v.push_back({{c_top.x(), c_top.y(), c_top.z()},
  456. {0, 1, 0},
  457. {k_uv_center, k_uv_center}});
  458. for (int i = 0; i <= radialSegments; ++i) {
  459. float const u = float(i) / float(radialSegments);
  460. float const ang = u * k_two_pi;
  461. QVector3D const p = sample_pos(t_top, ang);
  462. v.push_back({{p.x(), p.y(), p.z()},
  463. {0, 1, 0},
  464. {k_uv_center + k_uv_scale * std::cos(ang),
  465. k_uv_center + k_uv_scale * std::sin(ang)}});
  466. }
  467. for (int i = 1; i <= radialSegments; ++i) {
  468. idx.push_back(base_top);
  469. idx.push_back(base_top + i);
  470. idx.push_back(base_top + i + 1);
  471. }
  472. }
  473. {
  474. int base_bot = (int)v.size();
  475. float const t_bot = 0.0F;
  476. float const t_bot_s = invert_profile ? (1.0F - t_bot) : t_bot;
  477. QVector3D const c_bot(x_offset_at(t_bot_s), -half_h, z_offset_at(t_bot_s));
  478. v.push_back({{c_bot.x(), c_bot.y(), c_bot.z()},
  479. {0, -1, 0},
  480. {k_uv_center, k_uv_center}});
  481. for (int i = 0; i <= radialSegments; ++i) {
  482. float const u = float(i) / float(radialSegments);
  483. float const ang = u * k_two_pi;
  484. QVector3D const p = sample_pos(t_bot, ang);
  485. v.push_back({{p.x(), p.y(), p.z()},
  486. {0, -1, 0},
  487. {k_uv_center + k_uv_scale * std::cos(ang),
  488. k_uv_center + k_uv_scale * std::sin(ang)}});
  489. }
  490. for (int i = 1; i <= radialSegments; ++i) {
  491. idx.push_back(base_bot);
  492. idx.push_back(base_bot + i + 1);
  493. idx.push_back(base_bot + i);
  494. }
  495. }
  496. return new Mesh(v, idx);
  497. }
  498. } // namespace
  499. auto getUnitCylinder(int radialSegments) -> Mesh * {
  500. static std::unique_ptr<Mesh> const s_mesh(
  501. createUnitCylinderMesh(radialSegments));
  502. return s_mesh.get();
  503. }
  504. auto getUnitSphere(int latSegments, int lonSegments) -> Mesh * {
  505. static std::unique_ptr<Mesh> const s_mesh(
  506. createUnitSphereMesh(latSegments, lonSegments));
  507. return s_mesh.get();
  508. }
  509. auto getUnitCone(int radialSegments) -> Mesh * {
  510. static std::unique_ptr<Mesh> const s_mesh(createUnitConeMesh(radialSegments));
  511. return s_mesh.get();
  512. }
  513. auto getUnitCapsule(int radialSegments, int heightSegments) -> Mesh * {
  514. static std::unique_ptr<Mesh> const s_mesh(
  515. createCapsuleMesh(radialSegments, heightSegments));
  516. return s_mesh.get();
  517. }
  518. auto getUnitTorso(int radialSegments, int heightSegments) -> Mesh * {
  519. static std::unique_ptr<Mesh> const s_mesh(
  520. createUnitTorsoMesh(radialSegments, heightSegments));
  521. return s_mesh.get();
  522. }
  523. } // namespace Render::GL