SlideShare uma empresa Scribd logo
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
XNA Game Development
L07 – Skybox and Terrian
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
SkyBox
• How to make a sky?!
Skydome
• Skydome
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!
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!
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
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!
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!
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!
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!
Creating skydome
Apress, Chapter 13, Skydome approach
Creating skydome
• Loading the skydome “Hemisphere”
public void Load(string modelFileName)
{
model = Content.Load<Model>(GameAssetsPath.MODELS PATH + modelFileName);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
Another way?
SkyBox!
SkyBox
• The camera is always positioned in the center of the sky
• Innovative approach!
– Illusion of Endless Boundaries!
SkyBox
• How to create it?
SkyBox
• Creating the Box
– six faces, each face has a different texture
SkyBox
• Creating the Box
– six faces, each face has a different texture
SkyBox
• Creating the Box
– six faces, each face has a different texture
– only 12 triangles!
SkyBox
• Microsoft Book, Chapter 10
– “Adding Skies and Horizons to Your Levels”
SkyBox
• Loading Textures, Global Scope
Texture2D frontTexture, backTexture, groundTexture,
leftTexture, rightTexture, skyTexture;
SkyBox
• Loading Textures, Global Scope
• LoadContent()
Texture2D frontTexture, backTexture, groundTexture,
leftTexture, rightTexture, skyTexture;
frontTexture = Content.Load<Texture2D>("Imagesfront");
backTexture = Content.Load<Texture2D>("Imagesback");
leftTexture = Content.Load<Texture2D>("Imagesleft");
rightTexture = Content.Load<Texture2D>("Imagesright");
groundTexture = Content.Load<Texture2D>("Imagesground2");
skyTexture = Content.Load<Texture2D>("Imagessky");
SkyBox
• Then drawing 4 textures with? each time we (translate, rotate,... etc)
SkyBox
• Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
SkyBox
• Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
SkyBox
SkyBox
SkyBox
SkyBox
SkyBox
SkyBox at rbwhitaker web site
SkyBox at rbwhitaker web site
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
Terrain
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/
Terrain
– Innovative games Web Site
Terrain
– Innovative games Web Site
Terrain
• Innovative games
• Web Site Terrain with fog!
Terrain
http://www.packtpub.com/article/environmental-effects
Terrain
• http://www.packtpub.com/article/environmental-effects
Terrain - packtpub
Terrain
– Many ways to create a terrain
• From
– From File “image”
– From File “raw”
• With  Without
– With Shaders
– Without Shaders
Terrain
• Using Planetside’s Terragen!
– Create your own customized terrain!
– www.planetside.co.uk/terragen/
• Using EarthSculptor
– http://www.earthsculptor.com/
Height map
Planetside’s
Terragen
Planetside’s
Terragen
Planetside’s Terragen
Planetside’s
Terragen
Earth
Sculptor
Earth
Sculptor
Terrain – Rimer’s,
Creating from file “image”
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Terrain_from_file.php
Terrain – Rimer’s,
Creating from file “image”
Terrain - packtpub
• How to do it perfectly?!
Terrain
Chapter 11, Generating a Terrain
Page: 227
TerrainTerrain
Terrain
• Height Maps
Terrain
• Height Maps
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
Terrain
• Manipulate through VertexBuffer
Terrain
• Manipulate through VertexBuffer
Terrain
• Manipulate through VertexBuffer
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;
}
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;
}
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”?!
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
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;
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?
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!
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
Terrain
• Texturing
float tu = 0; float tv = 0;
float tuDerivative = 1.0f / (vertexCountX - 1);
float tvDerivative = 1.0f / (vertexCountZ - 1);
Terrain
• Texturing
float tu = 0; float tv = 0;
float tuDerivative = 1.0f / (vertexCountX - 1);
float tvDerivative = 1.0f / (vertexCountZ - 1);
Terrain
• Creating Normals
Multitexturing Techniques
Multitexturing
Advanced Terrain
• Multitexturing
“App1-MultitexturedTerrain”
Advanced
Terrain
Advanced
Terrain
“App1-MultitexturedTerrain”
Advanced Terrain
Normal Mapping Technique
Advanced Terrain – Normal Mapping Technique
Advanced Terrain – Normal Mapping Technique
Advanced Terrain – Normal Mapping Technique
Advanced Terrain – Normal Mapping Technique
Using the normal mapping technique, you can add the illusion of small-scale details
to 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 your
terrain. Variations in lighting are created by the deviated normals.
Remember that the amount of lighting falling onto a triangle is determined by the
normals of its vertices.
• Creating the Terrain Effects (Apress, Page 277)
Find this at
“App2-MultitexturedNormalMappedTerrain”
Terrain - Querying the Terrain’s Height
Terrain - Querying the Terrain’s Height?
Terrain
• Querying the Terrain’s Height?
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.
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.
Terrain
• Quad Tree
Terrain
• Quad Tree – The concept
Terrain
• Check out the videos in the appendix
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?!
Terrain
• Querying the Terrain’s Height?
No Built-in Methods
Terrain
• Querying the Terrain’s Height?
Creating our own Transformation class
You 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.
Terrain
• Querying the Terrain’s Height
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));
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));
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));
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));
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));
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));
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));
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.
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)
Advanced Terrain
Ray and Terrain Collision
Advanced Terrain - Ray and Terrain Collision
Advanced Terrain - Ray and Terrain Collision
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);
}
Terrain
• Chapter 25: Terrain with Height Detection
• From “.raw” file
Terrain - packtpub
• How to do it perfectly?!
Terrain - packtpub
• Read the Height map and convert it to a proper array
Terrain - packtpub
• How to get the Heights?!
Color[] heightMapData = new Color[width * length];
heightMap.GetData<Color>(heightMapData);
Terrain - packtpub
• How to get the Heights?!
Color[] heightMapData = new Color[width * length];
heightMap.GetData<Color>(heightMapData);
Terrain - packtpub
heights[,]
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
Terrain - packtpub
• Read the Height map and convert it to a proper array?!
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!
Terrain
• Proper texture covers your terrain,
• Original terrain created is 257 pixels wide by 257 pixels high
floorTexture = Content.Load<Texture2D>("Imagesterrain");
const int NUM_COLS = 257;
const int NUM_ROWS = 257;
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
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;
}
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;
}
Terrain
• How to know the current HoverAmount?!
Terrain
• How to know the current HoverAmount?!
Terrain
• How to know the current HoverAmount?!
Terrain – Back to our awesome terrain
• http://www.packtpub.com/article/environmental-effects
Advanced Terrain - Multitexturing
Advanced Terrain – Multitexturing / packtpub
• Just another custom shader!
Advanced Terrain – Spice it Up!
Advanced Terrain – Spice it Up!
Adding a detail texture
to the terrain
Advanced Terrain –Detailing / packtpub
• Snow Texture!
Advanced Terrain –Detailing / packtpub
Advanced Terrain –Detailing / packtpub
• Details appear when camera is close to the terrain to fake a higher resolution
texture
Advanced Terrain –Detailing / packtpub
Advanced Terrain –Detailing / packtpub
Advanced Terrain –Detailing / packtpub
• How to add all these tress and bushes!
– Code?!!!!! :@
– XML
Advanced Terrain –Detailing / packtpub
Advanced Terrain –Detailing / packtpub
• Clouds!  Billboarding!
Advanced Terrain –Detailing / packtpub
Advanced Terrain
Advanced Terrain
Advanced Terrain

