Skip to content

Instantly share code, notes, and snippets.

@hb3p8
Created May 14, 2014 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hb3p8/819807106921773a67b1 to your computer and use it in GitHub Desktop.
Save hb3p8/819807106921773a67b1 to your computer and use it in GitHub Desktop.
ivec4 ObjectNearestHit (in int theBVHOffset, in int theVrtOffset, in int theTrgOffset,
in SRay theRay, in vec3 theInverse, inout SIntersect theHit, in int theSentinel)
{
int aHead = theSentinel; // stack pointer
int aNode = 0; // node to visit
ivec4 aTriIndex = INALID_HIT;
float aTimeOut;
float aTimeLft;
float aTimeRgh;
ivec3 mmax = ivec3 (theRay.Direct.x > 0.f ? 1 : 0,
theRay.Direct.y > 0.f ? 1 : 0,
theRay.Direct.z > 0.f ? 1 : 0);
ivec3 mmin = ivec3 (theRay.Direct.x <= 0.f ? 1 : 0,
theRay.Direct.y <= 0.f ? 1 : 0,
theRay.Direct.z <= 0.f ? 1 : 0);
while (true)
{
ivec3 aData = texelFetch (uObjectNodeInfoTexture, aNode + theBVHOffset).xyz;
if (aData.x == 0) // if inner node
{
vec3 aNodeMinLft = texelFetch (uObjectMinPointTexture, aData.y + theBVHOffset).xyz;
vec3 aNodeMaxLft = texelFetch (uObjectMaxPointTexture, aData.y + theBVHOffset).xyz;
vec3 aNodeMinRgh = texelFetch (uObjectMinPointTexture, aData.z + theBVHOffset).xyz;
vec3 aNodeMaxRgh = texelFetch (uObjectMaxPointTexture, aData.z + theBVHOffset).xyz;
mat2x3 aTimes = mat2x3 ((aNodeMinLft - theRay.Origin) * theInverse,
(aNodeMaxLft - theRay.Origin) * theInverse);
aTimeOut = min ( aTimes[mmax[0]].x, min (aTimes[mmax[1]].y, aTimes[mmax[2]].z) );
aTimeLft = max ( aTimes[mmin[0]].x, max (aTimes[mmin[1]].y, aTimes[mmin[2]].z) );
int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.f) & int(aTimeLft <= theHit.Time);
aTimes = mat2x3 ((aNodeMinRgh - theRay.Origin) * theInverse,
aTimeOut = min ( aTimes[mmax[0]].x, min (aTimes[mmax[1]].y, aTimes[mmax[2]].z) );
aTimeLft = max ( aTimes[mmin[0]].x, max (aTimes[mmin[1]].y, aTimes[mmin[2]].z) );
int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.f) & int(aTimeRgh <= theHit.Time);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment