I created a class like entity model that allows me to render to texture instead using the original class (with DrawableGameComponent). I was having problems with the original class because it was rendering to the back buffer and getting in the way of the HUD and menu. Here is my code:
Code: Select all
public static class EntityModel
{
public static List<Entity> Entities = new List<Entity>();
public static Matrix Transform;
static Matrix[] boneTransforms;
static Model currentModel;
public static Model CurrentModel
{
get { return currentModel; }
set { currentModel = value; }
}
public static void Add(Entity EntitytoAdd, Model modelToAdd, Matrix transform)
{
Entities.Add(EntitytoAdd);
currentModel = modelToAdd;
Transform = transform;
if (boneTransforms != null)
boneTransforms = new Matrix[modelToAdd.Bones.Count];
foreach (ModelMesh mesh in modelToAdd.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
}
}
}
public static void Draw(Matrix viewMatrix, Matrix projectionMatrix)
{
for (int i = 0; i < Entities.Count; i++)
{
Matrix worldMatrix = Transform * Entities[i].WorldTransform;
currentModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
foreach (ModelMesh mesh in currentModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
}
mesh.Draw();
}
}
}
}
Code: Select all
space.Add(new Box(Vector3.Zero, 2000, 1f, Height));
Code: Select all
space.Add(new Box(new Vector3(0, 4, 0), 1, 1, 1, 1));
space.Add(new Box(new Vector3(0, 8, 0), 1, 1, 1, 1));
space.Add(new Box(new Vector3(0, 12, 0), 1, 1, 1, 1));
space.Add(new Box(new Vector3(0, 16, 0), 1, 1, 1, 1));
Code: Select all
foreach (Entity e in space.Entities)
{
Box box = e as Box;
if (box != null) //This won't create any graphics for an entity that isn't a box since the model being used is a box.
{
Matrix scaling = Matrix.CreateScale(box.Width, box.Height, box.Length); //Since the cube model is 1x1x1, it needs to be scaled to match the size of each individual box.
//Add the drawable game component for this entity to the game.
EntityModel.Add(e, content.Load<Model>("Game/Player/cube"), scaling);
}
}
Code: Select all
EntityModel.Draw(viewMatrix, projectionMatrix);
But my ground still does not render. All the other boxes are there, and I know my ground is there because the boxes look like they are sitting on an invisible box.
http://img829.imageshack.us/img829/7596/kams36011.png
Any help???