光线跟踪圆柱体在旋转时会退化

时间:2016-06-11 17:05:55

标签: 3d intersection raytracing

我正在尝试开发一个简单的光线跟踪器并将球体,平面和锥体对齐,但是我面临的问题是我无法绕过头脑。 我为无限圆柱体尝试了几种不同的公式,并且当使用不是0或1且仅在一个轴上的旋转时,所有那些应该将旋转考虑在内的人使其“退化”...例如:

0,0,1有效,但是0,0,0.5给了我一个椭球体,0,1,1给了我一个双曲面......

这是我用来将光线与圆柱相交的代码。

enum e_bool test_intersect(double t[2], double *current_z)
{
    enum e_bool retvalue;

    retvalue = false;
    if ((t[0] > DOUBLE_ZERO)
    && (t[0] < *(current_z) || double_equal(*(current_z), t[0])))
    {
        *(current_z) = t[0];
        retvalue = true;
    }
    if (!double_equal(t[0], t[1])
    && (t[1] > DOUBLE_ZERO) 
    && (t[1] < *(current_z) || double_equal(*(current_z), t[1])))
    {
        *(current_z) = t[1];
        retvalue = true;
    }
    return (retvalue);
}

enum e_bool intersect_cylinder(t_primitive cp, t_ray r, double *current_z)
{
    t_vec3 eye = vec3_substract(r.origin, cp.position);
    double a = vec3_dot(r.direction, r.direction) - pow(vec3_dot(r.direction, cp.direction), 2);
    double b = 2 * (vec3_dot(r.direction, eye) - vec3_dot(r.direction, cp.direction) * vec3_dot(eye, cp.direction));
    double c = vec3_dot(eye, eye) - pow(vec3_dot(eye, cp.direction), 2) - cp.radius * cp.radius;
    double  t[2];
    double delta;
    delta = sqrt((b * b) - (4.0 * a * c));
    if (delta < 0)
        return (false);
    t[0] = (-b - (delta)) / (2.0 * a);
    t[1] = (-b + (delta)) / (2.0 * a);
    return (test_intersect(t, current_z));
}

Here is the cylinder with a rotation of 1, 0, 0

Here it is with a rotation of 1, 1, 0

我缺少什么,透视或等距光线投射的问题是相同的,所以它与交叉算法有关,但我找不到有什么问题......

1 个答案:

答案 0 :(得分:0)

在阅读这些页面后,我找到了解决我的问题的方法(gamedev帮了我很多):

http://mrl.nyu.edu/~dzorin/cg05/lecture12.pdf

http://www.gamedev.net/topic/467789-raycylinder-intersection/

我重新做了这样的交叉方程式并且它正常工作:

enum e_bool intersect_cylinder(t_primitive cp, t_ray r, double *current_z)
{
    t_vec3  pdp = vec3_substract(cp.direction, cp.position);
    t_vec3  eyexpdp = vec3_cross(vec3_substract(r.origin, cp.position), pdp);
    t_vec3  rdxpdp = vec3_cross(r.direction, pdp);
    float   a = vec3_dot(rdxpdp, rdxpdp);
    float   b = 2 * vec3_dot(rdxpdp, eyexpdp);
    float   c = vec3_dot(eyexpdp, eyexpdp) - (cp.radius * cp.radius * vec3_dot(pdp, pdp));
    double  t[2];
    double delta;
    delta = sqrt((b * b) - (4.0 * a * c));
    if (delta < 0)
        return (false);
    t[0] = (-b - (delta)) / (2.0 * a);
    t[1] = (-b + (delta)) / (2.0 * a);
    return (test_intersect(t, current_z));
}

现在我的常规是错的,但这不是一个很难解决的问题。