Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008...

32
Water Surface Rendering MorganN¨atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson Examiner: Per Lindstr¨ om Ume ˚ a University Department of Computing Science SE-901 87 UME ˚ A SWEDEN

Transcript of Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008...

Page 1: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Water Surface Rendering

Morgan Natterlund

March 6, 2008Master’s Thesis in Computing Science, 20 credits

Supervisor at CS-UmU: Pedher JohanssonExaminer: Per Lindstrom

Umea UniversityDepartment of Computing Science

SE-901 87 UMEASWEDEN

Page 2: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson
Page 3: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Abstract

Water covers 71% of our planet. This makes it an important element to simulatewhen visualising outdoor environments in the computer world. Water simulations canalmost always be divided into two parts, a physical part and a visual part. This thesistries to introduce a visualisation technique to achieve high frame rates when renderingmany water surfaces with different local reflections. This technique is aimed to workon planar surfaces with almost still water. To achieve this in real-time this thesis onlyrecalculates the reflections at specific intervals. The technique used is a linear interpo-lation between two accumulated reflections according to the current time that producea smooth transformation between reflections. The introduced approach generates a lotof overhead but it works well as long as the viewer do not move fast parallel to thereflecting surface.

Page 4: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

ii

Page 5: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Contents

1 Introduction 11.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Problem Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Water algorithms 32.1 Diffuse and Specular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Rasterization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.4 Ray-tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.5 Light model for water . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.6 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.7 Reflection interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.8 Refraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.9 Fresnel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.10 Water Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.11 Water in movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.12 Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Accomplishment 113.1 Reflection Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.1.1 Surface grading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.1.2 Reflections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.2 Render Scene Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.3 Water Stencil Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.4 Normal Generation Step . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.4.1 Surface Animation . . . . . . . . . . . . . . . . . . . . . . . . . . 143.5 Render Surfaces Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

3.5.1 Refractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.5.2 Stencil Cutoff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.5.3 Fresnel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

iii

Page 6: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

iv CONTENTS

3.5.4 Water Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4 Results 19

5 Conclusions 215.1 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.2 Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.3 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

References 22

Page 7: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

List of Figures

2.1 Diffuse and Specular, Diffuse reflection on an uneven surface such thatrays shatters in a number of angles. Specular reflection is the complementwhere rays reflect on a smooth surface. . . . . . . . . . . . . . . . . . . . 3

2.2 Ray Tracing, Rays reflecting on objects (A,B,C,D) sampling color startingfrom the viewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.3 Illumination Model, L is a ray from the light source, N is the normal ofthe surface, R is the reflected light and E is the ray from the viewer. . . 5

2.4 Reflected Camera, C is the current camera, R is the reflected camera andN is the normal of the surface. . . . . . . . . . . . . . . . . . . . . . . . 6

2.5 Refracted Camera, C is the Camera and R is the refracted camera. . . . 72.6 Mac-Grid, The solid dots represent where the pressure, density and such

are sampled. The arrows is the velocity which is shifted 1/2 units. . . . . 9

3.1 Steps, Reflection Textures are rendered in the first step, second step ren-ders the scene without alpha objects, third creates a stencil where there arewater, forth generates a normal map and finally fifth renders the watersurfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.2 Screenshot: Reflection Interpoation, Reflection calculated by interpolatingbetween the two textures in the bottom left corner. . . . . . . . . . . . . 13

3.3 Perlin, Random values are added together with different scale to producea height map that generates a normal map from the heights. . . . . . . . 15

3.4 Interactive Waves, Waves bouncing within the bounds of the elephant. . 163.5 Water Depth, The depth of the water is shown by a darker color for the

deeper parts of the water. . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4.1 Waves, Shows artifacts when the heights is not smoothed on the left sideand smoothed on the right. . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4.2 Water pool, Screen shot of the final water. . . . . . . . . . . . . . . . . . 20

v

Page 8: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

vi LIST OF FIGURES

Page 9: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Chapter 1

Introduction

1.1 Background

A general rule in computer graphics is that reflections is needed to make a scene lookwet [1]. Water has many different visual properties, two of those are that it reflectlight from the environment and that it refracts light that passes through it. These twoproperties are adjusted with a Fresnel function, which plays an important roll for aphoto-realistic water visualization. Another effect to enhance the visualization of thewater is to choose water color depending on the depth of the water.

