为什么raytracer渲染球体为椭圆形? [英] Why does raytracer render spheres as ovals?
问题描述
我不知道可能是什么原因造成的。这可能是射线球交叉点代码,如下所示:
bool Sphere :: intersect(Ray ray,glm :: vec3& hitPoint){
//计算A,B和C系数
float a = glm :: dot(ray.dir,ray.dir);
float b = 2.0 * glm :: dot(ray.dir,ray.org-pos);
float c = glm :: dot(ray.org-pos,ray.org-pos) - (rad * rad);
//查找判别式
float disc = b * b - 4 * a * c;
//如果判别式为负数,则没有真正的根,因此返回
// false,因为光线未命中sphere
if(disc <0)
返回false ;
//计算q
float distSqrt = sqrt(disc);
float q;
if(b <0)
q =( - b - distSqrt)/2.0;
else
q =(-b + distSqrt)/2.0;
//计算t0和t1
浮动t0 = q / a;
float t1 = c / q;
//确保t0小于t1
if(t0> t1){
//如果t0大于t1将它们交换
float temp = t0;
t0 = t1;
t1 = temp;
}
//如果t1小于零,则该物体处于光线的负方向
//,因此光线会遗漏球体
if(t1 < 0)
返回false;
//如果t0小于零,交点在t1
if(t0 <0){
hitPoint = ray.org + t1 * ray.dir ;
返回true;
} else {//其他交点在t0
hitPoint = ray.org + t0 * ray.dir;
返回true;
}
}
或者它可能是另一回事。有人有想法吗?非常感谢!
看起来您正在使用非常广泛的 field of view (FoV)。这产生了鱼眼镜头的效果,扭曲了画面,尤其是对边缘。典型的例如90度(即任一方向45度)给出了一个合理的图片。
折射实际上看起来相当好,它是倒转的,因为折射率非常高。美丽的图片在这个问题。
I've been hacking up a raytracer for the first time over the past few days. However, there are a few quirks which bother me and I don't really know how to work out. One that has been there since the beginning is the shape of spheres in the scene - when rendered, they actually look like ovals. Of course, there is perspective in the scene, but the final shape still seems odd. I have attached a sample rendering, the problem I have is especially visible on the reflective sphere in the lower left part of the image.
I don't really know what could be causing this. It might be the ray-sphere intersection code which looks as follows:
bool Sphere::intersect(Ray ray, glm::vec3& hitPoint) {
//Compute A, B and C coefficients
float a = glm::dot(ray.dir, ray.dir);
float b = 2.0 * glm::dot(ray.dir, ray.org-pos);
float c = glm::dot(ray.org-pos, ray.org-pos) - (rad * rad);
// Find discriminant
float disc = b * b - 4 * a * c;
// if discriminant is negative there are no real roots, so return
// false as ray misses sphere
if (disc < 0)
return false;
// compute q
float distSqrt = sqrt(disc);
float q;
if (b < 0)
q = (-b - distSqrt)/2.0;
else
q = (-b + distSqrt)/2.0;
// compute t0 and t1
float t0 = q / a;
float t1 = c / q;
// make sure t0 is smaller than t1
if (t0 > t1) {
// if t0 is bigger than t1 swap them around
float temp = t0;
t0 = t1;
t1 = temp;
}
// if t1 is less than zero, the object is in the ray's negative direction
// and consequently the ray misses the sphere
if (t1 < 0)
return false;
// if t0 is less than zero, the intersection point is at t1
if (t0 < 0) {
hitPoint = ray.org + t1 * ray.dir;
return true;
} else { // else the intersection point is at t0
hitPoint = ray.org + t0 * ray.dir;
return true;
}
}
Or it could be another thing. Does anyone have an idea? Thanks so much!
It looks like you're using a really wide field of view (FoV). This gives the effect of a fish-eye lens, distorting the picture, especially towards the edges. Typically something like 90 degrees (i.e. 45 degrees in either direction) gives a reasonable picture.
The refraction actually looks quite good; it's inverted because the index of refraction is so high. Nice pictures are in this question.
这篇关于为什么raytracer渲染球体为椭圆形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!