bounds.cpp 17 KB

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