Пример #1
0
        private bool CheckValue(int x, int y, int z, Block[,,] cubeBlocks, int size)
        {
            if (x < 0 || y < 0 || z < 0 || x >= size || y >= size || z >= size || cubeBlocks[x, y, z].Material is AirMaterial)
                return true;

            return false;
        }
Пример #2
0
        public Ch0nkWorld(Game game)
        {
            _realm = new Realm();

            _dimension = _realm.Dimensions[0];

            _dimension.GenerateAt(new Vector3i(0, 0, 0));

            _game = game;

            _realm.Dimensions[0].ChangeMaterial(new BoundingSphere(new Vector3i(32, 32, 64), 20), new AirMaterial());
            _realm.Dimensions[0].ChangeMaterial(new BoundingSphere(new Vector3i(0, 32, 64), 20), new SandMaterial());
            _realm.Dimensions[0].ChangeMaterial(new BoundingSphere(new Vector3i(0, 64, 32), 20), new StoneMaterial());

            //List<Block> blocks = new List<Block>();
            //blocks.Add(new Block(c, new Vector3b(0, 0, 0), new GrassMaterial(), 1));
            /*Stopwatch watch = new Stopwatch();
            watch.Start();
            blocks = _dimension.GetAllBlocks();*/

            List<Block> blocks = _dimension.GetRandomTestingBlocks(new Vector3i());

            Stopwatch watch = new Stopwatch();

            Block[,,] cubeBlocks = new Block[64,64,64];
            foreach (Block block in blocks)
            {
                cubeBlocks[block.RelativePosition.X, block.RelativePosition.Y, block.RelativePosition.Z] = block;
            }

            int size = 32;
            watch.Start();
            for (int i = 0; i < size; i++)
                for (int j = 0; j < size; j++)
                    for (int k = 0; k < size; k++)
                    {
                        cubeBlocks[i, j, k].ViewFacesArray = new BitArray(6, false);
                        cubeBlocks[i, j, k].ViewFaces = 0x00;

                        //back and front
                        cubeBlocks[i, j, k].ViewFacesArray.Set(0, CheckValue(i, j - 1, k, cubeBlocks, size));
                        cubeBlocks[i, j, k].ViewFacesArray.Set(1, CheckValue(i, j + 1, k, cubeBlocks, size));

                        //left and right
                        cubeBlocks[i, j, k].ViewFacesArray.Set(2, CheckValue(i + 1, j, k, cubeBlocks, size));
                        cubeBlocks[i, j, k].ViewFacesArray.Set(3, CheckValue(i - 1, j, k, cubeBlocks, size));

                        //bottom and top
                        cubeBlocks[i, j, k].ViewFacesArray.Set(4, CheckValue(i, j, k - 1, cubeBlocks, size));
                        cubeBlocks[i, j, k].ViewFacesArray.Set(5, CheckValue(i, j, k + 1, cubeBlocks, size));

                        /*
                        //back and front
                        cubeBlocks[i, j, k].ViewFaces |= CheckValue(i - 1, j, k, cubeBlocks);
                        cubeBlocks[i, j, k].ViewFaces |= (byte)(CheckValue(i + 1, j, k, cubeBlocks) << 1);

                        //left and right
                        cubeBlocks[i, j, k].ViewFaces |= (byte)(CheckValue(i, j - 1, k, cubeBlocks) << 3);
                        cubeBlocks[i, j, k].ViewFaces |= (byte)(CheckValue(i, j + 1, k, cubeBlocks) << 2);

                        //bottom and top
                        cubeBlocks[i, j, k].ViewFaces |= (byte)(CheckValue(i, j, k-1, cubeBlocks) << 4);
                        cubeBlocks[i, j, k].ViewFaces |= (byte)(CheckValue(i, j, k+1, cubeBlocks) << 5);
                         * */
                    }
            Console.WriteLine(watch.ElapsedMilliseconds);

                /*List<Block>[,] verticalBlocks = new List<Block>[64,64];

                for (int i = 0; i < 64; i++ )
                {
                    for (int j = 0; j < 64; j++)
                    {
                        for (int k = 0; k < 64; k++)
                        {
                            if(verticalBlocks[i,j] == null)
                            {
                                verticalBlocks[i,j] = new List<Block>();
                                verticalBlocks[i,j].Add(cubeBlocks[i,j,k]);
                            }
                            else
                            {
                                Block lastBlock = verticalBlocks[i, j].Last();
                                if(lastBlock.Material.Equals(cubeBlocks[i,j,k].Material))
                                {
                                    lastBlock.Size += 1;
                                }
                                else
                                {
                                    verticalBlocks[i, j].Add(cubeBlocks[i, j, k]);
                                }
                            }
                        }
                    }
                }

                blocks = new List<Block>();

                foreach (List<Block> verticalBlock in verticalBlocks)
                {
                    blocks.Add(verticalBlock.First());
                }*/

                //blocks = new List<Block>();

            /*for (int i = 0; i < 64; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    for (int k = 0; k < 64; k++)
                    {
                        if((i == 0 || i == 63) ||
                            (j == 0 || j == 63) ||
                            (k == 0 || k == 63))
                        {
                            blocks.Add(cubeBlocks[i, j, k]);
                        }
                    }
                }
            }*/

            /*Face face = new Face();
            for (int j = 0; j < 64; j++)
            {
                for (int k = 0; k < 64; k++)
                {
                    if (cubeBlocks[0, j, k].Material is AirMaterial)
                    {
                        for(int i = 0; i < 64; i++)
                        {
                             if (!(cubeBlocks[i, j, k].Material is AirMaterial))
                             {
                                 face.data[j, k] = cubeBlocks[i, j, k];
                                 break;
                             }
                        }
                    }
                    else
                        face.data[j, k] = cubeBlocks[0, j, k];
                }
            }*/

            /*Face[] faces = new Face[6];
            for (int i = 0; i < 6; i++)
            {
                faces[i] = new Face();
                for (int j = 0; j < 64; j++)
                {
                    for (int k = 0; k < 64; k++)
                    {
                        faces[i].data[i,j]
                    }
                }
                faces[i].data[]
            }*/

            //blocks.AddRange(face.GetBlocks());

            //Console.WriteLine(watch.ElapsedMilliseconds);
            //_buffer = new DrawableBuffer(game, blocks);
            _buffer = new DrawableFaceBuffer(game, blocks);
            //Console.WriteLine(watch.ElapsedMilliseconds);
        }
