xna l04–primitives, indexbuffer and vertexbuffer

70
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L04 – Primitives, IndexBuffer and VertexBuffer

Upload: mohammad-shaker

Post on 14-May-2015

320 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: XNA L04–Primitives, IndexBuffer and VertexBuffer

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL04 – Primitives, IndexBuffer and VertexBuffer

Page 2: XNA L04–Primitives, IndexBuffer and VertexBuffer

3D World

Page 3: XNA L04–Primitives, IndexBuffer and VertexBuffer

3D World

Page 4: XNA L04–Primitives, IndexBuffer and VertexBuffer

3D World

Page 5: XNA L04–Primitives, IndexBuffer and VertexBuffer

Primitives

• 1 - Drawing Triangles

• 2 - A Little Practice

• 3 - Index and Vertex Buffers

• 4 - Primitive Types

Page 6: XNA L04–Primitives, IndexBuffer and VertexBuffer

Primitives

Page 7: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

Page 8: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing TrianglesWhat do we need to draw?!

Page 9: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

App1-Triangles

Page 10: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

Initialize

LoadContent

UnloadContent

Update

Draw

Game1

Page 11: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• Global ScopeSpriteBatch spriteBatch;

VertexPositionColor[] vertices;

BasicEffect basicEffect;

Matrix world = Matrix.CreateTranslation(0, 0, 0);

Matrix view = Matrix.CreateLookAt(

new Vector3(0, 0, 3),

new Vector3(0, 0, 0),

new Vector3(0, 1, 0));

Matrix projection = Matrix.CreatePerspectiveFieldOfView(

MathHelper.ToRadians(45),

800f / 600f,

0.01f,

100f);

Page 12: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

SpriteBatch spriteBatch;

VertexPositionColor[] vertices;

BasicEffect basicEffect;

Matrix world = Matrix.CreateTranslation(0, 0, 0);

Matrix view = Matrix.CreateLookAt(

new Vector3(0, 0, 3),

new Vector3(0, 0, 0),

new Vector3(0, 1, 0));

Matrix projection = Matrix.CreatePerspectiveFieldOfView(

MathHelper.ToRadians(45),

800f / 600f,

0.01f,

100f);

Page 13: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• LoadContent()

protected override void LoadContent()

{

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

basicEffect = new BasicEffect(GraphicsDevice);

vertices = new VertexPositionColor[3];

vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red);

vertices[1] = new VertexPositionColor(new Vector3(+0.5f, 0, 0), Color.Green);

vertices[2] = new VertexPositionColor(new Vector3(-0.5f, 0, 0), Color.Blue);

}

Page 14: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• LoadContent()

protected override void LoadContent()

{

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

basicEffect = new BasicEffect(GraphicsDevice);

vertices = new VertexPositionColor[3];

vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red);

vertices[1] = new VertexPositionColor(new Vector3(+0.5f, 0, 0), Color.Green);

vertices[2] = new VertexPositionColor(new Vector3(-0.5f, 0, 0), Color.Blue);

}

Page 15: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• Draw()

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.World = world;

basicEffect.View = view;

basicEffect.Projection = projection;

basicEffect.VertexColorEnabled = true;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives<VertexPositionColor>

(PrimitiveType.TriangleList, vertices, 0, 1);

}

base.Draw(gameTime);

}

Page 16: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

App1-Triangles

Page 17: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• Cube

Page 18: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

Page 19: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

• PrimitiveType.Points

• PrimitiveType.LineList

• PrimitiveType.LineStrip

• PrimitiveType.TriangleList

• PrimitiveType.TriangleStrip

• PrimitiveType.TriangleFan

Page 20: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

Page 21: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

Page 22: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

Page 23: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

Page 24: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• Using TrianglesList

• App2-Tetrahedron-TriangleList

Page 25: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• How to do it with a simple

rotation?!

Page 26: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• How to do it with a simple rotation?!

• First, What’s that 3D Object is?!

Tetrahedron! (Faces: 4, Vertices: 12)

Page 27: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• How to do it with a simple rotation?!

• First, What’s that 3D Object is?!

Tetrahedron! (Faces: 4, Vertices: 12(Each one at a time or a one shot?!))

Page 28: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• How to do it with a simple rotation?!

• First, What’s that 3D Object is?!

Tetrahedron! (Faces: 4, Vertices: 12(use TriangleList))

Page 29: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Simple Rotation

• How to do it with a simple rotation?!

vertices = new VertexPositionColor[12];

vertices[0] = new VertexPositionColor(new Vector3(0.000f, 1.000f, 0.000f), Color.Red);

vertices[1] = new VertexPositionColor(new Vector3(-0.816f, -0.333f, -0.471f), Color.Blue);

vertices[2] = new VertexPositionColor(new Vector3(0.000f, -0.333f, 0.943f), Color.Green);

vertices[3] = new VertexPositionColor(new Vector3(0.000f, 1.000f, 0.000f), Color.Red);

