Exemplo n.º 1
0
        void UpdateVertexBuffer()
        {
            if (this.invalid == false)
            {
                return;
            }
            var watch = Stopwatch.StartNew();
            List <BlockVertex> vertices = new List <BlockVertex>();

            Vector3[] colors = new[]
            {
                Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ,
                new Vector3(1, 1, 0), new Vector3(1, 0, 1), new Vector3(0, 1, 1),
                new Vector3(1, 1, 1)
            };

            for (int x = this.world.LowerX; x <= this.world.UpperX; x++)
            {
                for (int z = this.world.LowerZ; z <= this.world.UpperZ; z++)
                {
                    for (int y = this.world.LowerY; y <= this.world.UpperY; y++)
                    {
                        Block block = this.world[x, y, z];
                        if (block == null)
                        {
                            continue;
                        }

                        Vector3 origin = new Vector3(x, y, z);

                        Block.AdjacentBlocks neighborhood = new Block.AdjacentBlocks();
                        neighborhood.Bottom    = Block.IsSimilar(block, this.world[x, y - 1, z]) == false;
                        neighborhood.Top       = Block.IsSimilar(block, this.world[x, y + 1, z]) == false;
                        neighborhood.NegativeX = Block.IsSimilar(block, this.world[x - 1, y, z]) == false;
                        neighborhood.PositiveX = Block.IsSimilar(block, this.world[x + 1, y, z]) == false;
                        neighborhood.NegativeZ = Block.IsSimilar(block, this.world[x, y, z - 1]) == false;
                        neighborhood.PositiveZ = Block.IsSimilar(block, this.world[x, y, z + 1]) == false;

                        BlockVertex[] blockVertices = block.CreateMesh(neighborhood).ToArray();
                        for (int i = 0; i < blockVertices.Length; i++)
                        {
                            blockVertices[i].position += origin;
                        }
                        vertices.AddRange(blockVertices);
                    }
                }
            }

            GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBuffer);
            {
                BlockVertex[] data = vertices.ToArray();
                this.vertexCount = data.Length;
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(BlockVertex.SizeInBytes * data.Length), data, BufferUsageHint.StaticDraw);
            }
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            watch.Stop();
            Console.WriteLine("Regenereted world in {0}", watch.Elapsed);

            this.invalid = false;
        }
Exemplo n.º 2
0
        void UpdateVertexBuffer()
        {
            if (this.invalid == false)
                return;
            var watch = Stopwatch.StartNew();
            List<BlockVertex> vertices = new List<BlockVertex>();
            Vector3[] colors = new[]
            {
                Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ,
                new Vector3(1, 1, 0), new Vector3(1, 0, 1), new Vector3(0, 1, 1),
                new Vector3(1, 1, 1)
            };

            for (int x = this.world.LowerX; x <= this.world.UpperX; x++)
            {
                for (int z = this.world.LowerZ; z <= this.world.UpperZ; z++)
                {
                    for (int y = this.world.LowerY; y <= this.world.UpperY; y++)
                    {
                        Block block = this.world[x, y, z];
                        if (block == null)
                            continue;

                        Vector3 origin = new Vector3(x, y, z);

                        Block.AdjacentBlocks neighborhood = new Block.AdjacentBlocks();
                        neighborhood.Bottom = Block.IsSimilar(block, this.world[x, y - 1, z]) == false;
                        neighborhood.Top = Block.IsSimilar(block, this.world[x, y + 1, z]) == false;
                        neighborhood.NegativeX = Block.IsSimilar(block, this.world[x - 1, y, z]) == false;
                        neighborhood.PositiveX = Block.IsSimilar(block, this.world[x + 1, y, z]) == false;
                        neighborhood.NegativeZ = Block.IsSimilar(block, this.world[x, y, z - 1]) == false;
                        neighborhood.PositiveZ = Block.IsSimilar(block, this.world[x, y, z + 1]) == false;

                        BlockVertex[] blockVertices = block.CreateMesh(neighborhood).ToArray();
                        for (int i = 0; i < blockVertices.Length; i++)
                        {
                            blockVertices[i].position += origin;
                        }
                        vertices.AddRange(blockVertices);
                    }
                }
            }

            GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBuffer);
            {
                BlockVertex[] data = vertices.ToArray();
                this.vertexCount = data.Length;
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(BlockVertex.SizeInBytes * data.Length), data, BufferUsageHint.StaticDraw);
            }
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            watch.Stop();
            Console.WriteLine("Regenereted world in {0}", watch.Elapsed);

            this.invalid = false;
        }