Ray->Sphere intersection?

Blitz3D Forums/Blitz3D Programming/Ray->Sphere intersection?

bytecode77(Posted 2007) [#1]
hi there...
i'm thinking of making a raytracer again. this time, a better one. so i need a triangle-sphere intersection...

where is one?
here it is: http://www.blitzbasic.com/codearcs/codearcs.php?code=947

well then, i implemented this one pretty well, but i have 2 problems:

1. ==== backward intersection ====
the line which intersects the sphere, hits the sphere even when the sphere is "behind" the line. it looks like "backward projection" then...
2. ==== the reflectional ray ====
what i need to calculate is a ray that is reflected by the sphere. this reflectional ray starts at the intersection point between the line and the sphere and the direction it goes, is what i need to calculate.

if you would be so good and help me :) ;)
i would realy appreciate any help comming here

edit: my try
(original code by elias_t)



bytecode77(Posted 2007) [#2]
ummh. seems like i'm pretty much on my own right now, huh?
still waiting for someone who helps me...


Wayne(Posted 2007) [#3]
What about this: http://www.blitzbasic.com/codearcs/codearcs.php?code=793


bytecode77(Posted 2007) [#4]
thats useless, because it intersects a polygon, not a sphere. the only matter in that code you posted is the speed


MCP(Posted 2007) [#5]
Hi Devils Child,
I've written a function to reflect a ray below. To use it you need to perform the following steps:-

1 - Create a Unit Vector called Ray from Line start x,y,z to sphere intersection point ix,iy,iz:-

    global Ray.vector=new vector
    Ray\x=ix-x : Ray\y=iy-y : Ray\z=iz-z
    UnitVector(Ray)


2 - Create a Normal Unit Vector from the sphere center sx,sy,sz to the intersection point ix,iy,iz:-

   global Normal.vector=new vector
   Normal\x=ix-sx : Normal\y=iy-sy : Normal\z=iz-sz
   UnitVector(Normal)


3 - Now you can present these to the function ReflectRay to get the new ray direction:-

  newRay.vector=ReflectRay(Normal,Ray)


Hope that helps!

Cheers,

Roy




Wayne(Posted 2007) [#6]
Solving the above for u =

(x3 - x1)(x2 - x1) + (y3 - y1)(y2 - y1) + (z3 - z1)(z2 - z1)
----------------------------------------------------------------------
(x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1) + (z2 - z1)(z2 - z1)

If u is not between 0 and 1 then the closest point is not between P1 and P2

Given u, the intersection point can be found, it must also be less than the radius r. If these two tests succeed then the earlier calculation of the actual intersection point can be applied.


bytecode77(Posted 2007) [#7]
robFerriby: thank you for this explaining reflection code. it explains the reflection system to me pretty well (well it's 3d code but i'll be able to do it in 2d)

wayne: thank you for this one!

besides i have asked in an other forum since i got no answer for a whole day, and they replied pretty fast and i got it working


you will hear from me when my raytracer is finished.
i know the screenie looks pretty boring. but lighting and shadowing will come!


MCP(Posted 2007) [#8]
Glad I could help. The results look pretty good so far, even at this early stage. I'll be looking forward to seeing the final version. :)

Cheers,

Roy


bytecode77(Posted 2007) [#9]
i think you wanna see my current one, too ;)



there are soft shadows! there are two methods to render soft shadows. the combination of both is looking very good. and it took me 18 seconds to render this.

i still need to work on the shading of the spheres, they doesnt seem to receive any light at all.

btw: the ground is NOT a plane; it's a giant sphere ;)


ShadowTurtle(Posted 2007) [#10]
Do you not use clusters for raytracing? With this you will have the same quality but even with less calculations ( = faster).

See gamedev.com or any of the 3d-expert coding community for more informations. I think you increased since ~2004, so you can now realy learn from the stuff.


bytecode77(Posted 2007) [#11]
hm clusters? what is that?


MCP(Posted 2007) [#12]
The shadowing looks pretty cool. The spheres must be receiving some light though otherwise you'd see nothing but blackness. I noticed in your first pic that the spheres had some reflective qualities. Did you take that out?
Looking forward to the next screen shot ;)

Cheers,

Roy


bytecode77(Posted 2007) [#13]
i currently commented the reflection code out. i need to concentrate on the shadowing first, then i continue on reflection/refraction.


also i use a script engine for the render, so you can script your scenes such as in povRay...


edit: hm. i would really appreciate if anyone tells me where the bug is, that the spheres aren't lighted...


MCP(Posted 2007) [#14]
Remember that unit normal that you computed from the sphere center to the intersection point?
Well you'll need to perform a second unit normal which points from the intersection point to the position of the light source. Using these two normals perform a DotProduct test which should yeld a result between -1 (complete darkness) and 1 (fully lit).
Add 1 to the result and divide by 2 to give a range between 0 and 1. Now you can multiply your sphere surface RGB values against it to arrive at the correct color on the sphere surface for that particular ray position.

Cheers,

Roy


bytecode77(Posted 2007) [#15]
wow. i got that fixed before i read that!
will be interesting to know if i want to have shaded balls



edit: sorry, Inarie for using your scene :) its just brilliant!!


MCP(Posted 2007) [#16]
Great job! It's looking very good now :)


bytecode77(Posted 2007) [#17]


this is a skylight with 50x sampling

a skylight is nothing more than a light which is very big...


big10p(Posted 2007) [#18]
Looking good.

You need ray to box intersection?

This is a long shot, but I did this demo ages ago (messy), which you may be able to expand upon and use? It does ray-to-box intersection, but only ray to centre of box. Box can have any rotation and doesn't use picks.



bytecode77(Posted 2007) [#19]
thank you for this demonstration but i can not use tform-points because this is all in 2D. also i need to be able to pick from any point to any direction, and not to the centre. but thanks for your attention and your help attempt :)


big10p(Posted 2007) [#20]
No problem. I didn't think it would be much use, but I thought I'd post it just in case.

Sorry, I know nothing about ray tracers. :)


sswift(Posted 2007) [#21]

Finding the closest point on a line to another point.
-----------------------------------------------------
(x1, y1, z1), (x2, y2, z2) = ends of line segment
(x3, y3, y3) = point


u =

(x3 - x1)(x2 - x1) + (y3 - y1)(y2 - y1) + (z3 - z1)(z2 - z1)
-----------------------------------------------------------
(x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1) + (z2 - z1)(z2 - z1)



u = the location between the two end points of the line which is closest to the point.

If u is not between 0 and 1 then the closest point is not between P1 and P2




Find that point on the line closest to the center of the sphere, then calculate the squared distance of it from the center of the sphere. If the squared distance of that is less than the sqaured radius of the sphere, then the line intersects the sphere.


If you need to know the first point where the ray intersects the sphere though, I can't help you there. There's probably some way to calculate that based on the closest point, and the distance of that closest point to the center of the sphere, because the entry and exit points are probably equal distances from that closest point on the line, and those distances are probably related to how deeply the line is embedded in the sphere.


bytecode77(Posted 2007) [#22]
sswift: i already have a sphere intersection routine, but i still need a cube intersection routine. lately i saw a plane routine which i will use soon.

but now i barely understand how sphere picking works!!
thank you for explination :)


edit:

implemented sphere shading. it works simple. the brightness multipler is the vector dot product between the surface normal and a normalised vector that points toward a light!