XNA倍增立方体

时间:2014-06-20 17:39:34

标签: c# xna voxel

首先,原谅我的英语,我是法国人...... 我写了一个不起作用的代码。 我想绘制一个像Minecraft一样的多个块,但是在Voxel中。 在我的地图的一侧,结果很好:

This render is opaque, good !

但另一方面,我得到了这个:

But in this side of my map, it's no good :(

有些面孔出现在其他面孔上...... 谁有想法?我的常规?

我的函数Remake()未被使用,它用于删除面...

    namespace LandOfCube.Models
{
    class Block
    {
        VertexBuffer instanceBuffer;
       public enum Form
        {
            all,
            noUp,
            noDown,
            noRight,
            noLeft,
            noFront,
            noBack
        }
       public Form cubeForm = Form.all;

        GraphicsDevice Graphics;
        Effect shader;
        private VertexBuffer vb;
        Color CubeColor;
        public Vector3 pos;

        VertexPositionColorNormal[] vertices;
        VertexPositionColorNormal[] verticesNoUp;
        VertexPositionColorNormal[] verticesNoDown;
        VertexPositionColorNormal[] verticesNoFront;
        VertexPositionColorNormal[] verticesNoBack;
        VertexPositionColorNormal[] verticesNoRight;
        VertexPositionColorNormal[] verticesNoLeft;


        private VertexPositionColorNormal[] Front = new VertexPositionColorNormal[5];
        private VertexPositionColorNormal[] Back = new VertexPositionColorNormal[5];
        private VertexPositionColorNormal[] Right = new VertexPositionColorNormal[5];
        private VertexPositionColorNormal[] Up = new VertexPositionColorNormal[5];
        private VertexPositionColorNormal[] Left = new VertexPositionColorNormal[5];
        private VertexPositionColorNormal[] Down = new VertexPositionColorNormal[5];

        public Vector3 normalFront = Vector3.Forward;
        public Vector3 normalBack = Vector3.Backward;
        public Vector3 normalTop = Vector3.Up;
        public Vector3 normalBottom = Vector3.Down;
        public Vector3 normalLeft = Vector3.Left;
        public Vector3 normalRight = Vector3.Right;

        public Block(GraphicsDevice graph, Color color, Vector3 position, ContentManager content)
        {


            Graphics = graph;

            shader = content.Load<Effect>("effects");
            CubeColor = color;
            pos = position;

            Init();
            SetVertices();

        }





        bool test = false;
        public void Draw(Camera camera)
        {



            Graphics.SetVertexBuffer(vb);

            shader.CurrentTechnique = shader.Techniques["Colored"];


            shader.Parameters["xView"].SetValue(camera.View);
            shader.Parameters["xProjection"].SetValue(camera.Projection);
            shader.Parameters["xWorld"].SetValue(Matrix.Identity);
            Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
            lightDirection.Normalize();
            shader.Parameters["xLightDirection"].SetValue(lightDirection);
            shader.Parameters["xAmbient"].SetValue(0.1f);
            shader.Parameters["xEnableLighting"].SetValue(true);


            foreach (EffectPass pass in shader.CurrentTechnique.Passes)
            {

                pass.Apply();
                this.Graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
            }




        }








        #region Methode abstraites

        private void Init()
        {

            InitFront();
            InitBack();
            InitDown();
            InitUp();
            InitLeft();
            InitRight();
        }



        public void Remake()
        {
            switch (cubeForm)
            {
                case Form.noBack:
                    vertices = verticesNoBack;
                    break;
                case Form.noFront:
                    vertices = verticesNoFront;
                    break;
                case Form.noUp:
                    vertices = verticesNoUp;
                    break;
                case Form.noDown:
                    vertices = verticesNoDown;
                    break;
                case Form.noRight:
                    vertices = verticesNoRight;
                    break;
                case Form.noLeft:
                    vertices = verticesNoLeft;
                    break;
            }
            vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None);
            vb.SetData(vertices);

            verticesNoBack = null;
            verticesNoDown = null;
            verticesNoFront = null;
            verticesNoLeft = null;
            verticesNoRight = null;
            verticesNoUp = null;

            test = true;
        }




        public void SetVertices()
        {
            this.vertices = new VertexPositionColorNormal[36];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }

            setNoBack();
            setNoFront();
            setNoUp();
            setNoDown();
            setNoRight();
            setNoLeft();

            vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None);
            vb.SetData(vertices);
            Clean();
        }

        #region InitFaces

        public void setNoBack()
        {
            this.verticesNoBack = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }
        }
        public void setNoFront()
        {
            this.verticesNoFront = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }
        }
        public void setNoUp()
        {
            this.verticesNoUp = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }
        }
        public void setNoDown()
        {
            this.verticesNoFront = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }
        }
        public void setNoLeft()
        {
            this.verticesNoLeft = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Right[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }
        }
        public void setNoRight()
        {
            this.verticesNoRight = new VertexPositionColorNormal[30];
            int y = 0;
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Up[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Down[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Back[x]; y++;
            }

            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Front[x]; y++;
            }
            for (int x = 0; x < 6; x++)
            {
                this.vertices[y] = Left[x]; y++;
            }
        }


        public void InitFront()
        {
            this.Front = new VertexPositionColorNormal[6];

            Front[0].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
            Front[0].Color = Color.Blue;
            Front[0].Normal = normalFront;
            Front[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
            Front[1].Color = Color.Blue;
            Front[1].Normal = normalFront;
            Front[2].Position = new Vector3(pos.X, pos.Y, pos.Z);
            Front[2].Color = Color.Blue;
            Front[2].Normal = normalFront;


            Front[3].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
            Front[3].Color = Color.Blue;
            Front[3].Normal = normalFront;
            Front[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
            Front[4].Color = Color.Blue;
            Front[4].Normal = normalFront;
            Front[5].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
            Front[5].Color = Color.Blue;
            Front[5].Normal = normalFront;
        }
        public void InitBack()
        {
            this.Back = new VertexPositionColorNormal[6];

            Back[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
            Back[0].Color = Color.Red;
            Back[0].Normal = normalBack;
            Back[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Back[1].Color = Color.Red;
            Back[1].Normal = normalBack;
            Back[2].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
            Back[2].Color = Color.Red;
            Back[2].Normal = normalBack;


            Back[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
            Back[3].Color = Color.Red;
            Back[3].Normal = normalBack;
            Back[4].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Back[4].Color = Color.Red;
            Back[4].Normal = normalBack;
            Back[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
            Back[5].Color = Color.Red;
            Back[5].Normal = normalBack;
        }
        public void InitUp()
        {
            this.Up = new VertexPositionColorNormal[6];

            Up[0].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Up[0].Color = Color.Black;
            Up[0].Normal = normalTop;
            Up[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
            Up[1].Color = Color.Black;
            Up[1].Normal = normalTop;
            Up[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
            Up[2].Color = Color.Black;
            Up[2].Normal = normalTop;


            Up[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Up[3].Color = Color.Black;
            Up[3].Normal = normalTop;
            Up[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
            Up[4].Color = Color.Black;
            Up[4].Normal = normalTop;
            Up[5].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
            Up[5].Color = Color.Black;
            Up[5].Normal = normalTop;
        }
        public void InitDown()
        {
            this.Down = new VertexPositionColorNormal[6];

            Down[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
            Down[0].Color = Color.Orange;
            Down[0].Normal = normalBottom;
            Down[1].Position = new Vector3(pos.X, pos.Y, pos.Z);
            Down[1].Color = Color.Orange;
            Down[1].Normal = normalBottom;
            Down[2].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
            Down[2].Color = Color.Orange;
            Down[2].Normal = normalBottom;


            Down[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
            Down[3].Color = Color.Orange;
            Down[3].Normal = normalBottom;
            Down[4].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
            Down[4].Color = Color.Orange;
            Down[4].Normal = normalBottom;
            Down[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
            Down[5].Color = Color.Orange;
            Down[5].Normal = normalBottom;
        }
        public void InitRight()
        {
            this.Right = new VertexPositionColorNormal[6];

            Right[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
            Right[0].Color = Color.Green;
            Right[0].Normal = normalRight;
            Right[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
            Right[1].Color = Color.Green;
            Right[1].Normal = normalRight;
            Right[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
            Right[2].Color = Color.Green;
            Right[3].Normal = normalRight;


            Right[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
            Right[3].Color = Color.Green;
            Right[3].Normal = normalRight;
            Right[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
            Right[4].Color = Color.Green;
            Right[4].Normal = normalRight;
            Right[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
            Right[5].Color = Color.Green;
            Right[5].Normal = normalRight;
        }
        public void InitLeft()
        {
            this.Left = new VertexPositionColorNormal[6];

            Left[0].Position = new Vector3(pos.X, pos.Y, pos.Z);
            Left[0].Color = Color.Aqua;
            Left[0].Normal = normalLeft;
            Left[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Left[1].Color = Color.Aqua;
            Left[1].Normal = normalLeft;
            Left[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
            Left[2].Color = Color.Aqua;
            Left[2].Normal = normalLeft;


            Left[3].Position = new Vector3(pos.X, pos.Y, pos.Z);
            Left[3].Color = Color.Aqua;
            Left[3].Normal = normalLeft;
            Left[4].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
            Left[4].Color = Color.Aqua;
            Left[4].Normal = normalLeft;
            Left[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
            Left[5].Color = Color.Aqua;
            Left[5].Normal = normalLeft;
        }

        #endregion
        private void setTriangleNormal(VertexPositionColorNormal v1, VertexPositionColorNormal v2, VertexPositionColorNormal v3)
        {

            Vector3 side1 = v1.Position - v3.Position;
            Vector3 side2 = v1.Position - v2.Position;
            Vector3 normal = Vector3.Cross(side1, side2);

            v1.Normal += normal;
            v2.Normal += normal;
            v3.Normal += normal;

            v1.Position.Normalize();
            v1.Position.Normalize();
            v1.Position.Normalize();
        }



        public void Clean()
        {
            Front = null;
            Back = null;
            Right = null;
            Left = null;
            Up = null;
            Down = null;

        }
        #endregion

    }
}

1 个答案:

答案 0 :(得分:1)

我的猜测是你将方向盘背面的顶点向错误的方向缠绕,使它们的正常点指向立方体内部而不是外部。 3d代码中一个非常常见的优化是仅绘制具有面向相机的法线的三角形。