Rendering many different reflections in a scene may be time consuming for the systemdepending on the technique used. In a level editor for outdoor environment such as in agolf game there might be many water streams or lakes that should have different objectsfrom different angles reflected in them. When reflecting the environment in small riversit is common to have a different reflected camera for each part that is above or beneathanother or has different angle. The task at hand is to make realistic water in real-time.The problem is when there are many surfaces that reflect with different heights or angles.

This thesis were made at a company named Coldwood interactive. Coldwood In-teractive was founded 2003 with many formal employees from DayDream interactive.Coldwood released there first game ”Herman Maier’s Ski Racing 2005” in 2004. At themoment they are developing a golf game with water floating through the terrain.

1.2 Problem Description

In a level editor for a game the user might have the ability to draw where in the environ-ment there should be water. The ground in an editor has different height that the watershould follow for a nice visualization. To do this each surface with a different height orangle usually needs its own reflection texture to visualize the reflection on the surface,this is expensive if there are a lot of surfaces. In most of the current applications thatuses many water surfaces some of the local reflections are replaced with reflections ofonly light sources or a static cube map for most of the surfaces. To hide the lack oflocal reflections they add some turbulence to the water that distorts the reflections inthe water surface.

The goal in this thesis is to visualize a realistic water in an outdoor environmentwith a low computational cost and still use local reflections for all surfaces instead of

1

Page 10: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

2 Chapter 1. Introduction

hiding it with only colors from light sources. This includes reflections, refractions, waterdepth and interpolation between water states like flowing water and still water. Thisshall all be generated and visualized when working in the level editor.

1.3 Outline

The report starts with a description of the problem, generally the problem of makingmany reflecting surfaces in real-time as seen in Section 1.2. Chapter 2 describes differentalgorithms used to simulate water properties, such as waves and light calculations. Thefollowing chapter 3 is about how the work was done with some illustrations of how allthe steps in the implementation is coupled. The last part of the report discuses theresult and what could have been done differently to achieve a better result, mainly adifferent approach where there are only one reflection texture to visualize the reflections.

Page 11: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Chapter 2

Water algorithms

Water is a complex medium that has properties like swells, waves, translucence at thesurface, foam with all it physical behavior, bubbles in the foam that changes the color ofwaves, curling waves in shallow water, mist, interactions between waves, wind etc. Thissection is an in depth study on water algorithms, both for real-time and non-real-timesystems.

2.1 Diffuse and Specular

When light bounces on a material light reflects differently depending on the material.The effect when the light gets shattered in different directions is called diffuse reflectionand can be seen in figure 2.1 on the left side. The right figure is when light bounces ona smooth surface and is called specular reflection.

Figure 2.1: Diffuse and Specular, Diffuse reflection on an uneven surface such that raysshatters in a number of angles. Specular reflection is the complement where rays reflecton a smooth surface.

2.2 Rasterization

The currently most used rendering technique is rasterization, this is a very fast methodcompared to approaches like ray tracing. Rasterization is not based on physical lighttransport and must therefore fake the complexity of lighting effects. Rasterization iswhen vector based information is translated to a grid of color points. Combining ras-terization with radiosity gives a nice result for diffuse light and can be precomputed.

3

Page 12: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

4 Chapter 2. Water algorithms

2.3 Radiosity

In radiosity algorithms the light in the scene is calculated by dividing the scene intopatches [2]. By calculating the information on how the patches relate to each other,the energy that leaves each patch can be calculated by a few passes or until the systemconverge. Radiosity is not view dependent and can therefore be precomputed in mostcases. The energy that leaves a patch is calculated by equation 2.1. Where B is theradiosity on the surface, E is the emissivity, R is the reflectance and F is the factor ofradiosity from another surface.

Bi = Ei +Ri

∑BjFi,j (2.1)

2.4 Ray-tracing

To get photo-realistic rendering, ray-tracing is a good technique to simulate realistic re-flections and shadows. As the name says, this technique trace rays reflecting in differentways on objects that hits the viewer, figure 2.2. The technique produces results for moreadvanced optical effects, such as accurate simulations of reflection and refraction, andis well used for high quality rendering in non-real-time. Intel is developing hardware forray-tracing in real-time, at the moment there are only rasterisation hardware in personalcomputers. The team at Intel estimates that within 2 years or so, the hardware willexist that will allow ”game quality” ray tracing on a desktop machine.

Figure 2.2: Ray Tracing, Rays reflecting on objects (A,B,C,D) sampling color startingfrom the viewer.

