Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain...

59
Terrain and water rendering with hardware tessellation Xavier Bonaventura Brugu´ es May 20, 2010

Transcript of Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain...

Page 1: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Terrain and water rendering with hardware

tessellation

Xavier Bonaventura Brugues

May 20, 2010

Page 2: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Abstract

This thesis studies the hardware tessellator that is a recent addition to theincremental rendering pipeline of graphics processing units, only available usingthe latest hardware. The main field of application for hardware tessellationis level-of-detail rendering of complex geometries like natural phenomena andprocedurally generated features.This thesis applies this technique in two important fields: the rendering of a largeand detailed terrain with different levels of detail depending on the distance, andthe simulation and visualization of ocean wave on a large open water surfacewith physically-based motion and Fresnel reflections.

Page 3: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Contents

1 Introduction 41.1 DirectX 11 Graphics Pipeline . . . . . . . . . . . . . . . . . . . . 5

1.1.1 Hull Shader Stage . . . . . . . . . . . . . . . . . . . . . . 51.1.2 Tessellator Stage . . . . . . . . . . . . . . . . . . . . . . . 51.1.3 Domain Shader Stage . . . . . . . . . . . . . . . . . . . . 8

2 Implementation 92.1 Previous Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.1.1 Differences between DirectX 10 and DirectX 11 . . . . . . 92.1.2 Effects11 and DXUT . . . . . . . . . . . . . . . . . . . . . 9

2.2 Terrain rendering with tessellation . . . . . . . . . . . . . . . . . 102.2.1 Definition of terrain geometry . . . . . . . . . . . . . . . . 102.2.2 Tessellation Factor . . . . . . . . . . . . . . . . . . . . . . 112.2.3 Vertex position and texture coordinates . . . . . . . . . . 132.2.4 Vertex normals . . . . . . . . . . . . . . . . . . . . . . . . 142.2.5 Tessellation correction depending on the camera angle . . 142.2.6 Graphic User Interface . . . . . . . . . . . . . . . . . . . . 16

2.3 Ocean rendering with tessellation . . . . . . . . . . . . . . . . . . 182.3.1 Definition of ocean geometry . . . . . . . . . . . . . . . . 182.3.2 Tessellation Factor . . . . . . . . . . . . . . . . . . . . . . 182.3.3 Vertex position and texture coordinates . . . . . . . . . . 202.3.4 Vertex normals . . . . . . . . . . . . . . . . . . . . . . . . 202.3.5 Ocean texturing . . . . . . . . . . . . . . . . . . . . . . . 202.3.6 Graphic User Interface . . . . . . . . . . . . . . . . . . . . 21

3 Performance Testing 233.1 Different texture sizes . . . . . . . . . . . . . . . . . . . . . . . . 233.2 Tessellation Factor vs. Number of Patches . . . . . . . . . . . . . 23

4 Results 25

5 Conclusions and future work 305.1 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305.2 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

1

Page 4: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

5.2.1 Terrain and ocean rendering . . . . . . . . . . . . . . . . . 305.2.2 Terrain rendering . . . . . . . . . . . . . . . . . . . . . . . 31

A Terrain fx file 33

B Ocean fx file 46

2

Page 5: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

List of Figures

1.1 DirectX 10 and DirectX 11 Pipelines . . . . . . . . . . . . . . . . 61.2 Tessellation depending on the factors . . . . . . . . . . . . . . . . 7

2.1 Division of the terrain into a grid: Vx and Ex represents verticesand edges respectively in every patch where x is the index toaccess. Inside 0 and 1 represents the direction of the tessellationinside a patch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2 Lines between patches when tessellation factors are wrong . . . . 112.3 Tessellation factor depending on the distance: (b) is the function

used to get the final tessellation with round to the exponent, (a)is the function of the exponent used to calculate (b). . . . . . . . 12

2.4 Mipmap level depending on the camera distance . . . . . . . . . 142.5 Rendering the terrain normals . . . . . . . . . . . . . . . . . . . . 152.6 Normals calculation . . . . . . . . . . . . . . . . . . . . . . . . . 152.7 Terrain rendering with different options . . . . . . . . . . . . . . 172.8 Tessellation factor depending on the distance: (b) is the function

used to get the final tessellation factor, (a) is the function of theexponent used to calculate (b). . . . . . . . . . . . . . . . . . . . 19

2.9 Rendering the ocean normals . . . . . . . . . . . . . . . . . . . . 212.10 Ocean rendering with and without bump-mapped ripples . . . . 222.11 Ocean rendering with different options . . . . . . . . . . . . . . . 22

4.1 Ocean rendering without angle correction . . . . . . . . . . . . . 264.2 Ocean rendering with angle correction . . . . . . . . . . . . . . . 264.3 Terrain rendering without angle correction . . . . . . . . . . . . . 274.4 Terrain rendering with angle correction . . . . . . . . . . . . . . 274.5 Ocean rendering: Min Tess Factor 2, Max Tess Factor 64 . . . . 284.6 Ocean rendering: Min Tess Factor 4, Max Tess Factor 16 . . . . 284.7 Terrain rendering: Min Tess Factor 2, Max Tess Factor 64 . . . . 294.8 Terrain rendering: Min Tess Factor 4, Max Tess Factor 16 . . . . 29

3

Page 6: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Chapter 1

Introduction

Nowadays, one of the biggest problems in computer graphics is the detail intoscenes. To get more realistic scenes you need high details models but then thecomputer goes slow. To increase the number of frames per second you can uselow detail models but then it doesn’t seems realistic. The solution is to combinehigh models near to the camera and low models far to the camera but this isnot easy.One of the most used technique consist of a set of different detail models andin the runtime change it depending on the distance of the camera. This processit’s done into the CPU and this is a problem because the CPU it’s not intendedfor this work and you waste a lot of time sending meshes from the CPU to theGPU.In DirectX 10[1] you could change the detail of meshes into the GPU doingtessellation into the geometry shader but this shader is not intended for thiskind of jobs and it’s not really the best solution. The output of the geometryshader is limited and this work is serial.The best solution to tessellate is the recently appear tessellator stage in DirectX11. This stage together with two more allows to the programmer tessellate veryquickly into the GPU. With this way you can send low level detail meshes to theGPU and generate the missing geometry to the GPU depending on the cameradistance, angle or whatever you want.First of all in the section 1.1 we will take a look to the new stages in DirectX 11and how they work. Then into the chapter 2 we will explain the implementationof water and terrain rendering with these tools. Into the chapter 3 we analyze theperformance using different size of textures and changing the relation betweenthe size of the mesh and the tessellation factor. Chapter 4 will show the resultsand chapter 5 the conclusions and future works.

4

Page 7: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

1.1 DirectX 11 Graphics Pipeline

The DirectX 11 graphics pipeline[2] add three new stages in the DirectX 10which are hull shader stage, tessellator stage and domain shader stage (Fig-ure 1.1). The first and third are programmable and the second one is config-urable. These are after the vertex shader and before the geometry shader andthey are intended to do tessellation into the graphic card.

1.1.1 Hull Shader Stage

The hull shader stage is the first part into the tessellation bloc and it goesafter the vertex shader stage and before the tessellator stage. The data usedin it is new in DirectX 11 and it uses a new primitive topology called controlpoint patch list. As its name suggests it represents a collection of control pointswhere the number in every patch can go from 1 to 32. These control points areresponsible to define the mesh.The output data in this stage is composed of two parts, one are the input controlpoints that can be modified and the other are some constant data that will beused into the tessellator and domain shader stages.To calculate the output data there are two functions, the first one is executedfor every patch and there you can calculate the tessellation factor for every edgeof the patch and inside it.The other part is executed for every control point into the patch and there youcan manipulate this control point. In both parts you have the information ofall control points into the patch, in addition, in the second part you have the idof the control point that you are processing.Example of hull shader headers:

HS CONSTANTDATAOUTPUT TerrainConstantHS ( InputPatch<VS CONTROL POINT OUTPUT, INPUT PATCH SIZE> ip , u intPatchID : SV PrimitiveID )

HS OUTPUT hsTerra in ( InputPatch<VS CONTROL POINT OUTPUT,INPUT PATCH SIZE> p , u int i : SV OutputControlPointID ,u int PatchID : SV PrimitiveID )

1.1.2 Tessellator Stage

The tessellator stage is a part of the new pipeline where the programmer onlycan change the behaviour setting some values. It goes after the hull shader stageand before the domain shader stage. It’s executed once per patch and the inputdata are the control points and the tessellation factors which are the outputfrom the hull shader. This stage is responsible to divide a quad, triangle or linein a lot of them depending on the tessellation factor and the type of partitioningdefined (Figure 1.2). The output is UV coordinates which goes from 0 to 1 andthey define the position of new vertices relative into the patch.

5

Page 8: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 1.1: DirectX 10 and DirectX 11 Pipelines

6

Page 9: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 1.2: Tessellation depending on the factors

7

Page 10: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

1.1.3 Domain Shader Stage

The domain shader stage is the last part to do tessellation and it goes beforethe geometry shader stage and after the tessellator stage. This part is executedonce per UV coordinate generated into the tessellator stage and it access tothe information from the hull shader stage output (control points and constantdata) and UV coordinates. In this final stage the aim is to calculate the finalposition of every vertex generated and all the associated information as normal,colour, texture coordinate, etc.Example of domain shader header:

DS OUTPUT dsTerra in ( HS CONSTANTDATAOUTPUT input , f l o a t 2 UV: SV DomainLocation , const OutputPatch<HS OUTPUT,OUTPUT PATCH SIZE> patch )

8

Page 11: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Chapter 2

Implementation

2.1 Previous Work

Before start to implementing the tessellation one of the most important works todo is to configure a DirectX 11 project. DirectX 10 and DirectX 11 have somedifferences and we have taken a DirectX 10 project with some basic featuresand we have converted it to DirectX11. This project include draw a mesh withindex buffer, the application of textures to a mesh, draw a mesh with instancing,how to use a static environment map, creation of a simple terrain and geometrygeneration through the geometry shader.

2.1.1 Differences between DirectX 10 and DirectX 11

