1
\$\begingroup\$

Not sure if I should post this here or on the Math forum, but I will try my luck.

Some background. I made my custom raytracer. The processing happens purely on the CPU for now, no GPU interaction whatsoever. It supports basic Phong shading and X levels of reflections.

Ray Tracer

I started fiddling with some ideas now and I realized that I need a way to display debug lines so that I could debug what I am doing, at least to some extent.

I thought of this solution: Debug line is a constrained plane, which means that it is basically a rectangle, with pixels having their normal always facing the camera. The line is defined by a start point, magnitude, direction, and thickness. Based on these properties I should be able to calculate everything I need to get it displayed.

Below is some pseudocode for checking if my ray is intersecting this debug line.

Defined in the line initialization are:

  • startPosition (where the line starts)
  • magnitude (lenght of the line)
  • direction (line direction)
  • thickness (thickness of the line)

Code:

function isIntersecting(_originRay, _rayOrigin)

    ray = _originRay.Normalize(); // Ray coming from the camera
    spreadVector = direction.Cross(ray).Normalize(); // Vector showing the line width direction
    lineNormal = ray * (-1); // Debug line pixel always faces the camera directly

    // Plane intersection
    denominator = ray.Dot(lineNormal);
    if (denominator > 0) // Plane not visible 
        return false;

    hitDistance = (startPosition - _rayOrigin).Dot(lineNormal) / denominator;
    hitPosition = _rayOrigin + (ray * hitDistance); // Get the intersection point in 3D space

    widthVector = spreadVector  * thickness;
    hitPositionTranslated = hitPosition - startPosition;

    if (hitPositionTranslated.Dot(widthVector) > widthVector.Length() || hitPositionTranslated.Dot(widthVector) < 0) // Ray hitting the plane outside the constrained area
        return false;

    // Code for checking height here - Not putting it cause width is not working at first place

    return true;

I get this when using pretty great width (should be ~100px), but it is thin - looks like the algorithm is not constraining the width correctly:

Debug Line

I am obviously doing something wrong here. Any kind of help would be much appreciated!

\$\endgroup\$
1
\$\begingroup\$

Found the problem. The dot product is the length of the projected vector multiplied by the length of the vector we are projecting into. That means that I needed to divide the dot product result by the length of the width vector to actually have a valid check:

hitPositionTranslated.Dot(widthVector) / widthVector.Length()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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