2.5 Light model for water

When calculating the color of the water there are two types of illumination. The colorof the surface with all its properties and the color of the water volume. The Phong-

Page 13: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

2.6. Reflection 5

illumination model can be used for the shading of the water surface with ambient, diffuseand specular colors as seen in Figure 2.3.

Figure 2.3: Illumination Model, L is a ray from the light source, N is the normal of thesurface, R is the reflected light and E is the ray from the viewer.

The final color with the addition of the reflection, refraction and depth colors canbe calculated as follows.

Color = ambient

+ ((diffuse+ refraction)× fresnel)× depth

+depthColour × (1− depth)+reflection× (1− fresnel)+specular

The depth color of the water could have effects such as caustics. Caustics is whenlight refract in the water surface and reflects in the bottom of the water pool [3]. Thebottom of a bathhouse pool is a good example where this effect is seen. Non-real-time techniques for this is Photon Mapping or Ray Tracing. Another method is touse a precomputed caustic texture, but this usually lack the dynamic behavior whensimulating waves. A real time technique for caustics is the caustics-volumes approach[3]. Particle systems is a well used technique to get splashes and more details in thevisualization and thereby a more realistic result.

2.6 Reflection

There are two types of reflections, specular reflection and diffuse reflection. The Diffusereflection is generally skipped when visualizing reflections because it is hard to simulatein real-time and does not do much for the realism. The Diffuse reflections changes thecolors depending on the environment and not the details in the reflection. One of the

Page 14: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

6 Chapter 2. Water algorithms

most common technique is to mirror the camera in the plane according to the normal asseen in Figure 2.4, then clip away everything that is beneath the plane. This techniqueonly works on planes. Another technique for reflecting the environment is a cube map.Where the color values for the reflected surface is sampled from a cube map along thesurface normals. This requires 6 extra render steps, one for every side of the cube. Theobject needs to be convex to reflect in a correct manner. If the environment is static thisis a well suited approach because the sampling of the reflection is not camera driven.There are some techniques where the world matrix is changed to project the scene asthe shape of the object. Ray tracing is the most accurate technique to compute mirrorreflections, but it is also the slowest [4], at the moment it can not be used recursivelyin real-time with standard computer hardware.

Figure 2.4: Reflected Camera, C is the current camera, R is the reflected camera and Nis the normal of the surface.

2.7 Reflection interpolation

To have the system running in real-time an algorithm that do not render all reflectionsevery frame is needed. This should depend on how important a surface is for thevisualization of the current scene. A linear interpolation on every standalone reflectionis used for the interpolation of the reflection textures. The algorithm to grade differentsurfaces can depend on how steep the surface is, how much of the screen the surfaceuse and the distance to the surface. To hide the loss of updates per frame a linearinterpolation between reflection textures can be used.

Page 15: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

2.8. Refraction 7

2.8 Refraction

Refraction is when the light rays passes between different mediums and change direction.This adjustment in the refraction ray direction is usually calculated by Snell’s law. Onemethod to simulate this is to alter the camera distance to the surface as seen in Figure2.5. Everything beneath the water plane needs to be clipped away to remove artifactsof objects above the surface refracting.

Figure 2.5: Refracted Camera, C is the Camera and R is the refracted camera.

Another method is to render the scene to a texture, distort the texture and cut awaydistortions sampled outside the screen positions of the surface with a stencil [5]. Thismethod does not work well with the accurate Snell’s law to refract, because if the waterdepth value are used to sample the refraction they depend on them self which givesunacceptable artifacts. A good approximation of the refraction is to only use distortion.This approximation builds on the fact that light refract most on horizontal angles tothe surface and if reflections is used they take over at those angles. This means that theassumption can be made that nair = nwater.

n1sinθ1 = n2sinθ2

where n is the wave velocity through the media.

2.9 Fresnel

The Fresnel function can be used to decide when a surface should reflect and when itshould refract [6]. When looking on a water plane from above the surface should onlyhave refractions and if we look at it horizontal the reflection should be taking over. Asin parameter this function takes the dot product between the normal and the viewingdirection. The following formula describes the Fresnel term for refraction.

Page 16: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

8 Chapter 2. Water algorithms

F (k) =(g − k)2

2 (g + k)2

(1 +

(k (g + k)− 1)2

(k (g − k)− 1)2

),wherek = − (N • E) , g =

