bounds.cpp 20 KB

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