Mais conteúdo relacionado

Semelhante a XNA L07–Skybox and Terrain

Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
Korhan Bircan
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5
Jussi Pohjolainen
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScripters
gerbille
 
Peint talk
Peint talkPeint talk
Peint talk
CyruzDraxs
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
DroidConTLV
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billy
nimbleltd
 
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
JavaScript Meetup HCMC
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
feelinggifts
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
gerbille
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
tamillarasan
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymore
Remy Sharp
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Bin Shao
 
Stupid Canvas Tricks
Stupid Canvas TricksStupid Canvas Tricks
Stupid Canvas Tricks
deanhudson
 
WebGL and three.js - Web 3D Graphics
WebGL and three.js - Web 3D Graphics WebGL and three.js - Web 3D Graphics
WebGL and three.js - Web 3D Graphics
PSTechSerbia
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
dandylion13
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Paulo Ragonha
 
COMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptxCOMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptx
Egerton University
 
I Can't Believe It's Not Flash
I Can't Believe It's Not FlashI Can't Believe It's Not Flash
I Can't Believe It's Not Flash
Thomas Fuchs
 
Begin three.js.key
Begin three.js.keyBegin three.js.key
Begin three.js.key
Yi-Fan Liao
 

Semelhante a XNA L07–Skybox and Terrain (20)

Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScripters
 
