bounds.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bx/rng.h>
  6. #include <bx/math.h>
  7. #include "bounds.h"
  8. void aabbToObb(Obb& _obb, const Aabb& _aabb)
  9. {
  10. bx::memSet(_obb.m_mtx, 0, sizeof(_obb.m_mtx) );
  11. _obb.m_mtx[ 0] = (_aabb.m_max.x - _aabb.m_min.x) * 0.5f;
  12. _obb.m_mtx[ 5] = (_aabb.m_max.y - _aabb.m_min.y) * 0.5f;
  13. _obb.m_mtx[10] = (_aabb.m_max.z - _aabb.m_min.z) * 0.5f;
  14. _obb.m_mtx[12] = (_aabb.m_min.x + _aabb.m_max.x) * 0.5f;
  15. _obb.m_mtx[13] = (_aabb.m_min.y + _aabb.m_max.y) * 0.5f;
  16. _obb.m_mtx[14] = (_aabb.m_min.z + _aabb.m_max.z) * 0.5f;
  17. _obb.m_mtx[15] = 1.0f;
  18. }
  19. void toAabb(Aabb& _aabb, const Obb& _obb)
  20. {
  21. bx::Vec3 xyz = { 1.0f, 1.0f, 1.0f };
  22. bx::Vec3 tmp = bx::mul(xyz, _obb.m_mtx);
  23. _aabb.m_min = tmp;
  24. _aabb.m_max = tmp;
  25. for (uint32_t ii = 1; ii < 8; ++ii)
  26. {
  27. xyz.x = ii & 1 ? -1.0f : 1.0f;
  28. xyz.y = ii & 2 ? -1.0f : 1.0f;
  29. xyz.z = ii & 4 ? -1.0f : 1.0f;
  30. tmp = bx::mul(xyz, _obb.m_mtx);
  31. _aabb.m_min = bx::min(_aabb.m_min, tmp);
  32. _aabb.m_max = bx::max(_aabb.m_max, tmp);
  33. }
  34. }
  35. void toAabb(Aabb& _aabb, const Sphere& _sphere)
  36. {
  37. const float radius = _sphere.m_radius;
  38. _aabb.m_min = bx::sub(_sphere.m_center, radius);
  39. _aabb.m_max = bx::add(_sphere.m_center, radius);
  40. }
  41. void toAabb(Aabb& _aabb, const Disk& _disk)
  42. {
  43. // Reference(s):
  44. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  45. //
  46. const bx::Vec3 nsq = bx::mul(_disk.m_normal, _disk.m_normal);
  47. const bx::Vec3 one = { 1.0f, 1.0f, 1.0f };
  48. const bx::Vec3 tmp = bx::sub(one, nsq);
  49. const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);
  50. const bx::Vec3 extent =
  51. {
  52. _disk.m_radius * tmp.x * bx::sqrt((nsq.x + nsq.y * nsq.z) * inv),
  53. _disk.m_radius * tmp.y * bx::sqrt((nsq.y + nsq.z * nsq.x) * inv),
  54. _disk.m_radius * tmp.z * bx::sqrt((nsq.z + nsq.x * nsq.y) * inv),
  55. };
  56. _aabb.m_min = bx::sub(_disk.m_center, extent);
  57. _aabb.m_max = bx::add(_disk.m_center, extent);
  58. }
  59. void toAabb(Aabb& _aabb, const Cylinder& _cylinder)
  60. {
  61. // Reference(s):
  62. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  63. //
  64. const bx::Vec3 axis = bx::sub(_cylinder.m_end, _cylinder.m_pos);
  65. const bx::Vec3 asq = bx::mul(axis, axis);
  66. const bx::Vec3 nsq = bx::mul(asq, 1.0f/bx::dot(axis, axis) );
  67. const bx::Vec3 one = { 1.0f, 1.0f, 1.0f };
  68. const bx::Vec3 tmp = bx::sub(one, nsq);
  69. const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);
  70. const bx::Vec3 extent =
  71. {
  72. _cylinder.m_radius * tmp.x * bx::sqrt( (nsq.x + nsq.y * nsq.z) * inv),
  73. _cylinder.m_radius * tmp.y * bx::sqrt( (nsq.y + nsq.z * nsq.x) * inv),
  74. _cylinder.m_radius * tmp.z * bx::sqrt( (nsq.z + nsq.x * nsq.y) * inv),
  75. };
  76. const bx::Vec3 minP = bx::sub(_cylinder.m_pos, extent);
  77. const bx::Vec3 minE = bx::sub(_cylinder.m_end, extent);
  78. const bx::Vec3 maxP = bx::add(_cylinder.m_pos, extent);
  79. const bx::Vec3 maxE = bx::add(_cylinder.m_end, extent);
  80. _aabb.m_min = bx::min(minP, minE);
  81. _aabb.m_max = bx::max(maxP, maxE);
  82. }
  83. void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx)
  84. {
  85. aabbToObb(_obb, _aabb);
  86. float result[16];
  87. bx::mtxMul(result, _obb.m_mtx, _mtx);
  88. bx::memCopy(_obb.m_mtx, result, sizeof(result) );
  89. }
  90. void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  91. {
  92. bx::Vec3 min, max;
  93. uint8_t* vertex = (uint8_t*)_vertices;
  94. float* position = (float*)vertex;
  95. min.x = max.x = position[0];
  96. min.y = max.y = position[1];
  97. min.z = max.z = position[2];
  98. vertex += _stride;
  99. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  100. {
  101. position = (float*)vertex;
  102. vertex += _stride;
  103. bx::Vec3 pos =
  104. {
  105. position[0],
  106. position[1],
  107. position[2],
  108. };
  109. min = bx::min(pos, min);
  110. max = bx::max(pos, max);
  111. }
  112. _aabb.m_min = min;
  113. _aabb.m_max = max;
  114. }
  115. void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  116. {
  117. bx::Vec3 min, max;
  118. uint8_t* vertex = (uint8_t*)_vertices;
  119. min = max = bx::mul(bx::load<bx::Vec3>(vertex), _mtx);
  120. vertex += _stride;
  121. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  122. {
  123. bx::Vec3 pos = bx::mul(bx::load<bx::Vec3>(vertex), _mtx);
  124. vertex += _stride;
  125. min = bx::min(pos, min);
  126. max = bx::max(pos, max);
  127. }
  128. _aabb.m_min = min;
  129. _aabb.m_max = max;
  130. }
  131. float calcAreaAabb(const Aabb& _aabb)
  132. {
  133. const float ww = _aabb.m_max.x - _aabb.m_min.x;
  134. const float hh = _aabb.m_max.y - _aabb.m_min.y;
  135. const float dd = _aabb.m_max.z - _aabb.m_min.z;
  136. return 2.0f * (ww*hh + ww*dd + hh*dd);
  137. }
  138. void aabbExpand(Aabb& _aabb, float _factor)
  139. {
  140. _aabb.m_min.x -= _factor;
  141. _aabb.m_min.y -= _factor;
  142. _aabb.m_min.z -= _factor;
  143. _aabb.m_max.x += _factor;
  144. _aabb.m_max.y += _factor;
  145. _aabb.m_max.z += _factor;
  146. }
  147. void aabbExpand(Aabb& _aabb, const float* _pos)
  148. {
  149. const bx::Vec3 pos = { _pos[0], _pos[1], _pos[2] };
  150. _aabb.m_min = bx::min(_aabb.m_min, pos);
  151. _aabb.m_max = bx::max(_aabb.m_max, pos);
  152. }
  153. uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1)
  154. {
  155. const uint32_t ltMinX = _aabb0.m_max.x < _aabb1.m_min.x;
  156. const uint32_t gtMaxX = _aabb0.m_min.x > _aabb1.m_max.x;
  157. const uint32_t ltMinY = _aabb0.m_max.y < _aabb1.m_min.y;
  158. const uint32_t gtMaxY = _aabb0.m_min.y > _aabb1.m_max.y;
  159. const uint32_t ltMinZ = _aabb0.m_max.z < _aabb1.m_min.z;
  160. const uint32_t gtMaxZ = _aabb0.m_min.z > _aabb1.m_max.z;
  161. return 0
  162. | (ltMinX<<0)
  163. | (gtMaxX<<1)
  164. | (ltMinY<<2)
  165. | (gtMaxY<<3)
  166. | (ltMinZ<<4)
  167. | (gtMaxZ<<5)
  168. ;
  169. }
  170. void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
  171. {
  172. Aabb aabb;
  173. toAabb(aabb, _vertices, _numVertices, _stride);
  174. float minArea = calcAreaAabb(aabb);
  175. Obb best;
  176. aabbToObb(best, aabb);
  177. float angleStep = float(bx::kPiHalf/_steps);
  178. float ax = 0.0f;
  179. float mtx[16];
  180. for (uint32_t ii = 0; ii < _steps; ++ii)
  181. {
  182. float ay = 0.0f;
  183. for (uint32_t jj = 0; jj < _steps; ++jj)
  184. {
  185. float az = 0.0f;
  186. for (uint32_t kk = 0; kk < _steps; ++kk)
  187. {
  188. bx::mtxRotateXYZ(mtx, ax, ay, az);
  189. float mtxT[16];
  190. bx::mtxTranspose(mtxT, mtx);
  191. toAabb(aabb, mtxT, _vertices, _numVertices, _stride);
  192. float area = calcAreaAabb(aabb);
  193. if (area < minArea)
  194. {
  195. minArea = area;
  196. aabbTransformToObb(best, aabb, mtx);
  197. }
  198. az += angleStep;
  199. }
  200. ay += angleStep;
  201. }
  202. ax += angleStep;
  203. }
  204. bx::memCopy(&_obb, &best, sizeof(Obb) );
  205. }
  206. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  207. {
  208. Aabb aabb;
  209. toAabb(aabb, _vertices, _numVertices, _stride);
  210. bx::Vec3 center =
  211. {
  212. (aabb.m_min.x + aabb.m_max.x) * 0.5f,
  213. (aabb.m_min.y + aabb.m_max.y) * 0.5f,
  214. (aabb.m_min.z + aabb.m_max.z) * 0.5f,
  215. };
  216. float maxDistSq = 0.0f;
  217. uint8_t* vertex = (uint8_t*)_vertices;
  218. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  219. {
  220. float* position = (float*)vertex;
  221. vertex += _stride;
  222. const float xx = position[0] - center.x;
  223. const float yy = position[1] - center.y;
  224. const float zz = position[2] - center.z;
  225. const float distSq = xx*xx + yy*yy + zz*zz;
  226. maxDistSq = bx::max(distSq, maxDistSq);
  227. }
  228. _sphere.m_center = center;
  229. _sphere.m_radius = bx::sqrt(maxDistSq);
  230. }
  231. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  232. {
  233. bx::RngMwc rng;
  234. uint8_t* vertex = (uint8_t*)_vertices;
  235. bx::Vec3 center;
  236. float* position = (float*)&vertex[0];
  237. center.x = position[0];
  238. center.y = position[1];
  239. center.z = position[2];
  240. position = (float*)&vertex[1*_stride];
  241. center.x += position[0];
  242. center.y += position[1];
  243. center.z += position[2];
  244. center.x *= 0.5f;
  245. center.y *= 0.5f;
  246. center.z *= 0.5f;
  247. float xx = position[0] - center.x;
  248. float yy = position[1] - center.y;
  249. float zz = position[2] - center.z;
  250. float maxDistSq = xx*xx + yy*yy + zz*zz;
  251. float radiusStep = _step * 0.37f;
  252. bool done;
  253. do
  254. {
  255. done = true;
  256. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  257. {
  258. position = (float*)&vertex[index*_stride];
  259. xx = position[0] - center.x;
  260. yy = position[1] - center.y;
  261. zz = position[2] - center.z;
  262. float distSq = xx*xx + yy*yy + zz*zz;
  263. if (distSq > maxDistSq)
  264. {
  265. done = false;
  266. center.x += xx * radiusStep;
  267. center.y += yy * radiusStep;
  268. center.z += zz * radiusStep;
  269. maxDistSq = bx::lerp(maxDistSq, distSq, _step);
  270. break;
  271. }
  272. }
  273. } while (!done);
  274. _sphere.m_center = center;
  275. _sphere.m_radius = bx::sqrt(maxDistSq);
  276. }
  277. void buildFrustumPlanes(bx::Plane* _result, const float* _viewProj)
  278. {
  279. const float xw = _viewProj[ 3];
  280. const float yw = _viewProj[ 7];
  281. const float zw = _viewProj[11];
  282. const float ww = _viewProj[15];
  283. const float xz = _viewProj[ 2];
  284. const float yz = _viewProj[ 6];
  285. const float zz = _viewProj[10];
  286. const float wz = _viewProj[14];
  287. bx::Plane& near = _result[0];
  288. bx::Plane& far = _result[1];
  289. bx::Plane& left = _result[2];
  290. bx::Plane& right = _result[3];
  291. bx::Plane& top = _result[4];
  292. bx::Plane& bottom = _result[5];
  293. near.normal.x = xw - xz;
  294. near.normal.y = yw - yz;
  295. near.normal.z = zw - zz;
  296. near.dist = ww - wz;
  297. far.normal.x = xw + xz;
  298. far.normal.y = yw + yz;
  299. far.normal.z = zw + zz;
  300. far.dist = ww + wz;
  301. const float xx = _viewProj[ 0];
  302. const float yx = _viewProj[ 4];
  303. const float zx = _viewProj[ 8];
  304. const float wx = _viewProj[12];
  305. left.normal.x = xw - xx;
  306. left.normal.y = yw - yx;
  307. left.normal.z = zw - zx;
  308. left.dist = ww - wx;
  309. right.normal.x = xw + xx;
  310. right.normal.y = yw + yx;
  311. right.normal.z = zw + zx;
  312. right.dist = ww + wx;
  313. const float xy = _viewProj[ 1];
  314. const float yy = _viewProj[ 5];
  315. const float zy = _viewProj[ 9];
  316. const float wy = _viewProj[13];
  317. top.normal.x = xw + xy;
  318. top.normal.y = yw + yy;
  319. top.normal.z = zw + zy;
  320. top.dist = ww + wy;
  321. bottom.normal.x = xw - xy;
  322. bottom.normal.y = yw - yy;
  323. bottom.normal.z = zw - zy;
  324. bottom.dist = ww - wy;
  325. bx::Plane* plane = _result;
  326. for (uint32_t ii = 0; ii < 6; ++ii)
  327. {
  328. const float len = bx::length(plane->normal);
  329. plane->normal = bx::normalize(plane->normal);
  330. float invLen = 1.0f / len;
  331. plane->dist *= invLen;
  332. ++plane;
  333. }
  334. }
  335. bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc)
  336. {
  337. const bx::Vec3 axb = bx::cross(_pa.normal, _pb.normal);
  338. const bx::Vec3 bxc = bx::cross(_pb.normal, _pc.normal);
  339. const bx::Vec3 cxa = bx::cross(_pc.normal, _pa.normal);
  340. const bx::Vec3 tmp0 = bx::mul(bxc, _pa.dist);
  341. const bx::Vec3 tmp1 = bx::mul(cxa, _pb.dist);
  342. const bx::Vec3 tmp2 = bx::mul(axb, _pc.dist);
  343. const bx::Vec3 tmp3 = bx::add(tmp0, tmp1);
  344. const bx::Vec3 tmp4 = bx::add(tmp3, tmp2);
  345. const float denom = bx::dot(_pa.normal, bxc);
  346. const bx::Vec3 result = bx::mul(tmp4, -1.0f/denom);
  347. return result;
  348. }
  349. Ray makeRay(float _x, float _y, const float* _invVp)
  350. {
  351. Ray ray;
  352. const bx::Vec3 near = { _x, _y, 0.0f };
  353. ray.m_pos = bx::mulH(near, _invVp);
  354. const bx::Vec3 far = { _x, _y, 1.0f };
  355. bx::Vec3 tmp = bx::mulH(far, _invVp);
  356. const bx::Vec3 dir = bx::sub(tmp, ray.m_pos);
  357. ray.m_dir = bx::normalize(dir);
  358. return ray;
  359. }
  360. inline bx::Vec3 getPointAt(const Ray& _ray, float _t)
  361. {
  362. return bx::add(bx::mul(_ray.m_dir, _t), _ray.m_pos);
  363. }
  364. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
  365. {
  366. const bx::Vec3 invDir = bx::rcp(_ray.m_dir);
  367. const bx::Vec3 tmp0 = bx::sub(_aabb.m_min, _ray.m_pos);
  368. const bx::Vec3 t0 = bx::mul(tmp0, invDir);
  369. const bx::Vec3 tmp1 = bx::sub(_aabb.m_max, _ray.m_pos);
  370. const bx::Vec3 t1 = bx::mul(tmp1, invDir);
  371. const bx::Vec3 min = bx::min(t0, t1);
  372. const bx::Vec3 max = bx::max(t0, t1);
  373. const float tmin = bx::max(min.x, min.y, min.z);
  374. const float tmax = bx::min(max.x, max.y, max.z);
  375. if (tmax < 0.0f
  376. || tmin > tmax)
  377. {
  378. return false;
  379. }
  380. if (NULL != _hit)
  381. {
  382. _hit->m_normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
  383. _hit->m_normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
  384. _hit->m_normal.z = float( (t1.z == tmin) - (t0.z == tmin) );
  385. _hit->m_dist = tmin;
  386. _hit->m_pos = getPointAt(_ray, tmin);
  387. }
  388. return true;
  389. }
  390. static const Aabb s_kUnitAabb =
  391. {
  392. { -1.0f, -1.0f, -1.0f },
  393. { 1.0f, 1.0f, 1.0f },
  394. };
  395. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
  396. {
  397. Aabb aabb;
  398. toAabb(aabb, _obb);
  399. if (!intersect(_ray, aabb) )
  400. {
  401. return false;
  402. }
  403. float mtxInv[16];
  404. bx::mtxInverse(mtxInv, _obb.m_mtx);
  405. Ray obbRay;
  406. obbRay.m_pos = bx::mul(_ray.m_pos, mtxInv);
  407. obbRay.m_dir = bx::mulXyz0(_ray.m_dir, mtxInv);
  408. if (intersect(obbRay, s_kUnitAabb, _hit) )
  409. {
  410. if (NULL != _hit)
  411. {
  412. _hit->m_pos = bx::mul(_hit->m_pos, _obb.m_mtx);
  413. const bx::Vec3 tmp = bx::mulXyz0(_hit->m_normal, _obb.m_mtx);
  414. _hit->m_normal = bx::normalize(tmp);
  415. }
  416. return true;
  417. }
  418. return false;
  419. }
  420. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
  421. {
  422. bx::Plane plane;
  423. plane.normal = _disk.m_normal;
  424. plane.dist = -bx::dot(_disk.m_center, _disk.m_normal);
  425. Hit tmpHit;
  426. _hit = NULL != _hit ? _hit : &tmpHit;
  427. if (intersect(_ray, plane, _hit) )
  428. {
  429. const bx::Vec3 tmp = bx::sub(_disk.m_center, _hit->m_pos);
  430. return bx::dot(tmp, tmp) <= bx::square(_disk.m_radius);
  431. }
  432. return false;
  433. }
  434. static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
  435. {
  436. bx::Vec3 axis = bx::sub(_cylinder.m_end, _cylinder.m_pos);
  437. const bx::Vec3 rc = bx::sub(_ray.m_pos, _cylinder.m_pos);
  438. const bx::Vec3 dxa = bx::cross(_ray.m_dir, axis);
  439. const float len = bx::length(dxa);
  440. const bx::Vec3 normal = bx::normalize(dxa);
  441. const float dist = bx::abs(bx::dot(rc, normal) );
  442. if (dist > _cylinder.m_radius)
  443. {
  444. return false;
  445. }
  446. bx::Vec3 vo = bx::cross(rc, axis);
  447. const float t0 = -bx::dot(vo, normal) / len;
  448. vo = bx::normalize(bx::cross(normal, axis) );
  449. const float rsq = bx::square(_cylinder.m_radius);
  450. const float ddoto = bx::dot(_ray.m_dir, vo);
  451. const float ss = t0 - bx::abs(bx::sqrt(rsq - bx::square(dist) ) / ddoto);
  452. if (0.0f > ss)
  453. {
  454. return false;
  455. }
  456. const bx::Vec3 point = getPointAt(_ray, ss);
  457. const float axisLen = bx::length(axis);
  458. axis = bx::normalize(axis);
  459. const float pdota = bx::dot(_cylinder.m_pos, axis);
  460. const float height = bx::dot(point, axis) - pdota;
  461. if (height > 0.0f
  462. && height < axisLen)
  463. {
  464. if (NULL != _hit)
  465. {
  466. const float t1 = height / axisLen;
  467. const bx::Vec3 pointOnAxis = bx::lerp(_cylinder.m_pos, _cylinder.m_end, t1);
  468. _hit->m_pos = point;
  469. const bx::Vec3 tmp = bx::sub(point, pointOnAxis);
  470. _hit->m_normal = bx::normalize(tmp);
  471. _hit->m_dist = ss;
  472. }
  473. return true;
  474. }
  475. if (_capsule)
  476. {
  477. const float rdota = bx::dot(_ray.m_pos, axis);
  478. const float pp = rdota - pdota;
  479. const float t1 = pp / axisLen;
  480. const bx::Vec3 pointOnAxis = bx::lerp(_cylinder.m_pos, _cylinder.m_end, t1);
  481. const bx::Vec3 axisToRay = bx::sub(_ray.m_pos, pointOnAxis);
  482. if (_cylinder.m_radius < bx::length(axisToRay)
  483. && 0.0f > ss)
  484. {
  485. return false;
  486. }
  487. Sphere sphere;
  488. sphere.m_radius = _cylinder.m_radius;
  489. sphere.m_center = 0.0f >= height
  490. ? _cylinder.m_pos
  491. : _cylinder.m_end
  492. ;
  493. return intersect(_ray, sphere, _hit);
  494. }
  495. bx::Plane plane;
  496. bx::Vec3 pos;
  497. if (0.0f >= height)
  498. {
  499. plane.normal = bx::neg(axis);
  500. pos = _cylinder.m_pos;
  501. }
  502. else
  503. {
  504. plane.normal = axis;
  505. pos = _cylinder.m_end;
  506. }
  507. plane.dist = -bx::dot(pos, plane.normal);
  508. Hit tmpHit;
  509. _hit = NULL != _hit ? _hit : &tmpHit;
  510. if (intersect(_ray, plane, _hit) )
  511. {
  512. const bx::Vec3 tmp = bx::sub(pos, _hit->m_pos);
  513. return bx::dot(tmp, tmp) <= rsq;
  514. }
  515. return false;
  516. }
  517. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
  518. {
  519. return intersect(_ray, _cylinder, false, _hit);
  520. }
  521. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
  522. {
  523. BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
  524. return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
  525. }
  526. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
  527. {
  528. const bx::Vec3 axis = bx::sub(_cone.m_pos, _cone.m_end);
  529. const float len = bx::length(axis);
  530. const bx::Vec3 normal = bx::normalize(axis);
  531. Disk disk;
  532. disk.m_center = _cone.m_pos;
  533. disk.m_normal = normal;
  534. disk.m_radius = _cone.m_radius;
  535. Hit tmpInt;
  536. Hit* out = NULL != _hit ? _hit : &tmpInt;
  537. bool hit = intersect(_ray, disk, out);
  538. const bx::Vec3 ro = bx::sub(_ray.m_pos, _cone.m_end);
  539. const float hyp = bx::sqrt(bx::square(_cone.m_radius) + bx::square(len) );
  540. const float cosaSq = bx::square(len/hyp);
  541. const float ndoto = bx::dot(normal, ro);
  542. const float ndotd = bx::dot(normal, _ray.m_dir);
  543. const float aa = bx::square(ndotd) - cosaSq;
  544. const float bb = 2.0f * (ndotd*ndoto - bx::dot(_ray.m_dir, ro)*cosaSq);
  545. const float cc = bx::square(ndoto) - bx::dot(ro, ro)*cosaSq;
  546. float det = bb*bb - 4.0f*aa*cc;
  547. if (0.0f > det)
  548. {
  549. return hit;
  550. }
  551. det = bx::sqrt(det);
  552. const float invA2 = 1.0f / (2.0f*aa);
  553. const float t1 = (-bb - det) * invA2;
  554. const float t2 = (-bb + det) * invA2;
  555. float tt = t1;
  556. if (0.0f > t1
  557. || (0.0f < t2 && t2 < t1) )
  558. {
  559. tt = t2;
  560. }
  561. if (0.0f > tt)
  562. {
  563. return hit;
  564. }
  565. const bx::Vec3 hitPos = getPointAt(_ray, tt);
  566. const bx::Vec3 point = bx::sub(hitPos, _cone.m_end);
  567. const float hh = bx::dot(normal, point);
  568. if (0.0f > hh
  569. || len < hh)
  570. {
  571. return hit;
  572. }
  573. if (NULL != _hit)
  574. {
  575. if (!hit
  576. || tt < _hit->m_dist)
  577. {
  578. _hit->m_dist = tt;
  579. _hit->m_pos = hitPos;
  580. const float scale = hh / bx::dot(point, point);
  581. const bx::Vec3 pointScaled = bx::mul(point, scale);
  582. const bx::Vec3 tmp = bx::sub(pointScaled, normal);
  583. _hit->m_normal = bx::normalize(tmp);
  584. }
  585. }
  586. return true;
  587. }
  588. bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit)
  589. {
  590. float equation = bx::dot(_ray.m_pos, _plane.normal) + _plane.dist;
  591. if (0.0f > equation)
  592. {
  593. return false;
  594. }
  595. float ndotd = bx::dot(_ray.m_dir, _plane.normal);
  596. if (0.0f < ndotd)
  597. {
  598. return false;
  599. }
  600. if (NULL != _hit)
  601. {
  602. _hit->m_normal = _plane.normal;
  603. float tt = -equation/ndotd;
  604. _hit->m_dist = tt;
  605. _hit->m_pos = getPointAt(_ray, tt);
  606. }
  607. return true;
  608. }
  609. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
  610. {
  611. const bx::Vec3 rs = bx::sub(_ray.m_pos, _sphere.m_center);
  612. const float bb = bx::dot(rs, _ray.m_dir);
  613. if (0.0f < bb)
  614. {
  615. return false;
  616. }
  617. const float aa = bx::dot(_ray.m_dir, _ray.m_dir);
  618. const float cc = bx::dot(rs, rs) - bx::square(_sphere.m_radius);
  619. const float discriminant = bb*bb - aa*cc;
  620. if (0.0f >= discriminant)
  621. {
  622. return false;
  623. }
  624. const float sqrtDiscriminant = bx::sqrt(discriminant);
  625. const float invA = 1.0f / aa;
  626. const float tt = -(bb + sqrtDiscriminant)*invA;
  627. if (0.0f >= tt)
  628. {
  629. return false;
  630. }
  631. if (NULL != _hit)
  632. {
  633. _hit->m_dist = tt;
  634. const bx::Vec3 point = getPointAt(_ray, tt);
  635. _hit->m_pos = point;
  636. const bx::Vec3 tmp = bx::sub(point, _sphere.m_center);
  637. _hit->m_normal = bx::normalize(tmp);
  638. }
  639. return true;
  640. }
  641. bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit)
  642. {
  643. const bx::Vec3 edge10 = bx::sub(_triangle.m_v1, _triangle.m_v0);
  644. const bx::Vec3 edge02 = bx::sub(_triangle.m_v0, _triangle.m_v2);
  645. const bx::Vec3 normal = bx::cross(edge02, edge10);
  646. const bx::Vec3 vo = bx::sub(_triangle.m_v0, _ray.m_pos);
  647. const bx::Vec3 dxo = bx::cross(_ray.m_dir, vo);
  648. const float det = bx::dot(normal, _ray.m_dir);
  649. if (det > 0.0f)
  650. {
  651. return false;
  652. }
  653. const float invDet = 1.0f/det;
  654. const float bz = bx::dot(dxo, edge02) * invDet;
  655. const float by = bx::dot(dxo, edge10) * invDet;
  656. const float bx = 1.0f - by - bz;
  657. if (bx < 0.0f || by < 0.0f || bz < 0.0f)
  658. {
  659. return false;
  660. }
  661. if (NULL != _hit)
  662. {
  663. _hit->m_normal = bx::normalize(normal);
  664. const float tt = bx::dot(normal, vo) * invDet;
  665. _hit->m_dist = tt;
  666. _hit->m_pos = getPointAt(_ray, tt);
  667. }
  668. return true;
  669. }