Simple Ray Tracing With Cylinder?

4.6k views Asked by At

I am completely new to ray tracing and am having a problem with displaying a cylinder. I have implemented the code to find a sphere and have been following the tutorial I found here: http://woo4.me/wootracer/cylinder-intersection/ to add a simple cylinder (infinite to start). I have the following code written but get a solid flat surface across the image no matter what coordinates I give the cylinder. Can anyone give me a hint as to where I'm going wrong? I've searched google for three days and searched through Stack Overflow and have tried multiple ways suggested in forums and tutorials to no avail so hopefully someone here can see where I'm going wrong?? Thanks in advance!!

    virtual double findIntersection(Ray ray){
    Vect rayOrigin = ray.getRayOrigin();
    double rayOriginX = rayOrigin.getVectX();
    double rayOriginY = rayOrigin.getVectY();
    double rayOriginZ = rayOrigin.getVectZ();

    Vect rayDirection = ray.getRayDirection();
    double rayDirectionX = rayDirection.getVectX();
    double rayDirectionY = rayDirection.getVectY();
    double rayDirectionZ = rayDirection.getVectZ();

    //a=d x2 + d y2     b = ex d x + e y d y    c = ex2 + e y2−1
    double a = rayDirectionX * rayDirectionX + rayDirectionY * rayDirectionY;
    double b = 2* rayOriginX * rayDirectionX + 2* rayOriginY * rayDirectionY;
    double c = rayOriginX * rayOriginX + rayOriginY * rayOriginY - 1;

    double discriminant = b*b - 4 * a*c;

    if (discriminant > 0){
        //ray intersects
        double root1 = ((-1 * b - sqrt(discriminant)) / 2*a - 0.0001);

        if (root1 > 0){
            return root1; //first root is the smallest positive value
        }
        else{
            double root2 = ((sqrt(discriminant) - b) / 2*a - 0.0001);
            return root2; //the second root is the smallest positive value
        }
    }
    else{
        //ray misses the Cylinder
        return -1;
    }
}
![enter image description here][1]};
1

There are 1 answers

0
Daniel A. Thompson On
  • In my primitives' ray-intersection methods, I generally return a structure that includes the intersection point, the surface normal, and the shape that was hit, among other things. Why are you returning the root?
  • In your code, you say that the first root is the smallest positive value. I think this is incorrect. I would run through some test cases with different values of a, b, and c to convince yourself one way or the other.
  • Also, if the discriminant is 0, there is a double root, and the ray does hit the cylinder.