Why does raytracer render spheres as ovals?

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 (ie 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.

链接地址: http://www.djcxy.com/p/67952.html

上一篇: 如何用超链接提供ggplot2图形?

下一篇: 为什么raytracer渲染球体为椭圆形?