Jump to content


Quick'n'Dirty Ray Intersect Sphere


8 replies to this topic

#1 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 28 July 2003 - 08:41 PM

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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#2 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 28 July 2003 - 08:46 PM

btw, an ARB_fragment_program version of this function is actually used in Posted Image 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;
# quadricEquation 
TEMP params;
DP3 params.x,distance,direction;
DP3 params.y,distance,distance;
SUB params.y,params.y,sphere.w;
MAD params.z,params.x,params.x,-params.y;
KIL params.z;
RSQ params.z,params.z;
RCP params.z,params.z;
ADD params.x,-params.x,-params.z;
TEMP point;
MAD point,params.x,direction,origin;
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

davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#3 Dia

    DevMaster Staff

  • Administrators
  • 1120 posts

Posted 28 July 2003 - 08:49 PM

wow, very nice piece of code. very useful.

#4 Noor

    Senior Member

  • Members
  • PipPipPipPip
  • 503 posts

Posted 28 July 2003 - 08:54 PM

Nice picture. Thank for the Code!
"What ever happened to happily ever after?"

#5 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 28 July 2003 - 08:55 PM

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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#6 donBerto

    Senior Member

  • Members
  • PipPipPipPip
  • 369 posts

Posted 28 July 2003 - 09:59 PM

wow... it's so majestic.

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

:yes:
Imagine.

#7 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 28 July 2003 - 10:04 PM

hehe, thanks..

the death star never looked that round, did it? :D
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#8 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 29 July 2003 - 05:11 PM

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

however, you do appreciate it's smoothness at this size... :)
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.

#9 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 29 July 2003 - 05:14 PM

i could post the software version.. runs at the same speed as the hw version, but on 320x240 :D
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users