|
|
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
|
|