unwanted x-ray effect or objects are drawn in order

I am here drawing some meshes generated during the game Initialize () sphere, cube, octahedral
the thing is, as you can see objects appear with what looks like an x-ray effect I don’t really know how to describe it but I think it’s more related to the order of which the objects are drawn also the wireframe is visible throw any opject. also the cube center is (2.0.2) so the grid should intersect with it, but however i look at it the cube appears above the grid .
How to disable that? Or make the game ignore the draw order so the scene would look like any normal 3d scene ?




//Game Class usnig MonoGame.Forms
public class ViewPort : InvalidationControl
{
   
    Color MegaGray = new Color(59, 59, 59, 255);
    int gridSize = 6;
    public BasicEffect basicEffect;
    public ArcBallCamera _camera;
    public MouseComponent _mouseComponent;
    public KeyboardComponent _keyboardComponent;
    public Matrix WorldMatrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
    private MeshObject testshape;
    private MeshObject testshape2;
    private MeshObject testshape3;
    private Grid _Grid;
    private Axis _Axis;
    public ResourceLibary resourceLibary;
    public GraphicsDevice Device
    { 
        get{return Editor.GraphicsDevice;}

    }
    public GameComponentCollection Components
    {
        get { return Editor.Components; }
    }
    protected override void Initialize()
    {
        if (this.DesignMode)
        {
            return;
        }
        Editor.Content = new ContentManager(this.Services, "Content\\BuiltContent");
        _mouseComponent = new MouseComponent(this);
        _keyboardComponent = new KeyboardComponent(this);
        _camera = new ArcBallCamera(Device, _keyboardComponent, _mouseComponent);
        basicEffect = new BasicEffect(Editor.GraphicsDevice);
        resourceLibary=new ResourceLibary(this);
      //graphicsCard has no real use at the moment and can be ignored 
        GraphicsCardGeometry graphicsCard = new GraphicsCardGeometry(Editor.GraphicsDevice);
        _Grid = new Grid(this,graphicsCard, Enums.Up.Y, Color.WhiteSmoke, gridSize);
        _Axis = new Axis(this, graphicsCard);
        testshape = new Sphere(this, graphicsCard, Color.BlueViolet, Vector3.Zero, 1);
        testshape2 = new Cube(this, graphicsCard, new Vector3(2,0,2), Vector3.UnitX, Vector3.UnitY, 1, Color.Brown);
        testshape2.GetMeshWireFrame();
        testshape2.RenderWireFrame = true;
        testshape3 = new Octahedron(this, graphicsCard, Vector3.Zero, Vector3.UnitY, Color.Blue, 0.2f, 0.2f);
       
    }
   
    public void Update(GameTime gameTime)
    {
        if (this.DesignMode)
        {
            return;
        }
        _mouseComponent.Update(gameTime);
        _keyboardComponent.Update(gameTime);
        _camera.Update(gameTime);
    }

    protected override void Draw()
    {
        Editor.GraphicsDevice.Clear(MegaGray);
        DepthStencilState depthBufferState = new DepthStencilState();
        depthBufferState.DepthBufferEnable = true;
        depthBufferState.DepthBufferFunction = CompareFunction.LessEqual;
        Device.DepthStencilState = depthBufferState;

        basicEffect.VertexColorEnabled = true;
        basicEffect.View = _camera.ViewMatrix;
        basicEffect.Projection = _camera.ProjectionMatrix;
        basicEffect.World = WorldMatrix;

        //  GraphicsDevice.BlendState = BlendState.Additive;
        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
        }
       

        Editor.spriteBatch.Begin();

       

