Esempio n. 1
0
        private static MyColorClass Raytrace(ref Scene scene, ref List <Object> objects, ref Ray ray, ref int reflectRecursion, Object _intersectObj = null)
        {
            double             distance     = 9999999.9;
            Object             currentObj   = null;
            IntersectionObject currentInter = null;
            MyColorClass       finalColor   = new MyColorClass();

            for (int i = 0; i < objects.Count; i++)
            {
                if (_intersectObj == null || (_intersectObj != null && _intersectObj != objects[i]))
                {
                    IntersectionObject interSectObj = null;

                    try
                    {
                        interSectObj = objects[i].intersect(ray);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }

                    if (interSectObj.hasIntersection)
                    {
                        var tmpDistance = (interSectObj.intersectionPoint - ray.origin).getMagnitude();
                        if (tmpDistance <= distance)
                        {
                            distance     = tmpDistance;
                            currentInter = interSectObj;
                            currentObj   = objects[i];
                        }
                    }
                }
            }
            return(getLighting(ref currentInter, ref currentObj, ref objects, ref scene, finalColor, ref reflectRecursion));
        }
Esempio n. 2
0
        private static MyColorClass getLighting(ref IntersectionObject currentInter, ref Object currentObj, ref List <Object> objects, ref Scene scene, MyColorClass finalColor, ref int reflectRecursion)
        {
            bool lightBlocked = false;

            if (currentObj != null)
            {
                foreach (var l in scene.Lights)
                {
                    lightBlocked = false;
                    var lightVector        = currentInter.intersectionPoint - l.position;
                    var lightToObjDistance = lightVector.getMagnitude();
                    lightVector.normalize();

                    var lightRay = new Ray(l.position, lightVector);
                    for (int j = 0; j < objects.Count; j++)
                    {
                        currentObj = objects[j];
                        if (currentObj != currentInter.obj)
                        {
                            IntersectionObject lightInterSectObj = null;

                            try
                            {
                                lightInterSectObj = currentObj.intersect(lightRay);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }

                            if (lightInterSectObj.hasIntersection)
                            {
                                var lightToInterDistance = (lightInterSectObj.intersectionPoint - l.position).getMagnitude();
                                if (lightToInterDistance < lightToObjDistance)
                                {
                                    lightBlocked = true;
                                }
                            }
                        }
                    }

                    finalColor += l.getLighAt(currentInter, lightToObjDistance, lightBlocked);

                    if (currentInter.obj.material.reflection > 0 && reflectRecursion > 0)
                    {
                        return(computeReflection(ref reflectRecursion, ref currentInter, ref scene, ref objects, l,
                                                 ref lightToObjDistance));
                    }

                    if (currentInter.obj.material.diffuseColor.a < 1 && reflectRecursion > 0)
                    {
                        return(computeTransparency(ref reflectRecursion, ref currentInter, ref scene, ref objects, l,
                                                   ref lightToObjDistance));
                    }
                }
            }

            finalColor.normalizeColor();
            return(finalColor);
        }