0
\$\begingroup\$

community. Just started playing with Monogame and stuck with a quite simple, but still unanswered issue, at least I couldn't find any answer.

The model rendered in Monogame (XNA) is not overlapping with its own parts: enter image description here Originally in Blender, it looks OK: enter image description here

The same in Windows 3D Viewer: enter image description here

I believe it depends on the order I'm rendering bones:

private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
    {
        Matrix[] transforms = new Matrix[model.Bones.Count];
        model.CopyAbsoluteBoneTransformsTo( transforms );
        var fixBlenderRotation = Matrix.CreateRotationZ(MathHelper.ToRadians(90));
        var scale = Matrix.CreateScale(0.1f);
        //for (int i = model.Meshes.Count; i > 0; i--) // -> this looks OK
        for (int i = 1; i <= model.Meshes.Count; i++)  // -> this looks BAD
        {
            ModelMesh mesh = model.Meshes[i - 1];
            foreach (var effect in mesh.Effects)
            {
                if (effect is BasicEffect basicEffect)
                {
                    basicEffect.EnableDefaultLighting();
                    basicEffect.Alpha = 1;
                    basicEffect.View = view;
                    basicEffect.Projection = projection;
                    var boneTransform = transforms[mesh.ParentBone.Index];
                    basicEffect.World = (world  * boneTransform * scale *  Matrix.CreateTranslation(_modelPosition)) * fixBlenderRotation;
                }
            }

            mesh.Draw();
        }
    }

Depth buffer is enabled:

protected override void Initialize()
    {
        _graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        _world = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
        var cameraPosition = new Vector3(_radius, 0, 0);
        _camera = new FpsCamera(this, cameraPosition, Vector3.Zero, Vector3.Up);
        Components.Add(_camera);
        base.Initialize();
    }

So, cannot figure out how other tools render my model correctly while monogame is messing things up? The hierarchy of objects in Blender is correct:

enter image description here

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The fix is really simple, just need to move the following line of code:

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

from void Initialize() method to void Draw(GameTime gameTime):

protected override void Draw(GameTime gameTime)
{
      GraphicsDevice.Clear(Color.CornflowerBlue);
      GraphicsDevice.DepthStencilState = DepthStencilState.Default;
      DrawModel(_car, _world, _view, _projection);
      base.Draw(gameTime);
}

And that has fixed the issue:

correct render

Here I have found this solution.

\$\endgroup\$

You must log in to answer this question.

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