Question

So i'm trying to make a 3d game with 2d sprites. In blender I created a plane and used uv maps to map one of the sprites to the plane. When I push f12 and render the plane, the sprite is displayed.

I made sure to create a material for the plane and I made sure to add the texture as well as enabled uv mapping with the correct uv map.

I exported the file model as .fbx file and put this along with the texture image in the content folder of my project.

However when I render my model instead of the texture being displayed the plane is solid black.

What could be causing this? My draw looks like this:

public void Draw(Matrix View, Matrix Projection)
{
    // Calculate the base transformation by combining
    // translation, rotation, and scaling
    Matrix baseWorld = Matrix.CreateScale(Scale)
    * Matrix.CreateFromYawPitchRoll(
    Rotation.Y, Rotation.X, Rotation.Z)
    * Matrix.CreateTranslation(Position);

    foreach (ModelMesh mesh in Model.Meshes)
    {
        Matrix localWorld = modelTransforms[mesh.ParentBone.Index]
        * baseWorld;
        foreach (ModelMeshPart meshPart in mesh.MeshParts)
        {
            BasicEffect effect = (BasicEffect)meshPart.Effect;

            effect.World = localWorld;
            effect.View = View;
            effect.Projection = Projection;
            effect.EnableDefaultLighting();
        }
        mesh.Draw();
    }
}
Was it helpful?

Solution

I figured it out. The reason my texture is not being displayed on my model is because I am not setting effect.world correctly

i changed the way i set the world matrix and edited some of my model class code. In case anyone else wants to render textured models with a nice class

public class CModel
{
    public Matrix Position { get; set; }
    public Vector3 Rotation { get; set; }
    public Vector3 Scale { get; set; }
    public Model Model { get; private set; }
    private Matrix[] modelTransforms;




  public CModel(Model Model, Vector3 Position)
  {
    this.Model = Model;
    modelTransforms = new Matrix[Model.Bones.Count];
    Model.CopyAbsoluteBoneTransformsTo(modelTransforms);
    this.Position = Matrix.CreateTranslation(Position);

  }

  public void Draw(Matrix viewMatrix, Matrix proMatrix)
  {

    foreach (ModelMesh mesh in Model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();

            effect.View = viewMatrix;
            effect.Projection = proMatrix;
            effect.World = modelTransforms[mesh.ParentBone.Index] * Position;
        }

        mesh.Draw();
    }
  }

Draws the model at the specified position. If you want any other effects you can easily add them in to the draw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top