The main change in DirectX 11 has been that the functionalities of the devicehave been divided in two parts, the device and the device context.The device is responsible to create resources and to enumerate the capabilitiesof a display adapter. The device context includes the other parts like renderingoperations, map/unmap resources and operations to set the pipeline state.

2.1.2 Effects11 and DXUT

Into the development of this application has been used the Effects11 and DXUTframeworks to reduce the work that it’s not specific of hardware tessellation.These two frameworks are used into the DirectX sample and the aim is to createa project quickly with the general default values.With these frameworks has been possible to devote most of the time implement-ing shaders and not the skeleton of the application.

9

Page 12: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 2.1: Division of the terrain into a grid: Vx and Ex represents verticesand edges respectively in every patch where x is the index to access. Inside 0and 1 represents the direction of the tessellation inside a patch.

2.2 Terrain rendering with tessellation

Terrain rendering is a good field to apply tessellation because usually terrainsare huge and to render all with the same detail when it’s high it’s impossible.The problem with the terrain is that usually the mesh is big and you waste alot of time sending the information of the mesh from the CPU to the GPU.Hardware tessellation technique allows to send to the GPU practically all theinformation into the initialization part and then you can change the level ofdetail in it with a minimum interaction with the CPU.

2.2.1 Definition of terrain geometry

To tessellate the terrain we need to divide it in different patches. It can beapplied to a lot of shapes but it will be used the most intuitive shape, a patchwith four points. We will divide the terrain in patches of the same size like agrid (Figure 2.1). For every patch we will have to decide the tessellation factorin every edge and inside, it’s very important that two patches that share thesame edge they had the same tessellation factor in this one because if it doesn’thappens then you will see some line between patches (Figure 2.2).

10

Page 13: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 2.2: Lines between patches when tessellation factors are wrong

2.2.2 Tessellation Factor

In the tessellator stage you can define different kinds of tessellations (frac-tional even, fractional odd, integer or pow2). We will use the integer but inaddition we will impose one restriction more, this value must be a power oftwo to avoid a wave effect. With this way when a new vertex appear it will bethere until the tessellation factor decrease again and their x and z values willnot change. The only value that will change will be the y coordinate to avoidpopping.First of all we have to define two distances, one the minimum and the other themaximum, to calculate the tessellation factor. These distances will be wherethe tessellation factor will be maximum and minimum respectively. In additionwe have to define which will be the maximum and the minimum of tessellationfactor; maybe in some cases we don’t want that the minimum of tessellationfactor to be 1 if there are few patches or 64 if the computer goes to slow.Then the tessellation factor will be 2x where x will be a number whose rangewill be from log2MaxTessellation to log2MinTessellation linear interpolatedbetween the minimum and the maximum distance. This x will be round to thenearest integer to get a power of two tessellation factor (Figure 2.3).

(a)

tea(d) =

max(te), for d ≤ min(d),

round(diff(te)(1− d−min(d)diff(d) ) + min(te)), for min(d) < d < max(d),

min(te), for d ≥ max(d).

where diff(x) = max(x)−min(x)

11

Page 14: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

(a) Log2 of tessellation factor with round

(b) Tessellation factor with round in it Log2 to get a power of 2 value

Figure 2.3: Tessellation factor depending on the distance: (b) is the functionused to get the final tessellation with round to the exponent, (a) is the functionof the exponent used to calculate (b).

12

Page 15: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

(b)

teb(d) =

2max(te), for d ≤ min(d),

2round(diff(te)(1− d−min(d)diff(d)

)+min(te)), for min(d) < d < max(d),

2min(te), for d ≥ max(d).

where diff(x) = max(x)−min(x)

As we said before it’s very important that two patches that share the same edgethey have the same tessellation factor in this edge. To do this we will calculatefive different distances in every patch, one for every edge and one for inside. Tocalculate the tessellation factor for every edge we calculate the distance betweenthe camera and the central point of the edge. With this way in two adjacentpatches with the same edge the distance in the middle point will be the samebecause they share the two vertices that we use to calculate the middle point.To calculate the tessellation factor inside the patch in U and V direction wecalculate the distance between the camera position and the middle point of thepatch.

2.2.3 Vertex position and texture coordinates

In every result point we can easy calculate the x and z coordinates with a singleinterpolation between the position of the vertices of the patch but we also needthe y coordinate that represents the height of the terrain in every point and thetexture coordinates. As we have defined the terrain we only have to take thex and z final coordinates and divide by the size of the terrain. Once we havethe texture coordinates to get the height of the terrain we read the informationfrom a heightmap texture. To take this information we have to use mip-maplevels or we will see some popping when new vertices appear. To reduce thispopping the goal is to get the value from a texture where the concentration ofpoints in it are the same than the concentration in the area where we have thevertex. Four patches that share a vertex have to use the same mip-map level inthat vertex to be coherent, for this reason we calculate one mip-map level forevery vertex in a patch. Then, to calculate the mip-map level into the othervertices we only have to interpolate between the mip-map level of the verticesof the patch.The next formula is used to calculate the mip-map level in every vertex of thepatch:

MipMap(d) =

min(MipMap), for d ≤ min(d),

diff(MipMap)d−min(d)diff(d) + min(MipMap), for min(d) < d < max(d),

max(MipMap), for d ≥ max(d).

where diff(x) = max(x)−min(x)To calculate the minimum and the maximum value for the mip-map level vari-able we use these equations:

min(MipmapLevel) = log2(textSize)− log2(sqrtNumPatch2max(te))

13

Page 16: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 2.4: Mipmap level depending on the camera distance

max(MipmapLevel) = log2(textSize)− log2(sqrtNumPatch2min(te))

If the minimum value is less than 0 we take 0.

2.2.4 Vertex normals

In every vertex we have to calculate the normal vector to apply iluminationtechniques later (Figure 2.5). This is read from a texture with the same size ofthe height map that stores normal vectors in world coordinates. To access tothis texture we read from the same mip-map level that we read the height ofthe terrain.These normals are calculated previously with the same way that if we want tocalculate the normal of a vertex shared by 8 faces. In addition we have to use aweighted mean because the weight of the diagonal vectors is 1√

2times shorter

than the vertical and horizontal vectors (Figure 2.6).

Normal(C) =

−→C0×

−→C2+

−→C4×

−→C6

2 + 1√2

−→C1×

−→C3+

−→C5×

−→C7

2

1 + 1√2

2.2.5 Tessellation correction depending on the camera an-gle

Until now we have assume that the tessellation factor only depends on thedistance of the camera, nevertheless it’s not the same if you see a patch fromone direction or from another. For this reason we apply a correction to thetessellation factor that we have calculated previously. This correction will notbe applied to the final tessellation factor because we want that the final one to

14

Page 17: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 2.5: Rendering the terrain normals

Figure 2.6: Normals calculation

15

Page 18: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

be a power of two value, this correction will be applied to the value x beforerounded that we use in 2x to calculate the final tessellation factor.The angle to calculate this correction will be the angle between the unit vectorover an edge and the unit vector that goes from the middle of this edge to thecamera.To calculate the correction we will use this formula:

π2 − arccos (|c · e|)

π2

rank + 1− rank

2

The rank value is used to decide how important is the angle in comparison withthe distance is. If you decide a rank of 0.4 then the tessellation factor will bemultiplied by a value between 0.8 and 1.2.

π2 −

arccos(|c·e1|)+arccos(|c·e2|)2

π2

rank + 1− rank

2

To be coherent with this modification we have to apply the correction to thevalue that we use to access to the mipmap level in a texture. It is very importantto keep the coherence that four patches that share the same vertex they havethe same mipmap value in that vertex. To calculate the angle of the camera inthis point we calculate the mean of the angles between the camera and everyvector over the edges that share the point.

π2 −

arccos(|c·v0|)+arccos(|c·v1|)+arccos(|c·v2|)+arccos(|c·v3|)4

π2

rank + 1− rank

2

Into the hull shader we don’t know the information about the vertex of theother patches but this vectors can be calculated to the vertex shader becausewe know the size of the terrain and the number of patches.

2.2.6 Graphic User Interface

As we have seen before we have a lot of parameters to adjust in this terrainimplementation. For this reason the user can change these values through agraphic user interface.With this interface we can change some value to adjust the rendering:

• Min Distance: The distance where the tessellation factor will be maximum

• Max Distance: The distance where the tessellation factor will be minimum

• Min Tess Factor: The minimum value for the tessellation factor

• Max Tess Factor: The maximum value for the tessellation factor

In addition the user can see the terrain normals and a wire frame terrain to seethe size of every face (Figure 2.7). Finally, the user can decide to apply or notthe angle correction to see the difference.

16

Page 19: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

(a) Terrain (b) Terrain normals

(c) Wired terrain (d) Wired terrain normals

Figure 2.7: Terrain rendering with different options

17

Page 20: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

2.3 Ocean rendering with tessellation

Now we have seen an application of hardware tessellation in a terrain renderingbut it’s not the best field of application because we don’t have to send a hugemesh to the GPU but we have to send a huge heightmap. Into the next part wewill see the application of hardware tessellation in a ocean rendering, we don’tneed a heightmap, we just only need some functions and a simple mesh.

2.3.1 Definition of ocean geometry

With the ocean rendering we also have to define a basic ocean geometry wherewill apply the tessellation depending on the distance and the angle of the camera.This will be exactly the same than in the terrain rendering in subsection 2.2.1.

2.3.2 Tessellation Factor

The tessellation factor used in ocean rendering is a little different than in theterrain rendering. There was a problem that if we didn’t use a power of twotessellation factor into the terrain rendering we could see a wave effect becausethe x and z coordinates into the final vertices moved when we changed thetessellation factor.This happened because the terrain is a static mesh but now we are rendering aocean were the waves changes with the time. If the wave effect is smooth thiswill not be a problem because indeed we are rendering waves.For this reason we will use a fractional even tessellation that can take a valuefrom 2 to 64 and as it name suggests it can be a fractional value. The wayto calculate this value will be the same than in the terrain rendering, it willdepend on the distance and the angle of the camera, but we will not round theexponent.

(a)

tea(d) =

max(te), for d ≤ min(d),

diff(te)(1− d−min(d)diff(d) ) + min(te), for min(d) < d < max(d),

min(te), for d ≥ max(d).

where diff(x) = max(x)−min(x)

(b)

teb(d) =

2max(te), for d ≤ min(d),

2diff(te)(1− d−min(d)diff(d)

)+min(te), for min(d) < d < max(d),

2min(te), for d ≥ max(d).

where diff(x) = max(x)−min(x)

18

Page 21: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

(a) Log2 of tessellation factor without round

(b) Tessellation factor without round in it Log2

Figure 2.8: Tessellation factor depending on the distance: (b) is the functionused to get the final tessellation factor, (a) is the function of the exponent usedto calculate (b).

19

Page 22: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

2.3.3 Vertex position and texture coordinates

To calculate the final vertex position in the ocean rendering it’s not so easylike in the terrain rendering. Now we don’t have a heightmap and we have tocalculate the final position depending on the waves and the position into theworld. To get a real motion we will use the technique explained in ShaderX 6developed by Szecsi and Arman[3].First of all we have to imagine a single wave with a wavelength (λ), an amplitude(a) and a direction (k).It velocity (v) can be represented with this function:

v =

√gλ

Then the phase (ϕ) at time (t) in a point (p) is:

ϕ =2π

λ(p · k + vt)

Finally the displacement (s) to apply in that point is:

s = a[− cosϕ, sinϕ]

A ocean it’s not a simple wave and we have to combine all of them to get arealistic motion:

pΣ = p+

n∑i=0

s(p, ai, λi, ki)

2.3.4 Vertex normals

In every vertex we need to calculate the normal (Figure 2.9) but now it’s notnecessary to access to a normal texture because it can be calculated by a formula.We have a parametric function where the parameters are the x and z coordinatesthat we have used to calculate the position, if we do the cross product of thetwo partial derivatives then we get the normal vector in that point.

N =∂pΣ

∂z× ∂pΣ

∂x

2.3.5 Ocean texturing

For texturing the ocean we have combined two techniques. The first one isFresnel reflections to get a realistic colour of the water and the second one is theaddition of some noise to the normals to get the effect when the wind createslittle waves over the surface.Fresnel reflections (F (θ)) is used to combine the colour of the water (cwater)and the colour of the environment (cenv) to get the final colour depending on

20

Page 23: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 2.9: Rendering the ocean normals

the view angle of the camera (θ). In addition the colour of the water is a linearcombination between the colour of the deep water (cdeep) and the colour of theshallow water (cshallow).

F (θ) = 0.02 + 0.98(1− cos θ)5

cwater = cdeep cos θ + cshallow(1− cos θ)

c = cenvF (θ) + cwater(1− F (θ))

The other technique also used in the implementation of Szecsi and Arman[3] isthe bump-mapped ripples. It consists of the same than a bump-mapping[4] butit’s animated and with different periods to get chaotic ripples (Figure 2.10).

2.3.6 Graphic User Interface

The graphic user interface used in the ocean rendering is the same than inthe terrain rendering in subsection 2.2.6. The only differences are that now theminimum tessellation factors can not be less than 2 and the value can be decimalbecause we use the fractional even tessellation factor and it rank is from 2 to64 and in decimals (Figure 2.11).

21

Page 24: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

(a) Ocean normals (b) Wired ocean normals

Figure 2.10: Ocean rendering with and without bump-mapped ripples

(a) Ocean (b) Ocean normals

(c) Wired ocean (d) Wired ocean normals

Figure 2.11: Ocean rendering with different options

22

Page 25: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Chapter 3

Performance Testing

We have developed two similar applications to understand how tessellationworks but we have to release some performance testing to know their weak-ness. The first test is about the size of textures used into terrain rendering toknow the height, the second one is about the relation between the tessellationfactor and the number of patches used.These tests has been released in a Easynote TJ76 (Intel Core i5 processor 430M,ATI Mobility Radeon HD 5470 Graphics 512MB and 4GB Memory). Every ex-ecution in a test has been done with a resolution of 1366x768 during 60 secondsand then we have taken the minimum, maximum and average frames per second.

3.1 Different texture sizes

The terrain rendering uses textures, one for the height and another one for thenormals. The size of this texture have to be enough big to have all the infor-mation when the tessellation factor is high. If this texture it’s not enough bigwe have to interpolate linearly the value of two texture pixels to know the finalheight.In this test we can see that if the terrain texture is too big then the performancedrops. The values used in the test are: minimum distance (100), maximum dis-tance (1000), minimum tessellation factor (1) and maximum tessellation factor(64).

Texture size Min. FPS Max. FPS Avg. FPS1K x 1K 354 387 380.3002K x 2K 297 324 320.2674K x 4K 90 108 102.800

3.2 Tessellation Factor vs. Number of Patches

Another thing that we have to take care of is about the relation between thetessellation factor and the number of patches. If we tessellate all the terrain with

23

Page 26: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

the same tessellation factor it’s the same in these samples if we use 16 patchesand a tessellation factor of 32 than if we use 4 patches and a tessellation factorof 64.With these tests we want to prove if in our examples is better to use the maxi-mum power of tessellator or if it’s better to use less power and more patches. Inboth examples we have adjust the minimum and maximum tessellation factorto the same value to get a regular tessellation.Test with terrain:√

Number of patches Tess. Factor Min. FPS Max. FPS Avg. FPS4 64 299 325 321.1508 32 293 315 308.617

16 16 288 310 306.40032 8 279 302 298.15064 4 243 262 260.617

Test with ocean:√

Number of patches Tess. Factor Min. FPS Max. FPS Avg. FPS4 64 166 175 171.3678 32 162 171 169.217

16 16 127 133 131.03332 8 126 136 134.25064 4 127 137 133.350

In both examples we can see that it’s better to use the maximum power oftessellator than use less power and more patches.

24

Page 27: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Chapter 4

Results

With these images we will show you the results in the terrain and ocean ren-dering and the difference if we apply the angle correction or if we change thetessellation factor.In the figure 4.1 you can see the ocean rendering if we don’t take care of aboutthe angle of the camera. You can see that quads consist of two triangles the sizeof their edges projected to the screen are different. On the other hand if you seethe figure 4.2 where the correction angle is applied then these quads seems tobe the same size to the edges. In the figures 4.3 and 4.4 you can see the sameeffect with the terrain.In the figures 4.5 and 4.7 you can see the ocean and terrain rendering with aminimum tessellation factor of 2 and a maximum tessellation factor of 64. Inthe figures 4.6 and 4.8 you can see the same image but with a higher minimumtessellation factor (4) and a lower maximum tessellation factor (16). It canbe seen that in the second case the near detail is higher but the farther detailis lower, nevertheless the difference is more noticeable in the terrain renderingthan in the ocean rendering because the highest geometry is more irregular.

25

Page 28: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 4.1: Ocean rendering without angle correction

Figure 4.2: Ocean rendering with angle correction

26

Page 29: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 4.3: Terrain rendering without angle correction

Figure 4.4: Terrain rendering with angle correction

27

Page 30: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 4.5: Ocean rendering: Min Tess Factor 2, Max Tess Factor 64

Figure 4.6: Ocean rendering: Min Tess Factor 4, Max Tess Factor 16

28

Page 31: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Figure 4.7: Terrain rendering: Min Tess Factor 2, Max Tess Factor 64

Figure 4.8: Terrain rendering: Min Tess Factor 4, Max Tess Factor 16

29

Page 32: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Chapter 5

Conclusions and futurework

5.1 Conclusions

As we seen in this thesis, hardware tessellation it’s a powerful tool that reducethe information transfer from the CPU to the GPU. It adds three new stagesto the graphic pipeline and this allows a great flexibility to take advantage ofhardware tessellation. We have seen the application in two fields, terrain andwater rendering, but it can be used to others like other meshes.The main point that we have to bear in mind is that we can use other techniquesto calculate the tessellation factor but we always have to be coherent withtessellation factor and mipmap levels with all the patches to avoid the linesbetween them that we have seen.In addition we have seen another important thing, it is better if we can usefunctions to represent the mesh like the ocean because then the resolution canbe as high as the tessellation factor sets. If we use a heightmap like in the terrainthen can be possible that we haven’t enough information into the texture andwe have to interpolate between texels. In addition we have seen that if thetexture is too high then the performance falls.

5.2 Future work

In this thesis we have done a first rapprochement to hardware tessellation butthis work can be improved or it can lead to further research. Here we give someideas about future work but we have divided in two parts, the first one can beapplied to two examples, the second one can be applied to the terrain.

5.2.1 Terrain and ocean rendering

Improvements for the terrain and ocean rendering:

30

Page 33: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

• Tessellate tri-patches instead of quad-patches to see if it’s more efficientor more realistic.

• Decide the tessellation factor depending on the size of the edges projectedto the screen instead of the distance and the angle of the camera to see ifit’s more realistic or efficient.

• Define the geometry with vertex2 indeed vertex3 to avoid informationbecause we don’t use the y coordinate from the patch.

• Apply a view frustum culling.

5.2.2 Terrain rendering

Improvements for the terrain rendering:

• Put the terrain and normal texture in only one to be more efficient, threechannels for the normals and one channel for the heights.

• Apply some visibility techniques to avoid rendering some parts of theterrain that are not visible because another higher mountain is in front ofit.

• Decide the tessellation factor depending on the entropy of the heightmappoints.

31

Page 34: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Bibliography

[1] Wendy Jones. Beginning DirectX 10 Game Programming. Course Technol-ogy Press, Boston, MA, United States, 2007.

[2] Microsoft, 2009. Windows DirectX Grapics Documentation (August 2009).

[3] Laszlo Szecsi and Khashayar Arman. Procedural ocean effects. In ShaderX6:Advanced Rendering Techniques. Charles River Media, 2008.

[4] L. Szirmay-Kalos and T. Umenhoffer. Displacement mapping on the GPU -State of the Art. Computer Graphics Forum, 27(1), 2008.

32

Page 35: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Appendix A

Terrain fx file

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// F i l e : t e r r a i n . f x//// This f i l e shows an implementat ion o f the DirectX 11// Hardware Te s s e l l a t o r f o r render ing a t e r r a i n .//// Xavi Bonaventura Brugu s//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

