Texture Atlas


After finding the hardware instancing example for XNA, I started working on making a library out of the code so that I could use it in my game.  This wasn’t too difficult, because I just had create a ModelDisplayer class and added methods to pass the GraphicsDevice, data array (block locations), model, bones, and camera information.  Finally, I added a Draw method which displays blocks according to the positions in the data array, using a DynamicVertexBuffer which accepts the Matrix coordinates in its SetData method.  The code is flexible enough to accept any 2D array of row and column positions for the blocks.


After building my new library, I would able to add a reference to it in my BlastingBits game and set my block to set the Instanced Model processor.


As with the SkinnedModelProcessor, it is necessary to build the library for both Windows and XBox 360.  Otherwise, it will return an obscure framework warning which will prevent the library from being used.


The new InstancedModel library seemed to work great in my initial tests, but I found two problems.  First of all, only on the XBox 360 it would randomly throw an error on the SetData method.  I don’t understand all of the technical details, but I did deduce that this error only occurred because I was calling the DrawModelHardwareInstancing method multiple times in my main Draw method.  This was basically running the instance display code three times, once for the blocks in the current room and again for the blocks in the room to the left and right.  Removing the call to display blocks in the adjacent rooms made the error go away on the XBox 360.  Therefore, I combined the block arrays of all three rooms into one array, and then just called the hardware instance draw method only once.


This seemed to work well, but it would not display my own block and texture when I imported my FBX model and PNG texture file.  In the example, it uses an FBX model, but I could never find the associated texture.  My only guess is that the texture is somehow built into the FBX file.  However, I’ve never seen an example of how to load a packed texture from an FBX file generated by Blender, so I’ve always loaded the texture file separately into the Content project.  Therefore, I was stuck with the example block model which had a texture of a cat mapped to it which is unchangeable.


Fortunately, I found a really great article at float 4×4 (no author listed) that explains a similar hardware instancing process called “texture atlas”.  This is even better than the previous code, because it allows textures for a model to be changed at runtime, and it has all the benefits of hardware instancing to eliminate slowdown.  It’s like palette swapping for a 3D model.

I modified the example code to eliminate the random zoom and spinning, so that it displays blocks at X/Y cells, similar to my game maps.  It looks good, but it doesn’t do lighting so I will need to see if it is possible to add that.


As with the previous code, I extracted all of the code specific for rendering into its own library, so that I can link to it in the game.  I only want to handle the loading of the textures in my main game code. This code does not import a model (just draws cubes), so there is no model processor.  I manually updated the vertex location to make the blocks cubes with width, height, and depth of one (-0.5f to 0.5f) Similar to what I did the the previous example, I added a method which takes a 2D array as a parameter, so I can pass the block map to the AtlasDisplay to draw the blocks.  The only thing I wasn’t able to include in the library is the FX Effect file, since it has to go into the content project.  I’m not sure how to include that in the library, unless I create a second library just for that file, which is what I may do in the future.

I setup the new TextureAtlas, Effect, and Texture2D in my main game, and passed the instance of the TextureAtlas to the GameScreen3D class.  I setup all the necessary calls, and eventually I got the new block display using the texture atlas to work.  It now renders the blocks for all three rooms, and I am getting a solid 60 FPS on the XBox 360.  One thing to watch out for is that the code has a variable containing the number of blocks to render, which I currently have set to 1000.  For three rooms, the current maximum number of blocks is 1170 (390 * 3), but most rooms will probably never have more than half of the blocks filled in the room array.  Overall, the update to use the texture atlas method for hardware instancing was quite a bit of work for little noticeable change, but it was better to go ahead and solve the slowness issue now so that it isn’t a problem once I begin designing the levels.

Instanced Model

