display progress bar
This commit is contained in:
		
							parent
							
								
									61627fb76c
								
							
						
					
					
						commit
						80fed125b4
					
				
					 1 changed files with 60 additions and 35 deletions
				
			
		| 
						 | 
				
			
			@ -44,6 +44,26 @@ fn rayColor(ray: Ray, world: World) Color {
 | 
			
		|||
    return white.mul(1.0 - t).add(blue.mul(t));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn setProgress(surface: *sdl.c.SDL_Surface, width: usize, height: usize, percent: f32) void {
 | 
			
		||||
    if (percent == 1.0) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    const progressBarHeight = 3;
 | 
			
		||||
    const progressWidth = @floatToInt(usize, @intToFloat(f32, width) * percent);
 | 
			
		||||
    const progressColor = Color{ .x = 1.0 };
 | 
			
		||||
 | 
			
		||||
    var j: usize = 0;
 | 
			
		||||
    while (j < progressBarHeight) {
 | 
			
		||||
        var i: usize = 0;
 | 
			
		||||
        while (i < progressWidth) {
 | 
			
		||||
            sdl.setSurfacePixel(surface, i, j, progressColor);
 | 
			
		||||
            i += 1;
 | 
			
		||||
        }
 | 
			
		||||
        j += 1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn main() anyerror!void {
 | 
			
		||||
    if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
 | 
			
		||||
        std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
 | 
			
		||||
| 
						 | 
				
			
			@ -90,46 +110,13 @@ pub fn main() anyerror!void {
 | 
			
		|||
        pixel.* = Color{};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        _ = sdl.c.SDL_LockSurface(surface);
 | 
			
		||||
 | 
			
		||||
        var k: usize = 0;
 | 
			
		||||
        while (k < samplesPerPixel) {
 | 
			
		||||
            _ = sdl.c.SDL_LockSurface(surface);
 | 
			
		||||
 | 
			
		||||
            var j: usize = 0;
 | 
			
		||||
            while (j < imageHeight) {
 | 
			
		||||
                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));
 | 
			
		||||
                    const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1));
 | 
			
		||||
                    const r = camera.getRay(u, v);
 | 
			
		||||
                    const sample = rayColor(r, world);
 | 
			
		||||
                    pixelColor.* = pixelColor.*.add(sample);
 | 
			
		||||
 | 
			
		||||
                    const averageColor = color.averageColor(pixelColor, @intCast(i32, k));
 | 
			
		||||
                    // SDL coordinate system is flipped compared to the raytracer
 | 
			
		||||
                    sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor);
 | 
			
		||||
 | 
			
		||||
                    i += 1;
 | 
			
		||||
                }
 | 
			
		||||
                j += 1;
 | 
			
		||||
            }
 | 
			
		||||
            sdl.c.SDL_UnlockSurface(surface);
 | 
			
		||||
            _ = sdl.c.SDL_UpdateWindowSurface(window);
 | 
			
		||||
            k += 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
 | 
			
		||||
        std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
 | 
			
		||||
        return error.SDLUpdateWindowFailed;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var running = true;
 | 
			
		||||
    var k: usize = 0;
 | 
			
		||||
    while (running) {
 | 
			
		||||
        var event: sdl.c.SDL_Event = undefined;
 | 
			
		||||
        while (sdl.c.SDL_PollEvent(&event) != 0) {
 | 
			
		||||
| 
						 | 
				
			
			@ -140,6 +127,44 @@ pub fn main() anyerror!void {
 | 
			
		|||
                else => {},
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        sdl.c.SDL_Delay(1000 / fps);
 | 
			
		||||
 | 
			
		||||
        // Render
 | 
			
		||||
        {
 | 
			
		||||
            while (k < samplesPerPixel) {
 | 
			
		||||
                _ = sdl.c.SDL_LockSurface(surface);
 | 
			
		||||
 | 
			
		||||
                var j: usize = 0;
 | 
			
		||||
                while (j < imageHeight) {
 | 
			
		||||
                    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));
 | 
			
		||||
                        const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1));
 | 
			
		||||
                        const r = camera.getRay(u, v);
 | 
			
		||||
                        const sample = rayColor(r, world);
 | 
			
		||||
                        pixelColor.* = pixelColor.*.add(sample);
 | 
			
		||||
 | 
			
		||||
                        const averageColor = color.averageColor(pixelColor, @intCast(i32, k));
 | 
			
		||||
                        // SDL coordinate system is flipped compared to the raytracer
 | 
			
		||||
                        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) {
 | 
			
		||||
            sdl.c.SDL_Delay(1000 / fps);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue