Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
XNA Game Development
L03 – Models, BasicEffect and A...
Using 3D Models
Using 3D Models
Why using models?!
Some 3D Modeling Programs
• 3D Max
• Maya
• Blender
• Wings3D
• Google SketchUp
• … etc
Using 3D Models - Loading the Model
• Global Scope
private Model model; Initialize
LoadContent
UnloadContent
Update
Draw
G...
Using 3D Models - Loading the Model
• Global Scope
• LoadContent()
private Model model;
model = Content.Load<Model>("Ship"...
Using 3D Models - Drawing the Model
• Must set appropriate matrices
private Matrix world = Matrix.CreateTranslation(new Ve...
Using 3D Models
• DrawModel() and Draw()
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)...
Using 3D Models
• DrawModel() and Draw()
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)...
Using 3D Models
• “App-Using3DModels”
BasicEffect
BasicEffect
• Effects in XNA
– An effect is simply a method of designating how an object should be rendered on the screen....
BasicEffect
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
basicEffect.Worl...
BasicEffect
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
basicEffect.Worl...
BasicEffect
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
basicEffect.Worl...
BasicEffect
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) {
foreach (ModelMesh mesh in...
BasicEffect
• Changing BasicEffect Texture
effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
BasicEffect
• Changing BasicEffect Texture
effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
BasicEffect
• Not using Texture!
effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
Lighting
Lighting
• Diffuse Light
– Diffuse light is the basic kind of light. This is the kind of light that lights an object we ar...
Lighting
• Specular Light
– Specular light (or specular highlights) are the shiny spots that appear when an object is
some...
Lighting
• Ambient Light
– Ambient light is light that doesn't come from any particular light source, but instead is kind ...
Lighting
• Emissive Light
– Emissive light is light that is coming from the surface itself. In games, however, emissive li...
Lighting with the BasicEffect Class
• Default Lighting
effect.EnableDefaultLighting();
Lighting with the BasicEffect Class
• Default Lighting
• Custom Lighting
effect.EnableDefaultLighting();
Lighting with the BasicEffect Class
• Default Lighting
• Custom Lighting
effect.EnableDefaultLighting();
effect.LightingEn...
Lighting with the BasicEffect Class
• Default Lighting
• Custom Lighting
effect.EnableDefaultLighting();
effect.LightingEn...
Lighting with the BasicEffect Class
• Default Lighting
• Custom Lighting
effect.EnableDefaultLighting();
effect.LightingEn...
Lighting with the BasicEffect Class
• Default Lighting
• Custom Lighting
effect.EnableDefaultLighting();
effect.LightingEn...
Lighting with the BasicEffect Class
• You can turn individual lights on and off with
• you can set the effect's ambient li...
Lighting with the BasicEffect Class
• “App2-BasicEffectLighting”
BasicEffect Fog
Why using fog?
BasicEffect Fog
Why using fog?
For instance; Can be used to hide a close far-clipping plane
BasicEffect Fog
• Rendering Fog with the BasicEffect Class
effect.FogEnabled = true;
effect.FogColor = Color.CornflowerBlu...
BasicEffect Fog
• Rendering Fog with the BasicEffect Class
effect.FogEnabled = true;
effect.FogColor = Color.CornflowerBlu...
3D Animation
Basic Matrices
Basic Matrices
Basic Matrices
Basic Matrices
Basic Matrices
Basic Matrices
3D Animation
• Create a Place to Store the Position
private Vector3 position;
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Now we want to move the Model
– “Attaching position vector with our model”
world = Matrix.CreateTranslation...
3D Animation
• Now we want to move the Model
– “Attaching position vector with our model”
world = Matrix.CreateTranslation...
3D Animation
• Now we want to move the Model
– “Attaching position vector with our model”
world = Matrix.CreateTranslation...
3D Animation
• Now we want to move the Model
– “Attaching position vector with our model”
world = Matrix.CreateTranslation...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
pri...
3D Animation
3D Animation
3D Animation
3D Animation
3D Animation
3D Animation
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
3D Animation
3D Animation
3D Animation
3D Animation
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
• Create a Place to Store the Position
• Initialize the Position
• Creating animation/ Updating Position!
private Vector3 ...
3D Animation
• “App1-Animation”
Mesh-by-Mesh Animation
Mesh-by-Mesh Animation
What’s that?
Mesh-by-Mesh Animation
• Firing Up!
– Setup
• We will first need to acquire a model that has different parts that will all...
Mesh-by-Mesh Animation
• Adding global variables
private Model helicopterModel;
private float mainRotorAngle = 0;
private ...
Mesh-by-Mesh Animation
• Adding global variables
private Model helicopterModel;
private float mainRotorAngle = 0;
private ...
Mesh-by-Mesh Animation
• Adding global variables
private Model helicopterModel;
private float mainRotorAngle = 0;
private ...
Mesh-by-Mesh Animation
• Adding global variables
private Model helicopterModel;
private float mainRotorAngle = 0;
private ...
Mesh-by-Mesh Animation
• Adding global variables
private Model helicopterModel;
private float mainRotorAngle = 0;
private ...
Mesh-by-Mesh Animation
• Loading the model in LoadContent() method
• Updating the angles and location of the Helicopter mo...
Mesh-by-Mesh Animation
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matri...
Mesh-by-Mesh Animation
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matri...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• New Draw() method
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Co...
Mesh-by-Mesh Animation
• Creating a new DrawModel() method as we did before
private void DrawModel(Model model, Matrix wor...
Mesh-by-Mesh Animation
• Creating a new DrawModel() method as we did before
private void DrawModel(Model model, Matrix wor...
Mesh-by-Mesh Animation
• Creating a new DrawModel() method as we did before
private void DrawModel(Model model, Matrix wor...
Mesh-by-Mesh Animation
• Test it live
• “App1-Mesh-by-MeshAnimation”
XNA L03–Models, Basic Effect and Animation
Upcoming SlideShare
Loading in...5
×

XNA L03–Models, Basic Effect and Animation

338

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
338
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

XNA L03–Models, Basic Effect and Animation

  1. 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L03 – Models, BasicEffect and Animation
  2. 2. Using 3D Models
  3. 3. Using 3D Models Why using models?!
  4. 4. Some 3D Modeling Programs • 3D Max • Maya • Blender • Wings3D • Google SketchUp • … etc
  5. 5. Using 3D Models - Loading the Model • Global Scope private Model model; Initialize LoadContent UnloadContent Update Draw Game1
  6. 6. Using 3D Models - Loading the Model • Global Scope • LoadContent() private Model model; model = Content.Load<Model>("Ship"); Initialize LoadContent UnloadContent Update Draw Game1
  7. 7. Using 3D Models - Drawing the Model • Must set appropriate matrices private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f);
  8. 8. Using 3D Models • DrawModel() and Draw() private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) { foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); } Initialize LoadContent UnloadContent Update Draw Game1
  9. 9. Using 3D Models • DrawModel() and Draw() private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) { foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); } Initialize LoadContent UnloadContent Update Draw Game1
  10. 10. Using 3D Models • “App-Using3DModels”
  11. 11. BasicEffect
  12. 12. BasicEffect • Effects in XNA – An effect is simply a method of designating how an object should be rendered on the screen. – In the past (graphics API) • tell the graphics card everything it needed to know – HLSL – It can be quite a bit of work to create a complete effect from scratch • XNA guys delivered to us the “BasicEffect” class
  13. 13. BasicEffect 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<> … } base.Draw(gameTime); }
  14. 14. BasicEffect 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<> … } base.Draw(gameTime); }
  15. 15. BasicEffect 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<> … } base.Draw(gameTime); }
  16. 16. BasicEffect private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) { foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } }
  17. 17. BasicEffect • Changing BasicEffect Texture effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
  18. 18. BasicEffect • Changing BasicEffect Texture effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
  19. 19. BasicEffect • Not using Texture! effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"
  20. 20. Lighting
  21. 21. Lighting • Diffuse Light – Diffuse light is the basic kind of light. This is the kind of light that lights an object we are viewing, for the most part. The intensity of the light mostly comes from the angle the surface makes with the light itself, so surfaces that face away from the light don't aren't bright at all, while surfaces that face the light are lit up pretty well.
  22. 22. Lighting • Specular Light – Specular light (or specular highlights) are the shiny spots that appear when an object is somewhat reflective. This light is based on how reflective the surface is, as well as the angle that is being made between the light source, the surface, and the viewer.
  23. 23. Lighting • Ambient Light – Ambient light is light that doesn't come from any particular light source, but instead is kind of "background light" that comes from all over. In the real world, there is always a small amount of ambient light, and in our game, we will want to add a little bit to make our objects look more realistic.
  24. 24. Lighting • Emissive Light – Emissive light is light that is coming from the surface itself. In games, however, emissive light doesn't automatically light up nearby objects, so it often doesn't have the same effect that we would like, but it still has its uses.
  25. 25. Lighting with the BasicEffect Class • Default Lighting effect.EnableDefaultLighting();
  26. 26. Lighting with the BasicEffect Class • Default Lighting • Custom Lighting effect.EnableDefaultLighting();
  27. 27. Lighting with the BasicEffect Class • Default Lighting • Custom Lighting effect.EnableDefaultLighting(); effect.LightingEnabled = true; // turn on the lighting subsystem. effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights
  28. 28. Lighting with the BasicEffect Class • Default Lighting • Custom Lighting effect.EnableDefaultLighting(); effect.LightingEnabled = true; // turn on the lighting subsystem. effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights R G B
  29. 29. Lighting with the BasicEffect Class • Default Lighting • Custom Lighting effect.EnableDefaultLighting(); effect.LightingEnabled = true; // turn on the lighting subsystem. effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights R G B X Y Z
  30. 30. Lighting with the BasicEffect Class • Default Lighting • Custom Lighting effect.EnableDefaultLighting(); effect.LightingEnabled = true; // turn on the lighting subsystem. effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights R G B X Y Z R G B
  31. 31. Lighting with the BasicEffect Class • You can turn individual lights on and off with • you can set the effect's ambient light color effect.DirectionalLight0.Enabled = false; effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f); effect.EmissiveColor = new Vector3(1, 0, 0);
  32. 32. Lighting with the BasicEffect Class • “App2-BasicEffectLighting”
  33. 33. BasicEffect Fog Why using fog?
  34. 34. BasicEffect Fog Why using fog? For instance; Can be used to hide a close far-clipping plane
  35. 35. BasicEffect Fog • Rendering Fog with the BasicEffect Class effect.FogEnabled = true; effect.FogColor = Color.CornflowerBlue.ToVector3(); effect.FogStart = 9.75f; effect.FogEnd = 10.25f;
  36. 36. BasicEffect Fog • Rendering Fog with the BasicEffect Class effect.FogEnabled = true; effect.FogColor = Color.CornflowerBlue.ToVector3(); effect.FogStart = 9.75f; effect.FogEnd = 10.25f;
  37. 37. 3D Animation
  38. 38. Basic Matrices
  39. 39. Basic Matrices
  40. 40. Basic Matrices
  41. 41. Basic Matrices
  42. 42. Basic Matrices
  43. 43. Basic Matrices
  44. 44. 3D Animation • Create a Place to Store the Position private Vector3 position;
  45. 45. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0);
  46. 46. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0);
  47. 47. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0);
  48. 48. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0);
  49. 49. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); LoadContent() Update() Draw()
  50. 50. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); LoadContent() Update() Draw()
  51. 51. 3D Animation • Now we want to move the Model – “Attaching position vector with our model” world = Matrix.CreateTranslation(position);
  52. 52. 3D Animation • Now we want to move the Model – “Attaching position vector with our model” world = Matrix.CreateTranslation(position);
  53. 53. 3D Animation • Now we want to move the Model – “Attaching position vector with our model” world = Matrix.CreateTranslation(position);
  54. 54. 3D Animation • Now we want to move the Model – “Attaching position vector with our model” world = Matrix.CreateTranslation(position);
  55. 55. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position);
  56. 56. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position);
  57. 57. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position);
  58. 58. 3D Animation • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position);
  59. 59. 3D Animation
  60. 60. 3D Animation
  61. 61. 3D Animation
  62. 62. 3D Animation
  63. 63. 3D Animation
  64. 64. 3D Animation
  65. 65. 3D Animation
  66. 66. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  67. 67. 3D Animation
  68. 68. 3D Animation
  69. 69. 3D Animation
  70. 70. 3D Animation
  71. 71. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  72. 72. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  73. 73. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  74. 74. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  75. 75. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  76. 76. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; position= new Vector3(0, 0, 0); position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  77. 77. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; private float angle; position= new Vector3(0, 0, 0); angle = 0; position += new Vector3(0, 0.01f, 0); world = Matrix.CreateTranslation(position); 3D Animation
  78. 78. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; private float angle; position= new Vector3(0, 0, 0); angle = 0; position += new Vector3(0, 0.01f, 0); angle += 0.03f; world = Matrix.CreateTranslation(position); 3D Animation
  79. 79. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; private float angle; position= new Vector3(0, 0, 0); angle = 0; position += new Vector3(0, 0.01f, 0); angle += 0.03f; world = Matrix.CreateTranslation(position); 3D Animation
  80. 80. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; private float angle; position= new Vector3(0, 0, 0); angle = 0; position += new Vector3(0, 0.01f, 0); angle += 0.03f; world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position); 3D Animation
  81. 81. • Create a Place to Store the Position • Initialize the Position • Creating animation/ Updating Position! private Vector3 position; private float angle; position= new Vector3(0, 0, 0); angle = 0; position += new Vector3(0, 0.01f, 0); angle += 0.03f; world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position); 3D Animation
  82. 82. 3D Animation • “App1-Animation”
  83. 83. Mesh-by-Mesh Animation
  84. 84. Mesh-by-Mesh Animation What’s that?
  85. 85. Mesh-by-Mesh Animation • Firing Up! – Setup • We will first need to acquire a model that has different parts that will allow us to do the movements that we want • Our "Helicopter" model included in appendix
  86. 86. Mesh-by-Mesh Animation • Adding global variables private Model helicopterModel; private float mainRotorAngle = 0; private float tailRotorAngle = 0; private Vector3 location = new Vector3(0, 0, 0); private float angle = 0f; private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  87. 87. Mesh-by-Mesh Animation • Adding global variables private Model helicopterModel; private float mainRotorAngle = 0; private float tailRotorAngle = 0; private Vector3 location = new Vector3(0, 0, 0); private float angle = 0f; private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  88. 88. Mesh-by-Mesh Animation • Adding global variables private Model helicopterModel; private float mainRotorAngle = 0; private float tailRotorAngle = 0; private Vector3 location = new Vector3(0, 0, 0); private float angle = 0f; private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  89. 89. Mesh-by-Mesh Animation • Adding global variables private Model helicopterModel; private float mainRotorAngle = 0; private float tailRotorAngle = 0; private Vector3 location = new Vector3(0, 0, 0); private float angle = 0f; private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  90. 90. Mesh-by-Mesh Animation • Adding global variables private Model helicopterModel; private float mainRotorAngle = 0; private float tailRotorAngle = 0; private Vector3 location = new Vector3(0, 0, 0); private float angle = 0f; private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0)); private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY); private Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  91. 91. Mesh-by-Mesh Animation • Loading the model in LoadContent() method • Updating the angles and location of the Helicopter model in Update()method helicopterModel = Content.Load<Model>("Helicopter"); tailRotorAngle -= 0.15f; mainRotorAngle -= 0.15f; angle += 0.02f; location += Vector3.Transform(new Vector3(0.1f, 0, 0), Matrix.CreateRotationY(MathHelper.ToRadians(90) + angle));
  92. 92. Mesh-by-Mesh Animation protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  93. 93. Mesh-by-Mesh Animation protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  94. 94. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  95. 95. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  96. 96. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  97. 97. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  98. 98. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  99. 99. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  100. 100. Mesh-by-Mesh Animation • New Draw() method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix[] meshWorlds = new Matrix[3]; meshWorlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0)); meshWorlds[1] = Matrix.CreateRotationY(mainRotorAngle); meshWorlds[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * Matrix.CreateRotationX(tailRotorAngle) * Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f)); world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(location); DrawModel(helicopterModel, world, meshWorlds, view, projection); base.Draw(gameTime); }
  101. 101. Mesh-by-Mesh Animation • Creating a new DrawModel() method as we did before private void DrawModel(Model model, Matrix world, Matrix[] meshWorlds, Matrix view, Matrix projection) { for (int index = 0; index < model.Meshes.Count; index++) { ModelMesh mesh = model.Meshes[index]; foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = mesh.ParentBone.Transform * meshWorlds[index] * world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } }
  102. 102. Mesh-by-Mesh Animation • Creating a new DrawModel() method as we did before private void DrawModel(Model model, Matrix world, Matrix[] meshWorlds, Matrix view, Matrix projection) { for (int index = 0; index < model.Meshes.Count; index++) { ModelMesh mesh = model.Meshes[index]; foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = mesh.ParentBone.Transform * meshWorlds[index] * world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } }
  103. 103. Mesh-by-Mesh Animation • Creating a new DrawModel() method as we did before private void DrawModel(Model model, Matrix world, Matrix[] meshWorlds, Matrix view, Matrix projection) { for (int index = 0; index < model.Meshes.Count; index++) { ModelMesh mesh = model.Meshes[index]; foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = mesh.ParentBone.Transform * meshWorlds[index] * world; effect.View = view; effect.Projection = projection; } mesh.Draw(); } }
  104. 104. Mesh-by-Mesh Animation • Test it live • “App1-Mesh-by-MeshAnimation”
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×