Fix PLBVH with 0/1 nodes, fix optimized ray-AABB intersect.
This commit is contained in:
@@ -403,69 +403,39 @@ b3Vector3 b3Vector3_normalize(b3Vector3 v)
|
||||
b3Scalar b3Vector3_length2(b3Vector3 v) { return v.x*v.x + v.y*v.y + v.z*v.z; }
|
||||
b3Scalar b3Vector3_dot(b3Vector3 a, b3Vector3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
|
||||
|
||||
|
||||
/**
|
||||
|
||||
int rayIntersectsAabb_optimized(b3Vector3 rayFrom, b3Vector3 rayTo, b3Vector3 rayNormalizedDirection, b3AabbCL aabb)
|
||||
int rayIntersectsAabb(b3Vector3 rayOrigin, b3Scalar rayLength, b3Vector3 rayNormalizedDirection, b3AabbCL aabb)
|
||||
{
|
||||
// not functional -- need to fix
|
||||
|
||||
//aabb is considered as 3 pairs of 2 planes( {x_min, x_max}, {y_min, y_max}, {z_min, z_max} )
|
||||
//t_min is the first intersection, t_max is the second intersection
|
||||
b3Vector3 inverseRayDirection = (b3Vector3){1.0f, 1.0f, 1.0f, 0.0f} / rayNormalizedDirection;
|
||||
int4 sign = isless( inverseRayDirection, (b3Vector3){0.0f, 0.0f, 0.0f, 0.0f} ); //isless(x,y) returns (x < y)
|
||||
//AABB is considered as 3 pairs of 2 planes( {x_min, x_max}, {y_min, y_max}, {z_min, z_max} ).
|
||||
//t_min is the point of intersection with the closer plane, t_max is the point of intersection with the farther plane.
|
||||
//
|
||||
//if (rayNormalizedDirection.x < 0.0f), then max.x will be the near plane
|
||||
//and min.x will be the far plane; otherwise, it is reversed.
|
||||
//
|
||||
//In order for there to be a collision, the t_min and t_max of each pair must overlap.
|
||||
//This can be tested for by selecting the highest t_min and lowest t_max and comparing them.
|
||||
|
||||
//select(b, a, condition) == condition ? a : b
|
||||
b3Vector3 t_min = ( select(aabb.m_min, aabb.m_max, sign) - rayFrom ) * inverseRayDirection;
|
||||
b3Vector3 t_max = ( select(aabb.m_min, aabb.m_max, (int4){1,1,1,1} - sign) - rayFrom ) * inverseRayDirection;
|
||||
int4 isNegative = isless( rayNormalizedDirection, (b3Vector3){0.0f, 0.0f, 0.0f, 0.0f} ); //isless(x,y) returns (x < y)
|
||||
|
||||
//When using vector types, the select() function checks the most signficant bit,
|
||||
//but isless() sets the least significant bit.
|
||||
isNegative <<= 31;
|
||||
|
||||
//select(b, a, condition) == condition ? a : b
|
||||
//When using select() with vector types, (condition[i]) is true if its most significant bit is 1
|
||||
b3Vector3 t_min = ( select(aabb.m_min, aabb.m_max, isNegative) - rayOrigin ) / rayNormalizedDirection;
|
||||
b3Vector3 t_max = ( select(aabb.m_max, aabb.m_min, isNegative) - rayOrigin ) / rayNormalizedDirection;
|
||||
|
||||
b3Scalar t_min_final = 0.0f;
|
||||
b3Scalar t_max_final = b3Sqrt( b3Vector3_length2(rayTo - rayFrom) );
|
||||
b3Scalar t_max_final = rayLength;
|
||||
|
||||
//Must use fmin()/fmax(); if one of the parameters is NaN, then the parameter that is not NaN is returned.
|
||||
//Behavior of min()/max() with NaNs is undefined. (See OpenCL Specification 1.2 [6.12.2] and [6.12.4])
|
||||
//Since the innermost fmin()/fmax() is always not NaN, this should never return NaN
|
||||
//Since the innermost fmin()/fmax() is always not NaN, this should never return NaN.
|
||||
t_min_final = fmax( t_min.z, fmax(t_min.y, fmax(t_min.x, t_min_final)) );
|
||||
t_max_final = fmin( t_max.z, fmin(t_max.y, fmin(t_max.x, t_max_final)) );
|
||||
|
||||
return (t_min_final <= t_max_final);
|
||||
}
|
||||
**/
|
||||
|
||||
void rayPlanePairTest(b3Scalar rayStart, b3Scalar rayNormalizedDirection,
|
||||
b3Scalar planeMin, b3Scalar planeMax,
|
||||
b3Scalar* out_t_min, b3Scalar* out_t_max)
|
||||
{
|
||||
if(rayNormalizedDirection < 0.0f)
|
||||
{
|
||||
//max is closer, min is farther
|
||||
*out_t_min = (planeMax - rayStart) / rayNormalizedDirection;
|
||||
*out_t_max = (planeMin - rayStart) / rayNormalizedDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
//min is closer, max is farther
|
||||
*out_t_min = (planeMin - rayStart) / rayNormalizedDirection;
|
||||
*out_t_max = (planeMax - rayStart) / rayNormalizedDirection;
|
||||
}
|
||||
}
|
||||
int rayIntersectsAabb(b3Vector3 rayFrom, b3Vector3 rayTo, b3Vector3 rayNormalizedDirection, b3AabbCL aabb)
|
||||
{
|
||||
b3Scalar t_min_x, t_min_y, t_min_z;
|
||||
b3Scalar t_max_x, t_max_y, t_max_z;
|
||||
|
||||
rayPlanePairTest(rayFrom.x, rayNormalizedDirection.x, aabb.m_min.x, aabb.m_max.x, &t_min_x, &t_max_x);
|
||||
rayPlanePairTest(rayFrom.y, rayNormalizedDirection.y, aabb.m_min.y, aabb.m_max.y, &t_min_y, &t_max_y);
|
||||
rayPlanePairTest(rayFrom.z, rayNormalizedDirection.z, aabb.m_min.z, aabb.m_max.z, &t_min_z, &t_max_z);
|
||||
|
||||
b3Scalar t_min_final = 0.0f;
|
||||
b3Scalar t_max_final = b3Sqrt( b3Vector3_length2(rayTo - rayFrom) );
|
||||
|
||||
t_min_final = fmax( t_min_z, fmax(t_min_y, fmax(t_min_x, t_min_final)) );
|
||||
t_max_final = fmin( t_max_z, fmin(t_max_y, fmin(t_max_x, t_max_final)) );
|
||||
|
||||
return (t_min_final <= t_max_final);
|
||||
}
|
||||
|
||||
__kernel void plbvhRayTraverse(__global b3AabbCL* rigidAabbs,
|
||||
|
||||
@@ -484,10 +454,13 @@ __kernel void plbvhRayTraverse(__global b3AabbCL* rigidAabbs,
|
||||
int rayIndex = get_global_id(0);
|
||||
if(rayIndex >= numRays) return;
|
||||
|
||||
//
|
||||
b3Vector3 rayFrom = rays[rayIndex].m_from;
|
||||
b3Vector3 rayTo = rays[rayIndex].m_to;
|
||||
b3Vector3 rayNormalizedDirection = b3Vector3_normalize(rays[rayIndex].m_to - rays[rayIndex].m_from);
|
||||
|
||||
b3Vector3 rayNormalizedDirection = b3Vector3_normalize(rayTo - rayFrom);
|
||||
b3Scalar rayLength = b3Sqrt( b3Vector3_length2(rayTo - rayFrom) );
|
||||
|
||||
//
|
||||
int stack[B3_PLVBH_TRAVERSE_MAX_STACK_SIZE];
|
||||
|
||||
int stackSize = 1;
|
||||
@@ -505,7 +478,7 @@ __kernel void plbvhRayTraverse(__global b3AabbCL* rigidAabbs,
|
||||
int bvhRigidIndex = (isLeaf) ? mortonCodesAndAabbIndices[bvhNodeIndex].m_value : -1;
|
||||
|
||||
b3AabbCL bvhNodeAabb = (isLeaf) ? rigidAabbs[bvhRigidIndex] : internalNodeAabbs[bvhNodeIndex];
|
||||
if( rayIntersectsAabb(rayFrom, rayTo, rayNormalizedDirection, bvhNodeAabb) )
|
||||
if( rayIntersectsAabb(rayFrom, rayLength, rayNormalizedDirection, bvhNodeAabb) )
|
||||
{
|
||||
if(isLeaf)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user