Exemplo n.º 1
0
        public override void Load()
        {
            // Preparing shaders
            using (ShaderBytecode byteCode = ShaderBytecode.CompileFromFile("Effet.fx", "bidon", "fx_5_0", ShaderFlags.OptimizationLevel3, EffectFlags.None))
            {
                _effect = new Effect(Master.I.device11, byteCode);
            }

            // Creating geometry
            List<Bl0ck> verticesList = new List<Bl0ck>();
            Realm realm = new Realm();
            var dimension = realm.Dimensions[0];
            {
                dimension.ChangeMaterial(new Ch0nkEngine.Data.Data.BoundingShapes.BoundingSphere(new Vector3i(32, 32, 64), 20), new AirMaterial());
                //var blocks = dimension.GetRandomTestingBlocks(new Vector3i());
                var blocks = dimension.GetAllBlocks();
                foreach(var block in blocks)
                {
                    if (block.Material.MaterialCode == 0)
                        continue;
                    verticesList.Add(new Bl0ck(new Vector3(block.AbsolutePosition.X, block.AbsolutePosition.Y, block.AbsolutePosition.Z),
                        block.Size));
                }
            }

            _numOfVertices = verticesList.Count;
            _vertexBufferSize = verticesList.Count * Bl0ck.SizeInBytes;
            // Creating vertex buffer
            var stream = new DataStream(_vertexBufferSize, true, true);
            stream.WriteRange(verticesList.ToArray());
            stream.Position = 0;

            _vertexBuffer = new Buffer(Master.I.device11, stream, new BufferDescription
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = (int)stream.Length,
                Usage = ResourceUsage.Dynamic
            });
            stream.Dispose();

            _box = Master.I.device11.ImmediateContext.MapSubresource(_vertexBuffer, 0, MapMode.WriteNoOverwrite, SlimDX.Direct3D11.MapFlags.None);
            // Texture
            Texture2D texture2D = Texture2D.FromFile(Master.I.device11, "yoda.jpg");
            ShaderResourceView view = new ShaderResourceView(Master.I.device11, texture2D);

            _effect.GetVariableByName("yodaTexture").AsResource().SetResource(view);
        }
Exemplo n.º 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);
        }