√(n2

n1

)2

+ k2 − 1

Air to water has an index of n2n1

= 1.33. There are many approximations of thisFresnel function, one well used for water is

(r + (1− r) k)−7 where r =n1 − n2

n1 + n2

2

A larger power of the function results in less reflection and a sharper edge between thereflection and the refraction. If the approximation nair = nwater is made r becomes 0which simplifies the equation some more. Another approach is to use a precomputedFresnel term from a 1D texture and then use the dot product between the normal andthe view direction to look-up values in the texture.

2.10 Water Transparency

Often in an outdoor environment the water has mud in it which causes less transparencyin deeper water. This can be simulated by a fog calculation on the water depth. Thedepth of the water is calculated from the depth buffer of the rest of the scene and thedepth to the water surface from the viewer. This method can be seen in Carsten WenzelsReal-time Atmospheric Effects in Games Siggraph article[7] on page 34. This is a simplemethod which give a lot of realism to shallow water. DirectX10 has the ability to readthe z-buffer values in a pixel shader. Otherwise the scene can be rendered to an extrasingle float target texture to achieve the same result.

2.11 Water in movies

In films like The Perfect Storm [8] there are a lot of water simulations with fluid dynamicsand visualization to simulate realistic water. In the movies Titanic and Water worldthey use a model that is not based on any physics models, but instead uses statisticalmodels based on observations of the real sea.

The big difference between movies and games is that they don’t simulate water withinteractive frame rates but can instead do some larger calculations with a lot of particlesystems for details of splashes and water particles and more accurate fluid dynamicsto simulate the behavior of the water. Reflections, refractions and shadows can all berecreated to extreme detail that is all dependent on how much processing power you arewilling and capable to shove at it. Because the detail levels should be very impressive,ray tracing is the choice of high-end movie makers.

2.12 Waves

Two of the approaches used to simulate wave motion of fluids are the Lagrangian andthe Eulerian. Lagrangian uses a particle system where every particle can be seen asa molecule. This molecule typically knows its position and velocity. The Eulerianmethod uses static positions to simulate the fluid flow. Examples of the values that

Page 17: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

2.12. Waves 9

could be simulated at this static points are density, pressure or temperature. GPUimplementations of Eulerian methods is best suited for implicit methods because thereare no way to write to other position then the current position in the grid. The previousvalues can be read from any position in the grid. There are many implementationsof Eulerian methods, one of the simplest is a semi-implicit Euler method [9] that usesheight differences to simulate the fluid.

The most general form of wave propagation is described by the linear hyperbolicwave equation, this equation can describe how the displacement of a surface with smallvariations should behave.

∂2y

∂t2− c2

(∂2y

∂x2− ∂2y

∂z2

)= 0

c specifies the speed of the displacement.When simulating fluid dynamics, the mostly used technique is some form of numer-

ical based Navier-Stokes equations(NSE) for incompressible fluids [10]. Navier-Stokesequations are typically computational expensive which makes them inappropriate forreal-time simulations on large surfaces. There are some GPU implementations of Navier-Stokes equation with interactive frame-rates that work well on small grids [11]. Shallowwater equation(SWE) reduce the complexity of the full Navier-Stokes equations to thesimplified two dimensional representation of a height field. Kass and Miller [12] usesshallow water to produce fast water animations. This wave equation can be efficientlysolved using a alternating-direction implicit method where the computational costs arelinear to the number of grid cells [13].

Stams Navier-Stokes equations example uses a regular collocated grid. Anothertechnique is the Marker and Cell Staggered grid (MAC-grid), where values like pressureand density are simulated in the middle of each grid cell and the velocity on the center ofthe edges as seen in Figure 2.6. Staggered grids allow one to calculate a correct centereddifference, by using only one adjacent cell.

Figure 2.6: Mac-Grid, The solid dots represent where the pressure, density and such aresampled. The arrows is the velocity which is shifted 1/2 units.

Another method is to use a finite sum of complex sinusoidal with different phases andamplitudes that are computed by utilizing an FFT algorithm [14]. In games with fluidsthey typically use many different algorithms. When simulating fluids around objectslocally in the water NSE can be used. On larger scale with interactions, SWE is wellsuited and as a whole ocean a sinusoidal is generally used. Breaking waves and shorefoam typically uses additional particle systems for the visualization.

Page 18: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

10 Chapter 2. Water algorithms

