1 00:00:00,120 --> 00:00:02,500 The following content is provided under a Creative 2 00:00:02,500 --> 00:00:03,910 Commons license. 3 00:00:03,910 --> 00:00:06,950 Your support will help MIT OpenCourseWare continue to 4 00:00:06,950 --> 00:00:10,600 offer high quality educational resources for free. 5 00:00:10,600 --> 00:00:13,500 To make a donation or view additional materials from 6 00:00:13,500 --> 00:00:17,430 hundreds of MIT courses, visit MIT OpenCourseWare at 7 00:00:17,430 --> 00:00:18,680 ocw.mit.edu. 8 00:00:22,940 --> 00:00:23,900 PROFESSOR: All right. 9 00:00:23,900 --> 00:00:25,340 We're going to get started now. 10 00:00:25,340 --> 00:00:28,700 For those of you who don't know me, I'm Josh Slocum, one 11 00:00:28,700 --> 00:00:30,240 of your TAs. 12 00:00:30,240 --> 00:00:33,430 And today's lecture is going to be a primer on ray tracing 13 00:00:33,430 --> 00:00:35,830 to help you get started on the final project. 14 00:00:41,200 --> 00:00:43,870 So first, we're going to start with a little background. 15 00:00:43,870 --> 00:00:46,190 Tell you about ray tracing. 16 00:00:46,190 --> 00:00:50,510 Ray tracing is essentially a physics simulation. 17 00:00:50,510 --> 00:00:54,070 You simulate the way photons bounce around and interact 18 00:00:54,070 --> 00:00:55,670 with materials. 19 00:00:55,670 --> 00:01:01,050 And from that, you generate an image of a scene. 20 00:01:01,050 --> 00:01:04,599 It's extremely parallel because you can trace a whole 21 00:01:04,599 --> 00:01:07,140 bunch of photons bouncing around at the same time. 22 00:01:10,008 --> 00:01:14,610 And it has a huge capacity for photorealism. 23 00:01:14,610 --> 00:01:18,550 So like this sample seen here, you have all sorts of 24 00:01:18,550 --> 00:01:22,140 reflections and light working in ways that you don't see in 25 00:01:22,140 --> 00:01:23,390 a normal rendering program. 26 00:01:27,200 --> 00:01:32,450 The final project, as you know if you've read the handout, is 27 00:01:32,450 --> 00:01:36,580 going to be groups of two or three people each. 28 00:01:36,580 --> 00:01:40,840 And you'll be given a working ray tracer program. 29 00:01:40,840 --> 00:01:44,370 A very slow one, but one that works. 30 00:01:44,370 --> 00:01:48,840 And your objective is to make it run as fast as possible. 31 00:01:48,840 --> 00:01:51,230 You can do pretty much anything you want with a few 32 00:01:51,230 --> 00:01:53,680 restrictions we'll talk about later. 33 00:01:53,680 --> 00:02:01,910 You can make algorithmic improvements, parallelize it, 34 00:02:01,910 --> 00:02:05,780 change the way the rendering works slightly. 35 00:02:05,780 --> 00:02:06,640 All sorts of things. 36 00:02:06,640 --> 00:02:09,310 Pretty much anything you can think of. 37 00:02:09,310 --> 00:02:12,920 The only deliverables for the final project are the 38 00:02:12,920 --> 00:02:18,260 preliminary report due next week, in which you'll have to 39 00:02:18,260 --> 00:02:24,130 show us that you've profiled the project, tell us what hot 40 00:02:24,130 --> 00:02:27,420 spots you've identified, and ideas you have for speeding up 41 00:02:27,420 --> 00:02:30,460 the ray tracer, as well as any actual work 42 00:02:30,460 --> 00:02:33,210 you've already done. 43 00:02:33,210 --> 00:02:37,330 The final project comes with two built-in scenes that can 44 00:02:37,330 --> 00:02:39,260 be ray traced. 45 00:02:39,260 --> 00:02:46,340 Scene one, this one here, is rendered much 46 00:02:46,340 --> 00:02:49,690 faster than scene two. 47 00:02:49,690 --> 00:02:52,730 And you're going to want to work on that one first. 48 00:02:52,730 --> 00:02:55,470 And then, there's scene two, which is slightly different. 49 00:02:55,470 --> 00:02:57,780 It has water and one less sphere. 50 00:03:01,430 --> 00:03:04,750 These are the restrictions on what you can do 51 00:03:04,750 --> 00:03:06,650 for the final project. 52 00:03:06,650 --> 00:03:09,350 The purpose of these restrictions is basically just 53 00:03:09,350 --> 00:03:12,690 to make sure that everyone's ray tracer is 54 00:03:12,690 --> 00:03:13,850 doing the same thing. 55 00:03:13,850 --> 00:03:17,360 So you're all working on the same problem, and you're not 56 00:03:17,360 --> 00:03:20,620 achieving speed ups by simplifying out some 57 00:03:20,620 --> 00:03:22,865 functionality of the program. 58 00:03:25,982 --> 00:03:28,730 Does anyone have any questions about the final project 59 00:03:28,730 --> 00:03:29,980 before I move on? 60 00:03:35,408 --> 00:03:37,610 All right. 61 00:03:37,610 --> 00:03:38,743 Go ahead. 62 00:03:38,743 --> 00:03:39,820 AUDIENCE: [INAUDIBLE] 63 00:03:39,820 --> 00:03:43,195 we're going to do a third scene similar to the first 64 00:03:43,195 --> 00:03:44,030 two, right? 65 00:03:44,030 --> 00:03:44,430 PROFESSOR: Oh. 66 00:03:44,430 --> 00:03:46,630 Yeah. 67 00:03:46,630 --> 00:03:51,230 In order to make sure that you're following these rules, 68 00:03:51,230 --> 00:03:55,490 we're going to test your ray tracer on one or more other 69 00:03:55,490 --> 00:03:58,040 scenes and make sure that they render properly. 70 00:03:58,040 --> 00:04:00,510 But these are the two scenes that you will be benchmarked 71 00:04:00,510 --> 00:04:02,010 on for your performance grade. 72 00:04:08,380 --> 00:04:08,880 All right. 73 00:04:08,880 --> 00:04:15,040 Next, we're going to go over how ray tracing works and the 74 00:04:15,040 --> 00:04:21,550 big concepts used in the ray tracer you'll be working on. 75 00:04:21,550 --> 00:04:25,470 So the basic idea behind the rendering is you're going to 76 00:04:25,470 --> 00:04:30,300 construct a geometric model of the scene you want to render, 77 00:04:30,300 --> 00:04:32,160 and so it will be made of polygons, 78 00:04:32,160 --> 00:04:35,690 or spheres, or planes. 79 00:04:35,690 --> 00:04:39,490 And then within that model, you insert a grid of pixels. 80 00:04:39,490 --> 00:04:42,240 They'll form the image you're going to render. 81 00:04:42,240 --> 00:04:45,110 And then, you just project the scene onto the grid of pixels, 82 00:04:45,110 --> 00:04:49,090 and you have some image. 83 00:04:49,090 --> 00:04:52,870 The way you do this with ray tracing is you use a method 84 00:04:52,870 --> 00:05:00,140 called ray casting where you take some camera and cast rays 85 00:05:00,140 --> 00:05:02,920 through each pixel onto the scene. 86 00:05:02,920 --> 00:05:06,810 And then, whatever color object the ray hits is the 87 00:05:06,810 --> 00:05:09,750 color that that pixel gets shaded in the scene. 88 00:05:13,710 --> 00:05:18,490 This method of tracing rays is called backwards ray tracing, 89 00:05:18,490 --> 00:05:21,710 because you're going in opposite direction that 90 00:05:21,710 --> 00:05:25,790 photons go in when you're actually observing something. 91 00:05:25,790 --> 00:05:28,400 So if this was an actual scene, you'd have light 92 00:05:28,400 --> 00:05:30,560 bouncing here and into you're eye. 93 00:05:30,560 --> 00:05:32,560 Whereas what you're doing is you're tracing backwards from 94 00:05:32,560 --> 00:05:33,810 the eye to the object. 95 00:05:36,150 --> 00:05:37,710 Ray casting-- 96 00:05:37,710 --> 00:05:39,930 this image here is a ray image-- 97 00:05:39,930 --> 00:05:41,480 is very primitive. 98 00:05:41,480 --> 00:05:44,130 It doesn't really get you much. 99 00:05:44,130 --> 00:05:47,900 So what you can do is you can improve your physics 100 00:05:47,900 --> 00:05:52,307 simulation by adding a whole bunch more things on top of 101 00:05:52,307 --> 00:05:57,640 it, like shadows, and reflections, and global 102 00:05:57,640 --> 00:06:00,090 illumination. 103 00:06:00,090 --> 00:06:03,450 All sorts of fun stuff. 104 00:06:03,450 --> 00:06:07,430 So if you want to start simulating shadows, and 105 00:06:07,430 --> 00:06:10,830 reflections, and defraction, you need to start recursively 106 00:06:10,830 --> 00:06:15,460 casting rays, which is called ray tracing. 107 00:06:15,460 --> 00:06:20,950 So when your ray bounces off of an object, you can cast a 108 00:06:20,950 --> 00:06:25,690 reflection ray at the same angle mirrored from the 109 00:06:25,690 --> 00:06:30,600 perpendicular of the object and see what color object that 110 00:06:30,600 --> 00:06:33,940 ray hits, and then, shade this object with that color. 111 00:06:33,940 --> 00:06:38,010 You can also cast a refracted ray, and use Snell's a lot to 112 00:06:38,010 --> 00:06:41,520 tell which direction it goes in and do the same thing. 113 00:06:41,520 --> 00:06:44,530 And then, you can cast a shadow ray at all the light 114 00:06:44,530 --> 00:06:46,470 sources in the scene. 115 00:06:46,470 --> 00:06:49,370 And if the shadow ray hits an object before hitting the 116 00:06:49,370 --> 00:06:52,570 light source, then you know that this object is in the 117 00:06:52,570 --> 00:06:54,420 shade and make it darker. 118 00:06:57,210 --> 00:07:02,430 And obviously, you have to limit the recursion depth for 119 00:07:02,430 --> 00:07:05,380 your recursive tracing, because otherwise, you'll 120 00:07:05,380 --> 00:07:06,630 never finish rendering. 121 00:07:11,570 --> 00:07:18,240 So, first way of improving lighting is to start using 122 00:07:18,240 --> 00:07:22,380 direct illumination, which is you have a light source at the 123 00:07:22,380 --> 00:07:23,940 top of the scene. 124 00:07:23,940 --> 00:07:29,320 And when you hit something like a wall here, you then 125 00:07:29,320 --> 00:07:32,900 bounce a ray at the light source. 126 00:07:32,900 --> 00:07:36,320 And depending on how far this intersection A is from the 127 00:07:36,320 --> 00:07:40,350 light source, you shade either brighter or darker. 128 00:07:40,350 --> 00:07:43,120 So it might be hard to see in this light, but the wall gets 129 00:07:43,120 --> 00:07:47,170 darker the further away it is from the light. 130 00:07:47,170 --> 00:07:51,960 And then, you do shadows the same way, except at point B 131 00:07:51,960 --> 00:07:56,530 here, the rate it bounces from the ground hits this ball. 132 00:07:56,530 --> 00:07:58,400 And so, point B is in the shade. 133 00:08:01,150 --> 00:08:03,990 Now, you can improve on shadows by making what's 134 00:08:03,990 --> 00:08:06,490 called soft shadows. 135 00:08:06,490 --> 00:08:09,160 Soft shadows are shadows that form a gradient 136 00:08:09,160 --> 00:08:10,950 from darker to lighter. 137 00:08:10,950 --> 00:08:13,200 They happen when you have a light source that's not a 138 00:08:13,200 --> 00:08:14,360 point light. 139 00:08:14,360 --> 00:08:16,510 So if you look at the shadows around you in the room, you'll 140 00:08:16,510 --> 00:08:19,420 see there are gradients, not just a 141 00:08:19,420 --> 00:08:21,540 solid block of darkness. 142 00:08:21,540 --> 00:08:24,660 And the way you achieve that is when you get an 143 00:08:24,660 --> 00:08:29,430 intersection, when your ray intersects a point, you cast 144 00:08:29,430 --> 00:08:32,289 multiple shadow rays at different points along the 145 00:08:32,289 --> 00:08:33,390 light source. 146 00:08:33,390 --> 00:08:36,250 And depending on how many of these rays hit something 147 00:08:36,250 --> 00:08:41,039 before hitting the light source, you shade some 148 00:08:41,039 --> 00:08:45,570 proportion of the light being casted, or the light coming 149 00:08:45,570 --> 00:08:46,980 from this light source. 150 00:08:46,980 --> 00:08:50,890 So you can see point D here, none of the rays hit the light 151 00:08:50,890 --> 00:08:53,480 source, and so, it's in a very dark area. 152 00:08:53,480 --> 00:08:56,300 Whereas point C, one of the rays hits the light source, 153 00:08:56,300 --> 00:08:58,130 and so it's in a slightly brighter area. 154 00:09:03,590 --> 00:09:07,620 And then reflection and refraction work very similarly 155 00:09:07,620 --> 00:09:12,880 to shadows, except instead of bouncing from here to the 156 00:09:12,880 --> 00:09:17,430 light source, you bounce off this metal ball at the angle 157 00:09:17,430 --> 00:09:18,680 of reflection. 158 00:09:18,680 --> 00:09:22,650 And you bounce through this glass ball using the angle of 159 00:09:22,650 --> 00:09:23,260 refraction. 160 00:09:23,260 --> 00:09:25,290 You can calculate using Snell's law. 161 00:09:31,650 --> 00:09:33,590 Any questions before we move on? 162 00:09:43,270 --> 00:09:43,920 All right. 163 00:09:43,920 --> 00:09:49,320 So as you can see pretty clearly in this image, there's 164 00:09:49,320 --> 00:09:50,400 still some problems. 165 00:09:50,400 --> 00:09:52,620 The ceiling is completely black. 166 00:09:52,620 --> 00:09:55,170 Shadows are mostly black. 167 00:09:55,170 --> 00:10:00,100 And there's no light being transmitted through this glass 168 00:10:00,100 --> 00:10:02,810 ball either into the shadow here. 169 00:10:02,810 --> 00:10:05,730 So there's definitely some improvements we can make on 170 00:10:05,730 --> 00:10:08,310 our physics simulation. 171 00:10:08,310 --> 00:10:11,100 The first thing we're going to look at is called global 172 00:10:11,100 --> 00:10:15,410 illumination, which is pretty much any sort of illumination 173 00:10:15,410 --> 00:10:17,650 that doesn't come directly from light sources. 174 00:10:20,230 --> 00:10:24,090 Now the reason this sort of illumination is important is 175 00:10:24,090 --> 00:10:29,240 that there's actually a lot of light that gets bounced around 176 00:10:29,240 --> 00:10:31,360 the room that doesn't come directly 177 00:10:31,360 --> 00:10:33,780 from the light source. 178 00:10:33,780 --> 00:10:36,945 Like that white wall, if I shine this laser pointer at 179 00:10:36,945 --> 00:10:38,730 it, as you can see, there's still a lot of light 180 00:10:38,730 --> 00:10:40,580 bouncing off of it. 181 00:10:40,580 --> 00:10:42,330 It's not all being absorbed. 182 00:10:42,330 --> 00:10:51,590 So what you can do is you can take your ray intersection, 183 00:10:51,590 --> 00:10:55,460 and you can use what's called the Monte Carlo method, which 184 00:10:55,460 --> 00:11:00,330 is you take a whole bunch of random samples and interpolate 185 00:11:00,330 --> 00:11:05,020 those samples to get some value that approximates what 186 00:11:05,020 --> 00:11:07,550 the illuminant should be here. 187 00:11:07,550 --> 00:11:09,950 The problem with that, though, is that when you trace 188 00:11:09,950 --> 00:11:13,430 backwards, you get this exponential explosion of 189 00:11:13,430 --> 00:11:16,950 recursively cast rays. 190 00:11:16,950 --> 00:11:19,940 And you have no guarantee that any of these paths will ever 191 00:11:19,940 --> 00:11:22,860 get to this light source. 192 00:11:22,860 --> 00:11:29,130 So what you have to do is trace forwards, so you start 193 00:11:29,130 --> 00:11:32,500 at the light source, and you cast photons in random 194 00:11:32,500 --> 00:11:38,040 directions and let them bounce all around the scene until 195 00:11:38,040 --> 00:11:39,750 they get absorbed. 196 00:11:39,750 --> 00:11:44,600 Now, when a photon bounces, a bunch of light gets absorbed. 197 00:11:44,600 --> 00:11:46,650 But not all the light. 198 00:11:46,650 --> 00:11:51,090 And so, the rest of the light keeps reflecting. 199 00:11:51,090 --> 00:11:54,090 And depending on the kind of material, different amounts 200 00:11:54,090 --> 00:11:56,700 and colors of light will be reflected. 201 00:11:56,700 --> 00:11:58,670 So as you can see, this ray here bounces 202 00:11:58,670 --> 00:12:00,060 off the orange wall. 203 00:12:00,060 --> 00:12:03,270 And so, orange light gets reflected. 204 00:12:03,270 --> 00:12:05,380 This ray bounces off the blue wall. 205 00:12:05,380 --> 00:12:07,280 And so, blue light gets reflected. 206 00:12:07,280 --> 00:12:10,330 And this ray bounces off this metal ball, and so it gets 207 00:12:10,330 --> 00:12:15,600 completely reflected up towards the ceiling. 208 00:12:15,600 --> 00:12:19,880 So if you remember each bounce in something called a photon 209 00:12:19,880 --> 00:12:25,190 map, then later, you can come back and say, oh, there's so 210 00:12:25,190 --> 00:12:29,800 many photons in this area. 211 00:12:29,800 --> 00:12:31,560 And that's what these yellow circles are. 212 00:12:31,560 --> 00:12:36,136 So the illuminance is going to be approximately this. 213 00:12:36,136 --> 00:12:40,870 The problem with that is unless you're simulating an 214 00:12:40,870 --> 00:12:45,090 extremely large number of photons, you'll have an uneven 215 00:12:45,090 --> 00:12:47,060 distribution. 216 00:12:47,060 --> 00:12:50,650 So what you can do is you can hybridize forwards ray tracing 217 00:12:50,650 --> 00:12:52,970 and the Monte Carlo method. 218 00:12:52,970 --> 00:12:56,400 So you construct your photon map, which is represented in 219 00:12:56,400 --> 00:13:00,170 this image by all the red dots scattered everywhere. 220 00:13:00,170 --> 00:13:03,150 And then you do backwards ray tracing. 221 00:13:03,150 --> 00:13:08,030 Then when you reach a point, you randomly sample different 222 00:13:08,030 --> 00:13:11,780 reflection rays and average their illuminance to get the 223 00:13:11,780 --> 00:13:13,800 illuminance at this point. 224 00:13:13,800 --> 00:13:17,290 So you have blue illuminance from the wall here. 225 00:13:17,290 --> 00:13:21,720 And then light gray from here and darker gray from here. 226 00:13:21,720 --> 00:13:25,430 And the result is this sort of pale blue, which you can't 227 00:13:25,430 --> 00:13:27,880 really see in the image because the light's bad. 228 00:13:30,880 --> 00:13:35,060 Yeah, so backwards trace, randomly sample, illumination. 229 00:13:40,530 --> 00:13:44,054 And any questions before we go on? 230 00:13:54,880 --> 00:14:01,260 So it turns out even with building a photon map and then 231 00:14:01,260 --> 00:14:04,300 randomly sampling the photon map, calculating the 232 00:14:04,300 --> 00:14:07,120 irradiance can still be very expensive. 233 00:14:07,120 --> 00:14:11,390 So what we've implemented to speed that up is what's called 234 00:14:11,390 --> 00:14:14,360 an irradiance cache. 235 00:14:14,360 --> 00:14:17,550 And the way the irradiance cache works is there are 236 00:14:17,550 --> 00:14:23,030 certain areas on objects that will have pretty much the same 237 00:14:23,030 --> 00:14:24,840 irradiance. 238 00:14:24,840 --> 00:14:26,360 It's pretty much the same irradiance levels. 239 00:14:26,360 --> 00:14:30,300 So you can interpolate from nearby points. 240 00:14:30,300 --> 00:14:33,390 So what we do with the irradiance cache is as the 241 00:14:33,390 --> 00:14:40,670 scene is rendering, if there is a cache miss, then we 242 00:14:40,670 --> 00:14:45,050 calculate the irradiance for some pixel which corresponds 243 00:14:45,050 --> 00:14:47,290 to a point in the scene. 244 00:14:47,290 --> 00:14:50,840 And then, if we then trace a ray very near to that same 245 00:14:50,840 --> 00:14:56,880 pixel, you can then interpolate from nearby cached 246 00:14:56,880 --> 00:15:03,000 irradiances and get something that looks right. 247 00:15:03,000 --> 00:15:07,800 So if you take a look at this image, where the green dots 248 00:15:07,800 --> 00:15:14,940 represent cache misses on these big planer objects here. 249 00:15:14,940 --> 00:15:17,150 There's very few cache misses. 250 00:15:17,150 --> 00:15:20,650 And it's almost entirely cache hits. 251 00:15:20,650 --> 00:15:26,850 Whereas at the interface between different objects, the 252 00:15:26,850 --> 00:15:31,160 cache misses become much more common. 253 00:15:31,160 --> 00:15:34,650 But overall, the number of irradiance calculations is 254 00:15:34,650 --> 00:15:35,900 greatly reduced. 255 00:15:39,650 --> 00:15:44,400 And then, the third kind of illumination that we implement 256 00:15:44,400 --> 00:15:46,270 is called caustics. 257 00:15:46,270 --> 00:15:53,220 Caustics are just light being lensed into patterns of 258 00:15:53,220 --> 00:15:58,520 brighter and darker spaces, such as you get from a light 259 00:15:58,520 --> 00:16:00,340 pointing towards a glass ball. 260 00:16:00,340 --> 00:16:03,310 Or if you ever see the sort of wavy pattern on the bottom of 261 00:16:03,310 --> 00:16:06,720 a pool, that's also from caustics. 262 00:16:06,720 --> 00:16:11,100 Caustics are also implemented with the photon map, except 263 00:16:11,100 --> 00:16:12,660 it's created slightly differently. 264 00:16:12,660 --> 00:16:15,770 Instead of bouncing photons everywhere, photons are 265 00:16:15,770 --> 00:16:19,070 absorbed as soon as they hit a diffuse object. 266 00:16:19,070 --> 00:16:24,840 So a photon might bounce off of this metal sphere and then 267 00:16:24,840 --> 00:16:29,010 hit the wall, and it will be immediately absorbed. 268 00:16:29,010 --> 00:16:32,330 Whereas if it hits this sphere, it'll bounce, and 269 00:16:32,330 --> 00:16:35,000 bounce, and then be absorbed. 270 00:16:35,000 --> 00:16:41,240 So the objective of caustics is to trace how photons are 271 00:16:41,240 --> 00:16:43,750 grouped based on the optical properties of 272 00:16:43,750 --> 00:16:45,000 materials in the scene. 273 00:16:47,700 --> 00:16:54,075 And then, caustics are rendered pretty simply. 274 00:16:56,740 --> 00:17:03,330 Because there's much fewer photons than in the photon map 275 00:17:03,330 --> 00:17:05,920 for global illumination, you don't need to use an 276 00:17:05,920 --> 00:17:07,440 irradiance cache. 277 00:17:07,440 --> 00:17:13,410 So you simply trace a ray to a point and then sample the 278 00:17:13,410 --> 00:17:15,359 number of photons nearby. 279 00:17:15,359 --> 00:17:18,700 And you end up with something like this if you just do 280 00:17:18,700 --> 00:17:19,950 caustics shading. 281 00:17:24,790 --> 00:17:29,810 So then at the end, you take your direct illumination, and 282 00:17:29,810 --> 00:17:33,380 then add it to caustics, and then add it to global 283 00:17:33,380 --> 00:17:34,800 illumination. 284 00:17:34,800 --> 00:17:36,250 And you get something that looks like this. 285 00:17:40,070 --> 00:17:43,060 Any questions before we go to the code overview? 286 00:17:49,140 --> 00:17:49,610 Go ahead. 287 00:17:49,610 --> 00:17:53,000 AUDIENCE: Do we weight them equally? 288 00:17:53,000 --> 00:17:54,110 PROFESSOR: So the question was, do 289 00:17:54,110 --> 00:17:55,830 we weight them equally? 290 00:17:55,830 --> 00:18:00,150 And the answer is yes. 291 00:18:00,150 --> 00:18:03,470 And you just sum them together. 292 00:18:09,290 --> 00:18:13,560 Ideally, you were clever and constructed your ray tracer so 293 00:18:13,560 --> 00:18:17,510 that they should be weighted equally, because there's 294 00:18:17,510 --> 00:18:20,080 weighting done internally, basically. 295 00:18:32,380 --> 00:18:36,630 So next, we're going to do a quick overview of the code 296 00:18:36,630 --> 00:18:39,610 that will be in your repositories just get you 297 00:18:39,610 --> 00:18:42,055 started and help you understand how it works. 298 00:18:46,080 --> 00:18:49,200 First, we're going to start by explaining some of the classes 299 00:18:49,200 --> 00:18:51,030 you'll find. 300 00:18:51,030 --> 00:18:57,580 First is SceneObject, which is the parent class for all of 301 00:18:57,580 --> 00:19:00,530 the objects that will be used to construct the scenes you'll 302 00:19:00,530 --> 00:19:03,030 be rendering. 303 00:19:03,030 --> 00:19:06,900 Every scene object has a ray intersection method that's 304 00:19:06,900 --> 00:19:09,830 called whenever you trace a ray. 305 00:19:09,830 --> 00:19:14,100 And it'll return where the ray 306 00:19:14,100 --> 00:19:19,700 intersection is and the object. 307 00:19:19,700 --> 00:19:23,310 And SceneObject is inherited by these objects here, which 308 00:19:23,310 --> 00:19:31,120 all of the objects will inherit from. 309 00:19:31,120 --> 00:19:36,700 Then we've got the Raytracer class, which 310 00:19:36,700 --> 00:19:38,960 contains the main method. 311 00:19:38,960 --> 00:19:40,920 So it starts the program. 312 00:19:40,920 --> 00:19:45,770 And it also contains methods for constructing the scene and 313 00:19:45,770 --> 00:19:50,760 then rendering the scene once you've 314 00:19:50,760 --> 00:19:53,690 constructed the scene first. 315 00:19:53,690 --> 00:19:58,150 We have the LightSource class, which is a base class for any 316 00:19:58,150 --> 00:19:59,450 light sources. 317 00:19:59,450 --> 00:20:04,330 In our ray tracer, the only subclass is square photon 318 00:20:04,330 --> 00:20:10,660 light, which is this light at the top of the scene here. 319 00:20:10,660 --> 00:20:15,520 And what that does, or what the light class does, is it 320 00:20:15,520 --> 00:20:19,990 contains methods for constructing the photon maps 321 00:20:19,990 --> 00:20:24,900 and getting illumination from either global, or caustic, or 322 00:20:24,900 --> 00:20:27,890 direct sources. 323 00:20:27,890 --> 00:20:32,720 And then there's also the iCache class, which 324 00:20:32,720 --> 00:20:35,610 essentially just implements the iCache for you. 325 00:20:39,820 --> 00:20:41,070 Any questions? 326 00:20:47,600 --> 00:20:52,530 So basic execution overview. 327 00:20:52,530 --> 00:20:55,770 You have the main method for Raytracer. 328 00:20:55,770 --> 00:20:57,910 Parses the command line. 329 00:20:57,910 --> 00:20:59,970 Constructs the scene. 330 00:20:59,970 --> 00:21:02,590 And the scene is just a command line switch. 331 00:21:02,590 --> 00:21:05,750 So if you say S1, it'll construct scene one. 332 00:21:05,750 --> 00:21:09,780 If you say S2, it'll construct scene two. 333 00:21:09,780 --> 00:21:12,680 Then for each light source, it tells the light source to make 334 00:21:12,680 --> 00:21:16,800 photon maps for the global and caustic illumination. 335 00:21:16,800 --> 00:21:19,058 GUEST SPEAKER: I should mention that we actually moved 336 00:21:19,058 --> 00:21:22,502 the main functionality into [INAUDIBLE] 337 00:21:22,502 --> 00:21:23,486 so you can replace the data. 338 00:21:23,486 --> 00:21:25,454 You don't have to touch that. 339 00:21:25,454 --> 00:21:28,160 It just constructs the scene, so that we can then add a 340 00:21:28,160 --> 00:21:29,390 scene [INAUDIBLE] from that. 341 00:21:29,390 --> 00:21:31,100 So you shouldn't need to do that. 342 00:21:31,100 --> 00:21:31,670 PROFESSOR: OK. 343 00:21:31,670 --> 00:21:37,020 So that's changed then. 344 00:21:37,020 --> 00:21:40,890 Then, main calls the render function of Raytracer. 345 00:21:40,890 --> 00:21:43,800 What the render function does-- 346 00:21:43,800 --> 00:21:45,760 excuse me, method does-- 347 00:21:45,760 --> 00:21:49,720 is it casts a ray for each pixel in the scene you're 348 00:21:49,720 --> 00:21:55,440 going to render, finds the nearest intersection, and then 349 00:21:55,440 --> 00:21:58,760 computes the shading at that intersection by recursively 350 00:21:58,760 --> 00:22:02,730 bouncing rays and [? painting ?] the illuminance 351 00:22:02,730 --> 00:22:03,980 cache for illumination. 352 00:22:07,670 --> 00:22:11,720 And that's basically what the Raytracer does. 353 00:22:11,720 --> 00:22:13,900 It's pretty simple. 354 00:22:13,900 --> 00:22:18,740 So we're also going to quickly go over how the light source 355 00:22:18,740 --> 00:22:23,810 traces photons for direct and global illumination. 356 00:22:23,810 --> 00:22:30,320 The difference is kind of settled, but pretty important. 357 00:22:30,320 --> 00:22:36,160 With global illumination, you want some total number of 358 00:22:36,160 --> 00:22:38,710 photons to be put in the map. 359 00:22:38,710 --> 00:22:45,380 And you put a photon in the map whenever it hits some 360 00:22:45,380 --> 00:22:46,630 diffuse surface. 361 00:22:50,460 --> 00:22:55,040 And then, that photon then continues bouncing around 362 00:22:55,040 --> 00:22:56,290 until it's absorbed. 363 00:22:58,380 --> 00:23:01,660 For caustic illumination, it's pretty similar. 364 00:23:01,660 --> 00:23:04,640 You have some set number of photons you want to 365 00:23:04,640 --> 00:23:06,400 get into the map. 366 00:23:06,400 --> 00:23:10,410 You cast a ray in some random direction. 367 00:23:10,410 --> 00:23:15,960 And then, the ray intersects with a diffuse material. 368 00:23:15,960 --> 00:23:18,990 You store the intersection in the photon map. 369 00:23:18,990 --> 00:23:19,850 And then you break. 370 00:23:19,850 --> 00:23:21,811 You don't keep bouncing the photons around. 371 00:23:24,900 --> 00:23:29,500 So that's basically it for the code overview. 372 00:23:29,500 --> 00:23:33,070 This slide has some resources that you can use if you want 373 00:23:33,070 --> 00:23:34,770 to learn more about ray tracing. 374 00:23:37,370 --> 00:23:40,080 Some of these will explain concepts that we haven't 375 00:23:40,080 --> 00:23:42,840 implemented. 376 00:23:42,840 --> 00:23:45,710 They're an interesting read, but not needed if you don't 377 00:23:45,710 --> 00:23:48,210 want to read them. 378 00:23:48,210 --> 00:23:57,260 And also, there's no standard way of doing global 379 00:23:57,260 --> 00:24:00,090 illumination or direct illumination. 380 00:24:00,090 --> 00:24:03,750 So you may come across resources that do things 381 00:24:03,750 --> 00:24:07,320 slightly different than our reference implementation. 382 00:24:07,320 --> 00:24:09,740 That doesn't mean either one is wrong. 383 00:24:09,740 --> 00:24:12,480 But you should be aware that there will almost certainly be 384 00:24:12,480 --> 00:24:13,730 differences. 385 00:24:25,020 --> 00:24:28,338 Does anyone have any questions? 386 00:24:28,338 --> 00:24:28,792 Go ahead. 387 00:24:28,792 --> 00:24:30,154 AUDIENCE: So we can use one of those alternatives? 388 00:24:30,154 --> 00:24:32,190 PROFESSOR: Can you say that louder please? 389 00:24:32,190 --> 00:24:33,820 AUDIENCE: So we can use one of those alternatives? 390 00:24:36,920 --> 00:24:39,290 PROFESSOR: These aren't implementations. 391 00:24:39,290 --> 00:24:41,920 They're descriptions of how ray tracing works. 392 00:24:44,590 --> 00:24:48,920 You should not change your code to work in this way. 393 00:24:48,920 --> 00:24:52,040 These are just to help with clarification if you want to 394 00:24:52,040 --> 00:24:54,620 learn more about what's being done in the code. 395 00:24:57,796 --> 00:24:58,282 Go ahead. 396 00:24:58,282 --> 00:24:59,740 AUDIENCE: [INAUDIBLE PHRASE] 397 00:24:59,740 --> 00:25:06,544 what specifically are we allowed to change for the 398 00:25:06,544 --> 00:25:08,280 [INAUDIBLE]? 399 00:25:08,280 --> 00:25:10,230 PROFESSOR: So what specifically are 400 00:25:10,230 --> 00:25:11,003 you allowed to change? 401 00:25:11,003 --> 00:25:13,280 AUDIENCE: Yeah. 402 00:25:13,280 --> 00:25:14,530 Which ever one's smaller [INAUDIBLE PHRASE]. 403 00:25:18,240 --> 00:25:22,280 PROFESSOR: Anything not prohibited by these rules, 404 00:25:22,280 --> 00:25:28,390 which are paraphrased from the project handout, you can do. 405 00:25:28,390 --> 00:25:31,650 So basically, as long as you're still performing the 406 00:25:31,650 --> 00:25:36,756 same essential calculations, you're fine. 407 00:25:36,756 --> 00:25:37,254 Hmm? 408 00:25:37,254 --> 00:25:40,574 GUEST SPEAKER: I would say the criteria for correctness is to 409 00:25:40,574 --> 00:25:42,860 look at the images, and say that they look 410 00:25:42,860 --> 00:25:43,160 more or less the same. 411 00:25:43,160 --> 00:25:46,060 They don't give you a pixel by pixel accurate. 412 00:25:46,060 --> 00:25:51,020 This isn't a graphics class that actually does that. 413 00:25:51,020 --> 00:25:56,000 This should be almost like a [INAUDIBLE]. 414 00:25:56,000 --> 00:25:58,180 PROFESSOR: They should visually look the same. 415 00:26:03,970 --> 00:26:12,160 If you find some approximation that looks almost the same and 416 00:26:12,160 --> 00:26:15,880 runs 100 times faster, go for it. 417 00:26:15,880 --> 00:26:19,570 You can't say, for example, simplify your ray tracer 418 00:26:19,570 --> 00:26:21,720 though by saying, OK, we're only going to be able to 419 00:26:21,720 --> 00:26:23,380 render these two scenes, but we'll be able to 420 00:26:23,380 --> 00:26:24,630 do it really fast.