        Editor.spriteBatch.End();
    }
  


}
// the creation of the sphere , other objects are generated in similar way 
    public class Sphere : MeshObject
    {
      
       

        public Sphere(ViewPort view ,IGraphicsCardGeometry context ,  Color color, Vector3 position, float radius, int thetaDiv = 32, int phiDiv = 32)
        {
            _world = view;
            Positions = new();
            Normals = new();
            Colors = new();
            Indices = new();
            TextureCoordinates = new();
            Context = context;
            DrawOrder =(int)ComponentDrawOrderEnum.RenderEngine;
            _state = view.resourceLibary.solidState;
            this.AddEllipsoid(position, color,radius, radius, radius , thetaDiv,phiDiv);
            _world.Components.Add(this);

        }



         
    }
        public static void AddEllipsoid(this ObjectModel mesh,Vector3 center,Color color ,float radiusx, double radiusy, double radiusz, int thetaDiv = 20, int phiDiv = 10)
        {
            var index0 = mesh.Positions.Count;
            var dt = 2 * (Single)Math.PI / thetaDiv;
            var dp = (Single)Math.PI / phiDiv;

            for (var pi = 0; pi <= phiDiv; pi++)
            {
                var phi = pi * dp;

                for (var ti = 0; ti <= thetaDiv; ti++)
                {
                    // we want to start the mesh on the x axis
                    var theta = ti * dt;

                    // Spherical coordinates
                    // http://mathworld.wolfram.com/SphericalCoordinates.html
                    var x = (Single)Math.Cos(theta) * (Single)Math.Sin(phi);
                    var y = (Single)Math.Sin(theta) * (Single)Math.Sin(phi);
                    var z = (Single)Math.Cos(phi);

                    var p = new Vector3((float)(center.X + (Single)(radiusx * x)), (float)(center.Y + (Single)(radiusy * y)), (float)(center.Z + (Single)(radiusz * z)));
                    mesh.Positions.Add(p);
                    mesh.Colors.Add(color);
                    if (mesh.Normals != null)
                    {
                        var n = new Vector3(x, y, z);
                        mesh.Normals.Add(n);
                    }

                    if (mesh.TextureCoordinates != null)
                    {
                        var uv = new Vector2(theta / (2 * (Single)Math.PI), phi / (Single)Math.PI);
                        mesh.TextureCoordinates.Add(uv);
                    }
                }
            }

            AddRectangularMeshTriangleIndices(mesh, index0, phiDiv + 1, thetaDiv + 1, true);
        }
        public static void AddRectangularMeshTriangleIndices(ObjectModel mesh, int index0, int rows, int columns, bool isSpherical = false)
        {
            for (var i = 0; i < rows - 1; i++)
            {
                for (var j = 0; j < columns - 1; j++)
                {
                    var ij = (i * columns) + j;
                    if (!isSpherical || i > 0)
                    {
                        mesh.Indices.Add(index0 + ij);
                        mesh.Indices.Add(index0 + ij + 1 + columns);
                        mesh.Indices.Add(index0 + ij + 1);
                    }

                    if (!isSpherical || i < rows - 2)
                    {
                        mesh.Indices.Add(index0 + ij + 1 + columns);
                        mesh.Indices.Add(index0 + ij);
                        mesh.Indices.Add(index0 + ij + columns);
                    }
                }
            }
        }

//the parent calss MeshObject's Draw method that drives from IDrawable, IGameComponent, //IUpdateable
   public override void Draw(GameTime gameTime)
   {
       
       foreach (EffectPass pass in _world.basicEffect.CurrentTechnique.Passes)
       {
           pass.Apply();
       }
       if (_world != null && Vertices != null)
       {
           if(_state != null)
           {
               _world.Device.RasterizerState = _state;
           }
          
           _world.Device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, Vertices.PositionColors, 0, Vertices.PositionColors.Length, Indices.ToArray(), 0, Indices.Count / 3);
           if (RenderWireFrame && MeshEdgs != null)
           {
               _world.Device.DrawUserPrimitives<VertexPositionColor>(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, MeshEdgs, 0, MeshEdgs.Length / 2);
           }
           if (ShowFaceNormals && FaceNormals != null)
           {
               _world.Device.DrawUserPrimitives<VertexPositionColor>(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, FaceNormals, 0, FaceNormals.Length / 2);
           }
          
       }
       base.Draw(gameTime);
   }

It’s not writing it’s depth, is the DepthStencilState - None?

Set it to default.

i.e. GraphicsDevice.DepthStencilState = DepthStencilState.Default;

3 Likes