Strange pinching effect with raytraced reflections

Started by
1 comment, last by BadEggGames 8 years, 7 months ago

Hi all

Finally making progress on reflections. I cant seem to find what is wrong with my reflection rays. It is creating reflections that are pinched to the center of the spheres:

Vector3 ReflectionDirection = newRay.direction.nor().cpy().sub(surfNorm.scl(newRay.direction.cpy().dot(surfNorm))).scl(2.0f);

Ray ReflectionRay = new Ray(inter, ReflectionDirection);

CastRay(ReflectionRay, true);

sphere_1440993676825.png

Any idea why this is happening?

Advertisement

Your code calculates (dir - dir * dot(dir, norm)) * 2, whereas you should be calculating dir - dir * dot(dir, norm) * 2, that is:


Vector3 ReflectionDirection = newRay.direction.nor().cpy().sub(surfNorm.scl(newRay.direction.cpy().dot(surfNorm)).scl(2.0f));

Or just:


Vector3 ReflectionDirection = newRay.direction.nor().cpy().sub(surfNorm.scl(2 * newRay.direction.cpy().dot(surfNorm)));

This is incidentally the main reason why I strongly dislike the lack of (reasonable) operator overloading in languages, it makes it impossible to write such things in a sane way (and, no, separating this simple equation into three or four temporary variables is not sane either). But anyway this is where the pinching comes from.

(actually it's pinching not so much because the reflection is wrong but really because of your error the resulting reflected vector is not unit length, which wreaks havoc on the rest of the ray tracing code something fierce; so a good thing to do whenever stuff looks really weird is to look at your directions vectors and check if they are actually unit length; if they aren't, you've screwed up somewhere)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Thanks again :)

sphere_1441004611970.png

This topic is closed to new replies.

Advertisement