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();

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

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();
        hitPoint = localToWorldMatrix * hitPoint;
        hitNormal = localToWorldMatrix * Vector(hitNormal.x, hitNormal.y, hitNormal.z, 0.0f);
        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

  • What's your question? It's unlikely that anyone else can debug your problem for you without a MCVE. – Dan Hulme Mar 17 at 14:13
  • @DanHulme I would like to know if the way im applying the transformation to the ray, as well as the hitpoint/normal is correct. – Nadir Mar 17 at 14:21
  • 1
    Probably not the only problem here but your normal transform is wrong: – Olivier Mar 17 at 15:46
  • 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 – Nadir Mar 17 at 16:12
  • 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 – Nadir Mar 17 at 16:18

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.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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