16
DH2323 Individual Project Advanced Ray Tracing DidrikNordstr¨om [email protected] August 13, 2013 Abstract A basic light simulator was implemented for rendering of 3D mod- els using ray tracing with the goal of producing realistic images with clearly visible optical phenomenas like indirect illumination, lens aber- ration and depth of field. An optical system consisting of a thin lens and a light sensor was modeled and tuned to act as a camera in the scene. Support for materials of lambertian and specular types was implemented, as well as an area light source. For user convenience, progressive and multi threaded rendering was incorporated in the im- plementation. The resulting images were noisy but had more realistic lighting than images produced with simpler rendering engines. It was concluded that light simulation using ray tracing is computationally expensive and relies on fast approximative algorithms to be usable in production environments. 1

Advanced Ray Tracing

Embed Size (px)

DESCRIPTION

Report about a basic 3D light simulator which was implemented using ray tracing, producing realistic images.

Citation preview

DH2323 Individual Project

Advanced Ray Tracing

Didrik [email protected]

August 13, 2013

Abstract

A basic light simulator was implemented for rendering of 3D mod-els using ray tracing with the goal of producing realistic images withclearly visible optical phenomenas like indirect illumination, lens aber-ration and depth of field. An optical system consisting of a thin lensand a light sensor was modeled and tuned to act as a camera in thescene. Support for materials of lambertian and specular types wasimplemented, as well as an area light source. For user convenience,progressive and multi threaded rendering was incorporated in the im-plementation. The resulting images were noisy but had more realisticlighting than images produced with simpler rendering engines. It wasconcluded that light simulation using ray tracing is computationallyexpensive and relies on fast approximative algorithms to be usable inproduction environments.

1

Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4.1 Optical system . . . . . . . . . . . . . . . . . . . . . . 54.2 Materials . . . . . . . . . . . . . . . . . . . . . . . . . 64.3 The direction of light . . . . . . . . . . . . . . . . . . 64.4 Area light source . . . . . . . . . . . . . . . . . . . . . 74.5 Indirect illumination . . . . . . . . . . . . . . . . . . . 74.6 Tuning the optical system . . . . . . . . . . . . . . . . 84.7 Rendering algorithm . . . . . . . . . . . . . . . . . . . 10

5 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125.1 Progressive rendering . . . . . . . . . . . . . . . . . . 135.2 Image resolution . . . . . . . . . . . . . . . . . . . . . 135.3 Depth of field . . . . . . . . . . . . . . . . . . . . . . . 145.4 Indirect illumination . . . . . . . . . . . . . . . . . . . 15

6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2

1 Introduction

The most common application for computer graphics is real-time renderingof complex 3D scenes; the advanced and expensive graphics cards of todayare a strong indication of that. We want to produce the best visual results,taking whatever shortcuts, cheats and tricks available, but we must alwaysmaintain high frame rates. Most of the time, that is what we use computergraphics for, and it is almost always in the context of some interactive medialike computer games.

Most of the time we need fast rendering, but sometimes we want the bestpossible image, and we accept that it might take a while, or even very long,for the computer to produce a single image. Architects, product designers,animated movie producers and others sometimes need to create photo real-istic images in order to achieve their goals. Even though it is impossible toavoid simplifications completely, this project is about simulating the basicphysics of light. If done properly, it can yield photo realistic images, muchmore accurate than what can be achieved with real-time graphics.

2 Purpose

The purposes of this project is to implement formulas from physics that pro-duce visible optical phenomena like depth of field, aberration and global illu-mination. Approximations and simplifications are allowed, both to simplifyalgorithms that can be difficult to implement and to improve performance.The purpose is not to implement existing rendering algorithms. It is ratherabout discovering relations between physical formulas and computation, andalso about understanding concepts about optics by using the renderer.

3

3 Prerequisites

The project is implemented in C++11 utilizing source code from the otherlabs in the computer graphics course. The external code base consists ofSDL (Simple DirectMedia Layer) for screen and pixel operations, and GLM(OpenGL Mathematics) as a linear algebra library.

The main source file consists of a main function, a rendering loop and afunction for finding the closest intersection between a ray and the triangles inthe scene (with custom lazy evaluation of solutions to the equation system,improving intersection detection speed with up to 200%).

Also provided is an instance of the Cornell box which is used as therendering subject throughout the lab.

(a) Realtime rasterized render (b) Raytraced render with shadows

Figure 1: The Cornell box scene used in previous labs

4

4 Method

4.1 Optical system

