bounds.cpp 18 KB

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