射线追踪锥。判别式给出-ve值,因此没有交叉点

时间:2012-12-09 00:12:35

标签: c++ raytracing

我在C ++中实现光线跟踪。球体工作正常,气缸几乎正常工作。但是光线追踪器无法找到锥形的交叉点。经过计算,基于链接here,我创建了一个交集函数。这是我的锥类的findIntersection函数。变比为-1 *半径*半径/(高度*高度);

virtual double findIntersection(Ray ray){


                Vect ray_origin = ray.getRayOrigin();
                double ray_origin_x = ray_origin.getVectX();
                double ray_origin_y = ray_origin.getVectY();
                double ray_origin_z = ray_origin.getVectZ();

                Vect ray_direction = ray.getRayDirection();
                double ray_direction_x = ray_direction.getVectX();
                double ray_direction_y = ray_direction.getVectY();
                double ray_direction_z = ray_direction.getVectZ();

                Vect cone_center  = center;
                double cone_center_x = center.getVectX();
                double cone_center_y = center.getVectY();
                double cone_center_z = center.getVectZ();

        Vect diff(cone_center_x - ray_origin_x, cone_center_y - ray_origin_y + height, cone_center_z - ray_origin_z);

        //Derive the coefficients of the quadratic equation
        double a = pow(ray_direction_x, 2) + pow(ray_direction_y, 2) * ratio + pow(ray_direction_z, 2);
        double b = diff.getVectX() * ray_direction_x +  diff.getVectY() * ratio * ray_direction_y + diff.getVectZ() * ray_direction_z;

        double c = diff.getVectX() * diff.getVectX() + ratio * diff.getVectY() * diff.getVectY() + diff.getVectZ() * diff.getVectZ();

        double discriminant = b * b - a * c;
        cout << discriminant << "\n";

        if (discriminant > 0){
            //The ray intersects this cone. Find the lower root
            double root_1 = (-1 * b - discriminant)/2 - 0.000001;
                       if (root_1 > 0) {

                                // the first root is the smallest positive root
                                return root_1;
                        }
                        else {
                                // the second root is the smallest positive root
                                double root_2 = ((sqrt(discriminant) - b)/2) - 0.000001;
                                return root_2;
                        }
                }
                else {
                        // the ray missed the cone
                        return -1;

        }

    }

问题在于变量判别式的计算。它们是负面的,因此没有返回交叉点。此程序中的交点函数返回从光线原点到交点的距离。

请有人看看锥体计算并告诉我是否做错了。

此致 莫伊拉

1 个答案:

答案 0 :(得分:0)

我无法关注该链接,但您的“判别”看起来不对。

假设ratio(无论是什么)是1.然后我们

a = ray 2
b = 差异。射线
c = 差异 2

判别式= b * b - a * c =(差异射线 2 - diff 2 射线 2

第一个词最多为| diff || ray | (当射线差异是平行的)时,判别式最多为零。