The first step in coming closer to a physical model of light is to exchangethe pinhole camera, used in the previous labs, for a slightly more advancedoptical system. One of the simplest optical systems that can produce animage consists of a rectangular light sensor and a thin lens, which is whatwe will use. The benefit of the thin lens is that it can be approximated to aflat surface, which enables fast and easy intersection detections. This meansthat the rays enter and exit the lens at the same point, which is never truein reality, but is a common approximation. The fact that we actually usea lens to focus the light beams will automatically enable for depth of field,which is an optical phenomena that the simple ray tracer and rasterizer didnot have. Tuning of the optical system will be explained later.

Figure 2: Setup of scene and camera, objects not according to scale. Thered box is the Cornell box (without objects), the blue disc is the thin lens,the yellow plane is the light sensor, the black line is the optical axis and thegreen dot is the subject.

4.1.1 Definitions

S1: Distance between the lens and subject

S2: Distance between the lens and the light sensor

R: Lens radius (not related to curvature)

LW , LH : Dimensions of the light sensor

5

4.1.2 Thin lens arithmetic

To simulate a lens properly, one should calculate the refraction twice, oncewhen the ray enters the lens and once when it exits. This also requirescalculation of the normals. Fortunately, for a thin lens, it is much simpler,both for the programmer and for the computer. Assume the thin lens havefocal length f . Let n1 be the normalized incident ray direction, let n2 bethe outgoing refracted ray direction and let r be the intersection point onthe lens, relative to the lens center. Note also that n1, n2 and r needs to betranslated according to the optical axis. Then n2 can be computed easily:

n2 = n1 −r

f(1)

4.2 Materials

In computer graphics, a BRDF (Bidirectional Reflectance Distribution Func-tion) describes, among other things, how light gets reflected depending onthe angle of the incoming light. The easiest BRDF to implement is Lamber-tian reflectance which is a perfectly diffuse surface that reflects light evenlyin all directions, regardless of the angle of the incident ray. This is solvedby randomizing a vector within a box of side 2 and checking if the vectoris within the unit sphere and the dot-product of the vector and the surfacenormal is positive; if it fails it tries again.

As an experiment, another material was created which is attached to theleft wall in the scene. This material first reduces the incoming light strengthby half. It incorporates specular reflection, but with small randomized dis-tortion in order to create the effect of a rippled glass mirror.

4.3 The direction of light

For the ray tracing, it was discovered that the best direction of shootingphotons is from the camera and towards the scene. Some experiments wereconducted with shooting rays from the light source and hoping they wouldbounce on the geometry and eventually enter the camera but they yieldedweak results, mainly due to that the lens is so small that it only catchesa small fraction of the rays. This reversed model should however not be aproblem, since the optical models used are symmetrical.

6

4.4 Area light source

With the decision to reverse the direction of light, an immediate problemwas raised; how would randomized rays hit a point light source? The answeris that they would not. Instead, the point light source from previous labsneeded to be exchanged for an area light source, which was made quitelarge and was placed just in front of the cube on the left side. Anotherbenefit of the area light source is that it is more physically correct and thusyields more realistic results; the most significant distinction is probably softshadows. The light source itself is not rendered due to that its size andintensity would dominate in the final image.

4.5 Indirect illumination

Indirect illumination was an important part of the project. The visual goalis to lighten up areas that are not directly illuminated by the light source.These areas are instead lit up by surrounding bright areas which in somescenes makes a big difference. This is often referred to as ambient lighting.

In the implementation, the rays may bounce a constant number of times,B. If they have not reached the light source by then, they are canceled. Theonly exception is the mirror surface on the left; if a ray hits it, that reflectionwill not contribute to the count.

4.5.1 Color mixing

Another problem that was not required to solve in the previous labs was howto compute the color of a ray that is bouncing on a colored surface. In reality,white light is a mixture of light of many visible wavelengths (colors); anda colored diffuse material reflects many of the wavelengths, some strongerthan others. In the computer’s color model, colors are simply a mixture ofred, green and blue. It is safe to say that if the color of a surface is red, andit receives a ray of white light, the reflected ray should now be red. But whatif the incident ray is red and the surface color is orange, what color shouldthe reflected ray get? Since the underlying model isn’t physically correct,it is hard to correct that mistake in this phase. In this implementation thecolor mixing is performed by taking the element-wise product of each of thecolor components. This can be seen in equation 2, where I is the color of theincident ray, S is the color of the surface and R is the color of the reflectedray.

R = (IRSR, IGSG, IBSB) (2)

7

4.6 Tuning the optical system

In optical systems like cameras, there are countless of configurations possibleto photograph an object. To narrow the problem down, some parameterswill be assigned based on reasonable guesses, as it will make it possible tocompute the other parameters.

