Slides built from Carter Chapter 6 and MSDN The Content Pipeline - - PowerPoint PPT Presentation
Slides built from Carter Chapter 6 and MSDN The Content Pipeline - - PowerPoint PPT Presentation
Slides built from Carter Chapter 6 and MSDN The Content Pipeline Youre an artist, you have your favorite tool to create content, do you want to learn yet another one? So the content pipeline must be able to take assets in various
The Content Pipeline
You’re an artist, you have your favorite tool to create
content, do you want to learn yet another one?
So the content pipeline must be able to take assets in
various formats and load them into an XNA game.
E.g. our 3d model must be converted to be used.
Do you want to build vertices and triangles for you
models?
Designed to work for both programmers and artists. The Content Pipeline will take our files (assets) as
input, then convert them so that XNA can compile them into an easily loadable format (F5 does this).
Content Importer
Reads data in
Standard imports
3D: Autodesk FBX and DirectX .x (NodeContent) Textures: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and
.tga. (TextureContent)
Spritefont (FontDescription) DirectX effects (EffectContent) XML (NodeContent)
Can build your own for other file types Can also find many on the web.
Content Processor
Standard processors
Effect Processor – compiles EffectContent program ModelProcessor – Takes a NodeContent and compiles it into a
ModelContent (can texture and transform)
PassThroughProcessor – Does nothing FontDescriptionProcessor – Compiles FontDescription into a
SpriteFontContent
FontTextureProcessor – Compiles TextureContent into a
SpriteFontContent (also takes Texture2Dcontent)
TextureProcessor – Processes TextureContent – allows you to
manipulate textures
XACT for sound
Also can write a custom processor Might also want to modify existing processor, i.e always scale.
Content Pipeline
From MSDN
3D models
We could build models by creating all of the vertices
and triangles in our code like last time.
Joy!
Or, we could get someone to create a model for us, or
find one on the web.
Maya, Blender, 3dStudioMax, etc. Numerous formats, there are some converters, you tend
to get what you pay for, i.e. free ones don’t always work so well
Let’s try the 2nd method.
Using the XELibrary (again)
Using XELibrary; private FPS fps; private FirstPersonCamera camera; private InputHandler input; input = new InputHandler(this); Components.Add(input); camera = new FirstPersonCamera(this); Components.Add(camera) #if DEBUG //draw 60fps and update as often as possible fps = new FPS(this, true, false); #else fps = new FPS(this, true, false); #endif Components.Add(fps)
Declarations Constructor()
Organizing project
Create some subdirs
Right click in solution explorer
- n content
Add new item -> folder
Create one for Models Create one for Textures Will reduce clutter
XNA logic
1.
Main app calls game constructor
2.
Game constructor creates any game components and calls their constructors
3.
XNA Framework calls the games Initialize()
4.
XNA calls each game component’s Initialize()
5.
XNA calls each Drawable Compenent’s LoadContent
6.
XNA calls game’s LoadContent()
XNA logic
7.
XNA calls game Update() method
8.
XNA calls each game component’s Update()
- 9. XNA calls game’s Draw()
- 10. XNA calls ech drawable component’s Draw()
- 11. Repeat 7-10 until exits
XNA Logic
- 12. If device is lost call UnloadContent()
- 13. If the device is reset start at step 6 (loadContent)
- 14. Gamer exits game
- 15. XNA calls game’s Dispose method.
- 16. Game’s dispose calls base object’s dispose ….
- 17. XNA calls each game component’s dispose.
- 18. XNA calls UnloadContent()
- 19. Game’s dispose get focus back and game exits.
Loading a 3D Model
private Model model;
LoadContent()
model = content.Load<Model>(@"Content\Models\asteroid1");
Drawing a 3D Model
Set up the world matrix (model to world transforms)
world = Matrix.CreateRotationZ(.1f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateScale(.5f) * Matrix.CreateTranslation(new Vector3(1000, 0, - 4500));
Now draw model
DrawModel(ref model, ref world);
Drawing a 3D Model
private void DrawModel(ref Model m, ref Matrix world, Texture2D texture) { Matrix[] transforms = new Matrix[m.Bones.Count]; m.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in m.Meshes) { foreach (BasicEffect be in mesh.Effects) { be.EnableDefaultLighting(); be.Projection = camera.Projection; be.View = camera.View; be.World = world * mesh.ParentBone.Transform; } mesh.Draw(); } }
Let’s texture
private Model model; model = content.Load<Model>(@"Content\Models\asteroid1"); greyAsteroid = content.Load<Texture2D>(@"Content\Textures\as teroid1-grey");
- riginalAsteroid =
content.Load<Texture2D>(@"Content\Textures\as teroid1")
Drawing a 3D Model
Set up the world matrix (model to world transforms)
world = Matrix.CreateRotationZ(.1f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateScale(.5f) * Matrix.CreateTranslation(new Vector3(1000, 0, - 4500));
Now draw model
DrawModel(ref model, ref world, greyAsteroid);
Drawing a 3D Model
private void DrawModel(ref Model m, ref Matrix world, Texture2D texture) { Matrix[] transforms = new Matrix[m.Bones.Count]; m.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in m.Meshes) { foreach (BasicEffect be in mesh.Effects) { be.EnableDefaultLighting(); if (texture != null) be.Texture = texture; be.Projection = camera.Projection; be.View = camera.View; be.World = world * mesh.ParentBone.Transform; } mesh.Draw(); } }
Draw Many Objects
world = Matrix.CreateTranslation(new Vector3(0, 0, 1)); DrawModel(ref penguinModel, ref world, originalAsteroid); world = Matrix.CreateTranslation(new Vector3(0, 0, 5)); DrawModel(ref penguinModel, ref world, null); world = Matrix.CreateRotationZ(.1f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateScale(.5f) * Matrix.CreateTranslation(new Vector3(1000, 0, -4500)); DrawModel(ref model, ref world, greyAsteroid); world = Matrix.CreateRotationY(.2f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3(-1000, 0, -4500)); DrawModel(ref model, ref world, originalAsteroid); world = Matrix.CreateRotationX( MathHelper.ToRadians(270.0f * (float)gameTime.TotalGameTime.TotalSeconds)) * Matrix.CreateTranslation(new Vector3(0, 0, -4500)); DrawModel(ref model, ref world, greyAsteroid); world = Matrix.CreateRotationY(1.57f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateRotationZ(.785f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3(0, 0, 4000)); DrawModel(ref model, ref world, originalAsteroid);