Visualization of small wave heights can use a bump map or a parallax effect. Fora better simulation of waves on the surface some sort of mesh grid needs to be used.Meshes can be generated with a Level of detail (LOD) or a algorithm called projectedgrid to filter away unnecessary cells.

Page 19: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Chapter 3

Accomplishment

In this thesis a reflection of the camera is used to reflect the environment in the material.This only works for flat surfaces, but because water reflects best in still flat water thisis a good approximation of the specular reflection. To make use of this in a game thereneeds to be some sort of interpolation between different water state types. This is madewith a blend surface that blends the states together.

The implementation of the system is made with C++ and the Gamebryo engine asmiddle-ware. The work started by gathering facts about all kinds of real-time watersimulation. This included information on both physical and visual techniques. The sec-ond part of the project was to learn how Gamebryo worked and as a last step implementthe system.

The implementation was made as steps in the rendering as components on top ofGamebryo as seen in Figure 3.1.

Figure 3.1: Steps, Reflection Textures are rendered in the first step, second step ren-ders the scene without alpha objects, third creates a stencil where there are water, forthgenerates a normal map and finally fifth renders the water surfaces.

11

Page 20: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

12 Chapter 3. Accomplishment

The rendering of the water is made by first calculating which reflections are mostimportant for the visualization and then rendering those according to its own reflectedcamera coupled with an own render-target texture. The main scene is rendered with adepth and stencil buffer attached. With those buffers all the water surfaces are renderedto get a stencil of the water as a texture to be passed to the shader program for thewater. In DirectX10 these buffers can be read directly in the GPU programs. Theblending surfaces are also included when rendering this stencil texture. The back bufferis copied to a texture so that the refraction distortion has a texture to work with. Anormal map is generated for the water waves. When this is done the rendering of thewater has all the data that it needed to render realistic water.

The method used in this thesis is the planar reflection integrated with a priorityalgorithm to calculate how often a reflection needs to be recalculated. There is no wayto render the whole scene for every reflection and still achieve good frame rates, soapproximations are made where the backdrop of the scene is used and only the mostimportant objects for the local reflections in the water.

3.1 Reflection Step

The reflection step contributes to the final water render step with textures of how thereflections look on the current frame. One texture for each different reflecting watersurfaces.

3.1.1 Surface grading

The grading of the importances of each reflection is made by the distance to the surfacebut could be extended to count for the angle between view direction and surface normaland the size that the surface takes up on the screen.

Algorithm:

1. Go through all surfaces

(a) for every surface add a value depending on the above gradings.

2. Render the surface reflections with the highest value and reset its current value.

This algorithm tells what surface reflection texture we need to update next.

3.1.2 Reflections

The camera is reflected along the surface normal and its up axis is rotated to be ableto map reflections directly to screen coordinates. A clip plane clips away everythingbeneath the plane. When using the interpolating reflections with the technique describedin section 2.7, two extra matrices is passed to the GPU for interpolating old cameratranslation and the new camera translation. The texture size of the reflecting textureis scaled down, because distortions can hide the lack of resolution. The reflection canalso be adjusted with a higher FOV to allow small samplings outside the screen positioncoordinates.

The reflection of the camera is calculated with the following equations.

Cr = Cd− (2Sn (Cd • Sn)) (3.1)

Page 21: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

3.1. Reflection Step 13

In equation 3.1 Cd - Camera Direction, Sn - Surface Normal, Cr - Camera Reflected.This reflects the camera direction vector by the normal of the plane.

Ct = 2Sn (Cd • (Sp− Cp)) (3.2)

In equation 3.2 Cd - Camera direction, Sn - Surface normal, Sp - Surface Position,Cp - Camera Position, Ct - Camera Translation. This translate the camera along thesurface normal to the reflected position.

Cu = Wu− (2Sn (Wu • Sn)) (3.3)

In equation 3.3 Wu - World Up, Sn - Surface Normal, Cu - Camera Up. Thiscalculation is made to get the new up vector for the camera to make the texture samplingwith the screen texture coordinates. As seen in the equation the camera up vector iscalculated as a reflection of the world up vector by the surface normal.

V =current− begin

end− begin(3.4)

Equation 3.4 is a simple interpolation of the time values to know how much of eachtexture to use at rendering time.

T = Tb (1− V ) + TeV (3.5)

In equation 3.5 Tb - Texture begin, Te - Texture end, T - Texture. This is what thelerp function do in the shading language.

