POV-Ray : Newsgroups : povray.off-topic : Ray tracers and transformations : Re: Ray tracers Server Time
10 Oct 2024 17:19:48 EDT (-0400)
  Re: Ray tracers  
From: Severi Salminen
Date: 13 Mar 2008 17:01:16
Message: <47d9a42c@news.povray.org>
Orchid XP v7 wrote:

>> As far as I know, the way POV-Ray does it (and probably most others)
>> is by creating a transformation matrix of all the individual
>> transformations, then inversing it, and using it to transform *rays*
>> before doing the intersection calculations.
> 
> I believe that is correct. (Transforming the object equation would be
> much harder...)

Ok, then I was on the right tracks. I only have to transform the ray
using conventional point and vector transformations. Thanks!

>> But don't trust me on this. I still haven't managed to solve the
>> ray-sphere intersection equation...

There is neat method also Pov-Ray uses. I used almost identical but
Pov's version had (IIRC) simpler form with 1-2 less arithmetic
operations. Here is my implementation:

"position" is the center of the sphere
"origin" and "direction" specify the ray
"nearestPointDistance" is the distance from origin to a point where the
ray is closest to the sphere
"halfCord2" is the square of...well...half of the cord the ray creates
inside the sphere :)

If you draw this on a paper it is very simple approach and quite fast,
too. It returns the distance to the closest intersection point.


double Mass::intersect(Vector origin, Vector direction) const
{
    Vector relPosition = position - origin;
    double radius2 = radius * radius;
    double nearestPointDistance = relPosition*direction;

    double halfChord2 = radius2 - relPosition*relPosition +
nearestPointDistance*nearestPointDistance;

    if(halfChord2 > 0.0)
    {
        double nearPoint = nearestPointDistance - sqrt(halfChord2);
        double farPoint = nearestPointDistance + sqrt(halfChord2);
        if(nearPoint > EPSILON)
            return nearPoint;
        else
            return farPoint;
    }
    else
        return -1.0;
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.