Figure 3: Determining angle of view

First of all, the distance between the lens and the focused object is keptthe same as in the previous labs, S1 = 2.9. It is now possible to determinethe angle of view, α, because the entire box should be visible in the image. Ifwe look at figure 3 we can calculate the angle using trigonometric functions:

α = 2 tan−1

(1

1.9

)≈ 55◦ (3)

And given the formula for angle of view,

α = 2 tan−1

(L

2f

)(4)

and a fixed sensor size, e.g. LW = LH = L = 0.1, we can compute thefocal length f :

L

2f=

1

1.9(5)

which gives us f = 0.095.Using the formula for thin lenses,

1

S1+

1

S2=

1

f(6)

we can subsequently determine S2 ≈ 0.0982. These parameters can nowremain fixed.

8

4.6.1 Depth of field

There is only one parameter left in the optical system, the aperture (or lensradius) R. In reality, this parameter will alter the brightness in the finalimage, but since all rays are sent through the lens no matter how small it is,the quantity of light passing through does not change. A common way tomeasure aperture diameter is with the f-number value N , which describesthe aperture diameter in relation to the focal length; typical values are2 ≤ N ≤ 16 for cameras. A higher number means smaller aperture.

2R =f

N(7)

The aperture will affect the depth of field, i.e. the distance between thenearest and the furthest object which is acceptably sharp. The smaller theaperture, the larger is the depth of field.

We first need to determine what is acceptably sharp. The circle of con-fusion, c, is a measurement of how large an optical spot is allowed to be inorder to be perceived as sharp when looking at an image. For the purposeof keeping the project simple we will use the commonly used approximationc = d/1500, where the d is the diagonal of the sensor, in this case d =

√2L.

In the following formulas, H is the hyperfocal distance (which for thepurpose of the project is only needed for computing depth of field), DN isthe near limit and DF is the far limit for the depth of field. Thus, the depthof field is DF −DN .

H =f2

Nc+ f (8)

DN =S1H

H + S1 − f(9)

DF =S1H

H − S1 − f(10)

The f-number’s impact on the depth of field can be inspected in table 1.

9

N H DN DF DF −DN

2 47.957 2.740 3.093 0.353

4 24.026 2.597 3.313 0.716

8 12.061 2.353 3.858 1.505

16 6.078 1.984 5.717 3.733

32 3.086 1.519 97.934 96.415

Table 1: Depth of field, DF −DN , varying as a function of aperture sizewhich is described by the f-number N . Subject distance S1 = 2.9.

4.7 Rendering algorithm

The idea of the rendering algorithm is to shoot a ray from a random locationon the light sensor, through a random location on the lens and then computewhere it ends up using the material and light models explained previously.When a ray hits the light source, the corresponding cell on the light sensorwill increase in intensity. A noteworthy observation is that the lens will flipthe image both horizontally and vertically which need to be accounted forduring drawing.

4.7.1 Light sensor

The light sensor consists of p number of cells, one per pixel, usually in theinterval 1002 ≤ p ≤ 8002, with one float value per color component per cell.These values are incremented with the computed light intensity of the raywhich hit the light source. Note that these float values are not limited to beless than one, but can be of arbitrary size.

4.7.2 Progressive rendering

To fully understand the renderer, one needs to be familiar with the distinc-tion between ray tracing and drawing. Ray tracing is the intense compu-tational work of tracing the light rays in the scene, in this respect it alsoinvolves the alteration of the light sensor’s color values. Drawing is simplythe process of traversing the cells in the light sensor and drawing the colorsto the screen. Ray tracing and drawing is performed asynchronously, ormore precisely, ray tracing is performed in parallel and drawing is done inthe main thread at a fixed interval. This allows for the render to be pro-

10

gressive and the user may stop the rendering when satisfied with the result.Longer render duration, t, yields more rays traced, r, which should improvethe result.

4.7.3 Multi threaded ray tracing

The only variable the ray tracing function updates is the sensor cells. Thisis very convenient, because it gets easy to delegate the work to multipleCPU cores. This is done using simple C++ threads. Since the threads mayupdate the same cell at the same time, a mutual exclusion lock is protectingthe sensor from concurrent alterations.

4.7.4 Exposure

In order to control the exposure of the image, the total amount of intensityfor all rays that have been traced are stored in a global variable and is usedfor computing the average intensity per cell before each frame is rendered.When a cell is drawn, the color value in the cell is then divided by theaverage per cell intensity, which results in that the image will never be over-or underexposed. The exposure can also be fine-tuned manually using thearrow keys during rendering.

11