Figure 3.2: Screenshot: Reflection Interpoation, Reflection calculated by interpolatingbetween the two textures in the bottom left corner.

Page 22: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

14 Chapter 3. Accomplishment

In Figure 3.2 the surface is generated by interpolating between part 1 and part 2from the texture representing the reflection. This texture is rendered one side at a timeand never cleared, so the old reflection can be sampled at the same time as the new. Thevertical split is due to the fact that it is easier to hide lack of resolution in a horizontalangle.

3.2 Render Scene Step

In this step the depth values of the scene is rendered to a texture. Alpha objects cannot be rendered in this step because it would make artifacts when using the the depthtexture in the following steps. The depth texture is used to simulate the depth effect ofthe water in section 3.5.4. The color of the scene is rendered to another texture for posteffects and for the refraction effect in section 3.5.1.

3.3 Water Stencil Step

This step draws a stencil of the water surfaces in the scene to a texture. It uses themain scenes depth-stencil buffer to know how the terrain clips the water surfaces. Thestencil texture is later used in section 3.5.1.

3.4 Normal Generation Step

The normal texture produced in this step is used in the final rendering of the watersurfaces in section 3.5. The normal texture is used for the light calculation of the watersurfaces and the refraction distortion.

3.4.1 Surface Animation

To simulate the physical properties of the water a Perlin noise texture was precomputedto be used as a height map, from this a normal map for the lighting of the surface wasprecomputed. Perlin noise is made by different scaled layers of smoothed random noiseadded together, figure 3.3.

To animate the surface from this normal map a number of prime numbers are usedto set the speed and scale of a number of animated texture coordinates in differentdirections when sampling from the normal map. The prime numbers are used to reducerepetitions in the water animation.

Mesh adjusted water interaction requires high resolution of the mesh or some kindof non uniform grid. In this thesis there are no adjustments in the mesh. The end resultanimates the surface by only altering the normal map for the light calculations andadds additional animated meshes on top of the surface for local splashes on the surface.Water rings can either be generated at run-time with a simple SWE or by scaling andmoving normal textures across the water. The water rings builds on the equation 3.6.

Page 23: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

3.4. Normal Generation Step 15

Figure 3.3: Perlin, Random values are added together with different scale to produce aheight map that generates a normal map from the heights.

a[i,j] = ut[i−1,j] + ut

[i+1,j] + ut[i,j−1] + ut

[i,j+1] − 4ut[i,j]

b[i,j] = 0.5(ut[i+1,j+1] + ut

[i−1,j+1] + ut[i+1,j−1] + ut

[i−1,j−1] − 4ut[i,j])

vt+1[i,j] = vt

[i,j]d+ c2dt(a[i,j] + b[i,j])/h2

ut+1[i,j] = ut

[i,j] + vt+1[i,j]dt

(3.6)

where a[i,j] and b[i,j] are height changes due to the neighboring grid cells. vt+1[i,j] is the

height changes and ut+1[i,j] is the height field. dt is the delta time and d is a dampening

constant. c is the wave speed and h is the grid spacing. In the simulation d is set to0.99, c is 5, h is 2 and dt is 0.016 with a frame rate of 60fps. To have the simulationstable dt must be less then h/c. Figure 3.4 shows the wave simulation on an elephantstencil. To spawn waves in the water a clipping plane to clip away everything above thewater surface is used. A vertical camera looking down on the water surface renders backfacing geometry that should interact with the water. This produce a stencil texture forwhere the interactions on the surface are. This is then used with the previous interactionsample to find out what is moving at the current step.

Page 24: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

16 Chapter 3. Accomplishment

Figure 3.4: Interactive Waves, Waves bouncing within the bounds of the elephant.

3.5 Render Surfaces Step

This step renders the final water with data from the previous steps.The water rendering uses modern hardware equipped with vertex and pixel shader.

The shading code is in HLSL code. HLSL is a c-like shader language that was introducedwith DirectX9.

To get the screen texture coordinates for the water surfaces the projected positionsis used, these are in the interval of [−1, 1]. The w coordinate when projecting objectspositions is a scaling value. This value needs to be passed on to the pixel-shader tobe interpolated correctly. In the pixel-shader this scale is divided with the x and ycoordinates of the projected position as the positions are scaled to the interval [0, 1].This calculation could have been done in the vertex-shader, but this introduce someartifacts when the object is near the camera.

3.5.1 Refractions

