【问题标题】:intersection between plane and sphere raytracing平面和球体光线追踪的交集
【发布时间】:2020-04-13 03:59:15
【问题描述】:

我试图为我的光线追踪器找到一条线和一个球体之间的交点。到目前为止我所做的工作,但是返回 15 的 z 交点,这对于半径为 1 的球体不利。我做错了什么。 new_origin 是射线与球体的交点。 new_direction 是那个路口的法线。显然new_origin 计算错误。

origindirection 是射线(线)的原点和方向。

我的代码:

bool Sphere::intersection(const glm::vec3 &origin, const glm::vec3 &direction, glm::vec3 &new_origin, glm::vec3 &new_direction)
{
    //
    // See this for explantion of the formula: https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
    //
    glm::vec3 trans_origin = origin - this->origin;
    float a = glm::dot(direction, direction);
    float b = 2 * glm::dot(trans_origin, direction);
    float c = glm::dot(trans_origin, trans_origin) - this->radius * this->radius;

    float discriminant = b * b - 4 * a * c;
    if (discriminant < 0.f) return false;

    float depth = (-b + sqrtf(discriminant)) / 2 * a;
    float t = (-b - sqrtf(discriminant)) / 2 * a;

    if(t < depth) {
        depth = t;
    }
    new_origin = origin + depth * direction;
    new_direction = glm::normalize(trans_origin + depth * direction);

    return true;
}

【问题讨论】:

  • (-b + sqrtf(discriminant)) / 2 * a 不正确。应该是(-b + sqrtf(discriminant)) / (2 * a)

标签: c++ linear-algebra raytracing


【解决方案1】:

3Dave 在他们的评论中已经指出了其中一个问题(运算符优先级)

(-b + sqrtf(discriminant)) / 2 * a 不正确。应该是(-b + sqrtf(discriminant)) / (2 * a)

The other comes later, when the lesser intersection is chosen.

if (t < depth) {
    depth = t;
}

鉴于射线有一个原点和一个方向,即使你找到两个交点,球体也可能在相反的方向上,或者射线的原点可能在球体内。

您需要的是低正解决方案。

另一个可能的问题是关于new_direction,但我并不完全清楚你在考虑哪个“正常”。

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2019-11-29
    • 2019-11-29
    • 2018-05-31
    • 1970-01-01
    • 2014-05-09
    相关资源
    最近更新 更多