// This a l l ow s us to compi le the shader wi th a #de f i n e to// choose the d i f f e r e n t p a r t i t i o n modes f o r the h u l l shader .// See the h u l l shader : [ p a r t i t i o n i n g (TERRAIN HS PARTITION) ]#ifndef TERRAIN HS PARTITION#define TERRAIN HS PARTITION ” i n t e g e r ”#endif

// The input patch s i z e . I t i s 4 con t r o l po in ts , the minimum// to de f i n e a patch . This va lue shou ld match the c a l l to// IASetPrimit iveTopo logy ( )#define INPUT PATCH SIZE 4

// The output patch s i z e . I t i s a l s o 4 con t r o l po in t s .#define OUTPUT PATCH SIZE 4

// So l i d t e r r a i nRas t e r i z e r S t a t e noCu l lRas t e r i z e r{

CullMode = Front ;Fil lMode = s o l i d ;

} ;

//Wireframe t e r r a i nRas t e r i z e r S t a t e w i r edRas t e r i z e r

33

Page 36: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

{CullMode = Front ;Fil lMode = wireframe ;

} ;

//BackgroundRas t e r i z e r S t a t e d e f a u l tRa s t e r i z e r{

CullMode = Back ;} ;

DepthStenc i lS tate de fau l tCompos i tor{} ;

BlendState de fau l tB l ende r{} ;

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Constant Bu f f e r s//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−cbu f f e r trans form{

f l o a t 4x4 viewProjMatrix ;f l o a t 4x4 o r i en tPro jMat r i x Inve r s e ;f l o a t 3 eyePos i t i on ;

}

Texture2D terrainHeightMap ;Texture2D terrainNormalMap ;Texture2D te r ra inTexture ;

f loat s i z eTe r r a i n ;f loat he i gh tMu l t i p l i e r ;f loat minDistance ;f loat maxDistance ;f loat minTessExp ;f loat maxTessExp ;bool app lyCorrect ion ;int sqrtNumPatch ;int t e x tS i z e ;

//BackgroundTextureCube envMap ;

SamplerState l inearSample r{

F i l t e r = MIN MAG MIP LINEAR;AddressU = Wrap ;

34

Page 37: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

AddressV = Wrap ;} ;

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Vertex shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct VS CONTROL POINT INPUT{

f l o a t 3 vPos i t i on : POSITION;} ;

struct VS CONTROL POINT OUTPUT{

f l o a t 3 vPos i t i on : POSITION;f l o a t 3 vVec0 : VECTOR0;f l o a t 3 vVec1 : VECTOR1;f l o a t 3 vVec2 : VECTOR2;f l o a t 3 vVec3 : VECTOR3;

} ;

// This v e r t e x shader passes the con t r o l po in t s s t r a i g h t// through to the h u l l shader g e t t i n g the h e i g h t from a// t e x t u r e . In add i t i on i t c a l c u l a t e s four v e c t o r s to// ad jacen t edges .// The input to the v e r t e x shader comes from the v e r t e x// b u f f e r .// The output from the v e r t e x shader w i l l go in t o the h u l l// shader .

VS CONTROL POINT OUTPUT vsTerra in ( VS CONTROL POINT INPUTInput )

{VS CONTROL POINT OUTPUT Output ;

//We c a l c u l a t e every vec t o r t ha t goes from the v e r t e x to the// ad jacen t v e r t e x s// In the h u l l shader we w i l l need t h i s v e c t o r s to c a l c u l a t e// the ang l e o f the camera in t h i s po in tf loat increment = s i z eTe r r a i n / sqrtNumPatch ;f l o a t 2 coord = f l o a t 2 ( Input . vPos i t i on . x , Input . vPos i t i on . z ) ;f l o a t 2 coord0 = f l o a t 2 ( Input . vPos i t i on . x , Input . vPos i t i on . z

+ increment ) ;f l o a t 2 coord1 = f l o a t 2 ( Input . vPos i t i on . x + increment , Input .

vPos i t i on . z ) ;f l o a t 2 coord2 = f l o a t 2 ( Input . vPos i t i on . x , Input . vPos i t i on . z− increment ) ;

f l o a t 2 coord3 = f l o a t 2 ( Input . vPos i t i on . x − increment , Input .vPos i t i on . z ) ;

35

Page 38: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f loat he ight = terrainHeightMap . SampleLevel ( l inearSampler ,coord / s i z eTe r ra in , 0) . x ∗ he i gh tMu l t i p l i e r ;

f loat he ight0 = terrainHeightMap . SampleLevel ( l inearSampler ,coord0 / s i z eTe r ra in , 0) . x ∗ he i gh tMu l t i p l i e r ;

f loat he ight1 = terrainHeightMap . SampleLevel ( l inearSampler ,coord1 / s i z eTe r ra in , 0) . x ∗ he i gh tMu l t i p l i e r ;

f loat he ight2 = terrainHeightMap . SampleLevel ( l inearSampler ,coord2 / s i z eTe r ra in , 0) . x ∗ he i gh tMu l t i p l i e r ;

f loat he ight3 = terrainHeightMap . SampleLevel ( l inearSampler ,coord3 / s i z eTe r ra in , 0) . x ∗ he i gh tMu l t i p l i e r ;

f l o a t 3 pos = f l o a t 3 ( coord . x , height , coord . y ) ;f l o a t 3 pos0 = f l o a t 3 ( coord0 . x , height0 , coord0 . y ) ;f l o a t 3 pos1 = f l o a t 3 ( coord1 . x , height1 , coord1 . y ) ;f l o a t 3 pos2 = f l o a t 3 ( coord2 . x , height2 , coord2 . y ) ;f l o a t 3 pos3 = f l o a t 3 ( coord3 . x , height3 , coord3 . y ) ;

Output . vVec0 = normal ize ( pos0 − pos ) ;Output . vVec1 = normal ize ( pos1 − pos ) ;Output . vVec2 = normal ize ( pos2 − pos ) ;Output . vVec3 = normal ize ( pos3 − pos ) ;

Output . vPos i t i on = pos ;

return Output ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Constant data func t i on f o r the hsTerrain . This i s execu ted// once per patch .//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct HS CONSTANTDATAOUTPUT{

f loat Edges [ 4 ] : SV TessFactor ;f loat I n s i d e [ 2 ] : SV Ins ideTessFactor ;f loat mipLevel [ 4 ] : MIPLEVELVERTEXS;

} ;

struct HS OUTPUT{

f l o a t 3 vPos i t i on : POSITION;} ;

// This cons tant h u l l shader i s execu ted once per patch . I t// w i l l be execu ted SQRT NUMBER OF PATCHS ∗// SQRT NUMBER OF PATCHS times . We c a l c u l a t e a v a r i a b l e// t e s s e l l a t i o n f a c t o r based on the ang l e and the d i s tnace// o f the camera .

36

Page 39: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

HS CONSTANTDATAOUTPUT TerrainConstantHS ( InputPatch<VS CONTROL POINT OUTPUT, INPUT PATCH SIZE> ip , u intPatchID : SV PrimitiveID )

