Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A few questions regarding the raytracing algorithm

0 views
Skip to first unread message

Sidney Richards

unread,
Jun 12, 2001, 12:39:37 PM6/12/01
to
G'day all,

I'm doing a maths project on raytracing (not programming at the moment, but
that's next). Since I'm a hobby C++ programmer, I got more from reading
sources then say, for instance, the Foley & Van Dam book. Another added
problem is I'm going to school in a non english speaking country, therefor
the math terms are a bit confusing as well. Anyway, I'll get to the point.
From several sources I found the equation to calculate T in Ray = Start + t
* Direction is (for spheres):

(x1^2 + y1^2 + z1^2 )*T^2 + (x1 * (x0 - a) + y1 * (y0 - b) + z1 * (z0 - c))
* 2T + ((x0 - a)^2 + (y0 - b)^2 + (z0 - c)^2 - r^2 ) = 0

Where:
x0, y0, z0 = coordinates of the starting point of the ray
x1, y1, z1 = the coordinates of the direction
a, b, c = the coordinates of the center of a sphere
r = radius of sphere

This seemed to be correct when compared to the internet sites. So I decided
to put it to the test and calculate the intersection at the first pixel in
an image (1, 1) with the following (very arbitrary) variables:

x0, y0, z0 = (10, 5, 2) -> I assume this is the point from which the ray
originates. I assume you could use any kind of values, though I am a bit
unsure.
x1, y1, z1 = (1, 1, 1) -> I assume this is where you fill in the coordinates
of the current pixel on the image. I leave the z value constant through out
the process (I again assume), and I use 1,1 as the first pixel in an image
a, b, c = (50, 60, 40) with radius 10-> Just chose a big sphere, quite far
away from the viewpoint so I wouldn't "miss it" so to speak.

Now, when I fill this data in I would expect there to be no intersection
(after all, x = 50 for the sphere centre is far aways from x = 1 for the
direction). Anyway, this is what I got:

(1^2 + 1^2 + 1^2 )*T^2 + (1 * (10 - 50) + 1 * (5 - 60) + 1 * (2 - 40)) * 2T
+ ((10 - 50)^2 + (5 - 60)^2 + (2 - 40)^2 - 10^2 ) = 0

Yielding the quadratic formula:

3T^2 - 266T + 5969 = 0

Discriminant = B^2 - 4 * A * C = (-266)^2 - 4 * 3 * 5969 = -872

A negative discriminant means there are no real solutions for T and the ray
doesn't intersect at point (1, 1).

Now for the questions.

1. This is the way I did it to test if it worked. First of all, the numbers
I've been using seem rather large. Should I be scaling them down?
2. Secondly, as I said, the variables are very arbitrary. Does my starting
point have to be between certain values? Does the Z axis of the direction
vector need to be a certain value? etc. etc.
3. If T is indeed positive, and I calculate X with X = Xstart + T * Xdir .
Will the point be a point *on* the sphere? How can I calculate the distance
from that point to the center of the sphere?
4. I've seen some people use "normals", and others not. I know how to
calculate a normal, but could anyone explain to me what the use is, and most
importantly, should I be using it above?

Any help would really really be appreciated, especially if you could explain
to me why I've been doing stuff wrong, or why something has to be done,
since I'm in a bit of a jam here.

Cheers,

Sidney Richards


Tim

unread,
Jun 12, 2001, 4:20:17 PM6/12/01
to
sidney....@xs4all.nl (Sidney Richards) wrote in
<9g5g8c$au4$1...@news1.xs4all.nl>:

Try an origin at [1 -2 -1], a direction of [1 2 4], and a sphere at [3 0 5]
with a radius of 3. You should get t=3.744 (unless I made a typo). The
directional vector should almost always be normalized (scaled so it's distance
is 1). In this case, you would get [0.218 0.436 0.873]. I copied this example
from 'An Introduction to Raytracing' edited by Andrew S. Glassner. This would
be a great book for you if you like to learn things this way. It has a few
examples where it will give you an equation and show an example calculated by
hand.

I suggest ditching that equation entirely. It's not very efficient. There's a
pretty good description of a better one in:
http://www.ddj.com/ftp/1994/1994.07/jul94_3.zip

The above archive has fairly clean code (in C, ick), and a nice word document
that's easy to follow. I used it to write my first raytracer.

>3. If T is indeed positive, and I calculate X with X = Xstart + T * Xdir
>. Will the point be a point *on* the sphere? How can I calculate the
>distance from that point to the center of the sphere?
>4. I've seen some people use "normals", and others not. I know how to
>calculate a normal, but could anyone explain to me what the use is, and
>most importantly, should I be using it above?

Yes, X will be a point on the sphere. I should mention that the floating point
numbers in computers are not perfect. You should always subtract a tiny
constant from the distance to make sure you aren't just inside the sphere.
Otherwise you'll hit the sphere again while doing lighting calculations and
reflections. If you're doing refractions, you should do the opposite, and add
the small constant. I use 1e-8 with a 64 bit double.

You don't have to calculate the distance from the point to the center of the
sphere. You already know that, it's the radius. You can get the normal for
the outside of the sphere by just subtracting the point at the center of the
sphere from X. Once again, it's a directional vector and should be normalized.