Peint talk
Peint talkPeint talk
Peint talk
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billy
 
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymore
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
 
Stupid Canvas Tricks
Stupid Canvas TricksStupid Canvas Tricks
Stupid Canvas Tricks
 
WebGL and three.js - Web 3D Graphics
WebGL and three.js - Web 3D Graphics WebGL and three.js - Web 3D Graphics
WebGL and three.js - Web 3D Graphics
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
COMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptxCOMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptx
 
I Can't Believe It's Not Flash
I Can't Believe It's Not FlashI Can't Believe It's Not Flash
I Can't Believe It's Not Flash
 
Begin three.js.key
Begin three.js.keyBegin three.js.key
Begin three.js.key
 

Mais de Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 

Mais de Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Último

5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 

Último (20)

5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 

XNA L07–Skybox and Terrain

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L07 – Skybox and Terrian
  • 2. 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
  • 3. SkyBox • How to make a sky?!
  • 5. 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!
  • 6. 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!
  • 7. 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
  • 8. 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!
  • 9. 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!
  • 10. 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!
  • 11. 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!
  • 12. Creating skydome Apress, Chapter 13, Skydome approach
  • 13. Creating skydome • Loading the skydome “Hemisphere” public void Load(string modelFileName) { model = Content.Load<Model>(GameAssetsPath.MODELS PATH + modelFileName); }
  • 14. 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); }
  • 15. 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); }
  • 16. 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); }
  • 17. 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); }
  • 18. 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); }
  • 19. 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); }
  • 20. 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); }
  • 21. 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); }
  • 22. 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); }
  • 23. 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); }
  • 24. 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); }
  • 25. 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); }
  • 26. 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); }
  • 27. 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); }
  • 28. 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); }
  • 29. 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); }
  • 30. 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); }
  • 33. SkyBox • The camera is always positioned in the center of the sky • Innovative approach! – Illusion of Endless Boundaries!
  • 34. SkyBox • How to create it?
  • 35. SkyBox • Creating the Box – six faces, each face has a different texture
  • 36. SkyBox • Creating the Box – six faces, each face has a different texture
  • 37. SkyBox • Creating the Box – six faces, each face has a different texture – only 12 triangles!
  • 38. SkyBox • Microsoft Book, Chapter 10 – “Adding Skies and Horizons to Your Levels”
  • 39. SkyBox • Loading Textures, Global Scope Texture2D frontTexture, backTexture, groundTexture, leftTexture, rightTexture, skyTexture;
  • 40. SkyBox • Loading Textures, Global Scope • LoadContent() Texture2D frontTexture, backTexture, groundTexture, leftTexture, rightTexture, skyTexture; frontTexture = Content.Load<Texture2D>("Imagesfront"); backTexture = Content.Load<Texture2D>("Imagesback"); leftTexture = Content.Load<Texture2D>("Imagesleft"); rightTexture = Content.Load<Texture2D>("Imagesright"); groundTexture = Content.Load<Texture2D>("Imagesground2"); skyTexture = Content.Load<Texture2D>("Imagessky");
  • 41. SkyBox • Then drawing 4 textures with? each time we (translate, rotate,... etc)
  • 42. SkyBox • Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
  • 43. SkyBox • Then drawing 4 textures with Transformation each time we (translate, rotate,... etc)
  • 51. 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
  • 53.
  • 54. 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/
  • 57. Terrain • Innovative games • Web Site Terrain with fog!
  • 61. Terrain – Many ways to create a terrain • From – From File “image” – From File “raw” • With Without – With Shaders – Without Shaders
  • 62. Terrain • Using Planetside’s Terragen! – Create your own customized terrain! – www.planetside.co.uk/terragen/ • Using EarthSculptor – http://www.earthsculptor.com/
  • 70. Terrain – Rimer’s, Creating from file “image”
  • 72. Terrain - packtpub • How to do it perfectly?!
  • 73. Terrain Chapter 11, Generating a Terrain Page: 227
  • 77. 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;
  • 78. 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;
  • 79. 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;
  • 80. 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;
  • 81. 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;
  • 82. 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;
  • 83. 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;
  • 84. 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;
  • 85. 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;
  • 86. 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;
  • 90. 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; }
  • 91. 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; }
  • 92. 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”?!
  • 93. 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
  • 94. 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;
  • 95. 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?
  • 96. 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!
  • 97. 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
  • 98. Terrain • Texturing float tu = 0; float tv = 0; float tuDerivative = 1.0f / (vertexCountX - 1); float tvDerivative = 1.0f / (vertexCountZ - 1);
  • 99. Terrain • Texturing float tu = 0; float tv = 0; float tuDerivative = 1.0f / (vertexCountX - 1); float tvDerivative = 1.0f / (vertexCountZ - 1);
  • 107. Advanced Terrain – Normal Mapping Technique
  • 108. Advanced Terrain – Normal Mapping Technique
  • 109. Advanced Terrain – Normal Mapping Technique
  • 110. Advanced Terrain – Normal Mapping Technique Using the normal mapping technique, you can add the illusion of small-scale details to 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 your terrain. Variations in lighting are created by the deviated normals. Remember that the amount of lighting falling onto a triangle is determined by the normals of its vertices.
  • 111. • Creating the Terrain Effects (Apress, Page 277)
  • 112.
  • 113.
  • 114.
  • 115.
  • 117. Terrain - Querying the Terrain’s Height
  • 118. Terrain - Querying the Terrain’s Height?
  • 119. Terrain • Querying the Terrain’s Height?
  • 120. 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.
  • 121. 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.
  • 123. Terrain • Quad Tree – The concept
  • 124. Terrain • Check out the videos in the appendix
  • 125. 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?!
  • 126. Terrain • Querying the Terrain’s Height? No Built-in Methods
  • 127. Terrain • Querying the Terrain’s Height? Creating our own Transformation class You 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.
  • 128. Terrain • Querying the Terrain’s Height
  • 129. 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));
  • 130. 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));
  • 131. 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));
  • 132. 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));
  • 133. 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));
  • 134. 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));
  • 135. 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));
  • 136. 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.
  • 137. 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)
  • 138. Advanced Terrain Ray and Terrain Collision
  • 139. Advanced Terrain - Ray and Terrain Collision
  • 140. Advanced Terrain - Ray and Terrain Collision
  • 141. 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); }
  • 142. Terrain • Chapter 25: Terrain with Height Detection • From “.raw” file
  • 143. Terrain - packtpub • How to do it perfectly?!
  • 144. Terrain - packtpub • Read the Height map and convert it to a proper array
  • 145. Terrain - packtpub • How to get the Heights?! Color[] heightMapData = new Color[width * length]; heightMap.GetData<Color>(heightMapData);
  • 146. Terrain - packtpub • How to get the Heights?! Color[] heightMapData = new Color[width * length]; heightMap.GetData<Color>(heightMapData);
  • 148. Terrain - packtpub • Read the Height map and convert it to a proper array?!
  • 149. Terrain - packtpub • Read the Height map and convert it to a proper array?!
  • 150. Terrain - packtpub • Read the Height map and convert it to a proper array?!
  • 151. 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!
  • 152. Terrain • Proper texture covers your terrain, • Original terrain created is 257 pixels wide by 257 pixels high floorTexture = Content.Load<Texture2D>("Imagesterrain"); const int NUM_COLS = 257; const int NUM_ROWS = 257;
  • 153. 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
  • 154. 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; }
  • 155. 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; }
  • 156. Terrain • How to know the current HoverAmount?!
  • 157. Terrain • How to know the current HoverAmount?!
  • 158. Terrain • How to know the current HoverAmount?!
  • 159. Terrain – Back to our awesome terrain • http://www.packtpub.com/article/environmental-effects
  • 160. Advanced Terrain - Multitexturing
  • 161. Advanced Terrain – Multitexturing / packtpub • Just another custom shader!
  • 162. Advanced Terrain – Spice it Up!
  • 163. Advanced Terrain – Spice it Up! Adding a detail texture to the terrain
  • 164. Advanced Terrain –Detailing / packtpub • Snow Texture!
  • 166. Advanced Terrain –Detailing / packtpub • Details appear when camera is close to the terrain to fake a higher resolution texture
  • 169. Advanced Terrain –Detailing / packtpub • How to add all these tress and bushes! – Code?!!!!! :@ – XML
  • 171. Advanced Terrain –Detailing / packtpub • Clouds! Billboarding!