only render 1 sample per event loop

master
Fabien Freling 2021-04-27 09:05:46 +02:00
parent 80fed125b4
commit a3df9a7707
1 changed files with 29 additions and 29 deletions

View File

@ -128,43 +128,43 @@ pub fn main() anyerror!void {
} }
} }
// Render if (k < samplesPerPixel) {
{
while (k < samplesPerPixel) {
_ = sdl.c.SDL_LockSurface(surface);
var j: usize = 0; // Render
while (j < imageHeight) { _ = sdl.c.SDL_LockSurface(surface);
var i: usize = 0;
while (i < imageWidth) {
var imageIndex = i + j * imageWidth;
var pixelColor = &pixelAccu[imageIndex];
const u = (@intToFloat(f32, i) + prng.random.float(f32)) / @intToFloat(f32, (imageWidth - 1)); var j: usize = 0;
const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1)); while (j < imageHeight) {
const r = camera.getRay(u, v); var i: usize = 0;
const sample = rayColor(r, world); while (i < imageWidth) {
pixelColor.* = pixelColor.*.add(sample); var imageIndex = i + j * imageWidth;
var pixelColor = &pixelAccu[imageIndex];
const averageColor = color.averageColor(pixelColor, @intCast(i32, k)); const u = (@intToFloat(f32, i) + prng.random.float(f32)) / @intToFloat(f32, (imageWidth - 1));
// SDL coordinate system is flipped compared to the raytracer const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1));
sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor); const r = camera.getRay(u, v);
const sample = rayColor(r, world);
pixelColor.* = pixelColor.*.add(sample);
i += 1; const averageColor = color.averageColor(pixelColor, @intCast(i32, k));
} // SDL coordinate system is flipped compared to the raytracer
j += 1; sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor);
i += 1;
} }
j += 1;
setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
sdl.c.SDL_UnlockSurface(surface);
_ = sdl.c.SDL_UpdateWindowSurface(window);
k += 1;
} }
}
if (k >= samplesPerPixel) { setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
sdl.c.SDL_UnlockSurface(surface);
_ = sdl.c.SDL_UpdateWindowSurface(window);
k += 1;
} else {
sdl.c.SDL_Delay(1000 / fps); sdl.c.SDL_Delay(1000 / fps);
} }
} }
} }