Ejemplo n.º 1
0
        public void Render(Structure structure, int x, int y, int z, BlockAtlas blockAtlas, ChunkBuffer vbi)
        {
            var block     = structure[x, y, z];
            var blockData = blockAtlas[block.Id];
            var tex       = blockData.Textures[0];

            var tc00 = new Uv(0, 0);
            var tc10 = new Uv(1, 0);
            var tc01 = new Uv(0, 1);
            var tc11 = new Uv(1, 1);

            if (tex != null)
            {
                var d = 0.0002f;                 // bleed compensation
                tc00 = new Uv(tex.MinU + d, tex.MinV + d);
                tc10 = new Uv(tex.MaxU - d, tex.MinV + d);
                tc01 = new Uv(tex.MinU + d, tex.MaxV - d);
                tc11 = new Uv(tex.MaxU - d, tex.MaxV - d);
            }

            var norm = (Vector3.UnitX + Vector3.UnitZ).Normalized();

            vbi.Append(new Vertex(x, y, z + 1), new Vertex(norm.X, norm.Y, norm.Z), tc01);
            vbi.Append(new Vertex(x + 1, y, z), new Vertex(norm.X, norm.Y, norm.Z), tc11);
            vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(norm.X, norm.Y, norm.Z), tc10);
            vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(norm.X, norm.Y, norm.Z), tc00);

            norm = (Vector3.UnitX - Vector3.UnitZ).Normalized();
            vbi.Append(new Vertex(x, y, z), new Vertex(norm.X, norm.Y, norm.Z), tc01);
            vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(norm.X, norm.Y, norm.Z), tc11);
            vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(norm.X, norm.Y, norm.Z), tc10);
            vbi.Append(new Vertex(x, y + 1, z), new Vertex(norm.X, norm.Y, norm.Z), tc00);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the VBO but does not modify graphics state
        /// </summary>
        public void Prerender()
        {
            const int color         = 0xFFFFFF;
            const int occludedColor = 0xC0C0C0;

            _vbi.Reset();

            for (var x = X * 16; x < X * 16 + 16; x++)
            {
                for (var z = Z * 16; z < Z * 16 + 16; z++)
                {
                    var nx = x - X * 16 + 1;
                    var nz = z - Z * 16 + 1;

                    // Heightmap value here
                    var valueHere = (float)_heightmap[nx, nz];

                    // Neighboring positions
                    var valuePosX = _heightmap[nx + 1, nz];
                    var valueNegX = _heightmap[nx - 1, nz];
                    var valuePosZ = _heightmap[nx, nz + 1];
                    var valueNegZ = _heightmap[nx, nz - 1];

                    var valuePosXPosZ = _heightmap[nx + 1, nz + 1];
                    var valueNegXPosZ = _heightmap[nx - 1, nz + 1];
                    var valuePosXNegZ = _heightmap[nx + 1, nz - 1];
                    var valueNegXNegZ = _heightmap[nx - 1, nz - 1];

                    // Comparisons used in cheap AO
                    var isPosXHigher = valuePosX > valueHere;
                    var isNegXHigher = valueNegX > valueHere;
                    var isPosZHigher = valuePosZ > valueHere;
                    var isNegZHigher = valueNegZ > valueHere;

                    var isPosXPosZHigher = valuePosXPosZ > valueHere;
                    var isNegXPosZHigher = valueNegXPosZ > valueHere;
                    var isPosXNegZHigher = valuePosXNegZ > valueHere;
                    var isNegXNegZHigher = valueNegXNegZ > valueHere;

                    // Always draw a top face for a block
                    _vbi.Append(
                        new Vertex(x, valueHere, z),
                        SmallVertex.UnitY,
                        isPosXHigher || isPosZHigher || isPosXPosZHigher ? occludedColor : color
                        );

                    _vbi.Append(
                        new Vertex(x - 1, valueHere, z),
                        SmallVertex.UnitY,
                        isNegXHigher || isPosZHigher || isNegXPosZHigher ? occludedColor : color
                        );

                    _vbi.Append(
                        new Vertex(x - 1, valueHere, z - 1),
                        SmallVertex.UnitY,
                        isNegXHigher || isNegZHigher || isNegXNegZHigher ? occludedColor : color
                        );

                    _vbi.Append(
                        new Vertex(x, valueHere, z - 1),
                        SmallVertex.UnitY,
                        isPosXHigher || isNegZHigher || isPosXNegZHigher ? occludedColor : color
                        );

                    // Try and draw the PosZ face
                    if (valuePosZ < valueHere)
                    {
                        _vbi.Append(
                            new Vertex(x, valueHere, z),
                            SmallVertex.UnitZ,
                            color
                            );

                        _vbi.Append(
                            new Vertex(x, valuePosZ, z),
                            SmallVertex.UnitZ,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valuePosZ, z),
                            SmallVertex.UnitZ,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueHere, z),
                            SmallVertex.UnitZ,
                            color
                            );
                    }

                    // Try and draw the NegZ face
                    if (valueNegZ < valueHere)
                    {
                        _vbi.Append(
                            new Vertex(x, valueHere, z - 1),
                            SmallVertex.UnitNZ,
                            color
                            );

                        _vbi.Append(
                            new Vertex(x, valueNegZ, z - 1),
                            SmallVertex.UnitNZ,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueNegZ, z - 1),
                            SmallVertex.UnitNZ,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueHere, z - 1),
                            SmallVertex.UnitNZ,
                            color
                            );
                    }

                    // Try and draw the PosX face
                    if (valuePosX < valueHere)
                    {
                        _vbi.Append(
                            new Vertex(x, valueHere, z),
                            SmallVertex.UnitX,
                            color
                            );

                        _vbi.Append(
                            new Vertex(x, valuePosX, z),
                            SmallVertex.UnitX,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x, valuePosX, z - 1),
                            SmallVertex.UnitX,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x, valueHere, z - 1),
                            SmallVertex.UnitX,
                            color
                            );
                    }

                    // Try and draw the NegX face
                    if (valueNegX < valueHere)
                    {
                        _vbi.Append(
                            new Vertex(x - 1, valueHere, z),
                            SmallVertex.UnitNX,
                            color
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueNegX, z),
                            SmallVertex.UnitNX,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueNegX, z - 1),
                            SmallVertex.UnitNX,
                            occludedColor
                            );

                        _vbi.Append(
                            new Vertex(x - 1, valueHere, z - 1),
                            SmallVertex.UnitNX,
                            color
                            );
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Render(Structure structure, int x, int y, int z, BlockAtlas blockAtlas, ChunkBuffer vbi)
        {
            var block     = structure[x, y, z];
            var blockData = blockAtlas[block.Id];
            var tex       = blockData.Textures[0];

            Uv       tc00, tc10, tc01, tc11;
            TexCoord front, back, left, right;

            front = back = left = right = blockData.Textures[1];

            var top    = blockData.Textures[0];
            var bottom = blockData.Textures[2];

            const float d = 0.0002f;             // bleed compensation

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosX, blockAtlas))
            {
                CreateUv(right, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.PosX), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosX), tc10);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.PosX), tc11);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.PosX), tc01);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegX, blockAtlas))
            {
                CreateUv(left, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegX), tc01);
                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.NegX), tc11);
                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.NegX), tc10);
                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.NegX), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosY, blockAtlas))
            {
                CreateUv(top, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.PosY), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosY), tc10);
                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.PosY), tc11);
                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.PosY), tc01);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegY, blockAtlas))
            {
                CreateUv(bottom, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegY), tc01);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.NegY), tc11);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.NegY), tc10);
                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.NegY), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosZ, blockAtlas))
            {
                CreateUv(back, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.PosZ), tc01);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.PosZ), tc11);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosZ), tc10);
                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.PosZ), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegZ, blockAtlas))
            {
                CreateUv(front, d, out tc00, out tc10, out tc01, out tc11);

                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.NegZ), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.NegZ), tc10);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.NegZ), tc11);
                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegZ), tc01);
            }
        }
