2

我正在编写一个简单的光线追踪器,为了保持简单,我决定在我的场景中只使用球体。我现在处于一个阶段,我只想确认我的光线是否正确地与场景中的球体相交,仅此而已。我创建了一个 Ray 和 Sphere 类,然后在我的主文件中创建了一个函数,该函数遍历每个像素以查看是否存在交叉点(相关代码将在下面发布)。问题是与球体的整个交叉点表现得相当奇怪。如果我创建一个中心为 (0, 0, -20) 且半径为 1 的球体,那么我只会得到一个交点,该交点始终位于我的图像的第一个像素(左上角)。一旦我到达 15 的半径,我会突然在左上角区域看到三个交叉点。

我怀疑我的射线球相交代码在这里可能有问题,但是通过查看它并通过网络查看更多信息,大多数解决方案都描述了我使用的相同方法,所以我认为它不应该(!)有问题这里。所以......我不确定我做错了什么,它可能是我的交集代码,也可能是其他导致问题的东西。我似乎无法找到它。给球体和光线赋值时,我想错了吗?下面是相关代码

球体类:

Sphere::Sphere(glm::vec3 center, float radius) 
: m_center(center), m_radius(radius), m_radiusSquared(radius*radius)
{
}

//Sphere-ray intersection. Equation: (P-C)^2 - R^2 = 0, P = o+t*d
//(P-C)^2 - R^2 => (o+t*d-C)^2-R^2 => o^2+(td)^2+C^2+2td(o-C)-2oC-R^2
//=> at^2+bt+c, a = d*d, b = 2d(o-C), c = (o-C)^2-R^2
//o = ray origin, d = ray direction, C = sphere center, R = sphere radius
bool Sphere::intersection(Ray& ray) const
{
    //Squared distance between ray origin and sphere center
    float squaredDist = glm::dot(ray.origin()-m_center, ray.origin()-m_center);

    //If the distance is less than the squared radius of the sphere...
    if(squaredDist <= m_radiusSquared)
    {
        //Point is in sphere, consider as no intersection existing
        //std::cout << "Point inside sphere..." << std::endl;
        return false;
    }

    //Will hold solution to quadratic equation
    float t0, t1;

    //Calculating the coefficients of the quadratic equation
    float a = glm::dot(ray.direction(),ray.direction()); // a = d*d
    float b = 2.0f*glm::dot(ray.direction(),ray.origin()-m_center); // b = 2d(o-C)
    float c = glm::dot(ray.origin()-m_center, ray.origin()-m_center) - m_radiusSquared; // c = (o-C)^2-R^2

    //Calculate discriminant
    float disc = (b*b)-(4.0f*a*c);

    if(disc < 0) //If discriminant is negative no intersection happens
    {
        //std::cout << "No intersection with sphere..." << std::endl;
        return false;
    }
    else //If discriminant is positive one or two intersections (two solutions) exists
    {
        float sqrt_disc = glm::sqrt(disc);
        t0 = (-b - sqrt_disc) / (2.0f * a);
        t1 = (-b + sqrt_disc) / (2.0f * a);
    }

    //If the second intersection has a negative value then the intersections
    //happen behind the ray origin which is not considered. Otherwise t0 is
    //the intersection to be considered
    if(t1<0)
    {
        //std::cout << "No intersection with sphere..." << std::endl;
        return false;
    }
    else
    {
        //std::cout << "Intersection with sphere..." << std::endl;
        return true;
    }
}

程序:

#include "Sphere.h"
#include "Ray.h"

void renderScene(const Sphere& s);

const int imageWidth = 400;
const int imageHeight = 400;

int main()
{
    //Create sphere with center in (0, 0, -20) and with radius 10
    Sphere testSphere(glm::vec3(0.0f, 0.0f, -20.0f), 10.0f);

    renderScene(testSphere);

    return 0;
}

//Shoots rays through each pixel and check if there's an intersection with
//a given sphere. If an intersection exists then the counter is increased.
void renderScene(const Sphere& s)
{
    //Ray r(origin, direction)
    Ray r(glm::vec3(0.0f), glm::vec3(0.0f));

    //Will hold the total amount of intersections
    int counter = 0;

    //Loops through each pixel...
    for(int y=0; y<imageHeight; y++)
    {
        for(int x=0; x<imageWidth; x++)
        {
            //Change ray direction for each pixel being processed
            r.setDirection(glm::vec3(((x-imageWidth/2)/(float)imageWidth), ((imageHeight/2-y)/(float)imageHeight), -1.0f));

            //If current ray intersects sphere...
            if(s.intersection(r))
            {
                //Increase counter
                counter++;
            }
        }
    }

    std::cout << counter << std::endl;
}
4

2 回答 2

2

t1在这种情况下,您对二次方程的第二个解 ( ) 是错误的disc > 0,您需要以下内容:

float sqrt_disc = glm::sqrt(disc);
t0 = (-b - sqrt_disc) / (2 * a);
t1 = (-b + sqrt_disc) / (2 * a);

我认为最好以这种形式写出方程,而不是将除以 2 变成乘以 0.5,因为代码越像数学,就越容易检查。

其他一些小评论:

  1. 重用名称似乎令人困惑discsqrt(disc)所以我在上面使用了一个新的变量名称。

  2. 您不需要测试t0 > t1,因为您知道asqrt_disc都是正数,因此t1总是大于t0

  3. 如果光线原点在球体内,则可能为t0负也可能为t1正。你似乎没有处理这个案子。

  4. 不需要特殊情况disc == 0,因为一般情况计算的值与特殊情况相同。(并且您拥有的特殊情况越少,检查代码就越容易。)

于 2012-10-01T17:54:13.683 回答
1

如果我正确理解您的代码,您可能想尝试:

r.setDirection(glm::vec3(((x-imageWidth/2)/(float)imageWidth),
                         ((imageHeight/2-y)/(float)imageHeight),
                         -1.0f));

现在,您已将相机放置在距离屏幕一单位的位置,但光线可以向右和向下拍摄多达 400 个单位。这是一个非常广阔的视野。而且,你的光线只扫过一个八分之一的空间。这就是为什么您只能在屏幕的左上角获得少量像素的原因。我上面写的代码应该可以纠正这个问题。

于 2012-10-01T18:35:03.700 回答