Skip to content

Instantly share code, notes, and snippets.

@awreece
Created October 31, 2012 02:18
Show Gist options
  • Save awreece/3984422 to your computer and use it in GitHub Desktop.
Save awreece/3984422 to your computer and use it in GitHub Desktop.
rtDeclareVariable( float4, sphere, , );
rtDeclareVariable( optix::Ray, ray, rtCurrentRay, );
rtDeclareVariable( float3, normal, attribute normal );
RT_PROGRAM void intersect_sphere( int prim_index ) {
float3 center = make_float3( sphere.x, sphere.y, sphere.z );
float radius = sphere.w;
float3 O = ray.origin - center;
float b = dot( O, ray.direction ); float c = dot( O, O ) - radius*radius; float disc = b*b - c;
if( disc > 0.0f ) {
float sdisc = sqrtf( disc );
float root1 = (-b - sdisc);
bool check_second = true;
if( rtPotentialIntersection( root1 ) ) {
shading_normal = geometric_normal = (O + root1*D) / radius;
if( rtReportIntersection( 0 ) ) check_second = false;
}
if( check_second ) {
float root2 = (-b + sdisc);
if( rtPotentialIntersection( root2 ) ) {
shading_normal = geometric_normal = (O + root2*D) / radius;
rtReportIntersection( 0 );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment