开发者

Odd Ray Tracing Artifact - wrong colored pixels

开发者 https://www.devze.com 2023-03-27 00:55 出处:网络
So I\'m trying to rewrite my ray tracer in C# and I\'ve been getting these odd artifacts. At first I might have thought that they were the black-spot artifacts that are due to not adding Epsilon to th

So I'm trying to rewrite my ray tracer in C# and I've been getting these odd artifacts. At first I might have thought that they were the black-spot artifacts that are due to not adding Epsilon to the shadow ray as described here.

However I am adding an epsilon and the spots aren't black (but I am adding an ambient). Here is an example picture (I made the background yellow so it stands out a bit)

Odd Ray Tracing Artifact - wrong colored pixels

Here is my lighting code:

private Vector4 CalculateBlinnPhongLight(Ray ray, Intersection its)
    {
        Vector4 blinnPhongColor = Color.Blue.ToVector4();
        //Step 1: Calculate intersection point. Since our direction was never normalized we need to do it here.
        Microsoft.Xna.Framework.Vector3 itsPoint = ray.Origin + ray.Direction*its.TimeInterval.StartTime;
        //Step 2: Calculate half vector h - bisector of angle of v (from intersection to eye) and l (intersection to light)
        Microsoft.Xna.Framework.Vector3 v = -1 * ray.Direction; v.Normalize();
        //Step 3: For ecah light in the scene grab it's contribution
        foreach (Light light in Lights)
        {
            //Step 4: Setup unit vectors for Blinn Phong Lighting model: pg.84
            Microsoft.Xna.Framework.Vector3 l = light.Position - itsPoint; l.Normalize();
            Microsoft.Xna.Framework.Vector3 h = v + l; h.Normalize();
            Microsoft.Xna.Framework.Vector3 n = its.Normal;
            //Step 5: Shadow-Check - send shadow ray and see if it intersects with anything
            if  (IsInShadow(l, itsPoint) )
            {
                continue;
     开发者_StackOverflow社区           //TO DO: Check if the material is dielectric (see through)
            }
            //Step 6: Calculate light attenuation
            //TODO:

            //Step 7: Perform Blinn Phong calculation
            //color = color + diffuse * I * max(0, n.l)
            //color = color + specular * I * max(0, n.h)^phong
            blinnPhongColor += its.ClosestNode.Material.DiffuseColor.ToVector4() * light.Color.ToVector4() * Math.Max(0, Microsoft.Xna.Framework.Vector3.Dot(n, l));
            blinnPhongColor += its.ClosestNode.Material.SpecularColor.ToVector4() * light.Color.ToVector4() * (float)Math.Pow(Math.Max(0, Vector3.Dot(n, h)), its.ClosestNode.Material.PhongExponent);
        }
        //Step 8: Add ambient Color
        blinnPhongColor += its.ClosestNode.Material.AmbientColor.ToVector4() * AmbientColor.ToVector4();
        return blinnPhongColor;
    }

    private bool IsInShadow(Microsoft.Xna.Framework.Vector3 dirToLight, Microsoft.Xna.Framework.Vector3 itsPoint)
    {
        //Step 1: Need to add epsilon go itsPoint otherwise we might intersect ourselves
        Microsoft.Xna.Framework.Vector3 epsItsPoint = itsPoint + 0.000001f*dirToLight;
        Ray shadowRay = new Ray(epsItsPoint, dirToLight);
        return ! this.Root.IntersectRay(shadowRay).Hit;
    }

It's perhaps worth noting that my ray Direction's are not unit vectors. I've done this purposefully based on the instructions on the book I am reading so that when I can transform the ray by the transformation matrices I give to each primitive to allow easy instancing.


Since those artifacts are getting the ambient light contribution, but are receiving neither the specular nor the diffuse, it is almost certainly your IsInShadow calculation. Depending on how you defined your sphere, there are various precision errors you should be hitting.

To verify that it is your shadow calculation, replace the diffuse and specular contributions with a single constant contribution. If the artifacts go away, then that is your issue. If they do not go away, then there is an error in both light contributions (unlikely), and you can verify this by repositioning the light in your scene.

Also, you really don't need to normalize your shadow ray, and not doing so spares you a division calculation, which is usually where those sorts of precision errors occur.

0

精彩评论

暂无评论...
验证码 换一张
取 消