平面与球面光线追踪之间的交点

问题描述 投票:1回答:1

我正在尝试为我的raytracer查找线和球之间的交点。到目前为止,我的工作正常,但是z交叉点的返回值为15,这对半径为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;
}
c++ linear-algebra raytracing
1个回答
0
投票

3Dave在其评论中已经指出了其中一个问题(操作员的优势)>

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

© www.soinside.com 2019 - 2024. All rights reserved.