protected override Task Handle(RenderSceneCommand request, CancellationToken cancellationToken) { var shapes = new List <IBasicShape>(); shapes.AddRange(request.Scene.Shapes.Select(ShapeFactory.MapApiToDomain)); var world = new World(shapes, LightFactory.MapApiToDomain(request.Scene.LightSource)); var camera = CameraFactory.MapApiToDomain(request.Scene.Camera); // Render image using var canvas = new System.Drawing.Bitmap(camera.HorizontalSize, camera.VerticalSize); for (int y = 0; y < camera.VerticalSize - 1; y++) { if (cancellationToken.IsCancellationRequested) { break; } for (int x = 0; x < camera.HorizontalSize - 1; x++) { if (cancellationToken.IsCancellationRequested) { break; } var ray = camera.GetRay(x, y); Color color = Lighting.CalculateColorWithPhongReflection(world, ray); canvas.SetPixel(x, y, color.Simplify(255)); } } if (!cancellationToken.IsCancellationRequested) { canvas.Save(request.FilePath); } return(cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) : Task.CompletedTask); }