3
$\begingroup$

Im writting a raytracer, for which I want to be able to test intersections with implicit surfaces (in my case spheres). Without applying any transformation it works great, but I would like to upgrade it to test intersections against non-uniform scaled spheres. For such purpose, I have computed the object's local to world matrix, as well as the world to local matrix.

During the intersection test, I first transform the ray origin and direction to the object local space:

    Vector o = ray.getOrigin();
    Vector l = ray.getDirection();

#ifdef _RT_TRANSFORM_RAY_TO_LOCAL_SPACE
    o = worldToLocalMatrix * o;
    l = worldToLocalMatrix * Vector(l.x, l.y, l.z, 0.0f);
    l = l.Normalize();
#endif

I perform the test, compute the intersection point in object's local space and then transform it back to world space:

Vector hitPoint(o + (l * distance));
        Vector hitNormal((hitPoint - center) / radius);
        hitNormal = hitNormal.Normalize();
#ifdef _RT_TRANSFORM_RAY_TO_LOCAL_SPACE
        hitPoint = localToWorldMatrix * hitPoint;
        hitNormal = localToWorldMatrix * Vector(hitNormal.x, hitNormal.y, hitNormal.z, 0.0f);
#endif
        hitNormal = hitNormal.Normalize();

If I run the raytracer, it works great: enter image description here

However, from some points of view (not all), I obtain wrong renders from the dielectric sphere (center in the above image) and the perfect specular (right in the above image), as well as one of the cubes:

enter image description here

If I just apply the affine transformations to the object's vertices and normals during start up(in the case of the sphere, I only apply translation since its an implicit surface), the render works flawlessly, so my guess its that the problem comes from the transformations applied to the ray.

To compute the local to world matrix: Model = rotation * translation * scale

To compute the world to local matrix: InvModel = inverse(scale) * inverse(translation) * inverse(rotation)

I would like to know if I'm applying the transformation to the ray as well as to the output hit properly

$\endgroup$
6
  • $\begingroup$ What's your question? It's unlikely that anyone else can debug your problem for you without a MCVE. $\endgroup$
    – Dan Hulme
    Commented Mar 17, 2018 at 14:13
  • $\begingroup$ @DanHulme I would like to know if the way im applying the transformation to the ray, as well as the hitpoint/normal is correct. $\endgroup$
    – Nadir
    Commented Mar 17, 2018 at 14:21
  • 1
    $\begingroup$ Probably not the only problem here but your normal transform is wrong: computergraphics.stackexchange.com/questions/1502 $\endgroup$
    – Olivier
    Commented Mar 17, 2018 at 15:46
  • $\begingroup$ I tried using the transpose of the inverse for the normal, but it created black areas where it shouldn't, and the refraction was inverted. Since the normal is computed from sphere center to hitpoint, I also tried to compute it after transforming the hitpoint and sphere center to world space, so I could avoid the normal transformation, but that didn't work either $\endgroup$
    – Nadir
    Commented Mar 17, 2018 at 16:12
  • $\begingroup$ Nevermind, I just figure it out. It was a problem related to the camera updating when moving, which wasnt computing the Up vector properly, and was causing the ray to be perturbed $\endgroup$
    – Nadir
    Commented Mar 17, 2018 at 16:18

1 Answer 1

1
$\begingroup$

I figured out the problem. It was not related to the ray / point transformation itself, but to a problem with the camera update after moving it (Up vector wasn't being computed properly).

I leave the answer in case someone have similar issues.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.