{HS CONSTANTDATAOUTPUT Output ;

// Ca l cu l a t i n g the c en t r a l po in t o f the patch and the c en t r a l// po in t f o r every edgef l o a t 3 middlePoint = ( ip [ 0 ] . vPos i t i on + ip [ 1 ] . vPos i t i on + ip

[ 2 ] . vPos i t i on + ip [ 3 ] . vPos i t i on ) / 4 ;f l o a t 3 middlePointEdge0 = ( ip [ 0 ] . vPos i t i on + ip [ 1 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge1 = ( ip [ 3 ] . vPos i t i on + ip [ 0 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge2 = ( ip [ 2 ] . vPos i t i on + ip [ 3 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge3 = ( ip [ 1 ] . vPos i t i on + ip [ 2 ] . vPos i t i on

) / 2 ;

// Ca l cu l a t i n g a co r r e c t i on t ha t w i l l be app l i e d to// t e s s e l l a t i o n f a c t o r depending on the ang l e o f the camera//We c a l c u l a t e a co r r e c t i on f o r every c en t r a l po in t o f the// edges and the middle to avoid some l i n e s between patches .f l o a t 2 c o r r e c t i o n I n s i d e = f l o a t 2 (1 , 1) ;f l o a t 4 co r r ec t i onEdges = f l o a t 4 (1 , 1 , 1 , 1) ;f l o a t 4 co r r e c t i onVe r t ex s = f l o a t 4 (1 , 1 , 1 , 1) ;i f ( app lyCorrect ion ){

f l o a t 3 i n s i d eD i r e c t i o n 0 = normal ize ( middlePointEdge0 −middlePointEdge2 ) ;

f l o a t 3 i n s i d eD i r e c t i o n 1 = normal ize ( middlePointEdge1 −middlePointEdge3 ) ;

f l o a t 3 edgeDi rec t i on0 = normal ize ( ip [ 0 ] . vPos i t i on − ip [ 1 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on1 = normal ize ( ip [ 3 ] . vPos i t i on − ip [ 0 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on2 = normal ize ( ip [ 2 ] . vPos i t i on − ip [ 3 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on3 = normal ize ( ip [ 1 ] . vPos i t i on − ip [ 2 ] .vPos i t i on ) ;

f l o a t 3 toCameraMiddle = normal ize ( eyePos i t i on −middlePoint ) ;

f l o a t 3 toCameraEdge0 = normal ize ( eyePos i t i on −middlePointEdge0 ) ;

f l o a t 3 toCameraEdge1 = normal ize ( eyePos i t i on −middlePointEdge1 ) ;

f l o a t 3 toCameraEdge2 = normal ize ( eyePos i t i on −middlePointEdge2 ) ;

37

Page 40: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f l o a t 3 toCameraEdge3 = normal ize ( eyePos i t i on −middlePointEdge3 ) ;

f loat rank = 0 . 4 ;f loat s h i f t = 1 − rank / 2 ;c o r r e c t i o n I n s i d e . x = ( ( 1 .57 − acos ( abs ( dot (

toCameraMiddle , i n s i d eD i r e c t i o n 0 ) ) ) ) / 1 .57 ) ∗rank + s h i f t ;

c o r r e c t i o n I n s i d e . y = ( ( 1 .57 − acos ( abs ( dot (toCameraMiddle , i n s i d eD i r e c t i o n 1 ) ) ) ) / 1 .57 ) ∗rank + s h i f t ;

co r r e c t i onEdges . x = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge0 , edgeDi rec t i on0 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges . y = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge1 , edgeDi rec t i on1 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges . z = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge2 , edgeDi rec t i on2 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges .w = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge3 , edgeDi rec t i on3 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

f l o a t 3 toCameraVertex0 = normal ize ( eyePos i t i on − ip [ 0 ] .vPos i t i on ) ;

f l o a t 3 toCameraVertex1 = normal ize ( eyePos i t i on − ip [ 1 ] .vPos i t i on ) ;

f l o a t 3 toCameraVertex2 = normal ize ( eyePos i t i on − ip [ 2 ] .vPos i t i on ) ;

f l o a t 3 toCameraVertex3 = normal ize ( eyePos i t i on − ip [ 3 ] .vPos i t i on ) ;

f loat angle0 = ( acos ( abs ( dot ( toCameraVertex0 , ip [ 0 ] .vVec0 ) ) ) + acos ( abs ( dot ( toCameraVertex0 , ip [ 0 ] .vVec1 ) ) ) + acos ( abs ( dot ( toCameraVertex0 , ip [ 0 ] .vVec2 ) ) ) + acos ( abs ( dot ( toCameraVertex0 , ip [ 0 ] .vVec3 ) ) ) ) / 4 ;

f loat angle1 = ( acos ( abs ( dot ( toCameraVertex1 , ip [ 1 ] .vVec0 ) ) ) + acos ( abs ( dot ( toCameraVertex1 , ip [ 1 ] .vVec1 ) ) ) + acos ( abs ( dot ( toCameraVertex1 , ip [ 1 ] .vVec2 ) ) ) + acos ( abs ( dot ( toCameraVertex1 , ip [ 1 ] .vVec3 ) ) ) ) / 4 ;

f loat angle2 = ( acos ( abs ( dot ( toCameraVertex2 , ip [ 2 ] .vVec0 ) ) ) + acos ( abs ( dot ( toCameraVertex2 , ip [ 2 ] .vVec1 ) ) ) + acos ( abs ( dot ( toCameraVertex2 , ip [ 2 ] .vVec2 ) ) ) + acos ( abs ( dot ( toCameraVertex2 , ip [ 2 ] .vVec3 ) ) ) ) / 4 ;

38

Page 41: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f loat angle3 = ( acos ( abs ( dot ( toCameraVertex3 , ip [ 3 ] .vVec0 ) ) ) + acos ( abs ( dot ( toCameraVertex3 , ip [ 3 ] .vVec1 ) ) ) + acos ( abs ( dot ( toCameraVertex3 , ip [ 3 ] .vVec2 ) ) ) + acos ( abs ( dot ( toCameraVertex3 , ip [ 3 ] .vVec3 ) ) ) ) / 4 ;

c o r r e c t i onVe r t ex s . x = ( 1 − ( ( 1 .57 − angle0 ) / 1 .57 ) )∗ rank + s h i f t ;

c o r r e c t i onVe r t ex s . y = ( 1 − ( ( 1 .57 − angle1 ) / 1 .57 ) )∗ rank + s h i f t ;

c o r r e c t i onVe r t ex s . z = ( 1 − ( ( 1 .57 − angle2 ) / 1 .57 ) )∗ rank + s h i f t ;

c o r r e c t i onVe r t ex s .w = ( 1 − ( ( 1 .57 − angle3 ) / 1 .57 ) )∗ rank + s h i f t ;

}

// Ca l cu l a t i n g the d i s t ance f o r every patch v e r t e x to the//cameraf l o a t 4 magnitudeVertexs ;magnitudeVertexs . x = clamp ( d i s t anc e ( ip [ 0 ] . vPos i t ion ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeVertexs . y = clamp ( d i s t anc e ( ip [ 1 ] . vPos i t ion ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeVertexs . z = clamp ( d i s t ance ( ip [ 2 ] . vPos i t ion ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeVertexs .w = clamp ( d i s t ance ( ip [ 3 ] . vPos i t ion ,

eyePos i t i on ) , minDistance , maxDistance ) ;

// Ca l cu l a t i n g the minimum and maximum mip−map l e v e l to be// usedf loat minMipmapLevel = log2 ( t e x tS i z e ) − l og2 ( sqrtNumPatch ∗

pow(2 , maxTessExp ) ) ;f loat maxMipmapLevel = log2 ( t e x tS i z e ) − l og2 ( sqrtNumPatch ∗

pow(2 , minTessExp ) ) ;i f ( minMipmapLevel < 0)

minMipmapLevel = 0 ;

// Ca l cu l a t i n g the d i f e r en c e between the maximum and the//minimum d i s t ancef loat d i f fD i s t a n c e = maxDistance − minDistance ;

// Ca l cu l a t i n g a mip map l e v e l f a c t o r from 0 to 1 f o r every// v e r t e x .//We app ly the co r r e c t i on depending on the ang l e o f the//camera .f l o a t 4 f a c t o rVe r t ex s = clamp ( ( ( magnitudeVertexs −

minDistance ) / d i f fD i s t a n c e ) ∗ co r r e c t i onVer t ex s ,f l o a t 4 (0 , 0 , 0 , 0) , f l o a t 4 (1 , 1 , 1 , 1) ) ;

//Linear i n t e r p o l a t i o n between the minimum and the maximum

39

Page 42: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

//mipmap l e v e lOutput . mipLevel [ 0 ] = l e r p (minMipmapLevel , maxMipmapLevel ,

f a c t o rVe r t ex s . x ) ;Output . mipLevel [ 1 ] = l e r p (minMipmapLevel , maxMipmapLevel ,

f a c t o rVe r t ex s . y ) ;Output . mipLevel [ 2 ] = l e r p (minMipmapLevel , maxMipmapLevel ,

f a c t o rVe r t ex s . z ) ;Output . mipLevel [ 3 ] = l e r p (minMipmapLevel , maxMipmapLevel ,

f a c t o rVe r t ex s .w) ;

// Ca l cu l a t i n g the d i s t ance from the c en t r a l po in t o f the// patch to the camera and from every c en t r a l po in t o f every// edge to the cameraf loat magnitude = clamp ( d i s t ance ( middlePoint , eyePos i t i on ) ,

minDistance , maxDistance ) ;f l o a t 4 magnitudeEdges ;magnitudeEdges . x = clamp ( d i s t anc e ( middlePointEdge0 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges . y = clamp ( d i s t anc e ( middlePointEdge1 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges . z = clamp ( d i s t ance ( middlePointEdge2 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges .w = clamp ( d i s t anc e ( middlePointEdge3 ,

eyePos i t i on ) , minDistance , maxDistance ) ;

// Ca l cu l a t i n g a t e s s e l l a t i o n f a c t o r from 0 to 1 f o r every// edge and the middle .//We app ly the co r r e c t i on depending on the ang l e o f the//camera .f l o a t 2 f a c t o r I n s i d e = 1 − s a tu ra t e ( ( ( magnitude −

minDistance ) / d i f fD i s t a n c e ) ∗ c o r r e c t i o n I n s i d e ) ;f l o a t 4 factorEdges = 1 − s a tu ra t e ( ( ( magnitudeEdges −

minDistance ) / d i f fD i s t a n c e ) ∗ co r r ec t i onEdges ) ;

//Linear i n t e r p o l a t i o n between the minimum and the maximum// t e s s e l l a t i o n exponent . Then we round i t and we ra i s ed 2// to t h i s va lue to ge t a power o f two t e s s e l l a t i o n f a c t o r .Output . Edges [ 0 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp ,

fac torEdges . x ) ) ) ;Output . Edges [ 1 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp ,

fac torEdges . y ) ) ) ;Output . Edges [ 2 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp ,

fac torEdges . z ) ) ) ;Output . Edges [ 3 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp ,

fac torEdges .w) ) ) ;

Output . I n s i d e [ 0 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp, f a c t o r I n s i d e . x ) ) ) ;

Output . I n s i d e [ 1 ] = pow( 2 , round ( l e r p (minTessExp , maxTessExp, f a c t o r I n s i d e . y ) ) ) ;

40

Page 43: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

return Output ;}

// The h u l l shader i s c a l l e d once per output c on t r o l point ,// which i s s p e c i f i e d wi th ou t pu t c on t r o l p o i n t s . In t h i s case ,// we take the con t r o l po in t s from the v e r t e x shader and pass// them d i r e c t l y o f f to the domain shader .// The input to the h u l l shader comes from the v e r t e x shader .// The output from the h u l l shader w i l l go to the domain// shader .// The t e s s e l l a t i o n fac to r , topo logy , and p a r t i t i o n mode w i l l// go to the f i x e d func t i on t e s s e l l a t o r s t a g e to c a l c u l a t e the// UVW and domain po in t s .

[ domain ( ”quad” ) ][ p a r t i t i o n i n g (TERRAIN HS PARTITION) ][ outputtopology ( ” t r i ang l e cw ” ) ][ ou tput con t ro lpo in t s (OUTPUT PATCH SIZE) ][ patchconstant func ( ”TerrainConstantHS” ) ]HS OUTPUT hsTerra in ( InputPatch<VS CONTROL POINT OUTPUT,

INPUT PATCH SIZE> p , u int i : SV OutputControlPointID ,u int PatchID : SV PrimitiveID )

{HS OUTPUT Output ;Output . vPos i t i on = p [ i ] . vPos i t i on ;return Output ;

}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Domain shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct DS OUTPUT{

f l o a t 4 vPos i t i on : SV POSITION ;f l o a t 3 vNormal : NORMAL;f l o a t 2 tex : TEXCOORD0;

} ;

// The domain shader i s run once per v e r t e x and c a l c u l a t e s the// f i n a l v e r t e x ’ s p o s i t i o n and a t t r i b u t e s . I t r e c e i v e s the UVW// from the f i x e d func t i on t e s s e l l a t o r and the con t r o l po in t// ou tpu t s from the h u l l shader . Since we are us ing the// DirectX 11 Te s s e l l a t i o n p i p e l i n e , i t i s the domain shader ’ s// r e s p o n s i b i l i t y to c a l c u l a t e the f i n a l SV POSITION fo r each// v e r t e x .// The input SV DomainLocation to the domain shader comes from// f i x e d func t i on t e s s e l l a t o r . And the OutputPatch comes from// the h u l l shader . From these , we must c a l c u l a t e the f i n a l// v e r t e x pos i t i on , co lor , texcoords , and o ther a t t r i b u t e s .

