bounds.cpp 20 KB

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