5 Results

All images are rendered with a 2.4 GHz Intel Core i7 CPU with four physicalcores, and one ray tracing thread per each of the eight virtual cores. In figure4, a typical rendering can be seen. The image is sharp but surfaces are noisydue to that the rays are randomized. The difference between the reflectivematerial on the left wall and the diffuse surfaces are clearly visible, just likethe soft shadows from the red cube, as a result of the area light source. Therendering time of 71.6 minutes is quite long with respect to the simplicityof the scene, and compared to commercial photo realistic rendering engines.Output shows that around 9.5% of the rays hit the light source; meaningthat more than 90% of the computations were not contributing to the imageat all.

Figure 4: p = 8002, B = 2, N = 16, r = 12× 107, t = 71.6 min

12

5.1 Progressive rendering

Just as expected, the duration of the rendering has a significant impact onthe final result. In figure 5, the gradual reduction of noise over time canbe inspected. Furthermore, the total amount of light in figure 5d is about15 times higher than in figure 5c, but the images are both at acceptablebrightness levels, which confirms that the automatic exposure adjustment isworking.

(a) t = 31 s (b) t = 78 s (c) t = 4.8 min (d) t = 71.6 min

Figure 5: The duration of the render has significant impact on the result;longer time leads to less noise.

5.2 Image resolution

The greater the resolution, the more cells in the sensor and subsequentlyit is sparser between the pixels in the image that are lit up. Theoretically,the render time for an image of an acceptable noise level should correlatelinearly with the resolution, p. In figure 6, visual inspection confirms sucha relation.

(a) p = 1002 (b) p = 2002 (c) p = 4002 (d) p = 8002

Figure 6: The relationship between rendering resolution p and the noiselevel. All images have been cropped to 100× 100 pixels. t = 140 s.

13

5.3 Depth of field

As shown previously in table 1, the depth of field is reduced when the aper-ture gets larger (smaller f-number). In figure 7, this effect can be inspected.The images confirms that the focus gets shallower. But if we have a closerlook at the largest aperture, in figure 7d, there is only focus in the centreof the image even though many objects, like the edges of the box, remainwithin the depth of field. The further away from the image center one looks,the stronger the blurriness seems. This is probably a result of optical aber-ration, either because a thin lens is not of ideal shape for focusing light, orthat the implementation of the thin lens is a completely flat surface, whichbecomes a problem when the lens size gets larger.

(a) N = 16, DoF = 3.73 (b) N = 8, DoF = 1.50

(c) N = 4, DoF = 0.72 (d) N = 2, DoF = 0.35

Figure 7: The relationship between the f-number, N , and the depth offield (DoF). A larger aperture (lower f-number) results in a shallower focus.

14

5.4 Indirect illumination

With more bounces of light rays allowed, the rendering time per ray tracingoperation increases. Since the light rays loses intensity for every bounce, itmakes sense to keep the number of bounces to a minimum. In figure 8, theeffect of the indirect illumination can be inspected. Note that B = 1 impliesthat there is only direct illumination, which can be seen in figure 8a wherethe area to the right of the red cube is pitch black. In the other two images,that area has been slightly illuminated. Also, note how the ray hit rate, r(portion of rays that were successful in hitting the light source), increaseswith B.

(a) B = 1, t = 12.7 min,r = 6.1%

(b) B = 2, t = 18.3 min,r = 9.3%

(c) B = 3, t = 21.7 min,r = 12%

Figure 8: Indirect illumination. The number of bounces B, and its effecton the final image. All images have r = 3.0× 107 rays traced.

15

6 Conclusions

Overall, the results are visually satisfying. The images are more realisticthan what was produced in the previous labs. Needless to say, CPU pow-ered ray tracing is very time consuming. The sample scene used has littlegeometry; with a more complex scene the rendering time will increase sig-nificantly. For production purposes the speed is unacceptably low. Thereshould however be room for substantial performance improvements. Onecould for example utilize the GPU for matrix computations or incorporateexisting algorithms like radiosity and photon mapping. Parallelism is, asshown in the report, highly achievable which makes it possible to distributerendering over many CPU cores, and even multiple machines.

When creating a photo realistic renderer one constantly needs to makechoices between physically correct light simulations and clever approxima-tions. Thus, the true challenge is, just like with real-time graphics, to createsimplified algorithms that are both fast and accurate.

For improved light simulation, there are many optical phenomenas thatwould be interesting to implement, e.g. refractive materials, more physicallycorrect BRDFs, more advanced optical systems for simulation of cameras,customizable aperture shape and diffraction (for star-burst effects) and sub-surface scattering.

16