Пример #3
0
        private void CreateCube(Block block, ref List<VertexNormalTexture> customVertexList, ref List<int> indicesList, ref int indexerValue)
        {
            Vector3 location = block.AbsolutePosition.ToVector3();
            float size = 1;

            //Attention! Texture coordinates are different from OpenGL: http://msdn.microsoft.com/en-us/library/windows/desktop/bb206245(v=vs.85).aspx
            //top face
            if(block.HasTop)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, size), Vector3.UnitZ, new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, size), Vector3.UnitZ, new Vector2(0, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, size), Vector3.UnitZ, new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, size), Vector3.UnitZ, new Vector2(1, 1)));
                AddIndices(ref indicesList, ref indexerValue);
            }

            //bottom face
            if (block.HasBottom)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, 0), -Vector3.UnitZ,new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, 0), -Vector3.UnitZ,new Vector2(1, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, 0), -Vector3.UnitZ,new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, 0), -Vector3.UnitZ,new Vector2(0, 0)));
                AddIndices(ref indicesList, ref indexerValue);
            }

            //back face
            if (block.HasBack)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, 0), -Vector3.UnitY, new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, size), -Vector3.UnitY, new Vector2(0, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, size), -Vector3.UnitY, new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, 0), -Vector3.UnitY, new Vector2(1, 1)));
                AddIndices(ref indicesList, ref indexerValue);
            }

            //front face
            if (block.HasFront)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, 0), Vector3.UnitY, new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, 0), Vector3.UnitY, new Vector2(1, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, size), Vector3.UnitY, new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, size), Vector3.UnitY, new Vector2(0, 0)));
                AddIndices(ref indicesList, ref indexerValue);
            }

            //right face
            if (block.HasRight)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, 0), Vector3.UnitX, new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, 0, size), Vector3.UnitX, new Vector2(0, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, size), Vector3.UnitX, new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(size, size, 0), Vector3.UnitX, new Vector2(1, 1)));
                AddIndices(ref indicesList, ref indexerValue);
            }

            //left face
            if (block.HasLeft)
            {
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, 0), -Vector3.UnitX, new Vector2(0, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, 0), -Vector3.UnitX, new Vector2(1, 1)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, size, size), -Vector3.UnitX, new Vector2(1, 0)));
                customVertexList.Add(new VertexNormalTexture(location + new Vector3(0, 0, size), -Vector3.UnitX, new Vector2(0, 0)));
                AddIndices(ref indicesList, ref indexerValue);
            }
        }