Search code examples
c#3dxnameshvertex

Why my Images flickers in my xna project?


I have some problems on the display of my model with texture. Everything works perfectly, however, I use a loop to repeat the texture to represent a floor 20 X 20 on the screen. My texture is repeated correctly. But I do not understand why all my textures generate a flicker ... I noticed that images are superimposed on each other. I'm sure I checked my loop is coded correctly.

see screenshot: enter image description here

my code (loop function generation ground):

//Function draw - ground land
    private void draw_groundLand(Vector3 position_model_origin)
    {
        //example generation mode 4x4 cubes
        int[,,] MatriceWorldCube = new int[1,2,2];
        MatriceWorldCube[0, 0, 0] = 1;
        MatriceWorldCube[0, 0, 1] = 1;
        MatriceWorldCube[0, 1, 0] = 2;
        MatriceWorldCube[0, 1, 1] = 1;

        int height = MatriceWorldCube.GetLength(0);
        int width = MatriceWorldCube.GetLength(1);
        int length = MatriceWorldCube.GetLength(2);

        Vector3 pos_reference = position_model_origin;

        for (int thickness = 0; thickness < height; thickness ++)
        {

            for (int column = 0; column < width; column ++)
            {

                for (int line = 0; line < length ; line ++)
                {


                        // Copy any parent transforms.
                        Matrix[] transforms = new Matrix[model_ground_land1.Bones.Count];
                        model_ground_land1.CopyAbsoluteBoneTransformsTo(transforms);

                        // Draw the model. A model can have multiple meshes, so loop.
                        foreach (ModelMesh mesh in model_ground_land1.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.CreateRotationY(cubeGroundLand1_modelRotation) * Matrix.CreateTranslation(position_model_origin);
                                effect.View = View;
                                effect.Projection = Projection;


                            }

                            // Draw the mesh, using the effects set above.
                            mesh.Draw();
                        }



                    position_model_origin.X = (float)(line +1);
                }
                position_model_origin.X = pos_reference.X;
                position_model_origin.Z = (float)(column +1);

            }
            position_model_origin.Z = pos_reference.Z;
            position_model_origin.Y = (float)(thickness+1);

        }
        position_model_origin.Y = pos_reference.Y;
        position_model_origin = pos_reference;
    }

Thank you in advance for your help. I lose patience (over a whole weekend ^ ^)


Solution

  • It's Z-fighting. Z-buffer precision falling with distance so far objects have more "flickers" GPU can't figure out which polygon is on top because tail of the z-buffer value isn't precise enough to distinguish 2 almost equal values.

    You have 6 ways to fix it:

    1. Move geometry or do not display part that is below.

    2. Use something like polygon offset in OpenGL

    3. Use CPU z-sorting instead of z-buffer.

    4. Use only one object with 2 textures instead of 2 objects + some shaders (i don't know what exactly you are trying to achieve)

    5. Use larger z-buffer.

    6. Move clip planes closer to each other it will increase precision.