/// <summary> /// Method that calculates the color of all pixels of the <see cref="HdrImage"/> /// datamember according to the rendering equation, /// specified by a <see cref="Render"/> object. It also implements /// antialiasing to avoid Moirè fringes effects. /// </summary> /// <param name="rend"> The type of renderer that transforms a <see cref="Ray"/> into a <see cref="Color"/>.</param> public void fireAllRays(Render rend) { int totalTicks = image.height; var options = new ProgressBarOptions { ProgressCharacter = '#', ForegroundColor = ConsoleColor.Yellow, ForegroundColorDone = ConsoleColor.Green, BackgroundColor = ConsoleColor.DarkBlue, ProgressBarOnBottom = true, }; using (var pbar = new ProgressBar(totalTicks, "", options)) { try { int rowCompleted = 0; Parallel.For(0, image.height, i => { for (int j = 0; j < image.width; j++) { Color appo = Constant.Black; if (this.samplesPerSide > 0) { for (int iPixRow = 0; iPixRow < samplesPerSide; iPixRow++) { for (int iPixCol = 0; iPixCol < samplesPerSide; iPixCol++) { float uPix = (iPixCol + pcg.randomFloat()) / (float)samplesPerSide; float vPix = (iPixRow + pcg.randomFloat()) / (float)samplesPerSide; Ray rr = this.fireRay(col: j, row: i, uPixel: uPix, vPixel: vPix); appo += rend.computeRadiance(rr); } } this.image.setPixel(j, i, appo * (1.0f / (float)Math.Pow(samplesPerSide, 2))); } else { Ray charles = this.fireRay(j, i); Color me_in = rend.computeRadiance(charles); this.image.setPixel(j, i, me_in); } } rowCompleted++; pbar.Tick($"Completed {rowCompleted}/{totalTicks} rows of the image"); }); } catch (AggregateException) { //lol } } }
public override Ray scatterRay(PCG r, Vec incomingDir, Point interactionPoint, Normal normal, int depth) { List<Vec> a = normal.createONBfromZ(); Vec e1 = a[0]; Vec e2 = a[1]; Vec e3 = a[2]; float cosThetaSq = r.randomFloat(); float cosTheta = MathF.Sqrt(cosThetaSq); float sinTheta = MathF.Sqrt(1.0f - cosThetaSq); float phi = 2 * MathF.PI * r.randomFloat(); Vec dir = e1 * sinTheta * MathF.Cos(phi) + e2 * sinTheta * MathF.Sin(phi) + e3 * cosTheta; return new Ray(interactionPoint, dir, tm: 1e-5f, dep: depth); }