bounds.cpp 16 KB

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