The back buffer texture from section 3.2 is used in the shader along with the stenciltexture from section 3.3 to cut away everything in front of the water surface whensampling in the texture with distortion according to the normal texture. This techniquewas chosen over the standard refraction where the camera can be moved and a clip-planeclips away the geometry above the plane. The choice was made so that all the watersurfaces in the scene could be refracted without an extra pass with an extra clip plane.With this technique there is no need for a clip plane and there by lets the refractedsurface be other than flat.

Page 25: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

3.5. Render Surfaces Step 17

3.5.2 Stencil Cutoff

1. Distort the stencil buffer the same way the distortion should be made.

2. Sample with distortion in the texture if the stencil buffer allows it otherwise usethe original texture.

This stencil cutoff removes artifacts at the edges of objects in front of the surface.It requires a extra rendering pass to produce the masking stencil texture.

3.5.3 Fresnel

The Fresnel effect is the property some of the materials have when they refract moreat certain angles, explained in section 2.9. Water reflects well in horizontal angles butfrom the top it is mostly transparent [15]. This effect is calculated with the dot productbetween the normal and the normalized viewing vector as parameter to a approximatingFresnel function. The Fresnel calculation is made in the pixel-shader to handle normalsfrom the normal map of the water-surface.

3.5.4 Water Depth

When rendering shallow water the depth buffer can be used to produce fog in the water.This simulates mud in the water between the bottom floor of the water and the surfaceas seen in Figure 3.5.

Figure 3.5: Water Depth, The depth of the water is shown by a darker color for thedeeper parts of the water.

Page 26: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

18 Chapter 3. Accomplishment

Page 27: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Chapter 4

Results

Coldwood ended up with components they could use in there games for a nice lookingwater with depth, reflections, refractions and waves. A water interaction componentand a reflection component for planar surfaces with the interpolation technique and astandard fall back technique.

The water interpolation technique had a lot of overhead and needed twice as muchtexture memory as a standard plane reflection. In the shader program six extra floatswhere needed to be passed between the vertex shader and the pixel shader. The finalwater seen in Figure 3.5 with depth and all its effects took up all but four floats. Thismade the rendering of the final water with the interpolation technique impossible becauseof the lack of interpolated registers to pass between vertex and pixel shader. Figure 4.2show the result of the interpolated reflection technique with no water depth effect. Thescene is from tech demo Coldwood developed at the time I did my job, all the models inthe scene were made by Coldwood employees, my part was the water surfaces with allits properties. The reflection components in the implementation was also used as floorreflections in there tech demo. Floor reflections has no distortion and must therefore usehigher resolution on the reflection textures. The interpolation technique could be usedbecause this shader program did not need as many registers as the final water shaderprogram. The interpolation technique did not look good in floor reflections, so I had touse standard reflection calculations here for a good visual result.

The resulting implementation had a water interaction component to generate waveswhere the objects were clipped by the water plane to generate waves as discussed in theend of Section 3.4.1.

Figure 4.1: Waves, Shows artifacts when the heights is not smoothed on the left side andsmoothed on the right.

19

Page 28: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

20 Chapter 4. Results

Because of time constraints I did not have time to fix artifacts due to sharp edges inthe height map from the water interaction component as seen in Figure 4.1.

Figure 4.2: Water pool, Screen shot of the final water.

Page 29: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

Chapter 5

Conclusions

The work took more time then expected. The main reason for this was because I hadto learn how the middle-ware worked and the programming environment. As it is, thework started with too generalized thoughts about what the solution should handle.

5.1 Restrictions

I did not have time to implement the water in the golf editor. So I ended up withsome floor reflections and a pool with water in a tech demo for the company. Theresulting implementation had some limitation such as alpha objects in the water notbeing rendered. An assumption that the view point always lies above the water so thatunderwater rendering is abandoned. The final water with all the visual effects could notuse the interpolation technique because of lack of registers to use when passing databetween vertex and pixel shader.

5.2 Future

Now after the implementation is done a new approach has been thought of. It uses theknowledge that the water surfaces is almost at the same level and never overlapping,which can be used to only adjust the FOV of the camera and sample the reflection atthe lowest of all the visible surfaces on the screen with a surface normal as the worldup direction. Then these values are passed on to the GPU where the sampling fromthe texture is made with adjustment due to the surface position. The clipping could bedone as a pre-process. This should have cut down the reflection rendering to one takeper frame at this special case.

