void UpdateVertexBuffer()
        {
            if (m_env == null)
            {
                RemoveAndDispose(ref m_vertexBuffer);
                m_vertexList = null;
                return;
            }

            var envContents = m_env.Contents;

            if (m_vertexList != null && envContents.Count > m_vertexList.Count)
            {
                m_vertexList = null;
            }

            if (m_vertexList == null)
            {
                m_vertexList = new VertexList <SceneryVertex>(envContents.Count * 2);
            }

            IntGrid3 viewGrid = m_viewGridProvider.ViewGrid;

            m_vertexList.Clear();

            foreach (var ob in envContents.OfType <ConcreteObject>())
            {
                if (viewGrid.Contains(ob.Location) == false)
                {
                    continue;
                }

                var c = ob.Color;
                if (c == GameColor.None)
                {
                    c = ob.Material.Color;
                }

                m_vertexList.Add(new SceneryVertex(ob.Location.ToVector3(), ToColor(c), (uint)ob.SymbolID));
            }

            if (m_vertexList.Count > 0)
            {
                if (m_vertexBuffer == null || m_vertexBuffer.ElementCount < m_vertexList.Count)
                {
                    RemoveAndDispose(ref m_vertexBuffer);
                    m_vertexBuffer = ToDispose(SharpDX.Toolkit.Graphics.Buffer.Vertex.New <SceneryVertex>(this.GraphicsDevice, m_vertexList.Count));
                }

                m_vertexBuffer.SetData(m_vertexList.Data, 0, m_vertexList.Count);
            }
        }
Beispiel #2
0
        void CreateCube(IntVector3 p, Direction visibleFaces,
                        ref FaceTexture baseTexture, ref FaceTexture topTexture, VertexList <TerrainVertex> vertexList,
                        Direction sliceFaces)
        {
            var offset = p - this.ChunkOffset;

            int sides = (int)visibleFaces;

            for (int side = 0; side < 6 && sides != 0; ++side, sides >>= 1)
            {
                if ((sides & 1) == 0)
                {
                    continue;
                }

                var vertices = s_cubeFaceInfo[side].Vertices;

                IntVector3 v0, v1, v2, v3;

                v0 = vertices[0] + offset;
                v1 = vertices[1] + offset;
                v2 = vertices[2] + offset;
                v3 = vertices[3] + offset;

                Direction dir = (Direction)(1 << side);

                bool isSliceFace = (sliceFaces & dir) != 0;

                int occ0, occ1, occ2, occ3;

                if (isSliceFace)
                {
                    occ0 = occ1 = occ2 = occ3 = 0;
                }
                else
                {
                    GetOcclusionsForFace(p, (DirectionOrdinal)side,
                                         out occ0, out occ1, out occ2, out occ3);
                }

                var  tex       = side == (int)DirectionOrdinal.PositiveZ ? topTexture : baseTexture;
                byte sliceHack = isSliceFace ? (byte)1 : (byte)0;

                var vd = new TerrainVertex(v0, v1, v2, v3, occ0, occ1, occ2, occ3,
                                           tex, sliceHack);
                vertexList.Add(vd);
            }
        }
