|
| 1 | +using GeometryBasics |
| 2 | +using Trace |
| 3 | +using FileIO |
| 4 | +using ImageCore |
| 5 | +using BenchmarkTools |
| 6 | +using Makie, FileIO, ImageShow |
| 7 | + |
| 8 | + |
| 9 | +catmesh = load(Makie.assetpath("cat.obj")) |
| 10 | +img = load(Makie.assetpath("diffusemap.png")) |
| 11 | +m = normal_mesh(Tesselation(Sphere(Point3f(0), 1), 32)) |
| 12 | + |
| 13 | +function tmesh(prim, material) |
| 14 | + prim = prim isa Sphere ? Tesselation(prim, 64) : prim |
| 15 | + mesh = normal_mesh(prim) |
| 16 | + triangles = Trace.create_triangle_mesh(mesh, Trace.ShapeCore()) |
| 17 | + return [Trace.GeometricPrimitive(t, material) for t in triangles] |
| 18 | +end |
| 19 | + |
| 20 | +material_red = Trace.MatteMaterial( |
| 21 | + Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)), |
| 22 | + Trace.ConstantTexture(0.0f0), |
| 23 | +) |
| 24 | +material_blue = Trace.MatteMaterial( |
| 25 | + Trace.ConstantTexture(Trace.RGBSpectrum(0.251f0, 0.388f0, 0.847f0)), |
| 26 | + Trace.ConstantTexture(0.0f0), |
| 27 | +) |
| 28 | +material_white = Trace.MatteMaterial( |
| 29 | + Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)), |
| 30 | + Trace.ConstantTexture(0.0f0), |
| 31 | +) |
| 32 | +mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0))) |
| 33 | +glass = Trace.GlassMaterial( |
| 34 | + Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)), |
| 35 | + Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)), |
| 36 | + Trace.ConstantTexture(0.0f0), |
| 37 | + Trace.ConstantTexture(0.0f0), |
| 38 | + Trace.ConstantTexture(1.5f0), |
| 39 | + true, |
| 40 | +) |
| 41 | + |
| 42 | +LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius) |
| 43 | + |
| 44 | +begin |
| 45 | + s1 = tmesh(LowSphere(0.5f0), material_white) |
| 46 | + s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_blue) |
| 47 | + s3 = tmesh(LowSphere(0.3f0, Point3f(-0.5, 0.5, 0)), mirror) |
| 48 | + s4 = tmesh(LowSphere(0.4f0, Point3f(0.0, 1.0, 0)), material_white) |
| 49 | + ground = tmesh(Rect3f(Vec3f(-5, -5, 0), Vec3f(10, 10, -0.1)), mirror) |
| 50 | + left = tmesh(Rect3f(Vec3f(-3, 0, 0), Vec3f(-0.1, 10, -0.1)), mirror) |
| 51 | + |
| 52 | + bvh = Trace.BVHAccel([s1..., s2..., s3..., s4..., ground...], 1); |
| 53 | + |
| 54 | + lights = [ |
| 55 | + Trace.PointLight(Vec3f(0, 2.5, 4), Trace.RGBSpectrum(60.0f0)), |
| 56 | + Trace.PointLight(Vec3f(-5, 2, 0), Trace.RGBSpectrum(60.0f0)), |
| 57 | + ] |
| 58 | + scene = Trace.Scene(lights, bvh); |
| 59 | + resolution = Point2f(1024) |
| 60 | + f = Trace.LanczosSincFilter(Point2f(1.0f0), 3.0f0) |
| 61 | + film = Trace.Film(resolution, |
| 62 | + Trace.Bounds2(Point2f(0.0f0), Point2f(1.0f0)), |
| 63 | + f, 1.0f0, 1.0f0, |
| 64 | + "shadows_sppm_res.png", |
| 65 | + ) |
| 66 | + screen_window = Trace.Bounds2(Point2f(-1), Point2f(1)) |
| 67 | + camera = Trace.PerspectiveCamera( |
| 68 | + Trace.look_at(Point3f(0, 3, 3), Point3f(0, 0, 0), Vec3f(0, 0, 1)), |
| 69 | + screen_window, 0.0f0, 1.0f0, 0.0f0, 1.0f6, 45.0f0, film, |
| 70 | + ) |
| 71 | + integrator = Trace.WhittedIntegrator(camera, Trace.UniformSampler(8), 1) |
| 72 | + integrator(scene) |
| 73 | + img = reverse(film.framebuffer, dims=1) |
| 74 | +end |
| 75 | +x = Trace.scale(2, 2, 2) |
| 76 | +x.inv_m == inv(x.m) |
| 77 | +GLMakie.activate!(inline=true) |
| 78 | +# Computer projective camera transformations. |
| 79 | +resolution = Point2f(1024) |
| 80 | +resolution = Trace.scale(resolution[1], resolution[2], 1) |
| 81 | + |
| 82 | +window_width = screen_window.p_max .- screen_window.p_min |
| 83 | +inv_bounds = Trace.scale((1.0f0 ./ window_width)..., 1) |
| 84 | + |
| 85 | +offset = Trace.translate(Vec3f( |
| 86 | + -screen_window.p_min..., 0.0f0, |
| 87 | +)) |
| 88 | + |
| 89 | +ray = camera.core.raster_to_camera(Point3f(1024 / 2, 1024 / 2, 0)) |
| 90 | +camera.core.screen_to_raster(camera.core.camera_to_screen(ray)) |
| 91 | + |
| 92 | +camera.core.screen_to_raster(Point3f(-1, -1, 0)) |
| 93 | +camera.core.screen_to_raster(Point3f(1, 1, 0)) |
| 94 | + |
| 95 | +camera.core.raster_to_screen(Point3f(1024, 1024, 0)) |
| 96 | + |
| 97 | +camera.core.screen_to_raster(Point3f(-1, -1, 0))# == (0, 0, 0) |
| 98 | +camera.core.screen_to_raster(Point3f(1, 1, 0))# == (1024, 1024, 0) |
| 99 | + |
| 100 | +camera.core.raster_to_screen(Point3f(0, 0, 0)) # == (0, 0, 0) |
| 101 | +camera.core.screen_to_raster(Point3f(1, 1, 0)) # == (1024, 1024, 0) |
| 102 | + |
| 103 | + |
| 104 | +res_t = Trace.scale(resolution..., 1) |
| 105 | +window_width = screen_window.p_max .- screen_window.p_min |
| 106 | +inv_bounds = Trace.scale((1f0 ./ window_width)..., 1) |
| 107 | + |
| 108 | +offset = Trace.translate(Vec3f( |
| 109 | + (-screen_window.p_min)..., 0f0, |
| 110 | +)) |
| 111 | + |
| 112 | +inv(res_t.m) |
| 113 | +screen_to_raster = res_t * inv_bounds * offset |
| 114 | + |
| 115 | +screen_to_raster(Point3f(1, 1, 0)) |
| 116 | + |
| 117 | +raster_to_screen = inv(offset) * inv(inv_bounds) * inv(res_t) |
| 118 | + |
| 119 | +raster_to_screen(Point3f(1024, 1024, 0)) |
0 commit comments