5
\$\begingroup\$

I'm quite new to working with 3D objects. I'm currently trying to get one to load in Monogame. The object itself was imported into Blender and exported as an fbx file (which was the only file type I could get the new content pipeline to work with).

The command to load the model is here:

var _model = content.Load<Microsoft.Xna.Framework.Graphics.Model>("Ship");

And the code to draw it is:

public override void Draw(IDrawParameters p)
{
    // Copy any parent transforms.
    Matrix[] transforms = new Matrix[_model.Bones.Count];
    _model.CopyAbsoluteBoneTransformsTo(transforms);

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in _model.Meshes)
    {
        // This is where the mesh orientation is set, as well 
        // as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationX(_modelRotation) *
                Matrix.CreateRotationY(_modelRotation);
            effect.View = Matrix.CreateLookAt(eye, Vector3.Zero,
              Vector3.Up);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
              MathHelper.ToRadians(45.0f), _aspectRatio, 1.0f, 10000.0f);
        }
        // Draw the mesh, using the effects set above.
        mesh.Draw();
    }
    base.Draw(p);
}

I've tried a number of variations of the draw code, mostly taken from the web. However, whatever I try, I get nothing appearing on the screen - no error, but nothing on the screen.

Is there anything specific in the code below that might cause this; or, is there a possibility that the content.Load didn't work, but it just didn't tell me?

UPDATE

More than one of the answers below are correct in my case. The first problem that I had was the transform:

effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationX(_modelRotation) *
                Matrix.CreateRotationY(_modelRotation);

The second issue was with the Alpha flag; both of these together made it show, but only because I'd written a routine to change the eye position - the original rendering was inside the object.

\$\endgroup\$
3
  • \$\begingroup\$ Try to change the camera position, or eye as you call it in your code, other than that, the code seems fine to me. \$\endgroup\$
    – dimitris93
    Commented Aug 8, 2015 at 21:10
  • \$\begingroup\$ Also try playing around with the world scale. The fbx model might not be the size you expect it to be. \$\endgroup\$ Commented Aug 9, 2015 at 20:17
  • 1
    \$\begingroup\$ Make sure that the camera is not inside (like at the same position) of the object, otherwise it will be looking from the inside of the object outward, which is like looking at a bunch of culled triangles, aka nothing. \$\endgroup\$
    – Superdoggy
    Commented Aug 11, 2015 at 21:27

3 Answers 3

4
+50
\$\begingroup\$

I had a similar problem while trying to render a skybox. Mine was only a problem with .x files, but you might find some luck with this. It seems that the alpha is set to 0 on the rendering. My code inside the BasicEffect loop looked like this from XNA:

currentEffect.LightingEnabled = false;
currentEffect.PreferPerPixelLighting = false;
currentEffect.World = modelTransforms[mesh.ParentBone.Index]
 * Matrix.CreateTranslation(position)
 * Matrix.CreateScale(0.05f);
currentEffect.View = camera.ViewMatrix;
currentEffect.Projection = camera.ProjectionMatrix;

In order for the skybox to show I had to add this line to show it:

currentEffect.Alpha = 1;

It solved my problem. From what you've said in your question, you saw NOTHING when you rendered. Same with me, so it could be a problem with the effect.

\$\endgroup\$
2
\$\begingroup\$

Since it looks like your model is loaded correctly by the pipeline, this is the most barebones solution that should work to draw it(I've just tested it). It is built with VS2015 and the most recent MonoGame DirectX template using the standard blender cube.

public class Game1 : Game
{
    GraphicsDeviceManager graphics;     
    private Model model;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void LoadContent()
    {           
        model = Content.Load<Microsoft.Xna.Framework.Graphics.Model>("box");
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        foreach (ModelMesh mesh in model.Meshes)
        {              
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();
                effect.World = Matrix.CreateTranslation(new Vector3(0, 0, 0));
                effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
                effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 1.6f, 0.1f, 10000.0f);
            }

            mesh.Draw();
        }

        base.Draw(gameTime);
    }
}
\$\endgroup\$
0
\$\begingroup\$

If the content load failed you should have got an error. But it's easy to check if something was loaded. Set a breakpoint and open up _model, you should have at least one ModelMesh within it, and it should contain at least one MeshPart.

Have you checked if there's any effects to iterate in your rendering loop? Otherwise you might need to add a BasicEffect to each mesh at load time.

\$\endgroup\$
4
  • \$\begingroup\$ Yes, it definitely loads. It has one mesh and one effect. I've tried with various different objects, including the default cube you get with Blender. I've tried changing the object to force an error, and I get one as expected when it can't load. I've tried various recompiles and configurations in the new Monogame Pipeline tool... nothing seems to make a difference. \$\endgroup\$ Commented Aug 11, 2015 at 20:57
  • \$\begingroup\$ Have you tried drawing a simple triangle to see if it's really the model that's the problem? \$\endgroup\$ Commented Aug 11, 2015 at 21:03
  • \$\begingroup\$ Yes, I can get a primitive shape to appear np \$\endgroup\$ Commented Aug 11, 2015 at 21:20
  • \$\begingroup\$ Can you show us the main draw call? From where public override void Draw(IDrawParameters p) is called. \$\endgroup\$ Commented Aug 11, 2015 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .