Ejemplo n.º 1
0
        protected new void FillArrays(out HeightMapTerrainShader.Vertex[] vertices, out int[] indices)
        {
            // Create the vertex array.
            vertices = new HeightMapTerrainShader.Vertex[VertexCount];
            // Create the index array.
            indices = new int[IndexCount];
            var index = 0;

            for (var j = 0; j < TerrainHeight - 1; j++)
            {
                for (var i = 0; i < TerrainWidth - 1; i++)
                {
                    var indexBottomLeft  = TerrainHeight * j + i;
                    var indexBottomRight = TerrainHeight * j + (i + 1);
                    var indexUpperLeft   = TerrainHeight * (j + 1) + i;
                    var indexUpperRight  = TerrainHeight * (j + 1) + (i + 1);

                    #region First Triangle
                    // Upper left
                    vertices[index] = HeightMap[indexUpperLeft];
                    indices[index]  = index++;

                    // Upper right
                    vertices[index] = HeightMap[indexUpperRight];
                    indices[index]  = index++;

                    // Bottom Left
                    vertices[index] = HeightMap[indexBottomLeft];
                    indices[index]  = index++;
                    #endregion

                    #region Second Triangle
                    // Bottom Left
                    vertices[index] = HeightMap[indexBottomLeft];
                    indices[index]  = index++;

                    // Upper right
                    vertices[index] = HeightMap[indexUpperRight];
                    indices[index]  = index++;

                    // Bottom right
                    vertices[index] = HeightMap[indexBottomRight];
                    indices[index]  = index++;
                    #endregion
                }
            }
        }
Ejemplo n.º 2
0
        private Node?CreateTreeNode(Device device, float positionX, float positionZ, float width)
        {
            var node = new Node();

            // Store the node position and size.
            node.PositionX = positionX;
            node.PositionZ = positionZ;
            node.Width     = width;

            // Initialize the triangle count to zero for the node.
            node.TriangleCount = 0;

            // Initialize the vertex and index buffers
            node.VertexBuffer = null;
            node.IndexBuffer  = null;

            // Initialize the nodes
            node.Nodes = new Node?[4];

            // Count the number of the triangle that are inside this node.
            var numTriangles = CountTriangles(positionX, positionZ, width);

            // Case 1: If there are no triangles in this node then return as it is empty and requires no processing.
            if (numTriangles == 0)
            {
                return(null);
            }

            // Case 2: If there are too many triangles in this node then split it into four equal sized smaller tree nodes.
            if (numTriangles > MaxTriangles)
            {
                for (int i = 0; i < 4; i++)
                {
                    // Calculate the position offsets for the new child node.
                    var offsetX = (((i % 2) < 1) ? -1 : 1) * (width / 4);
                    var offsetZ = (((i % 4) < 2) ? -1 : 1) * (width / 4);

                    // See if there are triangles in the new node.
                    var count = CountTriangles((positionX + offsetX), (positionZ + offsetZ), width / 2);
                    if (count > 0)
                    {
                        // If there are triangles inside where this node would be then create the child node.
                        // Extend the tree starting from this child node now.
                        node.Nodes[i] = CreateTreeNode(device, (positionX + offsetX), (positionZ + offsetZ), width / 2);
                    }
                }
            }

            // Case 3: If this node is not empty and the triangle count for it is less than the max then
            // this node is at the bottom of the tree so create the list of triangles to store in it.
            node.TriangleCount = numTriangles;

            // Calculate the number of vertices
            var vertexCount = numTriangles * 3;

            // Create the vertex array
            var vertices = new HeightMapTerrainShader.Vertex[vertexCount];

            // Create the index array
            var indices = new int[vertexCount];

            // Create the vertex array
            node.VertexArray = new Vector3[vertexCount];

            // Initialize the index for this new vertex and index array.
            var index = 0;

            // Go through all the triangles in the vertex list.
            for (int i = 0; i < TriangleCount; i++)
            {
                // If the triangle is inside this node then add it to the vertex array.
                var result = IsTriangleContained(i, positionX, positionZ, width);
                if (result)
                {
                    // Calculate the index into the terrain vertex list
                    var vertexIndex = i * 3;

                    // Get the three vertices of this triangle from the vertex list
                    vertices[index]         = VertexList[vertexIndex];
                    indices[index]          = index;
                    node.VertexArray[index] = VertexList[vertexIndex].position;
                    index++;

                    vertexIndex++;
                    vertices[index]         = VertexList[vertexIndex];
                    indices[index]          = index;
                    node.VertexArray[index] = VertexList[vertexIndex].position;
                    index++;

                    vertexIndex++;
                    vertices[index]         = VertexList[vertexIndex];
                    indices[index]          = index;
                    node.VertexArray[index] = VertexList[vertexIndex].position;
                    index++;
                }
            }

            // Create the vertex buffer.
            node.VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertices);

            // Create the index buffer.
            node.IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices);

            return(node);
        }
Ejemplo n.º 3
0
        protected void FillArrays()
        {
            // Create the vertex array.
            Vertices = new HeightMapTerrainShader.Vertex[VertexCount];
            // Create the index array.
            Indices = new int[IndexCount];
            var index = 0;

            for (var j = 0; j < TerrainHeight - 1; j++)
            {
                for (var i = 0; i < TerrainWidth - 1; i++)
                {
                    var indexBottomLeft  = TerrainHeight * j + i;
                    var indexBottomRight = TerrainHeight * j + (i + 1);
                    var indexUpperLeft   = TerrainHeight * (j + 1) + i;
                    var indexUpperRight  = TerrainHeight * (j + 1) + (i + 1);

                    #region First Triangle
                    // Upper left
                    Vertices[index] = HeightMap[indexUpperLeft];
                    // Modify the texture coordinates to cover the top edge.
                    if (Vertices[index].texture.Y == 1.0f)
                    {
                        Vertices[index].texture.Y = 0.0f;
                    }
                    Indices[index] = index++;

                    // Upper right
                    Vertices[index] = HeightMap[indexUpperRight];
                    // Modify the texture coordinates to cover the top and right edges.
                    if (Vertices[index].texture.X == 0.0f)
                    {
                        Vertices[index].texture.X = 1.0f;
                    }
                    if (Vertices[index].texture.Y == 1.0f)
                    {
                        Vertices[index].texture.Y = 0.0f;
                    }
                    Indices[index] = index++;

                    // Bottom Left
                    Vertices[index] = HeightMap[indexBottomLeft];
                    Indices[index]  = index++;
                    #endregion

                    #region Second Triangle
                    // Bottom Left
                    Vertices[index] = HeightMap[indexBottomLeft];
                    Indices[index]  = index++;

                    // Upper right
                    Vertices[index] = HeightMap[indexUpperRight];
                    // Modify the texture coordinates to cover the top and right edges.
                    if (Vertices[index].texture.X == 0.0f)
                    {
                        Vertices[index].texture.X = 1.0f;
                    }
                    if (Vertices[index].texture.Y == 1.0f)
                    {
                        Vertices[index].texture.Y = 0.0f;
                    }
                    Indices[index] = index++;

                    // Bottom right
                    Vertices[index] = HeightMap[indexBottomRight];
                    // Modify the texture coordinates to cover the right edge.
                    if (Vertices[index].texture.X == 0.0f)
                    {
                        Vertices[index].texture.X = 1.0f;
                    }
                    Indices[index] = index++;
                    #endregion
                }
            }
        }