XNA L07–Skybox and Terrain
Click here to load reader
-
Upload
mohammad-shaker -
Category
Technology
-
view
375 -
download
2
Transcript of XNA L07–Skybox and Terrain
![Page 1: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshaker.com
@ZGTRShaker2011, 2012, 2013, 2014
XNA Game DevelopmentL07 – Skybox and Terrian
![Page 2: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/2.jpg)
SkyBox
• Advanced SkyBoxes
– With Terrain
• Riemers web site
– http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php
– Rbwhitaker web site
• http://rbwhitaker.wikidot.com/skyboxes-1
![Page 3: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/3.jpg)
SkyBox
• How to make a sky?!
![Page 4: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/4.jpg)
Skydome
• Skydome
![Page 5: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/5.jpg)
Skydome
• Skydome
– You’ll use is a conventional 3D model, previously
made in a modeling tool and processed by the
Content Pipeline.
– Handled through XNA’s Model class!
![Page 6: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/6.jpg)
Skydome
• Skydome
– You’ll use is a conventional 3D model, previously
made in a modeling tool and processed by the
Content Pipeline.
– Handled through XNA’s Model class!
![Page 7: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/7.jpg)
Skydome
• Whenever the camera moves, the skybox or skydome should move with the
camera, so the camera always remains in the center of the volume
![Page 8: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/8.jpg)
Skydome
• Skydome
– the sky is created as a hemisphere using only one texture, and is positioned above the scene
– is easy to animate its textures!
![Page 9: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/9.jpg)
Skydome
• Skydome
– the sky is created as a hemisphere using only one texture, and is positioned above the scene
– is easy to animate its textures!
![Page 10: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/10.jpg)
Skydome
• Skydome
– the sky is created as a hemisphere using only one texture, and is positioned above the scene
– is easy to animate its textures!
![Page 11: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/11.jpg)
Skydome
• Skydome
– the sky is created as a hemisphere using only one texture, and is positioned above the scene
– is easy to animate its textures!
![Page 12: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/12.jpg)
Creating skydomeApress, Chapter 13, Skydome approach
![Page 13: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/13.jpg)
Creating skydome
• Loading the skydome “Hemisphere”
public void Load(string modelFileName)
{
model = Content.Load<Model>(GameAssetsPath.MODELS PATH + modelFileName);
}
![Page 14: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/14.jpg)
Creating skydome
• Updating the Sky
public override void Update(GameTime time)
{
BaseCamera camera = cameraManager.ActiveCamera;
// Center the camera in the SkyDome
transformation.Translate = new Vector3(camera.Position.X,
0.0f, camera.Position.Z);
// Rotate the SkyDome slightly
transformation.Rotate += new Vector3(0,
(float)time.ElapsedGameTime.TotalSeconds * 0.5f, 0);
base.Update(time);
}
![Page 15: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/15.jpg)
Creating skydome
• Drawing the Sky
public override void Draw(GameTime time)
{
GraphicsDevice.DepthStencilState = DepthStencilState.None;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
base.Draw(time);
}
![Page 16: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/16.jpg)
Creating skydome
• Drawing the Sky
public override void Draw(GameTime time)
{
GraphicsDevice.DepthStencilState = DepthStencilState.None;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
base.Draw(time);
}
![Page 17: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/17.jpg)
Creating skydome
• Drawing the Sky
public override void Draw(GameTime time)
{
GraphicsDevice.DepthStencilState = DepthStencilState.None;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
base.Draw(time);
}
![Page 18: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/18.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 19: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/19.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 20: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/20.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.DepthStencilState = DepthStencilState.None;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
base.Draw(time);
}
![Page 21: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/21.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 22: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/22.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 23: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/23.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 24: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/24.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 25: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/25.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 26: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/26.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 27: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/27.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 28: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/28.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 29: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/29.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 30: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/30.jpg)
Creating skydome
public override void Draw(GameTime time)
{
GraphicsDevice.RenderState.DepthBufferEnable = false;
foreach (ModelMesh modelMesh in model.Meshes)
{
// We are only rendering models with BasicEffect
foreach (BasicEffect basicEffect in modelMesh.Effects)
SetEffectMaterial(basicEffect);
modelMesh.Draw();
}
GraphicsDevice.RenderState.DepthBufferEnable = true;
base.Draw(time);
}
![Page 31: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/31.jpg)
Another way?
![Page 32: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/32.jpg)
SkyBox!
![Page 33: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/33.jpg)
SkyBox
• The camera is always positioned in the center of the sky
• Innovative approach!
– Illusion of Endless Boundaries!
![Page 34: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/34.jpg)
SkyBox
• How to create it?
![Page 35: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/35.jpg)
SkyBox
• Creating the Box
– six faces, each face has a different texture
![Page 36: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/36.jpg)
SkyBox
• Creating the Box
– six faces, each face has a different texture
![Page 37: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/37.jpg)
SkyBox
• Creating the Box
– six faces, each face has a different texture
– only 12 triangles!
![Page 38: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/38.jpg)
SkyBox
• Microsoft Book, Chapter 10
– “Adding Skies and Horizons to Your Levels”
![Page 39: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/39.jpg)
SkyBox
• Loading Textures, Global Scope
Texture2D frontTexture, backTexture, groundTexture,
leftTexture, rightTexture, skyTexture;
![Page 40: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/40.jpg)
SkyBox
• Loading Textures, Global Scope
• LoadContent()
Texture2D frontTexture, backTexture, groundTexture,
leftTexture, rightTexture, skyTexture;
frontTexture = Content.Load<Texture2D>("Images\\front");
backTexture = Content.Load<Texture2D>("Images\\back");
leftTexture = Content.Load<Texture2D>("Images\\left");
rightTexture = Content.Load<Texture2D>("Images\\right");
groundTexture = Content.Load<Texture2D>("Images\\ground2");
skyTexture = Content.Load<Texture2D>("Images\\sky");
![Page 41: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/41.jpg)
SkyBox
• Then drawing 4 textures with? each time we (translate, rotate,... etc)
![Page 42: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/42.jpg)
SkyBox
• Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
![Page 43: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/43.jpg)
SkyBox
• Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
![Page 44: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/44.jpg)
SkyBox
![Page 45: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/45.jpg)
SkyBox
![Page 46: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/46.jpg)
SkyBox
![Page 47: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/47.jpg)
SkyBox
![Page 48: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/48.jpg)
SkyBox
![Page 49: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/49.jpg)
SkyBox at rbwhitaker web site
![Page 50: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/50.jpg)
SkyBox at rbwhitaker web site
![Page 51: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/51.jpg)
Where to look for skybox and textures?
• Acquiring SkyBox Textures
– A Google image search for "skybox" will usually give you all sorts of good skyboxes
– .dds file format
– terathon.com
– http://developer.amd.com/archive/gpu/cubemapgen/pages/default.aspx
![Page 52: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/52.jpg)
Terrain
![Page 53: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/53.jpg)
![Page 54: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/54.jpg)
Terrain
• Advanced Terrains
– Rbwhitaker web site
• http://rbwhitaker.wikidot.com/skyboxes-1
– Riemers web site
• http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php
• http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php
– Innovative games web site
• http://www.innovativegames.net/blog/blog/2009/05/29/xna-game-engine-tutorial-12-introduction-
to-hlsl-and-improved-terrain/
![Page 55: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/55.jpg)
Terrain
– Innovative games Web Site
![Page 56: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/56.jpg)
Terrain
– Innovative games Web Site
![Page 57: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/57.jpg)
Terrain
• Innovative games
• Web Site Terrain with fog!
![Page 58: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/58.jpg)
Terrainhttp://www.packtpub.com/article/environmental-effects
![Page 59: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/59.jpg)
Terrain
• http://www.packtpub.com/article/environmental-effects
![Page 60: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/60.jpg)
Terrain - packtpub
![Page 61: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/61.jpg)
Terrain
– Many ways to create a terrain
• From
– From File “image”
– From File “raw”
• With \ Without
– With Shaders
– Without Shaders
![Page 62: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/62.jpg)
Terrain
• Using Planetside’s Terragen!
– Create your own customized terrain!
– www.planetside.co.uk/terragen/
• Using EarthSculptor
– http://www.earthsculptor.com/
![Page 63: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/63.jpg)
Height map
![Page 64: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/64.jpg)
Planetside’sTerragen
![Page 65: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/65.jpg)
Planetside’sTerragen
![Page 66: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/66.jpg)
Planetside’s Terragen
![Page 67: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/67.jpg)
Planetside’sTerragen
![Page 68: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/68.jpg)
Earth Sculptor
![Page 69: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/69.jpg)
Earth Sculptor
![Page 70: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/70.jpg)
Terrain – Rimer’s,Creating from file “image”
![Page 71: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/71.jpg)
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Terrain_from_file.php
Terrain – Rimer’s,Creating from file “image”
![Page 72: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/72.jpg)
Terrain - packtpub
• How to do it perfectly?!
![Page 73: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/73.jpg)
Terrain
Chapter 11, Generating a TerrainPage: 227
![Page 74: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/74.jpg)
TerrainTerrain
![Page 75: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/75.jpg)
Terrain
• Height Maps
![Page 76: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/76.jpg)
Terrain
• Height Maps
![Page 77: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/77.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 78: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/78.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 79: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/79.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 80: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/80.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 81: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/81.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 82: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/82.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 83: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/83.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 84: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/84.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 85: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/85.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 86: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/86.jpg)
Terrain
• Creating Terrain Class
Texture2D heightMapTexture = Content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width*heightMapTexture.Height;
heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(heightMap);
this.vertexCountX = heightMapTexture.Width;
this.vertexCountZ = heightMapTexture.Height;
![Page 87: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/87.jpg)
Terrain
• Manipulate through VertexBuffer
![Page 88: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/88.jpg)
Terrain
• Manipulate through VertexBuffer
![Page 89: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/89.jpg)
Terrain
• Manipulate through VertexBuffer
![Page 90: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/90.jpg)
Terrain
private int[] GenerateTerrainIndices()
{
int numIndices = numTriangles * 3;
int[] indices = new int[numIndices];
int indicesCount = 0;
for (int i = 0; i < (vertexCountZ - 1); i++)
{
for (int j = 0; j < (vertexCountX - 1); j++)
{
int index = j + i * vertexCountZ;
// First triangle
indices[indicesCount++] = index;
indices[indicesCount++] = index + 1;
indices[indicesCount++] = index + vertexCountX + 1;
// Second triangle
indices[indicesCount++] = index + vertexCountX + 1;
indices[indicesCount++] = index + vertexCountX;
indices[indicesCount++] = index;
}
}
return indices;
}
![Page 91: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/91.jpg)
Terrain
private int[] GenerateTerrainIndices()
{
int numIndices = numTriangles * 3;
int[] indices = new int[numIndices];
int indicesCount = 0;
for (int i = 0; i < (vertexCountZ - 1); i++)
{
for (int j = 0; j < (vertexCountX - 1); j++)
{
int index = j + i * vertexCountZ;
// First triangle
indices[indicesCount++] = index;
indices[indicesCount++] = index + 1;
indices[indicesCount++] = index + vertexCountX + 1;
// Second triangle
indices[indicesCount++] = index + vertexCountX + 1;
indices[indicesCount++] = index + vertexCountX;
indices[indicesCount++] = index;
}
}
return indices;
}
![Page 92: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/92.jpg)
Terrain
• Generating the Position and Texture Coordinate of the Vertices
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;How to get the “Height” that correspond to each “Color value”?!
![Page 93: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/93.jpg)
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;
Terrain
you’ll simply take the red color component of each color as height for a vertex
![Page 94: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/94.jpg)
Terrain
• Generating the Position and Texture Coordinate of the Vertices
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;
![Page 95: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/95.jpg)
Terrain
• Generating the Position and Texture Coordinate of the Vertices
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;Are we done just yet?
![Page 96: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/96.jpg)
Terrain
• Generating the Position and Texture Coordinate of the Vertices
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;Texturing!
![Page 97: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/97.jpg)
Terrain
• Generating the Position and Texture Coordinate of the Vertices
for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
vertices[vertexCount].Position = new Vector3(j,heightMap[vertexCount].R * heightScale, i);
float terrainWidth = (vertexCountX - 1) * blockScale;
float terrainDepth = (vertexCountZ - 1) * blockScale;
float halfTerrainWidth = terrainWidth * 0.5f;
float halfTerrainDepth = terrainDepth * 0.5f;
Each vertex also has a U and V texture coordinate that should vary
between (0, 0) and (1, 1), where (0, 0) corresponds to the top left,
(1, 0) to the top right and (1, 1) to the bottom right of the texture
![Page 98: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/98.jpg)
Terrain
• Texturing
float tu = 0; float tv = 0;
float tuDerivative = 1.0f / (vertexCountX - 1);
float tvDerivative = 1.0f / (vertexCountZ - 1);
![Page 99: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/99.jpg)
Terrain
• Texturing
float tu = 0; float tv = 0;
float tuDerivative = 1.0f / (vertexCountX - 1);
float tvDerivative = 1.0f / (vertexCountZ - 1);
![Page 100: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/100.jpg)
Terrain
• Creating Normals
![Page 101: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/101.jpg)
Multitexturing Techniques
![Page 102: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/102.jpg)
Multitexturing
![Page 103: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/103.jpg)
Advanced Terrain
• Multitexturing
![Page 104: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/104.jpg)
“App1-MultitexturedTerrain”
Advanced Terrain
![Page 105: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/105.jpg)
Advanced Terrain
“App1-MultitexturedTerrain”
![Page 106: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/106.jpg)
Advanced TerrainNormal Mapping Technique
![Page 107: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/107.jpg)
Advanced Terrain – Normal Mapping Technique
![Page 108: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/108.jpg)
Advanced Terrain – Normal Mapping Technique
![Page 109: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/109.jpg)
Advanced Terrain – Normal Mapping Technique
![Page 110: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/110.jpg)
Advanced Terrain – Normal Mapping Technique
Using the normal mapping technique, you can add the illusion of small-scale detailsto the terrain’s mesh, without needing to increase the complexity of its mesh
You create this illusion by slightly manipulating the lighting in each pixel of yourterrain. Variations in lighting are created by the deviated normals.
Remember that the amount of lighting falling onto a triangle is determined by thenormals of its vertices.
![Page 111: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/111.jpg)
• Creating the Terrain Effects (Apress, Page 277)
![Page 112: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/112.jpg)
![Page 113: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/113.jpg)
![Page 114: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/114.jpg)
![Page 115: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/115.jpg)
![Page 116: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/116.jpg)
Find this at “App2-MultitexturedNormalMappedTerrain”
![Page 117: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/117.jpg)
Terrain - Querying the Terrain’s Height
![Page 118: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/118.jpg)
Terrain - Querying the Terrain’s Height?
![Page 119: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/119.jpg)
Terrain
• Querying the Terrain’s Height?
![Page 120: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/120.jpg)
Terrain
• Querying the Terrain’s Height?
– you first need to calculate this position relative to the terrain’s vertex grid.
– You can do this by subtracting the queried world position from the terrain’s origin position,
making sure to take the terrain’s world translation and rotation into account.
![Page 121: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/121.jpg)
Terrain
• Querying the Terrain’s Height?
– you first need to calculate this position relative to the terrain’s vertex grid.
– You can do this by subtracting the queried world position from the terrain’s origin position,
making sure to take the terrain’s world translation and rotation into account.
• Then you need to know in which quad of the terrain grid the position you are
querying is located, which you can do by dividing the calculated position (relative
to the terrain) by the terrain’s block scale.
![Page 122: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/122.jpg)
Terrain
• Quad Tree
![Page 123: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/123.jpg)
Terrain
• Quad Tree – The concept
![Page 124: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/124.jpg)
Terrain
• Check out the videos in the appendix
![Page 125: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/125.jpg)
Terrain
• Querying the Terrain’s Height?
– you first need to calculate this position relative to the terrain’s vertex grid.
– You can do this by subtracting the queried world position from the terrain’s origin position,
making sure to take the terrain’s world translation and rotation into account.
• Then you need to know in which quad of the terrain grid the position you are
querying is located, which you can do by dividing the calculated position (relative
to the terrain) by the terrain’s block scale.
How to get our current
position?!
![Page 126: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/126.jpg)
Terrain
• Querying the Terrain’s Height?
No Built-in Methods
![Page 127: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/127.jpg)
Terrain
• Querying the Terrain’s Height?
Creating our own Transformation classYou can store the transformations that are currently set on the
terrain (translate, rotate, and scale) inside the Terrain class, using the Transformation class created in Chapter 10, Apress.
![Page 128: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/128.jpg)
Terrain
• Querying the Terrain’s Height
![Page 129: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/129.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 130: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/130.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 131: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/131.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 132: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/132.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 133: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/133.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 134: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/134.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 135: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/135.jpg)
Terrain
• Querying the Terrain’s Height
// Get the position relative to the terrain grid
Vector2 positionInGrid = new Vector2(
positionX - (StartPosition.X + Transformation.Translate.X),
positionZ - (StartPosition.Y + Transformation.Translate.Z));
// Calculate the grid position
Vector2 blockPosition = new Vector2(
(int)(positionInGrid.X / blockScale),
(int)(positionInGrid.Y / blockScale));
![Page 136: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/136.jpg)
Terrain
• A block in the terrain grid. If the x position inside the block is bigger than the z
position, the object is in the top triangle. Otherwise, the object is in the bottom
triangle.
![Page 137: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/137.jpg)
Terrain
• After finding in which triangle the object is positioned, you can obtain the height
of a position inside this triangle through a bilinear interpolation of the height of
the triangle’s vertices.
• Use the following code for the GetHeight method to calculate the height of a
terrain’s position
private float GetHeight(float positionX, float positionZ)
![Page 138: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/138.jpg)
Advanced TerrainRay and Terrain Collision
![Page 139: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/139.jpg)
Advanced Terrain - Ray and Terrain Collision
![Page 140: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/140.jpg)
Advanced Terrain - Ray and Terrain Collision
![Page 141: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/141.jpg)
Advanced Terrain - Ray and Terrain Collision
• Quite Straight forward
// A good ray step is half of the blockScale
Vector3 rayStep = ray.Direction * blockScale * 0.5f;
Vector3 rayStartPosition = ray.Position;
// Linear search - Loop until you find a point inside and outside the terrain
Vector3 lastRayPosition = ray.Position;
ray.Position += rayStep;
float height = GetHeight(ray.Position);
while (ray.Position.Y > height && height >= 0)
{
lastRayPosition = ray.Position;
ray.Position += rayStep;
height = GetHeight(ray.Position);
}
![Page 142: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/142.jpg)
Terrain
• Chapter 25: Terrain with Height Detection
• From “.raw” file
![Page 143: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/143.jpg)
Terrain - packtpub
• How to do it perfectly?!
![Page 144: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/144.jpg)
Terrain - packtpub
• Read the Height map and convert it to a proper array
![Page 145: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/145.jpg)
Terrain - packtpub
• How to get the Heights?!
Color[] heightMapData = new Color[width * length];heightMap.GetData<Color>(heightMapData);
![Page 146: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/146.jpg)
Terrain - packtpub
• How to get the Heights?!
Color[] heightMapData = new Color[width * length];heightMap.GetData<Color>(heightMapData);
![Page 147: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/147.jpg)
Terrain - packtpub
heights[,]
![Page 148: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/148.jpg)
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
![Page 149: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/149.jpg)
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
![Page 150: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/150.jpg)
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
![Page 151: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/151.jpg)
Terrain
• The demonstration used in this chapter shows how to create and implement a
height map using an 8-bit “.raw” grayscale image.
• Each pixel in the.raw image stores information about the elevation in a range
between 0 and 255.
• the height data in each pixel can then be accessed with the pixel row and column
number!
![Page 152: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/152.jpg)
Terrain
• Proper texture covers your terrain,
• Original terrain created is 257 pixels wide by 257 pixels high
floorTexture = Content.Load<Texture2D>("Images\\terrain");
const int NUM_COLS = 257;
const int NUM_ROWS = 257;
![Page 153: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/153.jpg)
Terrain
• The vertex buffer used for storing the terrain vertices must now use the height
information from the height map.
InitializeVertexBuffer() // for NUM_COLS, NUM_ROWS
![Page 154: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/154.jpg)
Terrain
• Controlling Height!
void UpdateCameraHeight()
{
const float HOVER_AMOUNT = 0.25f;
float height = CellHeight(cam.position);
cam.view.Y += height - cam.position.Y + HOVER_AMOUNT;
cam.position.Y += height - cam.position.Y + HOVER_AMOUNT;
}
![Page 155: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/155.jpg)
Terrain
• Controlling Height!
void UpdateCameraHeight()
{
const float HOVER_AMOUNT = 0.25f;
float height = CellHeight(cam.position);
cam.view.Y += height - cam.position.Y + HOVER_AMOUNT;
cam.position.Y += height - cam.position.Y + HOVER_AMOUNT;
}
![Page 156: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/156.jpg)
Terrain
• How to know the current HoverAmount?!
![Page 157: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/157.jpg)
Terrain
• How to know the current HoverAmount?!
![Page 158: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/158.jpg)
Terrain
• How to know the current HoverAmount?!
![Page 159: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/159.jpg)
Terrain – Back to our awesome terrain
• http://www.packtpub.com/article/environmental-effects
![Page 160: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/160.jpg)
Advanced Terrain - Multitexturing
![Page 161: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/161.jpg)
Advanced Terrain – Multitexturing / packtpub
• Just another custom shader!
![Page 162: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/162.jpg)
Advanced Terrain – Spice it Up!
![Page 163: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/163.jpg)
Advanced Terrain – Spice it Up!Adding a detail texture
to the terrain
![Page 164: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/164.jpg)
Advanced Terrain –Detailing / packtpub
• Snow Texture!
![Page 165: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/165.jpg)
Advanced Terrain –Detailing / packtpub
![Page 166: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/166.jpg)
Advanced Terrain –Detailing / packtpub
• Details appear when camera is close to the terrain to fake a higher resolution
texture
![Page 167: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/167.jpg)
Advanced Terrain –Detailing / packtpub
![Page 168: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/168.jpg)
Advanced Terrain –Detailing / packtpub
![Page 169: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/169.jpg)
Advanced Terrain –Detailing / packtpub
• How to add all these tress and bushes!
– Code?!!!!! :@
– XML
![Page 170: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/170.jpg)
Advanced Terrain –Detailing / packtpub
![Page 171: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/171.jpg)
Advanced Terrain –Detailing / packtpub
• Clouds! \ Billboarding!
![Page 172: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/172.jpg)
Advanced Terrain –Detailing / packtpub
![Page 173: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/173.jpg)
Advanced Terrain
![Page 174: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/174.jpg)
Advanced Terrain
![Page 175: XNA L07–Skybox and Terrain](https://reader037.fdocuments.in/reader037/viewer/2022100304/5553b466b4c905d9448b4c69/html5/thumbnails/175.jpg)
Advanced Terrain