I was feeling good about the progress I have made with the game.  However, one thing I had not tried in some time is running the game on the XBox 360.  When deploying on the XBox, I came across a few problems.  First, the XBox version of the project did not have references to the SkinnedModelProcessor.  After adding a reference, it still returned an error about the library version number.  I figured out that this was because my SkinnedModel project DLL was being compiled for Windows.  In the SkinnedModelProcessor solution, I created a Copy of Project for XBox 360 and added a reference to the DLL created from that in my XBox 360 project instance and it ran correctly.

Unfortunately, I did notice some some slight slowdown on the XBox 360, and the FPS counter usually returned somewhere between 30 and 50 FPS.  I turned off the rendering of the two adjacent rooms, and it went back up to 60 FPS.  This is one reason I don’t like developing on a super powerful computer, because problems like this don’t arise until it’s deployed on a less powerful system like the XBox 360.  After going through my model rendering code, I was able to track down the slowness to the rendering of the “blocks” which make up the game map.  I modified the code so that it renders a block for each map cell, which is the worst case scenario (15 rows * 26 columns = 390 cell blocks).  This significantly lowered the frame rate on the XBox360 to anywhere from 7 FPS to 20 FPS.  The blocks are just six sided cubes with a texture, so the meshes are not complex


After doing multiple web searches, I was able to find one thread that discusses this problem.  This led me to the Instanced Model example code from Microsoft, which is able to display thousands of models at a time with frame rate not dropping below 60 FPS, even on the XBox 360.  I knew there had to be a way to do this, since the graphics processor is capable of rendering thousands of polygons at a time.  From my previous projects in OpenGL, I learned that this is handled by creating a “display list” in OpenGL, so I figured there had to be an equivalent in XNA.

I didn’t have a chance to modify my code according to the example, since my vacation is over and I have limited time to work on game programming.  Hopefully, I’ll have a chance over the weekend to get this working correctly.  Also, I still have the level 2 and 3 armor sets to add to the game.

3D Text


Today, I worked on getting the damage numbers to display correctly over each enemy’s head.  As I was working on this, it became apparent that my coordinate system was backwards.  I had noticed this before when I was trying to get the player to move, so I just flipped the sign of the X coordinate of the player.  This was happening because the example I had looked at placed the camera view at a negative Z coordinate.  After reading up more on the XNA 3D coordinate system at Toymaker by Keith Ditchburn, I learned that the positive Z coordinate comes “out” of the screen and the negative Z coordinate goes “into” the screen.  Therefore, my camera should have had a positive Z position value.  I changed this, but now I was seeing the backside of all of my models.  This was because the modification to the SkinnedModelProcessor I made rotated the model by 90 degrees on the X-axis and 180 degrees on the Z-axis.  Removing the Z-axis rotation makes the model face forward, but it is up-side-down.  I had this problem before, which I tried to fix by using a reflection transformation on the Y-plane.  This caused all of the normals to be flipped, which didn’t appear correctly.  After some more trial and error, I found that rotating my model in the processor by -90 degrees (which is the same as 270 degrees) will rotate the model in the correct position.

After fixing the model display, I updated the enemy and player objects to return a Vector3 in a method that returns the position in world coordinates.  This will eliminate the need to do the pixel to world conversion in my GameScreen3D class.  Unfortunately, this pushes it to the “model” classes, which isn’t great either and ideally I would like to convert everything to world coordinates.  However, updating all of the collision detection code will take some time, especially since the XNA Rectangle object doesn’t support floats.

Later, I found another good article by Shawn Hargreaves which explains how to draw text in the 3D world.  The examples were fairly easy to follow, and after some effort I was able to display text values on the screen.  With a little more effort, I was able to get text to follow an enemy on the screen.  His article also describes to to make a billboard, which will display the text facing forward regardless of camera orientation.  I may look into doing that later, along with the sprite optimization suggestions in the article.  However, since the game is happily running at 60 fps I will look into optimizing that later.


Finally, I worked on modeling the third armor set and second enemy.  I just have the models completed thus far, so I will need to go back and find and apply appropriate textures before adding them to my game.