Model not rendering intended way

So it looks like furthest triangles render instead of closest ones. In my code below I use _model.Draw() but I already tried drawing model per mash and even using GraphicsDevice.DrawIndexedPrimitives(). Model shows correctly on blender and online viewers. Currently I’m using .dae model format but I had to import it using FBX importer because of another issue. FBX format has the same issue. I also tried using free sword model from store and It had same issue. Found guy with same problem with XNA, post made 11 years ago. He was adviced to enable depthBuffer but this solution did not help me.

namespace VideoGame
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch; 
        public World _world;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            _graphics.PreferHalfPixelOffset = true;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            GraphicsDevice.SetRenderTarget(new RenderTarget2D(
                GraphicsDevice, 1920, 1080,
                false, SurfaceFormat.Color,
                DepthFormat.Depth24, 6,
                RenderTargetUsage.PlatformContents));

            _graphics.PreferredBackBufferWidth = 1903;
            _graphics.PreferredBackBufferHeight = 969;
            _graphics.PreferMultiSampling = true;
            _graphics.SynchronizeWithVerticalRetrace = true;
            this.IsFixedTimeStep = false;
            this.TargetElapsedTime = TimeSpan.FromSeconds(1 / 60f);
            _graphics.ApplyChanges();
            Window.AllowUserResizing = true;
            Window.Title = "Game";

            DepthStencilState depthStencilState; depthStencilState = new DepthStencilState();
            depthStencilState.DepthBufferFunction = CompareFunction.LessEqual;
            depthStencilState.DepthBufferWriteEnable = true;
            depthStencilState.DepthBufferEnable = true;
            GraphicsDevice.DepthStencilState = depthStencilState;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _spriteBatch.Begin();

            _world = 
                new World(
                    new GameClient(Window.ClientBounds, LevelConstructors.CreateKeyBoardControls(), GameClient.GameLanguage.English),
                    new SpriteDrawer(GraphicsDevice, Content
                ));

            _world.AddLevel(new Level("Level1", LevelConstructors.LoadLevel1));
            _world.AddLevel(new Level("Remote Room", LevelConstructors.LoadRemoteRoom));
            _world.AddLevel(new Level("Menu", LevelConstructors.LoadMenu));
            _world.LoadRootLevel("Level1", Content);
            _spriteBatch.End();

            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
                GraphicsDevice.DisplayMode.AspectRatio,
                1, 10000);
            viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
            worldMatrix = Matrix.CreateWorld(camTarget, Vector3.Forward, Vector3.Up);
            
            _model = Content.Load<Model>("sword");
        }
        Vector3 camTarget = new Vector3(0f, 0f, 0f);
        Vector3 camPosition = new Vector3(0f, 0f, -10f);
        Matrix projectionMatrix;
        Matrix viewMatrix;
        Matrix worldMatrix;
        Model _model;
        protected override void Update(GameTime gameTime)
        {
            //_world.Update(gameTime.ElapsedGameTime, Window);

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                worldMatrix *= Matrix.CreateRotationX(MathHelper.ToRadians(1));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                worldMatrix *= Matrix.CreateRotationX(-1 * MathHelper.ToRadians(1));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                worldMatrix *= Matrix.CreateRotationY(MathHelper.ToRadians(1));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                worldMatrix *= Matrix.CreateRotationY(-1 * MathHelper.ToRadians(1));
            }


            viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
                GraphicsDevice.DisplayMode.AspectRatio,
                1, 10000);



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

            _spriteBatch.Begin(samplerState: SamplerState.PointClamp);

            if (_world.IsReady)
            {
                //_world.Display(_spriteBatch);
            }

            _spriteBatch.End();
            
            _model.Draw(worldMatrix, viewMatrix, projectionMatrix);

            base.Draw(gameTime);

        }

    }

image
image


image

here’s the model Itself
https://drive.google.com/drive/folders/10mmEY2Mw2U8pXMxTMgoeJIHN6T25nd67?usp=sharing

c# - Model not rendering correctly XNA - Game Development Stack Exchange XNA issue post
[SOLVED] ContentLoadException in version 3.7.0.1232 Why I couldn’t use Open Asset Import Library.

Welcome,
Could you please edit the original post to descibe the problem in detail. From the images, the third one has UV issues. Is that your problem? Or is it something else?

Without annotated images against a reference, It is hard to disinguish your images.

1 Like

Remove all spritebatch.begin and .end calls and check if the model is rendered correctly. If I remember correctly the spritebatch changes some settings of the graphics device that you have to reset before you render models.

2 Likes

Thanks a lot! I figured out that SpriteBatch. Begin actually changes a lot of GraphicDevice settings

_sortMode = sortMode;
            _blendState = blendState ?? BlendState.AlphaBlend;
            _samplerState = samplerState ?? SamplerState.LinearClamp;
            _depthStencilState = depthStencilState ?? DepthStencilState.None;
            _rasterizerState = rasterizerState ?? RasterizerState.CullCounterClockwise;
            _effect = effect;
            _spriteEffect.TransformMatrix = transformMatrix;
            if (sortMode == SpriteSortMode.Immediate)
            {
                Setup();
            }

            _beginCalled = true;

For my issue it was only DepthStencilState that matters. Here is how I modified _spriteBatch.Begin:

_spriteBatch.Begin(samplerState: SamplerState.PointClamp, depthStencilState: new DepthStencilState());

Also if you don’t want to interfere in spriteBatch you can just do this after _spriteBatch.End:

GraphicsDevice.DepthStencilState = new DepthStencilState();

In my case I want to insert 3d render call in the middle of spriteBatch rendering process just like it is some sprite. I’ll update here when I figure out how to make it possible.


image