bounds.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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/fpumath.h>
  7. #include "bounds.h"
  8. void aabbToObb(Obb& _obb, const Aabb& _aabb)
  9. {
  10. 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. memcpy(_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. uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1)
  154. {
  155. const uint32_t ltMinX = _aabb0.m_max[0] < _aabb1.m_min[0];
  156. const uint32_t gtMaxX = _aabb0.m_min[0] > _aabb1.m_max[0];
  157. const uint32_t ltMinY = _aabb0.m_max[1] < _aabb1.m_min[1];
  158. const uint32_t gtMaxY = _aabb0.m_min[1] > _aabb1.m_max[1];
  159. const uint32_t ltMinZ = _aabb0.m_max[2] < _aabb1.m_min[2];
  160. const uint32_t gtMaxZ = _aabb0.m_min[2] > _aabb1.m_max[2];
  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::piHalf/_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. memcpy(&_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. float center[3];
  211. center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f;
  212. center[1] = (aabb.m_min[1] + aabb.m_max[1]) * 0.5f;
  213. center[2] = (aabb.m_min[2] + aabb.m_max[2]) * 0.5f;
  214. float maxDistSq = 0.0f;
  215. uint8_t* vertex = (uint8_t*)_vertices;
  216. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  217. {
  218. float* position = (float*)vertex;
  219. vertex += _stride;
  220. float xx = position[0] - center[0];
  221. float yy = position[1] - center[1];
  222. float zz = position[2] - center[2];
  223. float distSq = xx*xx + yy*yy + zz*zz;
  224. maxDistSq = bx::fmax(distSq, maxDistSq);
  225. }
  226. bx::vec3Move(_sphere.m_center, center);
  227. _sphere.m_radius = sqrtf(maxDistSq);
  228. }
  229. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  230. {
  231. bx::RngMwc rng;
  232. uint8_t* vertex = (uint8_t*)_vertices;
  233. float center[3];
  234. float* position = (float*)&vertex[0];
  235. bx::vec3Move(center, position);
  236. position = (float*)&vertex[1*_stride];
  237. center[0] += position[0];
  238. center[1] += position[1];
  239. center[2] += position[2];
  240. center[0] *= 0.5f;
  241. center[1] *= 0.5f;
  242. center[2] *= 0.5f;
  243. float xx = position[0] - center[0];
  244. float yy = position[1] - center[1];
  245. float zz = position[2] - center[2];
  246. float maxDistSq = xx*xx + yy*yy + zz*zz;
  247. float radiusStep = _step * 0.37f;
  248. bool done;
  249. do
  250. {
  251. done = true;
  252. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  253. {
  254. position = (float*)&vertex[index*_stride];
  255. xx = position[0] - center[0];
  256. yy = position[1] - center[1];
  257. zz = position[2] - center[2];
  258. float distSq = xx*xx + yy*yy + zz*zz;
  259. if (distSq > maxDistSq)
  260. {
  261. done = false;
  262. center[0] += xx * radiusStep;
  263. center[1] += yy * radiusStep;
  264. center[2] += zz * radiusStep;
  265. maxDistSq = bx::flerp(maxDistSq, distSq, _step);
  266. break;
  267. }
  268. }
  269. } while (!done);
  270. bx::vec3Move(_sphere.m_center, center);
  271. _sphere.m_radius = bx::fsqrt(maxDistSq);
  272. }
  273. void calcPlaneUv(const Plane& _plane, float* _udir, float* _vdir)
  274. {
  275. bx::vec3TangentFrame(_plane.m_normal, _udir, _vdir);
  276. }
  277. void buildFrustumPlanes(Plane* _result, const float* _viewProj)
  278. {
  279. const float xw = _viewProj[ 3];
  280. const float yw = _viewProj[ 7];
  281. const float zw = _viewProj[11];
  282. const float ww = _viewProj[15];
  283. const float xz = _viewProj[ 2];
  284. const float yz = _viewProj[ 6];
  285. const float zz = _viewProj[10];
  286. const float wz = _viewProj[14];
  287. Plane& near = _result[0];
  288. Plane& far = _result[1];
  289. Plane& left = _result[2];
  290. Plane& right = _result[3];
  291. Plane& top = _result[4];
  292. Plane& bottom = _result[5];
  293. near.m_normal[0] = xw - xz;
  294. near.m_normal[1] = yw - yz;
  295. near.m_normal[2] = zw - zz;
  296. near.m_dist = ww - wz;
  297. far.m_normal[0] = xw + xz;
  298. far.m_normal[1] = yw + yz;
  299. far.m_normal[2] = zw + zz;
  300. far.m_dist = ww + wz;
  301. const float xx = _viewProj[ 0];
  302. const float yx = _viewProj[ 4];
  303. const float zx = _viewProj[ 8];
  304. const float wx = _viewProj[12];
  305. left.m_normal[0] = xw - xx;
  306. left.m_normal[1] = yw - yx;
  307. left.m_normal[2] = zw - zx;
  308. left.m_dist = ww - wx;
  309. right.m_normal[0] = xw + xx;
  310. right.m_normal[1] = yw + yx;
  311. right.m_normal[2] = zw + zx;
  312. right.m_dist = ww + wx;
  313. const float xy = _viewProj[ 1];
  314. const float yy = _viewProj[ 5];
  315. const float zy = _viewProj[ 9];
  316. const float wy = _viewProj[13];
  317. top.m_normal[0] = xw + xy;
  318. top.m_normal[1] = yw + yy;
  319. top.m_normal[2] = zw + zy;
  320. top.m_dist = ww + wy;
  321. bottom.m_normal[0] = xw - xy;
  322. bottom.m_normal[1] = yw - yy;
  323. bottom.m_normal[2] = zw - zy;
  324. bottom.m_dist = ww - wy;
  325. Plane* plane = _result;
  326. for (uint32_t ii = 0; ii < 6; ++ii)
  327. {
  328. float invLen = 1.0f / bx::vec3Norm(plane->m_normal, plane->m_normal);
  329. plane->m_dist *= invLen;
  330. ++plane;
  331. }
  332. }
  333. void intersectPlanes(float _result[3], const Plane& _pa, const Plane& _pb, const Plane& _pc)
  334. {
  335. float axb[3];
  336. bx::vec3Cross(axb, _pa.m_normal, _pb.m_normal);
  337. float bxc[3];
  338. bx::vec3Cross(bxc, _pb.m_normal, _pc.m_normal);
  339. float cxa[3];
  340. bx::vec3Cross(cxa, _pc.m_normal, _pa.m_normal);
  341. float tmp0[3];
  342. bx::vec3Mul(tmp0, bxc, _pa.m_dist);
  343. float tmp1[3];
  344. bx::vec3Mul(tmp1, cxa, _pb.m_dist);
  345. float tmp2[3];
  346. bx::vec3Mul(tmp2, axb, _pc.m_dist);
  347. float tmp[3];
  348. bx::vec3Add(tmp, tmp0, tmp1);
  349. bx::vec3Add(tmp0, tmp, tmp2);
  350. float denom = bx::vec3Dot(_pa.m_normal, bxc);
  351. bx::vec3Mul(_result, tmp0, -1.0f/denom);
  352. }
  353. Ray makeRay(float _x, float _y, const float* _invVp)
  354. {
  355. Ray ray;
  356. const float near[3] = { _x, _y, 0.0f };
  357. bx::vec3MulMtxH(ray.m_pos, near, _invVp);
  358. float tmp[3];
  359. const float far[3] = { _x, _y, 1.0f };
  360. bx::vec3MulMtxH(tmp, far, _invVp);
  361. float dir[3];
  362. bx::vec3Sub(dir, tmp, ray.m_pos);
  363. bx::vec3Norm(ray.m_dir, dir);
  364. return ray;
  365. }
  366. inline void getPointAt(float* _result, const Ray& _ray, float _t)
  367. {
  368. float tmp[3];
  369. bx::vec3Mul(tmp, _ray.m_dir, _t);
  370. bx::vec3Add(_result, _ray.m_pos, tmp);
  371. }
  372. bool intersect(const Ray& _ray, const Aabb& _aabb, Intersection* _intersection)
  373. {
  374. float invDir[3];
  375. bx::vec3Rcp(invDir, _ray.m_dir);
  376. float tmp[3];
  377. float t0[3];
  378. bx::vec3Sub(tmp, _aabb.m_min, _ray.m_pos);
  379. bx::vec3Mul(t0, tmp, invDir);
  380. float t1[3];
  381. bx::vec3Sub(tmp, _aabb.m_max, _ray.m_pos);
  382. bx::vec3Mul(t1, tmp, invDir);
  383. float min[3];
  384. bx::vec3Min(min, t0, t1);
  385. float max[3];
  386. bx::vec3Max(max, t0, t1);
  387. const float tmin = bx::fmax3(min[0], min[1], min[2]);
  388. const float tmax = bx::fmin3(max[0], max[1], max[2]);
  389. if (tmax < 0.0f
  390. || tmin > tmax)
  391. {
  392. return false;
  393. }
  394. if (NULL != _intersection)
  395. {
  396. _intersection->m_normal[0] = float( (min[0] == tmin) - (max[0] == tmin) );
  397. _intersection->m_normal[1] = float( (min[1] == tmin) - (max[1] == tmin) );
  398. _intersection->m_normal[2] = float( (min[2] == tmin) - (max[2] == tmin) );
  399. _intersection->m_dist = tmin;
  400. getPointAt(_intersection->m_pos, _ray, tmin);
  401. }
  402. return true;
  403. }
  404. bool intersect(const Ray& _ray, const Disk& _disk, Intersection* _intersection)
  405. {
  406. Plane plane;
  407. bx::vec3Move(plane.m_normal, _disk.m_normal);
  408. plane.m_dist = -bx::vec3Dot(_disk.m_center, _disk.m_normal);
  409. Intersection tmpIntersection;
  410. _intersection = NULL != _intersection ? _intersection : &tmpIntersection;
  411. if (intersect(_ray, plane, _intersection) )
  412. {
  413. float tmp[3];
  414. bx::vec3Sub(tmp, _disk.m_center, _intersection->m_pos);
  415. return bx::vec3Dot(tmp, tmp) <= bx::fsq(_disk.m_radius);
  416. }
  417. return false;
  418. }
  419. bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Intersection* _intersection)
  420. {
  421. float axis[3];
  422. bx::vec3Sub(axis, _cylinder.m_end, _cylinder.m_pos);
  423. float rc[3];
  424. bx::vec3Sub(rc, _ray.m_pos, _cylinder.m_pos);
  425. float normal[3];
  426. bx::vec3Cross(normal, _ray.m_dir, axis);
  427. const float len = bx::vec3Norm(normal, normal);
  428. const float dist = bx::fabsolute(bx::vec3Dot(rc, normal) );
  429. if (dist > _cylinder.m_radius)
  430. {
  431. return false;
  432. }
  433. float vo[3];
  434. bx::vec3Cross(vo, rc, axis);
  435. const float t0 = -bx::vec3Dot(vo, normal) / len;
  436. bx::vec3Cross(vo, normal, axis);
  437. bx::vec3Norm(vo, vo);
  438. const float rsq = bx::fsq(_cylinder.m_radius);
  439. const float ddoto = bx::vec3Dot(_ray.m_dir, vo);
  440. const float ss = t0 - bx::fabsolute(bx::fsqrt(rsq - bx::fsq(dist) ) / ddoto);
  441. float point[3];
  442. getPointAt(point, _ray, ss);
  443. const float axisLen = bx::vec3Norm(axis, axis);
  444. const float pdota = bx::vec3Dot(_cylinder.m_pos, axis);
  445. const float height = bx::vec3Dot(point, axis) - pdota;
  446. if (height > 0.0f
  447. && height < axisLen)
  448. {
  449. if (NULL != _intersection)
  450. {
  451. const float t1 = height / axisLen;
  452. float pointOnAxis[3];
  453. bx::vec3Lerp(pointOnAxis, _cylinder.m_pos, _cylinder.m_end, t1);
  454. bx::vec3Move(_intersection->m_pos, point);
  455. float tmp[3];
  456. bx::vec3Sub(tmp, point, pointOnAxis);
  457. bx::vec3Norm(_intersection->m_normal, tmp);
  458. _intersection->m_dist = ss;
  459. }
  460. return true;
  461. }
  462. if (_capsule)
  463. {
  464. const float rdota = bx::vec3Dot(_ray.m_pos, axis);
  465. const float pp = rdota - pdota;
  466. const float t1 = pp / axisLen;
  467. float pointOnAxis[3];
  468. bx::vec3Lerp(pointOnAxis, _cylinder.m_pos, _cylinder.m_end, t1);
  469. float axisToRay[3];
  470. bx::vec3Sub(axisToRay, _ray.m_pos, pointOnAxis);
  471. if (_cylinder.m_radius < bx::vec3Length(axisToRay)
  472. && 0.0f > ss)
  473. {
  474. return false;
  475. }
  476. Sphere sphere;
  477. sphere.m_radius = _cylinder.m_radius;
  478. bx::vec3Move(sphere.m_center, 0.0f >= height
  479. ? _cylinder.m_pos
  480. : _cylinder.m_end
  481. );
  482. return intersect(_ray, sphere, _intersection);
  483. }
  484. Plane plane;
  485. float pos[3];
  486. if (0.0f >= height)
  487. {
  488. bx::vec3Neg(plane.m_normal, axis);
  489. bx::vec3Move(pos, _cylinder.m_pos);
  490. }
  491. else
  492. {
  493. bx::vec3Move(plane.m_normal, axis);
  494. bx::vec3Move(pos, _cylinder.m_end);
  495. }
  496. plane.m_dist = -bx::vec3Dot(pos, plane.m_normal);
  497. Intersection tmpIntersection;
  498. _intersection = NULL != _intersection ? _intersection : &tmpIntersection;
  499. if (intersect(_ray, plane, _intersection) )
  500. {
  501. float tmp[3];
  502. bx::vec3Sub(tmp, pos, _intersection->m_pos);
  503. return bx::vec3Dot(tmp, tmp) <= rsq;
  504. }
  505. return false;
  506. }
  507. bool intersect(const Ray& _ray, const Plane& _plane, Intersection* _intersection)
  508. {
  509. float equation = bx::vec3Dot(_ray.m_pos, _plane.m_normal) + _plane.m_dist;
  510. if (0.0f > equation)
  511. {
  512. return false;
  513. }
  514. float ndotd = bx::vec3Dot(_ray.m_dir, _plane.m_normal);
  515. if (0.0f < ndotd)
  516. {
  517. return false;
  518. }
  519. if (NULL != _intersection)
  520. {
  521. bx::vec3Move(_intersection->m_normal, _plane.m_normal);
  522. float tt = -equation/ndotd;
  523. _intersection->m_dist = tt;
  524. getPointAt(_intersection->m_pos, _ray, tt);
  525. }
  526. return true;
  527. }
  528. bool intersect(const Ray& _ray, const Sphere& _sphere, Intersection* _intersection)
  529. {
  530. float rs[3];
  531. bx::vec3Sub(rs, _ray.m_pos, _sphere.m_center);
  532. const float bb = bx::vec3Dot(rs, _ray.m_dir);
  533. if (0.0f < bb)
  534. {
  535. return false;
  536. }
  537. const float aa = bx::vec3Dot(_ray.m_dir, _ray.m_dir);
  538. const float cc = bx::vec3Dot(rs, rs) - bx::fsq(_sphere.m_radius);
  539. const float discriminant = bb*bb - aa*cc;
  540. if (0.0f >= discriminant)
  541. {
  542. return false;
  543. }
  544. const float sqrtDiscriminant = bx::fsqrt(discriminant);
  545. const float invA = 1.0f / aa;
  546. const float tt = -(bb + sqrtDiscriminant)*invA;
  547. if (0.0f >= tt)
  548. {
  549. return false;
  550. }
  551. if (NULL != _intersection)
  552. {
  553. _intersection->m_dist = tt;
  554. float point[3];
  555. getPointAt(point, _ray, tt);
  556. bx::vec3Move(_intersection->m_pos, point);
  557. float tmp[3];
  558. bx::vec3Sub(tmp, point, _sphere.m_center);
  559. bx::vec3Norm(_intersection->m_normal, tmp);
  560. }
  561. return true;
  562. }
  563. bool intersect(const Ray& _ray, const Tris& _triangle, Intersection* _intersection)
  564. {
  565. float edge10[3];
  566. bx::vec3Sub(edge10, _triangle.m_v1, _triangle.m_v0);
  567. float edge02[3];
  568. bx::vec3Sub(edge02, _triangle.m_v0, _triangle.m_v2);
  569. float normal[3];
  570. bx::vec3Cross(normal, edge02, edge10);
  571. float vo[3];
  572. bx::vec3Sub(vo, _triangle.m_v0, _ray.m_pos);
  573. float dxo[3];
  574. bx::vec3Cross(dxo, _ray.m_dir, vo);
  575. const float det = bx::vec3Dot(normal, _ray.m_dir);
  576. if (det > 0.0f)
  577. {
  578. return false;
  579. }
  580. const float invDet = 1.0f/det;
  581. const float bz = bx::vec3Dot(dxo, edge02) * invDet;
  582. const float by = bx::vec3Dot(dxo, edge10) * invDet;
  583. const float bx = 1.0f - by - bz;
  584. if (bx < 0.0f || by < 0.0f || bz < 0.0f)
  585. {
  586. return false;
  587. }
  588. if (NULL != _intersection)
  589. {
  590. bx::vec3Norm(_intersection->m_normal, normal);
  591. const float tt = bx::vec3Dot(normal, vo) * invDet;
  592. _intersection->m_dist = tt;
  593. getPointAt(_intersection->m_pos, _ray, tt);
  594. }
  595. return true;
  596. }