Пример #1
0
        public Vector3 applyLights(Ray r, Intersection i, Vector3 color, bool isDebugRay)
        {
            Vector3 returnColor    = color;
            Vector3 intersectPoint = r.Origin + (r.Dir * i.Distance);
            Vector3 surfaceNormal  = i.normal;
            Ray     shadowRay;

            foreach (Light light in lights)
            {
                //we willen vd light naar de intersectionpoint toe gaan
                Vector3 lightDir = (light.pos - intersectPoint).Normalized();

                //check if the light source is behind the primitive
                if (Vector3.Dot(surfaceNormal, lightDir) < 0)
                {
                    //do not do anything if the light is behind primitive
                }
                else
                {
                    Intersection result = null; //null on start, if it finds an intersection, it's not null anymore
                    Intersection intersection;

                    shadowRay        = new Ray();
                    shadowRay.Dir    = lightDir;
                    shadowRay.Origin = intersectPoint + (float.Epsilon * shadowRay.Dir);
                    //offset the ray origin by epsilon times the ray direction

                    foreach (Primitive p in primitives)
                    {
                        intersection = p.intersect(shadowRay);
                        if (intersection != null && (intersection.Distance < (light.pos - intersectPoint).Length - (2 * float.Epsilon)) &&
                            intersection.Distance > 0)    //restriction on ray: 0 < distance < Plight - I - (2 * float.epsilon)
                        {
                            //if we found a valid intersection, it means we shouldn't do anything with the light
                            result = intersection;

                            //if (isDebugRay)debug.RenderShadowRay(shadowRay, result);//render shadowray if it's debug ray <!-- not working -->
                            break; //break out since there is an intersection
                        }
                    }



                    //if no primitives in the way have been found, apply the light multiplication to the color
                    if (result == null)
                    {
                        float lightDistanceTravelled = (light.pos - intersectPoint).Length;
                        returnColor *= (new Vector3(1, 1, 1) + (light.intensity / (lightDistanceTravelled * lightDistanceTravelled)));
                        //if (isDebugRay) { debug.RenderShadowRay(shadowRay); } render debug rays they said, it will be fun they said
                    }
                }
            }

            //clamp values to max 1,1,1
            if (returnColor.X > 1)
            {
                returnColor.X = 1;
            }
            if (returnColor.Y > 1)
            {
                returnColor.Y = 1;
            }
            if (returnColor.Z > 1)
            {
                returnColor.Z = 1;
            }
            return(returnColor);
        }