光线球体相交的相交问题

Intersection problems with ray-sphere intersection

本文关键字:问题      更新时间:2023-10-16

我正在编写一个简单的光线跟踪器,为了保持简单,我决定在场景中只使用球体。我现在正处于这样一个阶段,我只想确认我的光线正确地与场景中的球体相交,而不是其他。我创建了一个Ray和Sphere类,然后在我的主文件中创建一个函数,该函数遍历每个像素,以查看是否存在交集(相关代码将在下面发布)。问题是,与球体的整个交点的行为相当奇怪。如果我创建一个圆心(0,0,-20)、半径为1的球体,那么我只得到一个交点,它总是在我的图像的第一个像素(左上角)。当我到达半径15时,我突然在左上角有三个十字路口。半径18给了我六个交点,一旦我达到半径20+,我就会突然得到每个像素的一个交点,所以有些东西的行为是不应该做的。

我怀疑我的射线球体相交代码可能在这里有问题,但在浏览了它并浏览了网络以获取更多信息后,大多数解决方案都描述了我使用的相同方法,所以我认为它不应该(!)在这里有错。所以…我不确定我做错了什么,可能是我的交叉口代码,也可能是其他原因导致了问题。我好像找不到它。可能是我在给球体和射线赋值时想错了吗?以下是相关代码

球体类:

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;
}

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

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

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

其他一些小评论:

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

  2. 你不需要检测t0 > t1,因为你知道asqrt_disc都是阳性的,所以t1总是大于t0

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

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

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

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

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