vertices[4] = new VertexPositionColor(new Vector3(0.816f, -0.333f, -0.471f), Color.Yellow);

vertices[5] = new VertexPositionColor(new Vector3(-0.816f, -0.333f, -0.471f), Color.Blue);

vertices[6] = new VertexPositionColor(new Vector3(0.000f, -0.333f, 0.943f), Color.Green);

vertices[7] = new VertexPositionColor(new Vector3(0.816f, -0.333f, -0.471f), Color.Yellow);

vertices[8] = new VertexPositionColor(new Vector3(0.000f, 1.000f, 0.000f), Color.Red);

vertices[9] = new VertexPositionColor(new Vector3(-0.816f, -0.333f, -0.471f), Color.Blue);

vertices[10]= new VertexPositionColor(new Vector3(0.816f, -0.333f, -0.471f), Color.Yellow);

vertices[11] = new VertexPositionColor(new Vector3(0.000f, -0.333f, 0.943f), Color.Green);

VertexPositionColor[] vertices;

Page 30: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Simple Rotation

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.World = world;

basicEffect.View = view;

basicEffect.Projection = projection;

basicEffect.VertexColorEnabled = true;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 4);

}

base.Draw(gameTime);

}

Page 31: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Simple Rotation

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.World = world;

basicEffect.View = view;

basicEffect.Projection = projection;

basicEffect.VertexColorEnabled = true;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 4);

}

base.Draw(gameTime);

}

Page 32: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Simple Rotation

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.World = world;

basicEffect.View = view;

basicEffect.Projection = projection;

basicEffect.VertexColorEnabled = true;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 4);

}

base.Draw(gameTime);

}

Page 33: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Simple Rotation

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.World = world;

basicEffect.View = view;

basicEffect.Projection = projection;

basicEffect.VertexColorEnabled = true;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 4);

}

base.Draw(gameTime);

}

Page 34: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Rotation Logic

Page 35: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles – Rotation LogicMatrices and Transformations

Page 36: XNA L04–Primitives, IndexBuffer and VertexBuffer

Drawing Triangles

• Using TrianglesList

• App3-TriangleList

Page 37: XNA L04–Primitives, IndexBuffer and VertexBuffer

Different Primitive Types

• Changing last tutorial PrimitiveType to LineList will result in

• “App5-VertexBuffer-LineList”

Page 38: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

Page 39: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex BuffersThe Concept

Page 40: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• Effectively store our data

• Greatly reduce the amount of data that needs to be send over to the graphics

device

– speed up our applications a looooooooooot!

• The only way to store your data

Page 41: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers• Look at this,

Page 42: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

True oder false?

• Look at this,

Page 43: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

True oder false?

• Look at this,

Page 44: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

Page 45: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

Page 46: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

Page 47: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

Page 48: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• An Icosahedron

Page 49: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• An Icosahedron

• “App4-VertexBuffer”

Page 50: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• Creating the Necessary Variables

VertexBuffer vertexBuffer;

IndexBuffer indexBuffer;

Page 51: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• Creating the Necessary Variables

VertexBuffer vertexBuffer;

IndexBuffer indexBuffer;

VertexPositionColor[] vertices;

Page 52: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• Creating the Necessary Variables

VertexBuffer vertexBuffer;

IndexBuffer indexBuffer;

VertexPositionColor[] vertices; // Needed in a global scope or not?

Page 53: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• Creating the Necessary Variables

VertexBuffer vertexBuffer;

IndexBuffer indexBuffer;

VertexPositionColor[] vertices; // Needed in a global scope or not?

Page 54: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers – The Creating of

• In LoadContent()VertexPositionColor[] vertexArray = new VertexPositionColor[12];

// vertex position and color information for icosahedron

vertexArray[0] = new VertexPositionColor(new Vector3(-0.26286500f, 0.0000000f, 0.42532500f), Color.Red);

//…..

vertexArray[11]= new VertexPositionColor(new Vector3(-0.42532500f, -0.26286500f, 0.0000000f),Color.Crimson);

// Set up the vertex buffer

vertexBuffer = new VertexBuffer( graphics.GraphicsDevice,

typeof(VertexPositionColor),

vertexArray.Length,

BufferUsage.WriteOnly);

vertexBuffer.SetData(vertexArray);

short[] indices = new short[60];

indices[0] = 0; indices[1] = 6; indices[2] = 1;

//…

indices[57] = 9; indices[58] = 11; indices[59] = 0;

indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

indexBuffer.SetData(indices);

Page 55: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 56: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 57: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 58: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 59: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 60: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 61: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 62: XNA L04–Primitives, IndexBuffer and VertexBuffer
Page 63: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 64: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 65: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 66: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 67: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 68: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 69: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• In Draw() Method

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

{

pass.Apply();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

graphics.GraphicsDevice.Indices = indexBuffer;

graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);

}

Page 70: XNA L04–Primitives, IndexBuffer and VertexBuffer

Index And Vertex Buffers

• An Icosahedron at last

has been built!