enable the GPU ray test in BasicGpuDemo, only for spheres at the moment
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
|
||||
#define SHAPE_CONVEX_HULL 3
|
||||
#define SHAPE_PLANE 4
|
||||
#define SHAPE_CONCAVE_TRIMESH 5
|
||||
#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
|
||||
#define SHAPE_SPHERE 7
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float4 m_from;
|
||||
@@ -30,67 +37,103 @@ typedef struct
|
||||
|
||||
typedef struct Collidable
|
||||
{
|
||||
int m_unused1;
|
||||
int m_unused2;
|
||||
union {
|
||||
int m_numChildShapes;
|
||||
int m_bvhIndex;
|
||||
};
|
||||
float m_radius;
|
||||
int m_shapeType;
|
||||
int m_shapeIndex;
|
||||
} Collidable;
|
||||
|
||||
bool sphere_intersect(float4 spherePos, float radius, float4 rayFrom, float4 rayTo)
|
||||
|
||||
|
||||
bool sphere_intersect(float4 spherePos, float radius, float4 rayFrom, float4 rayTo, float* hitFraction)
|
||||
{
|
||||
// rs = ray.org - sphere.center
|
||||
float4 rs = rayFrom - spherePos;
|
||||
rs.w = 0.f;
|
||||
float4 rayDir = (rayTo-rayFrom);
|
||||
float4 rs = rayFrom - spherePos;
|
||||
rs.w = 0.f;
|
||||
float4 rayDir = rayTo-rayFrom;
|
||||
rayDir.w = 0.f;
|
||||
rayDir = normalize(rayDir);
|
||||
float A = dot(rayDir,rayDir);
|
||||
float B = dot(rs, rayDir);
|
||||
float C = dot(rs, rs) - (radius * radius);
|
||||
|
||||
float D = B * B - A*C;
|
||||
|
||||
float B = dot(rs, rayDir);
|
||||
float C = dot(rs, rs) - (radius * radius);
|
||||
float D = B * B - C;
|
||||
if (D > 0.0)
|
||||
{
|
||||
float t = (-B - sqrt(D))/A;
|
||||
|
||||
if (D > 0.0)
|
||||
{
|
||||
float t = -B - sqrt(D);
|
||||
if ( (t > 0.0))// && (t < isect.t) )
|
||||
if ( (t >= 0.0f) && (t < (*hitFraction)) )
|
||||
{
|
||||
return true;//isect.t = t;
|
||||
}
|
||||
*hitFraction = t;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
float4 setInterpolate3(float4 from, float4 to, float t)
|
||||
{
|
||||
float s = 1.0f - t;
|
||||
float4 result;
|
||||
result = s * from + t * to;
|
||||
result.w = 0.f;
|
||||
return result;
|
||||
}
|
||||
|
||||
__kernel void rayCastKernel(
|
||||
int numRays,
|
||||
const __global b3RayInfo* rays,
|
||||
__global b3RayHit* hits,
|
||||
__global b3RayHit* hitResults,
|
||||
const int numBodies,
|
||||
__global Body* bodies,
|
||||
__global Collidable* collidables)
|
||||
{
|
||||
|
||||
bool hit=false;
|
||||
|
||||
int i = get_global_id(0);
|
||||
if (i<numRays)
|
||||
{
|
||||
hits[i].m_hitFraction = 1.f;
|
||||
hitResults[i].m_hitFraction = 1.f;
|
||||
|
||||
float4 rayFrom = rays[i].m_from;
|
||||
float4 rayTo = rays[i].m_to;
|
||||
float hitFraction = 1.f;
|
||||
int hitBodyIndex= -1;
|
||||
|
||||
int cachedCollidableIndex = -1;
|
||||
Collidable cachedCollidable;
|
||||
|
||||
for (int b=0;b<numBodies;b++)
|
||||
{
|
||||
|
||||
float4 pos = bodies[b].m_pos;
|
||||
// float4 orn = bodies[b].m_quat;
|
||||
if (cachedCollidableIndex !=bodies[b].m_collidableIdx)
|
||||
{
|
||||
cachedCollidableIndex = bodies[b].m_collidableIdx;
|
||||
cachedCollidable = collidables[cachedCollidableIndex];
|
||||
}
|
||||
|
||||
float radius = 1.f;
|
||||
|
||||
if (sphere_intersect(pos, radius, rayFrom, rayTo))
|
||||
hit = true;
|
||||
if (cachedCollidable.m_shapeType == SHAPE_SPHERE)
|
||||
{
|
||||
float radius = cachedCollidable.m_radius;
|
||||
|
||||
if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))
|
||||
{
|
||||
hitBodyIndex = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hitBodyIndex>=0)
|
||||
{
|
||||
hitResults[i].m_hitFraction = hitFraction;
|
||||
hitResults[i].m_hitPoint = setInterpolate3(rayFrom, rayTo,hitFraction);
|
||||
float4 hitNormal = (float4) (hitResults[i].m_hitPoint-bodies[hitBodyIndex].m_pos);
|
||||
hitResults[i].m_hitNormal = normalize(hitNormal);
|
||||
hitResults[i].m_hitResult0 = hitBodyIndex;
|
||||
}
|
||||
if (hit)
|
||||
hits[i].m_hitFraction = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user