Beispiel #3
0
        void CreateUndefinedChunk(ref IntGrid3 viewGrid, ref IntGrid3 chunkGrid, VertexList <TerrainVertex> vertexList,
                                  Direction visibleChunkFaces)
        {
            // Faces that are visible due to viewgrid
            Direction sliceFaces = GetGridSliceDirections(ref chunkGrid, ref viewGrid) & visibleChunkFaces;

            // Only faces revealed by viewgrid are visible
            Direction visibleFaces = sliceFaces;

            if (visibleFaces == 0)
            {
                return;
            }

            int sides = (int)visibleFaces;

            FaceTexture tex = Chunk.UndefinedFaceTexture;

            const int occlusion = 0;
            var       offset    = chunkGrid.Corner1 - this.ChunkOffset;
            var       size      = new IntVector3(chunkGrid.Size.Width, chunkGrid.Size.Height, chunkGrid.Size.Depth);

            // All faces are revealed by viewgrid
            byte sliceHack = (byte)1;

            if (Chunk.UseBigUnknownChunk)
            {
                /* Note: Using chunk sized quads causes t-junction problems */

                for (int side = 0; side < 6 && sides != 0; ++side, sides >>= 1)
                {
                    if ((sides & 1) == 0)
                    {
                        continue;
                    }

                    var vertices = s_cubeFaceInfo[side].Vertices;

                    IntVector3 v0 = vertices[0] * size + offset;
                    IntVector3 v1 = vertices[1] * size + offset;
                    IntVector3 v2 = vertices[2] * size + offset;
                    IntVector3 v3 = vertices[3] * size + offset;

                    var vd = new TerrainVertex(v0, v1, v2, v3, occlusion, occlusion, occlusion, occlusion, tex, sliceHack);
                    vertexList.Add(vd);
                }
            }
            else
            {
                for (int side = 0; side < 6 && sides != 0; ++side, sides >>= 1)
                {
                    if ((sides & 1) == 0)
                    {
                        continue;
                    }

                    int d0 = side / 2;
                    int d1 = (d0 + 1) % 3;
                    int d2 = (d0 + 2) % 3;

                    bool posFace = (side & 1) == 1;

                    var vertices = s_cubeFaceInfo[side].Vertices;

                    IntVector3 v0 = vertices[0] + offset;
                    IntVector3 v1 = vertices[1] + offset;
                    IntVector3 v2 = vertices[2] + offset;
                    IntVector3 v3 = vertices[3] + offset;

                    var vec1 = new IntVector3();
                    vec1[d1] = 1;

                    var vec2 = new IntVector3();
                    vec2[d2] = 1;

                    for (int v = 0; v < size[d1]; ++v)
                    {
                        for (int u = 0; u < size[d2]; ++u)
                        {
                            var off = vec1 * v + vec2 * u;
                            if (posFace)
                            {
                                off[d0] = size[d0] - 1;
                            }

                            var vd = new TerrainVertex(v0 + off, v1 + off, v2 + off, v3 + off,
                                                       occlusion, occlusion, occlusion, occlusion, tex, sliceHack);
                            vertexList.Add(vd);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        static void HandleTree(VertexList <SceneryVertex> sceneryVertexList, TileData td, ref IntVector3 pos)
        {
            SymbolID symbol;
            Color    color;

            switch (td.ID)
            {
            case TileID.Tree:
                switch (td.MaterialID)
                {
                case MaterialID.Fir:
                    symbol = SymbolID.ConiferousTree;
                    break;

                case MaterialID.Pine:
                    symbol = SymbolID.ConiferousTree2;
                    break;

                case MaterialID.Birch:
                    symbol = SymbolID.DeciduousTree;
                    break;

                case MaterialID.Oak:
                    symbol = SymbolID.DeciduousTree2;
                    break;

                default:
                    throw new Exception();
                }
                break;

            case TileID.Sapling:
                switch (td.MaterialID)
                {
                case MaterialID.Fir:
                    symbol = SymbolID.ConiferousSapling;
                    break;

                case MaterialID.Pine:
                    symbol = SymbolID.ConiferousSapling2;
                    break;

                case MaterialID.Birch:
                    symbol = SymbolID.DeciduousSapling;
                    break;

                case MaterialID.Oak:
                    symbol = SymbolID.DeciduousSapling2;
                    break;

                default:
                    throw new Exception();
                }
                break;

            case TileID.DeadTree:
                symbol = SymbolID.DeadTree;
                break;

            default:
                throw new Exception();
            }

            color = Color.ForestGreen;

            sceneryVertexList.Add(new SceneryVertex(pos.ToVector3(), Color.LightGreen, (uint)symbol));
        }