private Vector3 Reflection(RayHit hit, List <Light> lightList, List <EngineObject> objList, int depth) { if (depth <= 0 || hit.ObjHit.Finishing.Kr == 0) { return(Vector3.Zero); } Vector3 reflected = Reflect(hit.Normal); Ray scattered = new Ray(hit.Position, reflected); if (Vector3.Dot(scattered.Dir, hit.Normal) > 0) { return(scattered.RayColor(objList, lightList, depth - 1) * hit.ObjHit.Finishing.Kr); } return(Vector3.Zero); }
private void SaveFile() { int scannedPixels = 0; Parallel.For(0, Height, new ParallelOptions() { MaxDegreeOfParallelism = 1 }, (k) => { int j = Height - k; for (int i = 0; i < Width; i++) { Vector3 color = Vector3.Zero; for (int s = 0; s < samplesPerPixel; s++) { var u = (float)(i + Utils.Rand.NextDouble()) / (Width - 1); var v = (float)(j + Utils.Rand.NextDouble()) / (Height - 1); Ray r = Camera.GetRay(u, v); color += r.RayColor(EngineObjects, SceneLights, maxDepth); } //System.Threading.Interlocked.Increment(ref scannedPixels); //var tmp = MathF.Round((float)scannedPixels / ColorBuffer.Length * 100); //Console.WriteLine(tmp.ToString().Replace(',', '.') + " %"); ColorBuffer[(k * Width) + i] = color; } }); using StreamWriter sw = new StreamWriter(outputFilePath); sw.WriteLine("P3"); sw.WriteLine($"{Width} {Height}"); sw.WriteLine("255"); for (int i = 0; i < ColorBuffer.Length; i++) { sw.WriteColor(ColorBuffer[i], samplesPerPixel); } sw.Flush(); Console.WriteLine("Done!"); }