This commit is contained in:
Бранимир Караџић 2019-02-03 23:19:04 -08:00
parent 7468b6d006
commit a235f8028f
1 changed files with 57 additions and 32 deletions

View File

@ -21,7 +21,7 @@ Vec3 getExtents(const Aabb& _aabb)
Vec3 getCenter(const Triangle& _triangle) Vec3 getCenter(const Triangle& _triangle)
{ {
return bx::mul(bx::add(bx::add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f); return mul(add(add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f);
} }
void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extent) void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extent)
@ -529,9 +529,9 @@ static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule,
const Vec3 rc = sub(_ray.pos, _cylinder.pos); const Vec3 rc = sub(_ray.pos, _cylinder.pos);
const Vec3 dxa = cross(_ray.dir, axis); const Vec3 dxa = cross(_ray.dir, axis);
const float len = length(dxa); const float len = length(dxa);
const Vec3 normal = normalize(dxa); const Vec3 normal = normalize(dxa);
const float dist = bx::abs(dot(rc, normal) ); const float dist = bx::abs(dot(rc, normal) );
if (dist > _cylinder.radius) if (dist > _cylinder.radius)
{ {
@ -831,7 +831,7 @@ bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
return true; return true;
} }
void barycentric(float& _outU, float& _outV, float& _outW, const Triangle& _triangle, const Vec3& _pos) Vec3 barycentric(const Triangle& _triangle, const Vec3& _pos)
{ {
const Vec3 v0 = sub(_triangle.v1, _triangle.v0); const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
const Vec3 v1 = sub(_triangle.v2, _triangle.v0); const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
@ -844,22 +844,51 @@ void barycentric(float& _outU, float& _outV, float& _outW, const Triangle& _tria
const float dot12 = dot(v1, v2); const float dot12 = dot(v1, v2);
const float invDenom = 1.0f/(dot00*dot11 - square(dot01) ); const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
_outU = (dot11*dot02 - dot01*dot12)*invDenom;
_outV = (dot00*dot12 - dot01*dot02)*invDenom; const float uu = (dot11*dot02 - dot01*dot12)*invDenom;
_outW = 1.0f - _outU - _outV; const float vv = (dot00*dot12 - dot01*dot02)*invDenom;
const float ww = 1.0f - uu - vv;
return { uu, vv, ww };
} }
Vec3 closestPoint(const Plane& _plane, const Vec3 _pos) Vec3 cartesian(const Triangle& _triangle, const Vec3& _uvw)
{
const Vec3 b0 = mul(_triangle.v0, _uvw.x);
const Vec3 b1 = mul(_triangle.v1, _uvw.y);
const Vec3 b2 = mul(_triangle.v2, _uvw.z);
return add(add(b0, b1), b2);
}
void calcPlane(Plane& _outPlane, const Triangle& _triangle)
{
calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
}
Vec3 closestPoint(const Plane& _plane, const Vec3& _pos)
{ {
const float dist = distance(_plane, _pos); const float dist = distance(_plane, _pos);
return sub(_pos, mul(_plane.normal, dist) ); return sub(_pos, mul(_plane.normal, dist) );
} }
Vec3 closestPoint(const Aabb& _aabb, const Vec3 _pos) Vec3 closestPoint(const Aabb& _aabb, const Vec3& _pos)
{ {
return clamp(_pos, _aabb.min, _aabb.max); return clamp(_pos, _aabb.min, _aabb.max);
} }
Vec3 closestPoint(const Triangle& _triangle, const Vec3& _pos)
{
Plane plane;
calcPlane(plane, _triangle);
const Vec3 pos = closestPoint(plane, _pos);
const Vec3 uvw = barycentric(_triangle, pos);
return cartesian(_triangle, clamp(uvw, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}) );
}
bool overlap(const Sphere& _sphere, const Vec3& _pos) bool overlap(const Sphere& _sphere, const Vec3& _pos)
{ {
const Vec3 ba = sub(_sphere.center, _pos); const Vec3 ba = sub(_sphere.center, _pos);
@ -888,7 +917,7 @@ bool overlap(const Sphere& _sphere, const Plane& _plane)
bool overlap(const Sphere& _sphere, const Triangle& _triangle) bool overlap(const Sphere& _sphere, const Triangle& _triangle)
{ {
Plane plane; Plane plane;
calcPlane(plane, _triangle.v0, _triangle.v1, _triangle.v2); calcPlane(plane, _triangle);
if (!overlap(_sphere, plane) ) if (!overlap(_sphere, plane) )
{ {
@ -896,15 +925,12 @@ bool overlap(const Sphere& _sphere, const Triangle& _triangle)
} }
const Vec3 pos = closestPoint(plane, _sphere.center); const Vec3 pos = closestPoint(plane, _sphere.center);
const Vec3 uvw = barycentric(_triangle, pos);
float uu, vv, ww;
barycentric(uu, vv, ww, _triangle, pos);
const float nr = -_sphere.radius; const float nr = -_sphere.radius;
return uu >= nr return uvw.x >= nr
&& vv >= nr && uvw.y >= nr
&& ww >= nr && uvw.z >= nr
; ;
} }
@ -933,8 +959,8 @@ bool overlap(const Sphere& _sphere, const Disk& _disk)
return false; return false;
} }
bx::Plane plane; Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center); calcPlane(plane, _disk.normal, _disk.center);
return overlap(_sphere, plane); return overlap(_sphere, plane);
} }
@ -1017,7 +1043,7 @@ bool overlap(const Aabb& _aabb, const Plane& _plane)
bool overlap(const Aabb& _aabb, const Triangle& _triangle) bool overlap(const Aabb& _aabb, const Triangle& _triangle)
{ {
Plane plane; Plane plane;
calcPlane(plane, _triangle.v0, _triangle.v1, _triangle.v2); calcPlane(plane, _triangle);
if (!overlap(_aabb, plane) ) if (!overlap(_aabb, plane) )
{ {
@ -1053,8 +1079,8 @@ bool overlap(const Aabb& _aabb, const Disk& _disk)
return false; return false;
} }
bx::Plane plane; Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center); calcPlane(plane, _disk.normal, _disk.center);
return overlap(_aabb, plane); return overlap(_aabb, plane);
} }
@ -1065,14 +1091,13 @@ bool overlap(const Aabb& _aabb, const Obb& _obb)
return false; return false;
} }
bool overlap(const Triangle& _triangle, const bx::Vec3& _pos) bool overlap(const Triangle& _triangle, const Vec3& _pos)
{ {
float uu, vv, ww; const Vec3 uvw = barycentric(_triangle, _pos);
barycentric(uu, vv, ww, _triangle, _pos);
return uu >= 0.0f return uvw.x >= 0.0f
&& vv >= 0.0f && uvw.y >= 0.0f
&& ww >= 0.0f && uvw.z >= 0.0f
; ;
} }
@ -1086,7 +1111,7 @@ bool overlap(const Triangle& _triangle, const Aabb& _aabb)
return overlap(_aabb, _triangle); return overlap(_aabb, _triangle);
} }
bool overlap(const Triangle& _triangle, const bx::Plane& _plane) bool overlap(const Triangle& _triangle, const Plane& _plane)
{ {
const float dist0 = distance(_plane, _triangle.v0); const float dist0 = distance(_plane, _triangle.v0);
const float dist1 = distance(_plane, _triangle.v1); const float dist1 = distance(_plane, _triangle.v1);
@ -1131,8 +1156,8 @@ bool overlap(const Triangle& _triangle, const Disk& _disk)
return false; return false;
} }
bx::Plane plane; Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center); calcPlane(plane, _disk.normal, _disk.center);
return overlap(_triangle, plane); return overlap(_triangle, plane);
} }