Ejemplo n.º 4
0
        public void Render(Structure structure, int x, int y, int z, BlockAtlas blockAtlas, ChunkBuffer vbi)
        {
            var block     = structure[x, y, z];
            var blockData = blockAtlas[block.Id];
            var tex       = blockData.Textures[0];

            var tc00 = new Uv(0, 0);
            var tc10 = new Uv(1, 0);
            var tc01 = new Uv(0, 1);
            var tc11 = new Uv(1, 1);

            if (tex != null)
            {
                var d = 0.0002f;                 // bleed compensation
                tc00 = new Uv(tex.MinU + d, tex.MinV + d);
                tc10 = new Uv(tex.MaxU - d, tex.MinV + d);
                tc01 = new Uv(tex.MinU + d, tex.MaxV - d);
                tc11 = new Uv(tex.MaxU - d, tex.MaxV - d);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosX, blockAtlas))
            {
                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.PosX), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosX), tc10);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.PosX), tc11);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.PosX), tc01);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegX, blockAtlas))
            {
                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegX), tc01);
                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.NegX), tc11);
                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.NegX), tc10);
                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.NegX), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosY, blockAtlas))
            {
                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.PosY), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosY), tc10);
                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.PosY), tc11);
                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.PosY), tc01);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegY, blockAtlas))
            {
                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegY), tc01);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.NegY), tc11);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.NegY), tc10);
                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.NegY), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.PosZ, blockAtlas))
            {
                vbi.Append(new Vertex(x, y, z + 1), new Vertex(FaceDir.PosZ), tc01);
                vbi.Append(new Vertex(x + 1, y, z + 1), new Vertex(FaceDir.PosZ), tc11);
                vbi.Append(new Vertex(x + 1, y + 1, z + 1), new Vertex(FaceDir.PosZ), tc10);
                vbi.Append(new Vertex(x, y + 1, z + 1), new Vertex(FaceDir.PosZ), tc00);
            }

            if (structure.IsBorderingTransparent(x, y, z, FaceDir.NegZ, blockAtlas))
            {
                vbi.Append(new Vertex(x, y + 1, z), new Vertex(FaceDir.NegZ), tc00);
                vbi.Append(new Vertex(x + 1, y + 1, z), new Vertex(FaceDir.NegZ), tc10);
                vbi.Append(new Vertex(x + 1, y, z), new Vertex(FaceDir.NegZ), tc11);
                vbi.Append(new Vertex(x, y, z), new Vertex(FaceDir.NegZ), tc01);
            }
        }