0
101 Jul 28, 2003 at 20:41
float intersectRaySphere(Ray ray,Sphere sphere) {
Vec dst = ray.o - sphere.o;
Real B = dot(dst,ray.d);
Real C = dot(dst,dst) - sphere.r2;
Real D = B*B - C;
return D > 0 ? - B - sqrt(D) : std::numeric_limits<float>::infinity();
}


i bet there is a typo:D

#### 8 Replies

0
101 Jul 28, 2003 at 20:46

btw, an ARB_fragment_program version of this function is actually used in this pic

here’s the ARB_fragment_program implementing a ray-sphere test

!!ARBfp1.0 #rayTracer Sphere1
TEMP origin;
TEMP direction;
PARAM sphere = { 0.0,0.0,2.0,1.0 };
TEX origin, fragment.texcoord[0], texture[0], RECT;
TEX direction, fragment.texcoord[0], texture[1], RECT;
# intersectRaySphere
TEMP distance;
SUB distance,origin,sphere;
TEMP params;
DP3 params.x,distance,direction;
DP3 params.y,distance,distance;
SUB params.y,params.y,sphere.w;
KIL params.z;
RSQ params.z,params.z;
RCP params.z,params.z;
TEMP point;
TEMP normal;
SUB normal,point,sphere;
DP3 normal.w,normal,normal;
RSQ normal.w,normal.w;
MUL normal,normal,normal.w;
TEMP diffuse;
DP3 diffuse,normal,state.light[0].position;
PARAM color = { 1.0,0.5,0.25,0 };
MUL result.depth,params.x,0.125;
MUL result.color,color,diffuse;
END

0
158 Jul 28, 2003 at 20:49

wow, very nice piece of code. very useful.

0
101 Jul 28, 2003 at 20:54

Nice picture. Thank for the Code!

0
101 Jul 28, 2003 at 20:55

just be warned, it does only hit the outer hull (or so.., the near one:D)..

call it glEnable(GL_SPHERE_INSIDE_CULLING_RT); :D

0
101 Jul 28, 2003 at 21:59

wow… it’s so majestic.

[star wars “the force” theme solo on horn plays in my head]

:yes:

0
101 Jul 28, 2003 at 22:04

hehe, thanks..

the death star never looked that round, did it? :D

0
101 Jul 29, 2003 at 17:11

could you resize the pic down? it’s painfully large :D.

however, you do appreciate it’s smoothness at this size… :)

0
101 Jul 29, 2003 at 17:14

i could post the software version.. runs at the same speed as the hw version, but on 320x240 :D