示例#1
0
 public Chunk()
 {
     for (int z = 0; z < 16; ++z)
         for (int y = 0; y < 256; ++y)
             for (int x = 0; x < 16; ++x)
             {
                 blocks[x, y, z] = new Block(x, y, z, 0, 0);
             }
 }
示例#2
0
        public void update(Packet_MapChunk data)
        {
            x = data.X;
            z = data.Z;
            byte[] cubic_chunk_data = new byte[4096];
            //Loop over 16x16x16 chunks in the 16x256x16 column
            int ii = -1;
            for (int i = 0; i < 16; i++)
            {
                //If the bitmask indicates this chunk has been sent...
                if ((data.PrimaryBM & (1 << i)) != 0)
                {
                    ii = ii + 1;
                    //Read data...
                    Array.ConstrainedCopy(data.ChunkData, 4096 * ii, cubic_chunk_data, 0, 4096);

                    for (int j = 0; j < cubic_chunk_data.Length; j++)
                    {
                        //Retrieve x,y,z and data from each element in cubic_chunk_array

                        //Byte arrays
                        //int bx = data.x * 16 + j & 0x0F;
                        //int by = i * 16 + j >> 8;
                        //int bz = data.z * 16 + (j & 0xF0) >> 4;
                        int bx = j & 0x0F;
                        int by = (i * 16) + (j >> 8);
                        int bz = (j & 0xF0) >> 4;
                        short bid = cubic_chunk_data[j];

                        //Nibble arrays
                        int data1 = cubic_chunk_data[j] & 0x0F;
                        int data2 = cubic_chunk_data[j] >> 4;

                        int k = 2 * j;
                        int bx1 = data.X * 16 + k & 0x0F;
                        int by1 = i * 16 + k >> 8;
                        int bz1 = data.Z * 16 + (k & 0xF0) >> 4;

                        k++;
                        int bx2 = data.X * 16 + k & 0x0F;
                        int by2 = i * 16 + k >> 8;
                        int bz2 = data.Z * 16 + (k & 0xF0) >> 4;
                        blocks[bx, by, bz] = new Block(bx, by, bz, bid, 0);
                    }
                }
            }
        }