Normals are required to produce an image. They're needed to calculate the
lighting, and to calculate things like reflections and refractions. They
indicate the direction that the surface is facing at that point. You won't be
needing it to calculate the intersections above however.

>Any help would really really be appreciated, especially if you could
>explain to me why I've been doing stuff wrong, or why something has to
>be done, since I'm in a bit of a jam here.
>
>Cheers,
>
>Sidney Richards

If you are still having trouble, you should start off in 2D. It's much easier
to do by hand, and more simple to draw and visualize. Once you get the hang of
it, going to 3D is easy.

- Tim

Warp

unread,
Jun 13, 2001, 5:46:51 AM6/13/01
to
Sidney Richards <sidney....@xs4all.nl> wrote:
: 1. This is the way I did it to test if it worked. First of all, the numbers

: I've been using seem rather large. Should I be scaling them down?

There's no need for that. The only place where you have to scale the
numbers is when you are calculating a unit vector; in a simple raytracing
algorithm this is only needed when calculating lighting.

: 2. Secondly, as I said, the variables are very arbitrary. Does my starting


: point have to be between certain values? Does the Z axis of the direction
: vector need to be a certain value? etc. etc.

There usually isn't any need for limiting those values other than taking
care of not overflowing or underflowing the numbers. Using the 'double'
data type (which is usually 64 bits long) this is not likely to happen
(your scene would have to be extremely large or small).
Of course there might be some places where the accuracy of floating point
numbers may cause artifacts, but I have seldom found any (in fact, I have
recently written a small sphere raytracer just for fun and I didn't take
into account any rounding or accuracy problems and it worked like a charm
nevertheless).

: 3. If T is indeed positive, and I calculate X with X = Xstart + T * Xdir .


: Will the point be a point *on* the sphere? How can I calculate the distance
: from that point to the center of the sphere?

The point is on the sphere inside the accuracy limits of the floating point
numbers. The distance from the center of the sphere is the radius, so you
don't have to calculate it.
Note that if the ray hits the sphere, you get two values of T. You usually
want the closest intersection, thus you should use the smaller non-negative T.
Also when you are raytracing more than one sphere, you can get a T value
for several spheres (if the same ray hits more than one sphere). Here you
also usually want the closest intersection, so you just take the smallest T
(and the information about which sphere it hit; this is necessary in order
to calculate the proper normal vector).

: 4. I've seen some people use "normals", and others not. I know how to


: calculate a normal, but could anyone explain to me what the use is, and most
: importantly, should I be using it above?

Normals are essential in order to calculate lighting, reflection and
refraction. Without normals you just get uniformly colored silhouettes.
I can profundize in more details about different types of lighting and
how reflection is calculated if you want.

--
#macro N(D,I)#if(I<6)cylinder{M()#local D[I]=div(D[I],104);M().5,2pigment{
rgb M()}}N(D,(D[I]>99?I:I+1))#end#end#macro M()<mod(D[I],13)-6,mod(div(D[I
],13),8)-3,10>#end blob{N(array[6]{11117333955,
7382340,3358,3900569407,970,4254934330},0)}// - Warp -

Markus Lüdin

unread,
Jun 13, 2001, 9:25:07 AM6/13/01
to
Hello

Ray @ 10,5,2 Direction 1,1,1
Sphere @ 50,60,40 Radius 10

Yes my feeling says there is no intersection....
try Sphere @50,50,50 and i guess you have a interection....



> Now for the questions.
>
> 1. This is the way I did it to test if it worked. First of all, the numbers
> I've been using seem rather large. Should I be scaling them down?

doesen't matter...

> 2. Secondly, as I said, the variables are very arbitrary. Does my starting
> point have to be between certain values? Does the Z axis of the direction
> vector need to be a certain value? etc. etc.

nope

> 3. If T is indeed positive, and I calculate X with X = Xstart + T * Xdir .
> Will the point be a point *on* the sphere? How can I calculate the distance
> from that point to the center of the sphere?

yet's that's "on" the
sphere...distance=SquareRoot((P.x-S.x)^2+(P.y-S.y)^2+(P.z-S.z)^2)
P is your point on the Sphere and s the position of the sphere
(50,60,40)

> 4. I've seen some people use "normals", and others not. I know how to
> calculate a normal, but could anyone explain to me what the use is, and most
> importantly, should I be using it above?

you will need the normal to calculate reflection at this point and for
lightning....

> Any help would really really be appreciated, especially if you could explain
> to me why I've been doing stuff wrong, or why something has to be done,
> since I'm in a bit of a jam here.

i did not see what should be wrong...just go on :)

regards
Markus

Warp

unread,
Jun 13, 2001, 11:08:33 AM6/13/01
to
Markus Lüdin <markus...@siemens.ch> wrote:
: distance=SquareRoot((P.x-S.x)^2+(P.y-S.y)^2+(P.z-S.z)^2)

Isn't that equal to the radius?

Markus Lüdin

unread,
Jun 14, 2001, 3:15:51 AM6/14/01
to

Warp wrote:
>
> Markus Lüdin <markus...@siemens.ch> wrote:
> : distance=SquareRoot((P.x-S.x)^2+(P.y-S.y)^2+(P.z-S.z)^2)
>
> Isn't that equal to the radius?

*pulling my hear* of course :)

0 new messages