41

Page 44: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

// The output from the domain shader w i l l be a v e r t e x t ha t// w i l l go to the v ideo card ’ s r a s t e r i z a t i o n p i p e l i n e and ge t// drawn to the screen .

[ domain ( ”quad” ) ]DS OUTPUT dsTerra in ( HS CONSTANTDATAOUTPUT input , f l o a t 2 UV

: SV DomainLocation , const OutputPatch<HS OUTPUT,OUTPUT PATCH SIZE> patch )

{// A l l the i n i t i a l pa tches have to be the same he i g h t//Linear i n t e r p o l a t i o n between the p o s i t i o n o f the cornersf loat WorldPosX = patch [ 0 ] . vPos i t i on . x + ( ( patch [ 2 ] .

vPos i t i on . x − patch [ 0 ] . vPos i t i on . x ) ∗ UV. x ) ;f loat WorldPosY ;f loat WorldPosZ = patch [ 0 ] . vPos i t i on . z + ( ( patch [ 2 ] .

vPos i t i on . z − patch [ 0 ] . vPos i t i on . z ) ∗ UV. y ) ;f l o a t 2 textCoord = f l o a t 2 (WorldPosX , WorldPosZ ) /

s i z eTe r r a i n ;

// I n t e r p o l a t i o n o f mip−map l e v e l between four mip−map l e v e l// in the v e r t e x sf loat mipLevel01 = (UV. y ∗ input . mipLevel [ 1 ] + (1 − UV. y ) ∗

input . mipLevel [ 0 ] ) ;f loat mipLevel32 = (UV. y ∗ input . mipLevel [ 2 ] + (1 − UV. y ) ∗

input . mipLevel [ 3 ] ) ;f loat mipLevel = (UV. x ∗ mipLevel32 + (1 − UV. x ) ∗

mipLevel01 ) ;

// Se t t i n g the t e r r a i n h e i g h t from a he i g h t map t e x t u r e and// s c a l e by h e i g h tMu l t i p l i e rWorldPosY = terrainHeightMap . SampleLevel ( l inearSampler ,

textCoord , mipLevel ) . x ∗ he i gh tMu l t i p l i e r ;//Get t ing the normals from a normal map t e x t u r ef l o a t 3 normal = terrainNormalMap . SampleLevel ( l inearSampler ,

textCoord , mipLevel ) . xyz ;

DS OUTPUT Output ;

// Se t t i n g po s i t i on , world po s i t i on , normal and t e x t u r e// coord ina t e sOutput . vPos i t i on = mul ( f l o a t 4 ( f l o a t 3 (WorldPosX , WorldPosY ,

WorldPosZ ) ,1 ) , viewProjMatrix ) ;//We change the range from [ 0 , 1 ] to [−1 ,1]Output . vNormal = normal ize ( normal ∗ 2 .0 f − 1 .0 f ) ;Output . tex = textCoord ;

return Output ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

42

Page 45: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

// Shading p i x e l shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

// The p i x e l shader works the same as i t would in a normal// g raph i c s p i p e l i n e . In t h i s case , we ge t the co l o r from a// t e x t u r e and we perform a s imple l i g h t i n g .

f l o a t 4 psTerra in (DS OUTPUT input ) : SV TARGET{

//We have to normal ize again because can be not normal izedf l o a t 3 N = normal ize ( input . vNormal ) ;f l o a t 3 L = normal ize ( f l o a t 3 (3 , 2 , 0) ) ;

f l o a t 4 c o l o r = te r ra inTexture . Sample ( l inearSampler , input .tex ∗ 10) ;

return c o l o r ∗ s a tu ra t e ( dot (N, L) ) + co l o r ∗ 0 . 1 ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// So l i d co l o r shading p i x e l shader ( used f o r wireframe// ove r l ay )//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−f l o a t 4 psTerrainWired ( DS OUTPUT Input ) : SV TARGET{

// Return a s o l i d whi te co l o rreturn f l o a t 4 ( 1 , 1 , 1 , 1 ) ;

}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Normal co l o r shading p i x e l shader ( used f o r normal ove r l a y )//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−f l o a t 4 psTerrainNormal (DS OUTPUT input ) : SV TARGET{

//We have to normal ize again because can be not normal izedf l o a t 3 N = normal ize ( input . vNormal ) ;

return f l o a t 4 (N, 1) ;}

//Technique used to render the t e r r a i ntechnique11 t e r r a i n{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsTerra in ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsTerra in ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsTerra in ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( noCu l lRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psTerra in ( ) ) ) ;

43

Page 46: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

}}

//Technique used to render the t e r r a i n normalstechnique11 t e r r a in no rma l{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsTerra in ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsTerra in ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsTerra in ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( noCu l lRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psTerrainNormal ( ) )

) ;}

}

//Technique used to render the wireframe t e r r a i ntechnique11 w i r e d t e r r a i n{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsTerra in ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsTerra in ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsTerra in ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( w i r edRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psTerrainWired ( ) )

) ;}

}

//Technique used to render the wireframe t e r r a i n normalstechnique11 w i r ed t e r r a in no rma l{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsTerra in ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsTerra in ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsTerra in ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( w i r edRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psTerrainNormal ( ) )

) ;}

}

//This l a s t par t o f the f x f i l e i s to render the backgroundstruct QuadInput{

f l o a t 4 pos : POSITION;f l o a t 2 tex : TEXCOORD0;

44

Page 47: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

} ;

struct QuadOutput{

f l o a t 4 pos : SV POSITION ;f l o a t 2 tex : TEXCOORD0;f l o a t 3 viewDir : TEXCOORD1;

} ;

f l o a t 4 psBackground (QuadOutput input ) : SV TARGET{

return envMap . Sample ( l inearSampler , input . viewDir ) ;} ;

QuadOutput vsQuad (QuadInput input ){

QuadOutput output = (QuadOutput ) 0 ;

output . pos = input . pos ;f l o a t 4 hWorldPosMinusEye = mul ( input . pos ,

o r i en tPro jMat r i x Inve r s e ) ;hWorldPosMinusEye /= hWorldPosMinusEye .w;output . viewDir = hWorldPosMinusEye . xyz ;output . pos . z = 0 . 99999 ;output . tex = input . tex ;return output ;

} ;

t echnique11 background{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsQuad ( ) ) ) ;SetHul lShader ( NULL ) ;SetDomainShader ( NULL ) ;S e tRa s t e r i z e rS t a t e ( d e f a u l tRa s t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psBackground ( ) ) ) ;Se tDepthStenc i lS ta te ( defaultCompositor , 0) ;SetBlendState ( de fau l tB lender , f l o a t 4 ( 0 .0 f , 0 . 0 f , 0 . 0 f ,

0 . 0 f ) , 0 x f f f f f f f f ) ;}

}

45

Page 48: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

Appendix B

Ocean fx file

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// F i l e : ocean . f x//// This f i l e shows an implementat ion o f the DirectX 11// Hardware Te s s e l l a t o r f o r render ing a ocean .//// Xavi Bonaventura Brugu s//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

// This a l l ow s us to compi le the shader wi th a #de f i n e to// choose the d i f f e r e n t p a r t i t i o n modes f o r the h u l l shader .// See the h u l l shader : [ p a r t i t i o n i n g (OCEAN HS PARTITION) ]#ifndef OCEAN HS PARTITION#define OCEAN HS PARTITION ” f r a c t i o n a l e v e n ”#endif

// The input patch s i z e . I t i s 4 con t r o l po in ts , the minimum// to de f i n e a patch . This va lue shou ld match the c a l l to// IASetPrimit iveTopo logy ( )#define INPUT PATCH SIZE 4

// The output patch s i z e . I t i s a l s o 4 con t r o l po in t s .#define OUTPUT PATCH SIZE 4

#include ”waves . h”Ra s t e r i z e r S t a t e noCu l lRas t e r i z e r{

CullMode = Front ;Fil lMode = s o l i d ;

} ;

Ra s t e r i z e r S t a t e w i r edRas t e r i z e r{

46

Page 49: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

CullMode = Front ;Fil lMode = wireframe ;

} ;

//BackgroundRas t e r i z e r S t a t e d e f a u l tRa s t e r i z e r{

CullMode = Back ;} ;

DepthStenc i lS tate de fau l tCompos i tor{} ;

BlendState de fau l tB l ende r{} ;

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Constant Bu f f e r s//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−cbu f f e r trans form{

f l o a t 4x4 viewProjMatrix ;f l o a t 4x4 o r i en tPro jMat r i x Inve r s e ;f l o a t 3 eyePos i t i on ;

}

Texture2D oceanTexture ;Texture2D bumpTexture ;

f loat s i z eTe r r a i n ;f loat time ;

//BackgroundTextureCube envMap ;

SamplerState l inearSample r{

F i l t e r = MIN MAG MIP LINEAR;AddressU = Wrap ;AddressV = Wrap ;

} ;

bool app lyCorrect ion ;f loat minDistance ;f loat maxDistance ;f loat minTessExp ;f loat maxTessExp ;

47

Page 50: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Vertex shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct VS CONTROL POINT INPUT{

f l o a t 3 vPos i t i on : POSITION;} ;

struct VS CONTROL POINT OUTPUT{

f l o a t 3 vPos i t i on : POSITION;} ;

// This v e r t e x shader passes the con t r o l po in t s s t r a i g h t// through to the h u l l shader .// The input to the v e r t e x shader comes from the v e r t e x// b u f f e r .// The output from the v e r t e x shader w i l l go in t o the h u l l// shader .

VS CONTROL POINT OUTPUT vsOcean ( VS CONTROL POINT INPUT Input)

