为什么raytracer渲染球体为椭圆形? [英] Why does raytracer render spheres as ovals?

查看:282
本文介绍了为什么raytracer渲染球体为椭圆形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在第一次打破光线追踪器。然而,有一些怪癖困扰着我,我真的不知道如何解决。从一开始就一直存在的是场景中的球体形状 - 渲染时,它们看起来像椭圆形。当然,这个场景是有角度的,但最终的形状仍然很古怪。我附加了一个示例渲染,我在图像左下角的反射球上特别看到了这个问题。 b



我不知道可能是什么原因造成的。这可能是射线球交叉点代码,如下所示:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