示例#3
0
        ManualObject chunkMesh(Chunk c)
        {
            Mogre.ManualObject MeshChunk = mSceneMgr.CreateManualObject("MeshManChunk" + c.x + "_" + c.z);

            MeshChunk.Begin("BoxColor",RenderOperation.OperationTypes.OT_TRIANGLE_LIST);

            uint iVertex = 0;
            Block Block;
            Block Block1;
            float bo = 1f / 16f;
            //Console.WriteLine(bo);
            float U1, U2, V1, V2;
            for (int z = 0; z < 16; ++z)
            {
                for (int y = 0; y < 256; ++y)
                {
                    for (int x = 0; x < 16; ++x)
                    {
                        Block = c.blocks[x, y, z];
                        if (Block == null) continue;
                        if (Block.ID == 0) continue;

                        //Compute the block's texture coordinates
                        U1 = bo * (float)(Block.U());
                        U2 = U1 + bo;
                        V1 = bo * (float)(Block.V());
                        V2 = V1 + bo;
                        //x-1

                        Block1 = new Block(x, y, z, 0, 0);
                        if (x > 0) Block1 = c.blocks[x-1,y,z];
                        //if (x == 0) Block1 = chunks[(c.x - 1) + "_" + c.z].blocks[15, y, z];

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x, y,   z+1);	MeshChunk.Normal(-1,0,0);	MeshChunk.TextureCoord(U2, V2);
                            MeshChunk.Position(x, y+1, z+1);	MeshChunk.Normal(-1,0,0);	MeshChunk.TextureCoord(U2, V1);
                            MeshChunk.Position(x, y+1, z);		MeshChunk.Normal(-1,0,0);	MeshChunk.TextureCoord(U1, V1);
                            MeshChunk.Position(x, y,   z);		MeshChunk.Normal(-1,0,0);	MeshChunk.TextureCoord(U1, V2);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);

                            iVertex += 4;
                        }

                        //x+1
                        Block1 = new Block(x, y, z, 0, 0);
                        if (x < 15) Block1 = c.blocks[x + 1, y, z];
                        //if (x == 15) Block1 = chunks[(c.x + 1) + "_" + c.z].blocks[0, y, z];

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x+1, y,   z);	MeshChunk.Normal(1,0,0); MeshChunk.TextureCoord(U2, V2);
                            MeshChunk.Position(x+1, y+1, z);	MeshChunk.Normal(1,0,0); MeshChunk.TextureCoord(U2, V1);
                            MeshChunk.Position(x+1, y+1, z+1);	MeshChunk.Normal(1,0,0); MeshChunk.TextureCoord(U1, V1);
                            MeshChunk.Position(x+1, y,   z+1);	MeshChunk.Normal(1,0,0); MeshChunk.TextureCoord(U1, V2);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);

                            iVertex += 4;
                        }

                        //y-1
                        Block1 = new Block(x, y, z, 0, 0);
                        if (y > 0) Block1 = c.blocks[x, y - 1, z];

                        //if (Block1 == null) Block1 = new Block(x, y, z, 0, 0);

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x,   y, z);		MeshChunk.Normal(0,-1,0); MeshChunk.TextureCoord(U1, V2);
                            MeshChunk.Position(x+1, y, z);		MeshChunk.Normal(0,-1,0); MeshChunk.TextureCoord(U2, V2);
                            MeshChunk.Position(x+1, y, z+1);	MeshChunk.Normal(0,-1,0); MeshChunk.TextureCoord(U2,V1);
                            MeshChunk.Position(x,   y, z+1);	MeshChunk.Normal(0,-1,0); MeshChunk.TextureCoord(U1,V1);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);

                            iVertex += 4;
                        }

                        //y+1
                        Block1 = new Block(x, y, z, 0, 0);
                        if (y < 256 - 1) Block1 = c.blocks[x, y + 1, z];
                        //if (Block1 == null) Block1 = new Block(x, y, z, 0, 0);

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x,   y+1, z+1);		MeshChunk.Normal(0,1,0); MeshChunk.TextureCoord(U1, V2);
                            MeshChunk.Position(x+1, y+1, z+1);		MeshChunk.Normal(0,1,0); MeshChunk.TextureCoord(U2, V2);
                            MeshChunk.Position(x+1, y+1, z);		MeshChunk.Normal(0,1,0); MeshChunk.TextureCoord(U2,V1);
                            MeshChunk.Position(x,   y+1, z);		MeshChunk.Normal(0,1,0); MeshChunk.TextureCoord(U1,V1);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);

                            iVertex += 4;
                        }

                        //z-1
                        Block1 = new Block(x, y, z, 0, 0);
                        if (z > 0) Block1 = c.blocks[x, y, z - 1];
                        //if (x == 0) Block1 = chunks[c.x + "_" + (c.z - 1)].blocks[x, y, 15];
                        //if (Block1 == null) Block1 = new Block(x, y, z, 0, 0);

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x,   y+1, z);		MeshChunk.Normal(0,0,-1); MeshChunk.TextureCoord(U2, V1);
                            MeshChunk.Position(x+1, y+1, z);		MeshChunk.Normal(0,0,-1); MeshChunk.TextureCoord(U1, V1);
                            MeshChunk.Position(x+1, y,   z);		MeshChunk.Normal(0,0,-1); MeshChunk.TextureCoord(U1, V2);
                            MeshChunk.Position(x,   y,   z);		MeshChunk.Normal(0,0,-1); MeshChunk.TextureCoord(U2, V2);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);
                            iVertex += 4;
                        }

                            //z+1
                        Block1 = new Block(x,y,z,0,0);
                        if (z < 15) Block1 = c.blocks[x, y, z + 1];
                        //if (x == 15) Block1 = chunks[c.x+"_"+(c.z + 1)].blocks[x, y, 0];

                        //if (Block1 == null) Block1 = new Block(x, y, z, 0, 0);

                        if (Block1.ID == 0)
                        {
                            MeshChunk.Position(x,   y,   z+1);		MeshChunk.Normal(0,0,1); MeshChunk.TextureCoord(U1, V2);
                            MeshChunk.Position(x+1, y,   z+1);		MeshChunk.Normal(0,0,1); MeshChunk.TextureCoord(U2, V2);
                            MeshChunk.Position(x+1, y+1, z+1);		MeshChunk.Normal(0,0,1); MeshChunk.TextureCoord(U2,V1);
                            MeshChunk.Position(x,   y+1, z+1);		MeshChunk.Normal(0,0,1); MeshChunk.TextureCoord(U1,V1);

                            //MeshChunk.Triangle(iVertex, iVertex+1, iVertex+2);
                            //MeshChunk.Triangle(iVertex+2, iVertex+3, iVertex);
                            MeshChunk.Quad(iVertex, iVertex + 1, iVertex + 2, iVertex + 3);

                            iVertex += 4;
                        }
                    }
                }
            }

            MeshChunk.End();
            return MeshChunk;
        }