Tutorial 2 Additional: Basic Effects - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Tutorial 2 Additional: Basic Effects

Description:

protected override void LoadGraphicsContent(bool loadAllContent) ... Use the world matrix to tilt the cube ... game to exit on Xbox 360 and Windows. ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 10
Provided by: soonte
Category:

less

Transcript and Presenter's Notes

Title: Tutorial 2 Additional: Basic Effects


1
Tutorial 2 Additional Basic Effects
  • Soon Tee Teoh
  • CS 134

2
Class declaration
public class Game1 Microsoft.Xna.Framework.Game
GraphicsDeviceManager graphics
ContentManager content Matrix
worldMatrix Matrix viewMatrix
Matrix projectionMatrix
VertexPositionNormalTexture cubeVertices
VertexDeclaration basicEffectVertexDeclaration
VertexBuffer vertexBuffer
BasicEffect basicEffect
3
LoadGraphicsContent
protected override void LoadGraphicsContent(bool
loadAllContent)
InitializeTransform() if
(loadAllContent)
InitializeEffect()
InitializeCube()
4
InitializeTransform
float tilt MathHelper.ToRadians(22.5f) //
22.5 degree angle private void
InitializeTransform() //
Use the world matrix to tilt the cube along x and
y axes. worldMatrix
Matrix.CreateRotationX(tilt)
Matrix.CreateRotationY(tilt)
viewMatrix Matrix.CreateLookAt(new Vector3(0,
0, 5), Vector3.Zero,
Vector3.Up) projectionMatrix
Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), // 45 degree angle
(float)graphics.GraphicsDevice.View
port.Width /
(float)graphics.GraphicsDevice.Viewport.Height,
1.0f, 100.0f)
5
InitializeEffect
private void InitializeEffect()
basicEffectVertexDeclaration new
VertexDeclaration(
graphics.GraphicsDevice, VertexPositionNormalTextu
re.VertexElements) basicEffect new
BasicEffect(graphics.GraphicsDevice, null)
basicEffect.Alpha 1.0f
basicEffect.DiffuseColor new Vector3(1.0f,
0.0f, 1.0f) basicEffect.SpecularColor
new Vector3(0.25f, 0.25f, 0.25f)
basicEffect.SpecularPower 5.0f
basicEffect.AmbientLightColor new
Vector3(0.75f, 0.75f, 0.75f)
basicEffect.DirectionalLight0.Enabled true
basicEffect.DirectionalLight0.DiffuseColor
Vector3.One basicEffect.Directiona
lLight0.Direction Vector3.Normalize(new
Vector3(1.0f, -1.0f, -1.0f))
basicEffect.DirectionalLight0.SpecularColor
Vector3.One basicEffect.DirectionalLi
ght1.Enabled true
basicEffect.DirectionalLight1.DiffuseColor new
Vector3(0.5f, 0.5f, 0.5f)
basicEffect.DirectionalLight1.Direction
Vector3.Normalize(new Vector3(-1.0f, -1.0f,
1.0f)) basicEffect.DirectionalLight1.
SpecularColor new Vector3(0.5f, 0.5f, 0.5f)
basicEffect.LightingEnabled true
basicEffect.World worldMatrix
basicEffect.View viewMatrix
basicEffect.Projection projectionMatrix

6
InitializeCube
private void InitializeCube()
cubeVertices new VertexPositionNormalTexture3
6 Vector3 topLeftFront new
Vector3(-1.0f, 1.0f, 1.0f) Vector3
bottomLeftFront new Vector3(-1.0f, -1.0f,
1.0f) Vector3 topRightFront new
Vector3(1.0f, 1.0f, 1.0f) Vector3
bottomRightFront new Vector3(1.0f, -1.0f,
1.0f) Vector3 topLeftBack new
Vector3(-1.0f, 1.0f, -1.0f) Vector3
topRightBack new Vector3(1.0f, 1.0f, -1.0f)
Vector3 bottomLeftBack new
Vector3(-1.0f, -1.0f, -1.0f) Vector3
bottomRightBack new Vector3(1.0f, -1.0f,
-1.0f) Vector2 textureTopLeft new
Vector2(0.0f, 0.0f) Vector2
textureTopRight new Vector2(1.0f, 0.0f)
Vector2 textureBottomLeft new
Vector2(0.0f, 1.0f) Vector2
textureBottomRight new Vector2(1.0f, 1.0f)
Vector3 frontNormal new Vector3(0.0f,
0.0f, 1.0f) Vector3 backNormal new
Vector3(0.0f, 0.0f, -1.0f) Vector3
topNormal new Vector3(0.0f, 1.0f, 0.0f)
Vector3 bottomNormal new Vector3(0.0f,
-1.0f, 0.0f) Vector3 leftNormal
new Vector3(-1.0f, 0.0f, 0.0f)
Vector3 rightNormal new Vector3(1.0f, 0.0f,
0.0f)
7
InitializeCube
// Front face.
cubeVertices0 new
VertexPositionNormalTexture(
topLeftFront, frontNormal, textureTopLeft)
cubeVertices1 new
VertexPositionNormalTexture(
bottomLeftFront, frontNormal, textureBottomLeft)
cubeVertices2 new
VertexPositionNormalTexture(
topRightFront, frontNormal, textureTopRight)
cubeVertices3 new
VertexPositionNormalTexture(
bottomLeftFront, frontNormal, textureBottomLeft)
cubeVertices4 new
VertexPositionNormalTexture(
bottomRightFront, frontNormal, textureBottomRight)
cubeVertices5
new VertexPositionNormalTexture(
topRightFront, frontNormal, textureTopRight)
// Back face. cubeVertices6
new VertexPositionNormalTexture(
topLeftBack, backNormal,
textureTopRight)
8
Update
protected override void Update(GameTime
gameTime) // Allows the
default game to exit on Xbox 360 and Windows.
if (GamePad.GetState(PlayerIndex.One).Butt
ons.Back ButtonState.Pressed)
this.Exit() tilt
0.01f // Use the world matrix to
tilt the cube along x and y axes.
worldMatrix Matrix.CreateRotationX(tilt)
Matrix.CreateRotationY(tilt)
basicEffect.World worldMatrix
base.Update(gameTime)
9
Draw
protected override void Draw(GameTime gameTime)
graphics.GraphicsDevice.Clear
(Color.CornflowerBlue)
graphics.GraphicsDevice.RenderState.CullMode
CullMode.CullClockwiseFace
graphics.GraphicsDevice.VertexDeclaration
basicEffectVertexDeclaration
graphics.GraphicsDevice.Vertices0.SetSource(ve
rtexBuffer, 0, VertexPositionNormalTexture.SizeInB
ytes) // This code would go between
a device // BeginScene-EndScene
block. basicEffect.Begin()
foreach (EffectPass pass in basicEffect.CurrentT
echnique.Passes)
pass.Begin() graphics.GraphicsDev
ice.DrawPrimitives(
PrimitiveType.TriangleList, // primitive type
0, // start index
12 // number of primitives, there are 12
triangles )
pass.End()
basicEffect.End() base.Draw(gameTime)
Write a Comment
User Comments (0)
About PowerShow.com