XNA L05–Texturing

70
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L05 – Texturing

Transcript of XNA L05–Texturing

Page 1: XNA L05–Texturing

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL05 – Texturing

Page 2: XNA L05–Texturing

that appears in a video game needs to be textured; this includes everything from plants to people. If things aren’t textured well, your game just won’t look right.

Page 3: XNA L05–Texturing

Texturing

• What’s it?!

Textures are images applied to surfaces that are created using primitive objects

Page 4: XNA L05–Texturing

Texturing

• What’s it?!

Textures are images applied to surfaces that are created using primitive objects

XNA is Perfect at Texturingtextures can be colored, filtered, blended,

and transformed at run time!

Page 5: XNA L05–Texturing

Texturing

• What’s it?!

Textures are images applied to surfaces that are created using primitive objects

textures can be colored, filtered, blended, and transformed at run time!

XNA is Perfect at Texturing

XNA supports:

.bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga image formats for textures

Page 6: XNA L05–Texturing

Texturing

• UV Coordinates

– 2D World

• a texture is a two-dimensional object that is mapped onto a 2D polygon

– 3D World

• a texture is a two-dimensional object that is mapped onto a 3D polygon

Page 7: XNA L05–Texturing

Texturing

• UV Coordinates

Page 8: XNA L05–Texturing

Texturing

• Vertex Types for Textures

Page 9: XNA L05–Texturing

Texturing

• Vertex Types for Textures

– VertexPositionColorTexture

– VertexPositionNormalTexture

– VertexPositionTexture

Page 10: XNA L05–Texturing

Texturing

• VertexPositionColorTexture

– This format allows you to apply image textures to your primitive shapes, and you can even

shade your images with color.

– For example, with this vertex type you could draw a rectangle with an image texture and then

you could show it again with a different shade of color!

VertexPositionColorTexture vertex = new

VertexPositionColorTexture(Vector3 position, Color color, Vector2 uv);

Page 11: XNA L05–Texturing

Texturing

• VertexPositionNormalTexture

– This format allows you to add textures to your primitive objects. The normal data enables

lighting for this textured format.

VertexPositionNormalTexture vertex = new

VertexPositionNormalTexture(Vector3 position, Vector3 normal, Vector2 uv);

Page 12: XNA L05–Texturing

Texturing

• VertexPositionTexture

– This format only permits storage of position and texture data.

– It may be useful if you don’t need lighting and were concerned about saving space or

improving performance for large amounts of vertices.

VertexPositionTexture vertex = new

VertexPositionTexture(Vector3 position, Vector2 uv);

Page 13: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

Page 14: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

Page 15: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

Page 16: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

Page 17: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

An alpha channel can be used to “mask” all pixels of a specific color in an image. Alphadata is stored in the last color byte of a pixel after the red, green, and blue bytes.

When alpha blending is enabled in your XNA code and the alpha channel is active,transparency is achieved for the pixels where the alpha setting is set to 0.

New “Alpha” Channel!

Page 18: XNA L05–Texturing

Texturing

• TRANSPARENT TEXTURES

An alpha channel can be used to “mask” all pixels of a specific color in an image. Alphadata is stored in the last color byte of a pixel after the red, green, and blue bytes.

When alpha blending is enabled in your XNA code and the alpha channel is active, transparency is achieved for the pixels where the alpha setting is set to 0.

New “Alpha” Channel!

Page 19: XNA L05–Texturing

Texturing

• Texture Coloring “Shaders”

– Microsoft Book, Chapter 9, Page 119

• Texture Example (Learning XNA 3.0 “O’Riley” , P:205)

Page 20: XNA L05–Texturing

A Simple Texture Sample

Page 21: XNA L05–Texturing

Texturing

• Loading Textures through content manager

Texture2D texture = Content.Load<Texture2D>("Images\\imageName");

Page 22: XNA L05–Texturing

Texturing

• Let’s make a texture in a 3D world!

VertexPositionTexture[] verts;

Page 23: XNA L05–Texturing

Texturing

• LoadContent()

verts[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));

verts[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));

verts[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));

verts[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));

Page 24: XNA L05–Texturing

Texturing

• LoadContent()

verts[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));

verts[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));

verts[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));

verts[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));

Page 25: XNA L05–Texturing

Texturing

• LoadContent()

verts[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));

verts[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));

verts[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));

verts[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));

0,0 1,0

0,1 1,1

texture = Content.Load<Texture2D>(@"arrow");

Page 26: XNA L05–Texturing

Texturing

• Draw()

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world;

effect.View = view;

effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

0,0 1,0

0,1 1,1

Page 27: XNA L05–Texturing

Texturing

• Draw()

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world;

effect.View = view;

effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

0,0 1,0

0,1 1,1

Page 28: XNA L05–Texturing
Page 29: XNA L05–Texturing

Texturing

• Draw()

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world;

effect.View = view;

effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

Page 30: XNA L05–Texturing

Texturing

Page 31: XNA L05–Texturing

Texturing

• Draw()

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world;