{VS CONTROL POINT OUTPUT Output ;

Output . vPos i t i on = Input . vPos i t i on ;

return Output ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Constant data func t i on f o r the hsOcean . This i s execu ted// once per patch .//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct HS CONSTANTDATAOUTPUT{

f loat Edges [ 4 ] : SV TessFactor ;f loat I n s i d e [ 2 ] : SV Ins ideTessFactor ;

} ;

struct HS OUTPUT{

f l o a t 3 vPos i t i on : POSITION;} ;

// This cons tant h u l l shader i s execu ted once per patch . I t// w i l l be execu ted SQRT NUMBER OF PATCHS ∗// SQRT NUMBER OF PATCHS times . We c a l c u l a t e a v a r i a b l e// t e s s e l l a t i o n f a c t o r based on the ang l e and the d i s tnace// o f the camera .

48

Page 51: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

HS CONSTANTDATAOUTPUT OceanConstantHS ( InputPatch<VS CONTROL POINT OUTPUT, INPUT PATCH SIZE> ip , u intPatchID : SV PrimitiveID )

{HS CONSTANTDATAOUTPUT Output ;

// Ca l cu l a t i n g the c en t r a l po in t o f the patch and the c en t r a l// po in t f o r every edgef l o a t 3 middlePoint = ( ip [ 0 ] . vPos i t i on + ip [ 1 ] . vPos i t i on + ip

[ 2 ] . vPos i t i on + ip [ 3 ] . vPos i t i on ) / 4 ;f l o a t 3 middlePointEdge0 = ( ip [ 0 ] . vPos i t i on + ip [ 1 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge1 = ( ip [ 3 ] . vPos i t i on + ip [ 0 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge2 = ( ip [ 2 ] . vPos i t i on + ip [ 3 ] . vPos i t i on

) / 2 ;f l o a t 3 middlePointEdge3 = ( ip [ 1 ] . vPos i t i on + ip [ 2 ] . vPos i t i on

) / 2 ;

// Ca l cu l a t i n g a co r r e c t i on t ha t w i l l be app l i e d to// t e s s e l l a t i o n f a c t o r depending on the ang l e o f the camera//We c a l c u l a t e a co r r e c t i on f o r every c en t r a l po in t o f the// edges and the middle to avoid some l i n e s between patches .f l o a t 2 c o r r e c t i o n I n s i d e = f l o a t 2 (1 , 1) ;f l o a t 4 co r r ec t i onEdges = f l o a t 4 (1 , 1 , 1 , 1) ;i f ( app lyCorrect ion ){

f l o a t 3 i n s i d eD i r e c t i o n 0 = normal ize ( middlePointEdge0 −middlePointEdge2 ) ;

f l o a t 3 i n s i d eD i r e c t i o n 1 = normal ize ( middlePointEdge1 −middlePointEdge3 ) ;

f l o a t 3 edgeDi rec t i on0 = normal ize ( ip [ 0 ] . vPos i t i on − ip [ 1 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on1 = normal ize ( ip [ 3 ] . vPos i t i on − ip [ 0 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on2 = normal ize ( ip [ 2 ] . vPos i t i on − ip [ 3 ] .vPos i t i on ) ;

f l o a t 3 edgeDi rec t i on3 = normal ize ( ip [ 1 ] . vPos i t i on − ip [ 2 ] .vPos i t i on ) ;

f l o a t 3 toCameraMiddle = normal ize ( eyePos i t i on −middlePoint ) ;

f l o a t 3 toCameraEdge0 = normal ize ( eyePos i t i on −middlePointEdge0 ) ;

f l o a t 3 toCameraEdge1 = normal ize ( eyePos i t i on −middlePointEdge1 ) ;

f l o a t 3 toCameraEdge2 = normal ize ( eyePos i t i on −middlePointEdge2 ) ;

49

Page 52: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f l o a t 3 toCameraEdge3 = normal ize ( eyePos i t i on −middlePointEdge3 ) ;

f loat rank = 0 . 4 ;f loat s h i f t = 1 − rank / 2 ;c o r r e c t i o n I n s i d e . x = ( ( 1 .57 − acos ( abs ( dot (

toCameraMiddle , i n s i d eD i r e c t i o n 0 ) ) ) ) / 1 .57 ) ∗rank + s h i f t ;

c o r r e c t i o n I n s i d e . y = ( ( 1 .57 − acos ( abs ( dot (toCameraMiddle , i n s i d eD i r e c t i o n 1 ) ) ) ) / 1 .57 ) ∗rank + s h i f t ;

co r r e c t i onEdges . x = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge0 , edgeDi rec t i on0 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges . y = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge1 , edgeDi rec t i on1 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges . z = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge2 , edgeDi rec t i on2 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

co r r e c t i onEdges .w = ( ( 1 .57 − acos ( abs ( dot (toCameraEdge3 , edgeDi rec t i on3 ) ) ) ) / 1 .57 ) ∗ rank+ s h i f t ;

}

// Ca l cu l a t i n g the d i s t ance from the c en t r a l po in t o f the// patch to the camera and from every c en t r a l po in t o f every// edge to the cameraf loat magnitude = clamp ( d i s t ance ( middlePoint , eyePos i t i on ) ,

minDistance , maxDistance ) ;f l o a t 4 magnitudeEdges ;magnitudeEdges . x = clamp ( d i s t anc e ( middlePointEdge0 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges . y = clamp ( d i s t anc e ( middlePointEdge1 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges . z = clamp ( d i s t ance ( middlePointEdge2 ,

eyePos i t i on ) , minDistance , maxDistance ) ;magnitudeEdges .w = clamp ( d i s t anc e ( middlePointEdge3 ,

eyePos i t i on ) , minDistance , maxDistance ) ;

// Ca l cu l a t i n g the d i f e r en c e between the maximum and the//minimum d i s t ancef loat d i f fD i s t a n c e = maxDistance − minDistance ;

// Ca l cu l a t i n g a t e s s e l l a t i o n f a c t o r from 0 to 1 f o r every// edge and the middle .//We app ly the co r r e c t i on depending on the ang l e o f the//camera .

50

Page 53: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f l o a t 2 f a c t o r I n s i d e = 1 − s a tu ra t e ( ( ( magnitude −minDistance ) / d i f fD i s t a n c e ) ∗ c o r r e c t i o n I n s i d e ) ;

f l o a t 4 factorEdges = 1 − s a tu ra t e ( ( ( magnitudeEdges −minDistance ) / d i f fD i s t a n c e ) ∗ co r r ec t i onEdges ) ;

//Linear i n t e r p o l a t i o n between the minimum and the maximum// t e s s e l l a t i o n exponent . Then we ra i s ed 2// to t h i s va lue to ge t the t e s s e l l a t i o n f a c t o r .Output . Edges [ 0 ] = pow(2 , l e r p (minTessExp , maxTessExp ,

fac torEdges . x ) ) ;Output . Edges [ 1 ] = pow(2 , l e r p (minTessExp , maxTessExp ,

fac torEdges . y ) ) ;Output . Edges [ 2 ] = pow(2 , l e r p (minTessExp , maxTessExp ,

fac torEdges . z ) ) ;Output . Edges [ 3 ] = pow(2 , l e r p (minTessExp , maxTessExp ,

fac torEdges .w) ) ;

Output . I n s i d e [ 0 ] = pow(2 , l e r p (minTessExp , maxTessExp ,f a c t o r I n s i d e . x ) ) ;

Output . I n s i d e [ 1 ] = pow(2 , l e r p (minTessExp , maxTessExp ,f a c t o r I n s i d e . y ) ) ;

return Output ;}

// The h u l l shader i s c a l l e d once per output c on t r o l point ,// which i s s p e c i f i e d wi th ou t pu t c on t r o l p o i n t s . In t h i s case ,// we take the con t r o l po in t s from the v e r t e x shader and pass// them d i r e c t l y o f f to the domain shader .// The input to the h u l l shader comes from the v e r t e x shader .// The output from the h u l l shader w i l l go to the domain// shader .// The t e s s e l l a t i o n fac to r , topo logy , and p a r t i t i o n mode w i l l// go to the f i x e d func t i on t e s s e l l a t o r s t a g e to c a l c u l a t e the// UVW and domain po in t s .

[ domain ( ”quad” ) ][ p a r t i t i o n i n g (OCEAN HS PARTITION) ][ outputtopology ( ” t r i ang l e cw ” ) ][ ou tput con t ro lpo in t s (OUTPUT PATCH SIZE) ][ patchconstant func ( ”OceanConstantHS” ) ]HS OUTPUT hsOcean ( InputPatch<VS CONTROL POINT OUTPUT,

INPUT PATCH SIZE> p , u int i : SV OutputControlPointID ,u int PatchID : SV PrimitiveID )

{HS OUTPUT Output ;Output . vPos i t i on = p [ i ] . vPos i t i on ;return Output ;

}

51

Page 54: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Domain shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−struct DS OUTPUT{

f l o a t 4 vPos i t i on : SV POSITION ;f l o a t 3 vWorldPos : WORLDPOS;f l o a t 3 vNormal : NORMAL;f l o a t 3 vBinormal : BINORMAL;f l o a t 3 vTangent : TANGENT;

} ;

// The domain shader i s run once per v e r t e x and c a l c u l a t e s the// f i n a l v e r t e x ’ s p o s i t i o n and a t t r i b u t e s . I t r e c e i v e s the UVW// from the f i x e d func t i on t e s s e l l a t o r and the con t r o l po in t// ou tpu t s from the h u l l shader . Since we are us ing the// DirectX 11 Te s s e l l a t i o n p i p e l i n e , i t i s the domain shader ’ s// r e s p o n s i b i l i t y to c a l c u l a t e the f i n a l SV POSITION fo r each// v e r t e x .// The input SV DomainLocation to the domain shader comes from// f i x e d func t i on t e s s e l l a t o r . And the OutputPatch comes from// the h u l l shader . From these , we must c a l c u l a t e the f i n a l// v e r t e x pos i t i on , co lor , texcoords , and o ther a t t r i b u t e s .// The output from the domain shader w i l l be a v e r t e x t ha t// w i l l go to the v ideo card ’ s r a s t e r i z a t i o n p i p e l i n e and ge t// drawn to the screen .

[ domain ( ”quad” ) ]DS OUTPUT dsOcean ( HS CONSTANTDATAOUTPUT input , f l o a t 2 UV :

SV DomainLocation , const OutputPatch<HS OUTPUT,OUTPUT PATCH SIZE> patch )

{//Linear i n t e r p o l a t i o n between the p o s i t i o n o f the cornersf loat WorldPosX = patch [ 0 ] . vPos i t i on . x + ( ( patch [ 2 ] .

vPos i t i on . x − patch [ 0 ] . vPos i t i on . x ) ∗ UV. x ) ;f loat WorldPosZ = patch [ 0 ] . vPos i t i on . z + ( ( patch [ 2 ] .

vPos i t i on . z − patch [ 0 ] . vPos i t i on . z ) ∗ UV. y ) ;

// Ca l cu l a t i n g the p o s i t i o n and normal o f every v e r t e x// depending on the wavesf l o a t 3 du = f l o a t 3 (1 , 0 , 0) ;f l o a t 3 dv = f l o a t 3 (0 , 0 , 1) ;f l o a t 3 disp lacement = 0 ;

for ( int i =0; i<NWAVES; i++){

f loat pdotk = dot ( f l o a t 3 (WorldPosX , 0 , WorldPosZ ) , wave [ i] . d i r e c t i o n ) ;

f loat ve l = sq r t ( 1 .5621 f ∗ wave [ i ] . wavelength ) ;

52

Page 55: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f loat phase = 6.28 f / wave [ i ] . wavelength ∗( pdotk + ve l ∗time ) ;

f l o a t 2 o f f s e t ;s i n c o s ( phase , o f f s e t . x , o f f s e t . y ) ;o f f s e t . x ∗= −wave [ i ] . amplitude ;o f f s e t . y ∗= −wave [ i ] . amplitude ;

d i sp lacement += f l o a t 3 (wave [ i ] . d i r e c t i o n . x ∗ o f f s e t . x , −o f f s e t . y , wave [ i ] . d i r e c t i o n . z ∗ o f f s e t . x ) ;

f l o a t 3 da = f l o a t 3 (wave [ i ] . d i r e c t i o n . x ∗ o f f s e t . y , o f f s e t .x , wave [ i ] . d i r e c t i o n . z ∗ o f f s e t . y ) ;

da ∗= 6.28 / wave [ i ] . wavelength ;du += da ∗ wave [ i ] . d i r e c t i o n . x ;dv += da ∗ wave [ i ] . d i r e c t i o n . z ;

}

// Ca l cu l a t i on o f the f i n a l p o s i t i o nf l o a t 3 po s i t i o n = f l o a t 3 (WorldPosX , 0 , WorldPosZ ) +

disp lacement ;

DS OUTPUT Output ;

Output . vPos i t i on = mul ( f l o a t 4 ( po s i t i on , 1 ) , viewProjMatrix );

Output . vWorldPos = po s i t i o n ;Output . vBinormal = normal ize (du) ;Output . vTangent = normal ize (dv ) ;Output . vNormal = normal ize ( c r o s s (dv , du) ) ;

return Output ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Shading p i x e l shader s e c t i on//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

// The p i x e l shader works the same as i t would in a normal// g raph i c s p i p e l i n e . In t h i s case , we app ly a Fresne l// r e f l e c t i o n and we add some noi se to the normal v e c t o r .

f l o a t 4 psOcean (DS OUTPUT input ) : SV TARGET{

f l o a t 3 pos i t ionWorld = input . vWorldPos ;f l o a t 2 coord = f l o a t 2 ( pos i t ionWorld . x , pos i t ionWorld . z ) /

s i z eTe r r a i n ;

//We add some noi se to g e t a more r e a l i s t i c water

53

Page 56: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

f l o a t 4 t0 = bumpTexture . Sample ( l inearSampler , f l o a t 2 ( coord ∗100 ∗ 0 .02 + 2∗0 .07 ∗ f l o a t 2 (1 , 0 ) ∗ ( time / 10) ∗

f l o a t 2 (−1,−1) ) ) ;f l o a t 4 t1 = bumpTexture . Sample ( l inearSampler , f l o a t 2 ( coord ∗

100 ∗ 0 .03 + 2∗0.057 ∗ f l o a t 2 ( 0 . 8 6 , 0 . 5 ) ∗ ( time / 10)∗ f l o a t 2 (1 ,−1) ) ) ;

f l o a t 4 t2 = bumpTexture . Sample ( l inearSampler , f l o a t 2 ( coord ∗100 ∗ 0 .05 + 2∗0.047 ∗ f l o a t 2 ( 0 . 8 6 , −0.5) ∗ ( time / 10)∗ f l o a t 2 (−1 ,1) ) ) ;

f l o a t 4 t3 = bumpTexture . Sample ( l inearSampler , f l o a t 2 ( coord ∗100 ∗ 0 .07 + 2∗0.037 ∗ f l o a t 2 ( 0 . 7 , 0 . 7 ) ∗ ( time / 10) ) )

;

f l o a t 3 N = ( t0 + t1 + t2 + t3 ) . xzy ∗ 2 .0 − 3 . 3 ;N. xz ∗= 2 . 0 ;

N = normal ize (N) ;

f l o a t 3x3 m;m[ 0 ] = input . vTangent ;m[ 1 ] = input . vNormal ;m[ 2 ] = input . vBinormal ;

f l o a t 3 normalWorld = mul (N, m) ;normalWorld = normal ize ( normalWorld ) ;//normalWorld = input . vNormal ; //remove bumps

f l o a t 3 toCamera = normal ize ( eyePos i t i on − pos i t ionWorld ) ;

f loat cosAngle = max( dot ( toCamera , normalWorld ) , 0) ;f loat F = 0.02 + 0.98 ∗ pow( ( 1 − cosAngle ) , 5 ) ;

f l o a t 4 sha l lowColor = f l o a t 4 ( 0 . 5 6 , 0 . 62 , 0 . 8 ,0 ) ;f l o a t 4 deepColor = f l o a t 4 ( 0 . 2 , 0 . 3 , 0 . 5 , 0) ;

f l o a t 3 r e f l D i r = r e f l e c t ( eyePos i t i on − posit ionWorld , N) ;f l o a t 4 envColor = envMap . Sample ( l inearSampler , r e f l D i r ) ;

f l o a t 4 waterColor = deepColor ∗ cosAngle + sha l lowColor ∗ (1 − cosAngle ) ;

waterColor ∗= oceanTexture . Sample ( l inearSampler , coord ∗100) ;

f l o a t 4 c o l o r = envColor ∗ F + waterColor ∗ ( 1 − F ) ;

return c o l o r ;}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// So l i d co l o r shading p i x e l shader ( used f o r wireframe// ove r l ay )

54

Page 57: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−f l o a t 4 psOceanWired ( DS OUTPUT Input ) : SV TARGET{

// Return a s o l i d whi te co l o rreturn f l o a t 4 ( 1 , 1 , 1 , 1 ) ;

}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−// Normal co l o r shading p i x e l shader ( used f o r normal ove r l a y )//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−f l o a t 4 psOceanNormal (DS OUTPUT input ) : SV TARGET{

//We have to normal ize again because can be not normal izedf l o a t 3 N = normal ize ( input . vNormal ) ;return f l o a t 4 (N, 1) ;

}

//Technique used to render the oceantechnique11 ocean{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsOcean ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsOcean ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsOcean ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( noCu l lRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psOcean ( ) ) ) ;

}}

//Technique used to render the ocean normalstechnique11 ocean normal{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsOcean ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsOcean ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsOcean ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( noCu l lRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psOceanNormal ( ) ) )

;}

}

//Technique used to render the wireframe oceantechnique11 wired ocean{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsOcean ( ) ) ) ;

55

Page 58: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

SetHul lShader ( CompileShader ( hs 5 0 , hsOcean ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsOcean ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( w i r edRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psOceanWired ( ) ) ) ;

}}

