bounds.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 calcPlaneUv(const Plane& _plane, bx::Vec3& _udir, bx::Vec3& _vdir)
  278. {
  279. bx::calcTangentFrame(_udir, _vdir, _plane.m_normal);
  280. }
  281. void buildFrustumPlanes(Plane* _result, const float* _viewProj)
  282. {
  283. const float xw = _viewProj[ 3];
  284. const float yw = _viewProj[ 7];
  285. const float zw = _viewProj[11];
  286. const float ww = _viewProj[15];
  287. const float xz = _viewProj[ 2];
  288. const float yz = _viewProj[ 6];
  289. const float zz = _viewProj[10];
  290. const float wz = _viewProj[14];
  291. Plane& near = _result[0];
  292. Plane& far = _result[1];
  293. Plane& left = _result[2];
  294. Plane& right = _result[3];
  295. Plane& top = _result[4];
  296. Plane& bottom = _result[5];
  297. near.m_normal.x = xw - xz;
  298. near.m_normal.y = yw - yz;
  299. near.m_normal.z = zw - zz;
  300. near.m_dist = ww - wz;
  301. far.m_normal.x = xw + xz;
  302. far.m_normal.y = yw + yz;
  303. far.m_normal.z = zw + zz;
  304. far.m_dist = ww + wz;
  305. const float xx = _viewProj[ 0];
  306. const float yx = _viewProj[ 4];
  307. const float zx = _viewProj[ 8];
  308. const float wx = _viewProj[12];
  309. left.m_normal.x = xw - xx;
  310. left.m_normal.y = yw - yx;
  311. left.m_normal.z = zw - zx;
  312. left.m_dist = ww - wx;
  313. right.m_normal.x = xw + xx;
  314. right.m_normal.y = yw + yx;
  315. right.m_normal.z = zw + zx;
  316. right.m_dist = ww + wx;
  317. const float xy = _viewProj[ 1];
  318. const float yy = _viewProj[ 5];
  319. const float zy = _viewProj[ 9];
  320. const float wy = _viewProj[13];
  321. top.m_normal.x = xw + xy;
  322. top.m_normal.y = yw + yy;
  323. top.m_normal.z = zw + zy;
  324. top.m_dist = ww + wy;
  325. bottom.m_normal.x = xw - xy;
  326. bottom.m_normal.y = yw - yy;
  327. bottom.m_normal.z = zw - zy;
  328. bottom.m_dist = ww - wy;
  329. Plane* plane = _result;
  330. for (uint32_t ii = 0; ii < 6; ++ii)
  331. {
  332. const float len = bx::length(plane->m_normal);
  333. plane->m_normal = bx::normalize(plane->m_normal);
  334. float invLen = 1.0f / len;
  335. plane->m_dist *= invLen;
  336. ++plane;
  337. }
  338. }
  339. bx::Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc)
  340. {
  341. const bx::Vec3 axb = bx::cross(_pa.m_normal, _pb.m_normal);
  342. const bx::Vec3 bxc = bx::cross(_pb.m_normal, _pc.m_normal);
  343. const bx::Vec3 cxa = bx::cross(_pc.m_normal, _pa.m_normal);
  344. const bx::Vec3 tmp0 = bx::mul(bxc, _pa.m_dist);
  345. const bx::Vec3 tmp1 = bx::mul(cxa, _pb.m_dist);
  346. const bx::Vec3 tmp2 = bx::mul(axb, _pc.m_dist);
  347. const bx::Vec3 tmp3 = bx::add(tmp0, tmp1);
  348. const bx::Vec3 tmp4 = bx::add(tmp3, tmp2);
  349. const float denom = bx::dot(_pa.m_normal, bxc);
  350. const bx::Vec3 result = bx::mul(tmp4, -1.0f/denom);
  351. return result;
  352. }
  353. Ray makeRay(float _x, float _y, const float* _invVp)
  354. {
  355. Ray ray;
  356. const bx::Vec3 near = { _x, _y, 0.0f };
  357. ray.m_pos = bx::mulH(near, _invVp);
  358. const bx::Vec3 far = { _x, _y, 1.0f };
  359. bx::Vec3 tmp = bx::mulH(far, _invVp);
  360. const bx::Vec3 dir = bx::sub(tmp, ray.m_pos);
  361. ray.m_dir = bx::normalize(dir);
  362. return ray;
  363. }
  364. inline bx::Vec3 getPointAt(const Ray& _ray, float _t)
  365. {
  366. return bx::add(bx::mul(_ray.m_dir, _t), _ray.m_pos);
  367. }
  368. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
  369. {
  370. const bx::Vec3 invDir = bx::rcp(_ray.m_dir);
  371. const bx::Vec3 tmp0 = bx::sub(_aabb.m_min, _ray.m_pos);
  372. const bx::Vec3 t0 = bx::mul(tmp0, invDir);
  373. const bx::Vec3 tmp1 = bx::sub(_aabb.m_max, _ray.m_pos);
  374. const bx::Vec3 t1 = bx::mul(tmp1, invDir);
  375. const bx::Vec3 min = bx::min(t0, t1);
  376. const bx::Vec3 max = bx::max(t0, t1);
  377. const float tmin = bx::max(min.x, min.y, min.z);
  378. const float tmax = bx::min(max.x, max.y, max.z);
  379. if (tmax < 0.0f
  380. || tmin > tmax)
  381. {
  382. return false;
  383. }
  384. if (NULL != _hit)
  385. {
  386. _hit->m_normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
  387. _hit->m_normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
  388. _hit->m_normal.z = float( (t1.z == tmin) - (t0.z == tmin) );
  389. _hit->m_dist = tmin;
  390. _hit->m_pos = getPointAt(_ray, tmin);
  391. }
  392. return true;
  393. }
  394. static const Aabb s_kUnitAabb =
  395. {
  396. { -1.0f, -1.0f, -1.0f },
  397. { 1.0f, 1.0f, 1.0f },
  398. };
  399. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
  400. {
  401. Aabb aabb;
  402. toAabb(aabb, _obb);
  403. if (!intersect(_ray, aabb) )
  404. {
  405. return false;
  406. }
  407. float mtxInv[16];
  408. bx::mtxInverse(mtxInv, _obb.m_mtx);
  409. Ray obbRay;
  410. obbRay.m_pos = bx::mul(_ray.m_pos, mtxInv);
  411. obbRay.m_dir = bx::mulXyz0(_ray.m_dir, mtxInv);
  412. if (intersect(obbRay, s_kUnitAabb, _hit) )
  413. {
  414. if (NULL != _hit)
  415. {
  416. _hit->m_pos = bx::mul(_hit->m_pos, _obb.m_mtx);
  417. const bx::Vec3 tmp = bx::mulXyz0(_hit->m_normal, _obb.m_mtx);
  418. _hit->m_normal = bx::normalize(tmp);
  419. }
  420. return true;
  421. }
  422. return false;
  423. }
  424. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
  425. {
  426. Plane plane;
  427. plane.m_normal = _disk.m_normal;
  428. plane.m_dist = -bx::dot(_disk.m_center, _disk.m_normal);
  429. Hit tmpHit;
  430. _hit = NULL != _hit ? _hit : &tmpHit;
  431. if (intersect(_ray, plane, _hit) )
  432. {
  433. const bx::Vec3 tmp = bx::sub(_disk.m_center, _hit->m_pos);
  434. return bx::dot(tmp, tmp) <= bx::square(_disk.m_radius);
  435. }
  436. return false;
  437. }
  438. static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
  439. {
  440. bx::Vec3 axis = bx::sub(_cylinder.m_end, _cylinder.m_pos);
  441. const bx::Vec3 rc = bx::sub(_ray.m_pos, _cylinder.m_pos);
  442. const bx::Vec3 dxa = bx::cross(_ray.m_dir, axis);
  443. const float len = bx::length(dxa);
  444. const bx::Vec3 normal = bx::normalize(dxa);
  445. const float dist = bx::abs(bx::dot(rc, normal) );
  446. if (dist > _cylinder.m_radius)
  447. {
  448. return false;
  449. }
  450. bx::Vec3 vo = bx::cross(rc, axis);
  451. const float t0 = -bx::dot(vo, normal) / len;
  452. vo = bx::normalize(bx::cross(normal, axis) );
  453. const float rsq = bx::square(_cylinder.m_radius);
  454. const float ddoto = bx::dot(_ray.m_dir, vo);
  455. const float ss = t0 - bx::abs(bx::sqrt(rsq - bx::square(dist) ) / ddoto);
  456. if (0.0f > ss)
  457. {
  458. return false;
  459. }
  460. const bx::Vec3 point = getPointAt(_ray, ss);
  461. const float axisLen = bx::length(axis);
  462. axis = bx::normalize(axis);
  463. const float pdota = bx::dot(_cylinder.m_pos, axis);
  464. const float height = bx::dot(point, axis) - pdota;
  465. if (height > 0.0f
  466. && height < axisLen)
  467. {
  468. if (NULL != _hit)
  469. {
  470. const float t1 = height / axisLen;
  471. const bx::Vec3 pointOnAxis = bx::lerp(_cylinder.m_pos, _cylinder.m_end, t1);
  472. _hit->m_pos = point;
  473. const bx::Vec3 tmp = bx::sub(point, pointOnAxis);
  474. _hit->m_normal = bx::normalize(tmp);
  475. _hit->m_dist = ss;
  476. }
  477. return true;
  478. }
  479. if (_capsule)
  480. {
  481. const float rdota = bx::dot(_ray.m_pos, axis);
  482. const float pp = rdota - pdota;
  483. const float t1 = pp / axisLen;
  484. const bx::Vec3 pointOnAxis = bx::lerp(_cylinder.m_pos, _cylinder.m_end, t1);
  485. const bx::Vec3 axisToRay = bx::sub(_ray.m_pos, pointOnAxis);
  486. if (_cylinder.m_radius < bx::length(axisToRay)
  487. && 0.0f > ss)
  488. {
  489. return false;
  490. }
  491. Sphere sphere;
  492. sphere.m_radius = _cylinder.m_radius;
  493. sphere.m_center = 0.0f >= height
  494. ? _cylinder.m_pos
  495. : _cylinder.m_end
  496. ;
  497. return intersect(_ray, sphere, _hit);
  498. }
  499. Plane plane;
  500. bx::Vec3 pos;
  501. if (0.0f >= height)
  502. {
  503. plane.m_normal = bx::neg(axis);
  504. pos = _cylinder.m_pos;
  505. }
  506. else
  507. {
  508. plane.m_normal = axis;
  509. pos = _cylinder.m_end;
  510. }
  511. plane.m_dist = -bx::dot(pos, plane.m_normal);
  512. Hit tmpHit;
  513. _hit = NULL != _hit ? _hit : &tmpHit;
  514. if (intersect(_ray, plane, _hit) )
  515. {
  516. const bx::Vec3 tmp = bx::sub(pos, _hit->m_pos);
  517. return bx::dot(tmp, tmp) <= rsq;
  518. }
  519. return false;
  520. }
  521. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
  522. {
  523. return intersect(_ray, _cylinder, false, _hit);
  524. }
  525. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
  526. {
  527. BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
  528. return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
  529. }
  530. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
  531. {
  532. const bx::Vec3 axis = bx::sub(_cone.m_pos, _cone.m_end);
  533. const float len = bx::length(axis);
  534. const bx::Vec3 normal = bx::normalize(axis);
  535. Disk disk;
  536. disk.m_center = _cone.m_pos;
  537. disk.m_normal = normal;
  538. disk.m_radius = _cone.m_radius;
  539. Hit tmpInt;
  540. Hit* out = NULL != _hit ? _hit : &tmpInt;
  541. bool hit = intersect(_ray, disk, out);
  542. const bx::Vec3 ro = bx::sub(_ray.m_pos, _cone.m_end);
  543. const float hyp = bx::sqrt(bx::square(_cone.m_radius) + bx::square(len) );
  544. const float cosaSq = bx::square(len/hyp);
  545. const float ndoto = bx::dot(normal, ro);
  546. const float ndotd = bx::dot(normal, _ray.m_dir);
  547. const float aa = bx::square(ndotd) - cosaSq;
  548. const float bb = 2.0f * (ndotd*ndoto - bx::dot(_ray.m_dir, ro)*cosaSq);
  549. const float cc = bx::square(ndoto) - bx::dot(ro, ro)*cosaSq;
  550. float det = bb*bb - 4.0f*aa*cc;
  551. if (0.0f > det)
  552. {
  553. return hit;
  554. }
  555. det = bx::sqrt(det);
  556. const float invA2 = 1.0f / (2.0f*aa);
  557. const float t1 = (-bb - det) * invA2;
  558. const float t2 = (-bb + det) * invA2;
  559. float tt = t1;
  560. if (0.0f > t1
  561. || (0.0f < t2 && t2 < t1) )
  562. {
  563. tt = t2;
  564. }
  565. if (0.0f > tt)
  566. {
  567. return hit;
  568. }
  569. const bx::Vec3 hitPos = getPointAt(_ray, tt);
  570. const bx::Vec3 point = bx::sub(hitPos, _cone.m_end);
  571. const float hh = bx::dot(normal, point);
  572. if (0.0f > hh
  573. || len < hh)
  574. {
  575. return hit;
  576. }
  577. if (NULL != _hit)
  578. {
  579. if (!hit
  580. || tt < _hit->m_dist)
  581. {
  582. _hit->m_dist = tt;
  583. _hit->m_pos = hitPos;
  584. const float scale = hh / bx::dot(point, point);
  585. const bx::Vec3 pointScaled = bx::mul(point, scale);
  586. const bx::Vec3 tmp = bx::sub(pointScaled, normal);
  587. _hit->m_normal = bx::normalize(tmp);
  588. }
  589. }
  590. return true;
  591. }
  592. bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
  593. {
  594. float equation = bx::dot(_ray.m_pos, _plane.m_normal) + _plane.m_dist;
  595. if (0.0f > equation)
  596. {
  597. return false;
  598. }
  599. float ndotd = bx::dot(_ray.m_dir, _plane.m_normal);
  600. if (0.0f < ndotd)
  601. {
  602. return false;
  603. }
  604. if (NULL != _hit)
  605. {
  606. _hit->m_normal = _plane.m_normal;
  607. float tt = -equation/ndotd;
  608. _hit->m_dist = tt;
  609. _hit->m_pos = getPointAt(_ray, tt);
  610. }
  611. return true;
  612. }
  613. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
  614. {
  615. const bx::Vec3 rs = bx::sub(_ray.m_pos, _sphere.m_center);
  616. const float bb = bx::dot(rs, _ray.m_dir);
  617. if (0.0f < bb)
  618. {
  619. return false;
  620. }
  621. const float aa = bx::dot(_ray.m_dir, _ray.m_dir);
  622. const float cc = bx::dot(rs, rs) - bx::square(_sphere.m_radius);
  623. const float discriminant = bb*bb - aa*cc;
  624. if (0.0f >= discriminant)
  625. {
  626. return false;
  627. }
  628. const float sqrtDiscriminant = bx::sqrt(discriminant);
  629. const float invA = 1.0f / aa;
  630. const float tt = -(bb + sqrtDiscriminant)*invA;
  631. if (0.0f >= tt)
  632. {
  633. return false;
  634. }
  635. if (NULL != _hit)
  636. {
  637. _hit->m_dist = tt;
  638. const bx::Vec3 point = getPointAt(_ray, tt);
  639. _hit->m_pos = point;
  640. const bx::Vec3 tmp = bx::sub(point, _sphere.m_center);
  641. _hit->m_normal = bx::normalize(tmp);
  642. }
  643. return true;
  644. }
  645. bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit)
  646. {
  647. const bx::Vec3 edge10 = bx::sub(_triangle.m_v1, _triangle.m_v0);
  648. const bx::Vec3 edge02 = bx::sub(_triangle.m_v0, _triangle.m_v2);
  649. const bx::Vec3 normal = bx::cross(edge02, edge10);
  650. const bx::Vec3 vo = bx::sub(_triangle.m_v0, _ray.m_pos);
  651. const bx::Vec3 dxo = bx::cross(_ray.m_dir, vo);
  652. const float det = bx::dot(normal, _ray.m_dir);
  653. if (det > 0.0f)
  654. {
  655. return false;
  656. }
  657. const float invDet = 1.0f/det;
  658. const float bz = bx::dot(dxo, edge02) * invDet;
  659. const float by = bx::dot(dxo, edge10) * invDet;
  660. const float bx = 1.0f - by - bz;
  661. if (bx < 0.0f || by < 0.0f || bz < 0.0f)
  662. {
  663. return false;
  664. }
  665. if (NULL != _hit)
  666. {
  667. _hit->m_normal = bx::normalize(normal);
  668. const float tt = bx::dot(normal, vo) * invDet;
  669. _hit->m_dist = tt;
  670. _hit->m_pos = getPointAt(_ray, tt);
  671. }
  672. return true;
  673. }