private static MyColorClass computeReflection(ref int reflectRecursion, ref IntersectionObject currentInter, ref Scene scene, ref List <Object> objects, Light l, ref double lightToObjDistance) { reflectRecursion -= 1; var reflectVector = currentInter.iPointNormal; var reflectRay = new Ray(currentInter.intersectionPoint, reflectVector); reflectVector.normalize(); return(l.getLighAt(currentInter, lightToObjDistance, true) * (1 - currentInter.obj.material.reflection) + Raytrace(ref scene, ref objects, ref reflectRay, ref reflectRecursion, currentInter.obj) * currentInter.obj.material.reflection); }
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)); }
private static MyColorClass computeTransparency(ref int reflectRecursion, ref IntersectionObject currentInter, ref Scene scene, ref List <Object> objects, Light l, ref double lightToObjDistance) { reflectRecursion -= 1; return(l.getLighAt(currentInter, lightToObjDistance, true) * (1 - currentInter.obj.material.diffuseColor.a) + Raytrace(ref scene, ref objects, ref currentInter.ray, ref reflectRecursion, currentInter.obj) * currentInter.obj.material.diffuseColor.a); }
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); }