bounds.cpp 21 KB

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