Exporting FBX

screen089

Today, I worked some more on getting my player model to render correctly in XNA.  Previously, I got the animated model to display, however I noticed some clipping issues with polygons.  To tackle this problem, I decided to make an animated textured cube, since that would be easy to analyze.  I quickly realized that I was seeing the inside of the cube instead of the outside.

screen088This was happening because I was using a reflect modifier to display my model.  Originally, the player model was displaying on its side because Blender uses a Z-Up coordinate system, so in my code I would apply an X rotation of Pi (180 degrees) to fix that.  However, this resulted in the model displaying up-side-down.  To fix that, I used a reflection on the x-z plane where Y equals zero, Matrix.CreateReflection(new Plane(0f, 1f, 0f, 0f)), to display it correctly.  What I didn’t realize was that this apparently also flips all of the normals, so what I was actually seeing was the inside of the player model (with the outside transparent).  I believe there is a way to render both sides of the polygons, but that is typically wasteful since the player shouldn’t ever see the inside of a model.

After some searching, I found a good article by John C Brown at Diabolical the Shooter which explains how to make the necessary rotations in the content processor to get all models to display correctly.  Similar to his rotation example, I just added a method to the content processor which rotates the NodeContent by MathHelper.PiOver2 on the X axis and MathHelper.Pi on the Z axis.  Then that rotation matrix is passed to the MeshHelper.TransformScene method to rotate the model and animation.  It is important to note that this modification does not replace the “XNA Rotate Animation Hack” export option in Blender, which is still required to be checked when exporting a model.

After updating that code, I reran the Skinning Sample project with my player model and it now displays correctly.  The existing model (“Dude”) is now displaying on its side, since the rotation for Blender is applied to all objects.  It would have been nice if XNA provided a method natively to import models using Z Up coordinates.  Maybe they consider this a part of “paying your dues” to get a model to display.

Fun with XML

screen087

Tonight, I decided to go ahead and work on the Level Editor some more, so that I can start churning out some levels for a playable demo.  In Resistor, I used Mappy to generate arrays that I hardcoded as constants in my level class.  This probably wasn’t the best coding style, even though I’d bet it is faster than loading from disk.  In addition, I want players to be able to create their own levels in Blasting Bits, which means that having constant arrays is not an option.

screen085

To keep my data clean and organized, I decided to go with the XML format.  I was glad to find out that XNA has methods for handling reading XML files.  I found a good tutorial video by Richard Key of Paranoid Ferret Productions on how to do this.  I tried using this to load a String object from XML stored in my Content project area and it worked perfectly.  The “Boo” message that I entered into the XML file was loaded into the String variable I created using the Content.Load message, similar to a Sprite, Font, or Model.

screen086

However, I quickly realized that there is a problem when trying to create an XML data file for one of my own custom classes.  When trying to set the asset type to one of my classes, it is not able to find my class.  I created a simple Foo class with a Sting and int instance variables.  At first I thought the problem was due to my class not being public, but changing the scope of the class made no difference.  Doing a quick web search showed that this problem has been encountered before.  After digging deeper and consulting the video mentioned before, I believe that the reason why it can’t find my class is because the Content project doesn’t have a Reference defined to my main game project.  I tried adding a Reference, but it doesn’t allow this because it says that it would create a circular reference.  The only solution I was able to find is to create a third project just to hold the XML files.  This seemed really excessive, and this method probably won’t support loading XML files generated by the player so I found another approach.

screen083After finding a great example written by Microsoft, I was able to take  a new Foo object and serialize it.  In the example, it writes the XML to the Console.  Unfortunately, that isn’t very helpful aside from displaying the serialized object.  After some more trial and error, I found that instead of passing Console.Out to the Serialize method I could instead pass another Stream.  At first I tried using StreamReader and StreamWriter, but both of those require a filename, while I just want to store it as a String.  Finally, I found the StringWriter class, which takes no parameters and can hold the value of the serialized object.  The XML can be extracted using the ToString() method on the StringWriter object.  I was able to display the serialized data on the level editor screen.  From there, I just pass the String to my FileSaver class in ResistorKit, which handles opening and writing to the storage device.

screen084

That was enough to melt my brain for one night, so I will continue on later with reading the XML value back into my game object.  Last night I was able to get the remainder of my armor models (helmet, hands, boots, chest) to display in the game.  I also got the models for the basic collectible for money and the gun to display as well.  For now, I just used the second level helmet as the enemy model.  All of these models continually rotate during gameplay.

XNA Model Animation

https://www.youtube.com/watch?v=qndvb8N91j0

Demonstration of switching between 2D and 3D rendering, along displaying an animated Blender model in XNA

Today, I worked on getting my simple character model animation to display in the game world.  Currently, the player model displays fine, but it does not display the arms and legs moving as the player moves.  I found quite a few references for animating a Model, but none were very simplistic.  One thing in common with all of the examples is that model animations are not handled “out of the box” by XNA.  The technique used requires that the animation information be extracted from the model using the “Tag” property, which is sent to a custom Pipeline animation processor.  Two good examples of how to do this are the Microsoft SkinnedSample project and Michael Neel’s XNA 3D Primer.  Using these references, I was able to get my player model moving in a XNA game window.

screen081

However, I found that the method I was using to generate the sprite animations was not compatible with importing for 3D animations.  The process for preparing a Blender model for export to FBX format for XNA is nicely explained in this article by StormCode.  Using that information, I was able to use the Action Editor in Blender to create a named animation.  In the 2.6 version of Blender, this is located under the DopeSheet screen.  The article also gives valuable tips on how to create a keyframe containing all bone information and how to duplicate keyframes.  Previously, I used the record button in Blender to create keyframes, but that only records the bones that are actually changed.

After exporting my model to FBX format, I was able to import it into the SkinnedSample project.  I created new Model and Animation objects for my model.  That project is covered by the Microsoft Permissive License (Ms-PL), so I think I can use that code in my own project.  However, before I do that I want to compile it into its own library, so I can just link it and pass my models to it without having to add Microsoft’s code to my own project.  The only problem I’ve seen so far is that some polygons will sometimes incorrectly overlap each other, so that is something I will need to resolve.

screen082

While I was working on the game screen, I was able to resolve the issue with the model appearing so dark.  This was because the diffuse property on the mesh effect needed to be set to White.