//Technique used to render the wireframe ocean normalstechnique11 wired ocean normal{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsOcean ( ) ) ) ;SetHul lShader ( CompileShader ( hs 5 0 , hsOcean ( ) ) ) ;SetDomainShader ( CompileShader ( ds 5 0 , dsOcean ( ) ) ) ;S e tRa s t e r i z e rS t a t e ( w i r edRas t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psOceanNormal ( ) ) )

;}

}

//This l a s t par t o f the f x f i l e i s to render the backgroundstruct QuadInput{

f l o a t 4 pos : POSITION;f l o a t 2 tex : TEXCOORD0;

} ;

struct QuadOutput{

f l o a t 4 pos : SV POSITION ;f l o a t 2 tex : TEXCOORD0;f l o a t 3 viewDir : TEXCOORD1;

} ;

f l o a t 4 psBackground (QuadOutput input ) : SV TARGET{

return envMap . Sample ( l inearSampler , input . viewDir ) ;} ;

QuadOutput vsQuad (QuadInput input ){

QuadOutput output = (QuadOutput ) 0 ;

output . pos = input . pos ;f l o a t 4 hWorldPosMinusEye = mul ( input . pos ,

o r i en tPro jMat r i x Inve r s e ) ;hWorldPosMinusEye /= hWorldPosMinusEye .w;output . viewDir = hWorldPosMinusEye . xyz ;output . pos . z = 0 . 99999 ;

56

Page 59: Terrain and water rendering with hardware tessellationima.udg.edu/~xavierb/BonaventuraFDP.pdfTerrain and water rendering with hardware tessellation Xavier Bonaventura Brugu es May

output . tex = input . tex ;return output ;

} ;

t echnique11 background{

pass P0{

SetVertexShader ( CompileShader ( vs 5 0 , vsQuad ( ) ) ) ;SetHul lShader ( NULL ) ;SetDomainShader ( NULL ) ;S e tRa s t e r i z e rS t a t e ( d e f a u l tRa s t e r i z e r ) ;SetPixe lShader ( CompileShader ( ps 5 0 , psBackground ( ) ) ) ;Se tDepthStenc i lS ta te ( defaultCompositor , 0) ;SetBlendState ( de fau l tB lender , f l o a t 4 ( 0 .0 f , 0 . 0 f , 0 . 0 f ,

0 . 0 f ) , 0 x f f f f f f f f ) ;}

}

57