Jump to content
• Advertisement

# Ray-tracer sphere-ray intersection is incorrect

This topic is 3180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

That is essentially my problem. The colour is equal to the distance along the ray that is needed to reach the surface of the sphere. Other than that white spot in the center, it looks correct to me. However, I cannot figure out what that white spot is. Here is my intersection code:
         float sphere::intersect(const ray &r)
{
// ray(t) = o + t * d
// sphere(p): dot(p - c, p - c) = r^2

// substitute and solve for t and we get a quadratic:
// a = dot(d, d)
// b = 2 * dot(o - c, d)
// c = dot(o - c, o - c) - r^2

vec3 dir = r.orig - pos;

float a = r.dir.sqlength();
float b = 2.0f * dir.dot(r.dir);
float c = dir.sqlength() - radius * radius;

float disc = b * b - 4.0f * a * c;

// imaginary roots, doesn't intersect the sphere
if (disc < 0)
return -1; // the renderer ignores negative distances

float t = (-b - std::sqrt(disc)) / (2.0f * a);

return t;
}

Any ideas? I've tried multiple intersection routines besides my own (above), and they all end up doing the same thing. Here is my ray-generation code:
    vec3 plane(float(x) / WIDTH * 2 - 1, float(y) / HEIGHT * 2 - 1, 0);

r.orig = vec3(0, 0, 0);
r.dir = plane + vec3(0, 0, -1);
r.dir.normalize();

I'm pretty sure that's not the issue, though, since even making some simple orthographic rays yields the same issue.

#### Share this post

##### Share on other sites
Advertisement
Your ray-sphere intersection is IMHO correct, but you're rendering wrong colors. Just render fully red sphere (because you're going over value 1.0 while distributing color by-depth, try to re-range your depth color, something like colorBuffer = depth / 100.0f; should do the trick)

I also would like to point that your ray-sphere intersection will behave very slow as compared to faster ones (with early ray termination in algorithm and square root at the end).

#### Share this post

##### Share on other sites
You're completely right!

I was just taking the float and just dividing by 255 to get it into a byte, without saturating the float into [0..1] first [embarrass]

Many thanks, I wouldn't have thought to look in that part of the code. [smile]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5
• Advertisement

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!