bounds.cpp 16 KB

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