effect.View = view;

effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 1);

}

Page 32: XNA L05–Texturing

Texturing

Page 33: XNA L05–Texturing

Texturing

• Now, How can we fix the black area?!

Page 34: XNA L05–Texturing

Texturing

• Now, How can we fix the black area?!

Page 35: XNA L05–Texturing

Texturing

• Now, How can we fix the black area?!

Page 36: XNA L05–Texturing

Texturing

• 1st , Make sure that the image supports transperancy “.png”

Page 37: XNA L05–Texturing

Texturing

• 2nd, We should tell the GraphicsDevice to blend the image for us

Page 38: XNA L05–Texturing

Texturing

• Alpha Channel!

Page 39: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime){

GraphicsDevice.Clear(Color.CornflowerBlue);BasicEffect effect = new BasicEffect(GraphicsDevice);effect.World = world; effect.View = view; effect.Projection = projection;effect.Texture = texture;effect.TextureEnabled = true;BlendState originalState = GraphicsDevice.BlendState;GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes){

pass.Apply();GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);}GraphicsDevice.BlendState = originalState;base.Draw(gameTime);

}

Page 40: XNA L05–Texturing

Texturing

• Cool!

• “App1-Texturing”

Page 41: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 42: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 43: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 44: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 45: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 46: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 47: XNA L05–Texturing

Texturing

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

BasicEffect effect = new BasicEffect(GraphicsDevice);

effect.World = world; effect.View = view; effect.Projection = projection;

effect.Texture = texture;

effect.TextureEnabled = true;

BlendState originalState = GraphicsDevice.BlendState;

GraphicsDevice.BlendState = BlendState.AlphaBlend;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>

(PrimitiveType.TriangleStrip, verts, 0, 2);

}

GraphicsDevice.BlendState = originalState;

base.Draw(gameTime);

}

Page 48: XNA L05–Texturing

Texture Tiling

Page 49: XNA L05–Texturing

Texturing

• TEXTURE TILING

Page 50: XNA L05–Texturing

Texturing

Tiling is a very simple effect that creates a repeating pattern of an image on the

surface of a primitive object.

Page 51: XNA L05–Texturing

Texturing

• TEXTURE TILING

Page 52: XNA L05–Texturing

Texturing

• TEXTURE TILING

Using a small image to cover a large surface makes tiling a useful way to increase the performance of your textures and decrease the size of

your image files.

Page 53: XNA L05–Texturing

Texture Tiling

• In Load Content

// Right Top

verts[0] = new VertexPositionTexture(

new Vector3(-1, 1, 0), new Vector2(10, 0));

// Left Top

verts[1] = new VertexPositionTexture(

new Vector3(1, 1, 0), new Vector2(1, 0));

// Right Bottom

verts[2] = new VertexPositionTexture(

new Vector3(-1, -1, 0), new Vector2(10, 10));

// Left Bottom

verts[3] = new VertexPositionTexture(

new Vector3(1, -1, 0), new Vector2(1, 10));

Page 54: XNA L05–Texturing

Texture Tiling

Page 55: XNA L05–Texturing

Texture Tiling

Still something wrong!

If the texture rotates, the back of the texture is not drawn!

Why?! And how to fix this?

Page 56: XNA L05–Texturing

Texture Tiling

• Fixing the culling

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

RasterizerState currentRasterizerState = GraphicsDevice.RasterizerState;

GraphicsDevice.RasterizerState = RasterizerState.CullNone;

// … our code

GraphicsDevice.RasterizerState = currentRasterizerState;

base.Draw(gameTime);

}

Page 57: XNA L05–Texturing

Texture Tiling

• Fixing the culling

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

RasterizerState currentRasterizerState = GraphicsDevice.RasterizerState;

GraphicsDevice.RasterizerState = RasterizerState.CullNone;

// … our code

GraphicsDevice.RasterizerState = currentRasterizerState;

base.Draw(gameTime);

}

Page 58: XNA L05–Texturing

Billboarding

Page 59: XNA L05–Texturing

Billboarding

• Billboarding “Microsoft Book – Page 129”

Page 60: XNA L05–Texturing

Billboarding

Page 61: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

Page 62: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

Page 63: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

Page 64: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

Page 65: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

Page 66: XNA L05–Texturing

Billboarding

• The math

Page 67: XNA L05–Texturing

Billboarding

• Atan2(y,x) Vs Atan(y/x)

Page 68: XNA L05–Texturing

Billboarding

• Atan2(y,x) Vs Atan(y/x)

Read more at http://en.wikipedia.org/wiki/Atan2/

Page 69: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

rotationY = Matrix.CreateRotationY(GetViewerAngle());

Page 70: XNA L05–Texturing

Billboarding

float GetViewerAngle()

{

// use camera look direction to get

// rotation angle about Y

float x = cam.view.X - cam.position.X;

float z = cam.view.Z - cam.position.Z;

return (float)Math.Atan2(x, z) + MathHelper.Pi;

}

rotationY = Matrix.CreateRotationY(GetViewerAngle());