Ray-tracing algorithms gives better visual quality than those generated by the stan-dard Z-buffer algorithm, but ray tracing algorithms is too slow for real-time use today.In the future ray tracing will become more used as computers get more parallel pro-cessing hardware. There are some reports that ray tracing will cross the rasterisationcomputing cost line within some years. At the moment rasterisation will probably beused for the most part of the rendering in games and ray tracing might be used for closeup reflection effects in car games where reflections is needed to make the cars shiny.

21

Page 30: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

22 Chapter 5. Conclusions

5.3 Acknowledgments

Many thanks toAll the people at Coldwood for helping me out with the thesis. Jakob Marklund as

supervisor at Coldwood. Pedher Johansson for reading and evaluating the report.

Page 31: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

References

[1] Natalya Tatarchuk. Artist-directable real-time rain rendering in city environments,2006.http://ati.amd.com/developer/gdc/2006/GDC06-Advanced D3D Tutorial Day-Tatarchuk-Rain.pdf, (visited 10 Sep. 2007).

[2] Stephen Spencer. Radiosity overview.http://www.siggraph.org/education/materials/HyperGraph/radiosity/overview 1.htm,(visited 14 Dec. 2007).

[3] Christoffer Sandberg and Tomas Falemo. Real-time rendering of water causticsusing programmable graphics hardware.http://www.ce.chalmers.se/uffe/xjobb/caustics.pdf, (visited 15 Dec. 2007).

[4] Jamis Buck. The recursive ray tracing algorithm, 2000.http://www.geocities.com/jamisbuck/raytracing.html, (visited 22 Sep. 2007).

[5] Matt Pharr and Randima Fernando. GPU Gems 2: Programming Techniques forHigh-Performance Graphics and General-Purpose Computation. Addison-Wesley,2005.

[6] Vladimir Belyae. Real-time simulation of water surface.http://www.graphicon.ru/2003/Proceedings/Technical/paper316.pdf,(visited 10 Dec. 2007).

[7] Carsten Wenzel. Real-time atmospheric effects in games, 2006.http://ati.amd.com/developer/siggraph06/Wenzel-Real-time Atmospheric Effects in Games.pdf, (visited 12 Sep. 2007).

[8] Jeff Tyson. How industrial light & magic works.http://entertainment.howstuffworks.com/perfect-storm5.htm,(visited 14 Dec. 2007).

[9] Matthias Muller. Real time fluids in games.http://www.matthiasmueller.info/talks/gameFluids2007.pdf,(visited 13 Dec. 2007).

[10] David Cardon David Cline and Parris K. Egbert. Fluid flow for the rest of us:Tutorial of the marker and cell method in computer graphics.http://poseidon.cs.byu.edu/cline/fluidFlowForTheRestOfUs.pdf,(visited 13 Dec. 2007).

23

Page 32: Water Surface Rendering - umu.se · Water Surface Rendering Morgan N atterlund March 6, 2008 Master’s Thesis in Computing Science, 20 credits Supervisor at CS-UmU: Pedher Johansson

24 REFERENCES

[11] Mark J. Harris. Fast fluid dynamics simulation on the gpu. In GPU Gems,volume 1, pages 637–665, 2004.http://kucg.korea.ac.kr/research/research group2/data/GPU Gems Fluids Chapter.pdf,(visited 14 Dec. 2007).

[12] Michael Kass and Gavin Miller. Rapid, stable fluid dynamics for computergraphics.http://delivery.acm.org/10.1145/100000/97884/p49-kass.pdf?key1=97884&key2=9833889911&coll=GUIDE&dl=GUIDE&CFID=49086928&CFTOKEN=46388701,(visited 10 Dec. 2007).

[13] Y. Nishidate and G. P. Nikishkov. Fast water animation using the wave equationwith damping.http://www.u-aizu.ac.jp/niki/papers/2005%20Fast%20water%20animation.pdf,(visited 14 Dec. 2007).

[14] Jason L. Mitchell. Real-time synthesis and rendering of ocean water, April 2005.http://ati.amd.com/developer/Mitchell-Real-Time Synthesisand Rendering of Ocean Water(ATITR Apr05).pdf, (visited 17 Oct. 2007).

[15] Chris Brennan. 3d application research group ati research.http://ati.amd.com/developer/shaderx/ShaderX PerPixelFresnel.pdf,(visited 14 Sep. 2007).