Beispiel #1
0
        // Methods
        public bool Initialize(DTerrain terrain, SharpDX.Direct3D11.Device device)
        {
            // Get the number of vertices in the terrain vertex array.
            int vertexCount = terrain.VertexCount;

            // Store the total triangle count for the vertex list.
            TriangleCount = vertexCount / 3;

            // Create a vertex array to hold all of the terrain vertices.
            VertexList = new DTerrain.DVertexType[vertexCount];

            // Copy the terrain vertices into the vertex list.
            VertexList = terrain.Verticies;

            // Calculate the center x,z and the width of the mesh.
            float centerX, centerZ, width;

            CalculateMeshDimensions(vertexCount, out centerX, out centerZ, out width);

            // Create the parent node for the quad tree.
            ParentNode = new DNodeType();
            DNodeType createdNode;

            // Recursively build the quad tree based on the vertex list data and mesh dimensions.
            CreateTreeNode2(device, ParentNode, centerX, centerZ, width, out createdNode);
            ParentNode = createdNode;

            // Release the vertex list since the quad tree now has the vertices in each node.
            VertexList = null;

            return(true);
        }
Beispiel #2
0
        private void CreateTreeNode2(SharpDX.Direct3D11.Device device, DNodeType node, float positionX, float positionZ, double width, out DNodeType createdNode)
        {
            // Store the node position and size.
            node.positionX = positionX;
            node.positionZ = positionZ;
            node.width     = (float)width;

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

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

            // Count the number of triangles that are inside this node.
            int 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)
            {
                createdNode = node;
                return;
            }

            // Initialize the children nodes of this node to null.
            node.Nodes = new DNodeType[4];

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

                    // See if there are any triangles in the new node.
                    int count = CountTriangles((positionX + offsetX), (positionZ + offsetZ), (width / 2.0D));

                    if (count > 0)
                    {
                        // If there are triangles inside where this new node would be then create the child node.
                        node.Nodes[i] = new DNodeType();
                        DNodeType aNewNode;

                        // Extend the tree starting from this new child node now.
                        CreateTreeNode2(device, node.Nodes[i], (positionX + offsetX), (positionZ + offsetZ), (width / 2.0D), out aNewNode);

                        node.Nodes[i] = aNewNode;
                        createdNode   = aNewNode;
                    }
                }

                createdNode = node;
            }

            // 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.
            int vertexCount = numTriangles * 3;

            // Create the vertex array.
            DTerrain.DVertexType[] vertices = new DTerrain.DVertexType[vertexCount];

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

            // Initialize the index for this new vertex and index array.
            int 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.
                if (IsTriangleContained(i, positionX, positionZ, width))
                {
                    // Calculate the index into the terrain vertex list.
                    int vertexIndex = i * 3;

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

                    vertexIndex++;
                    vertices[index] = VertexList[vertexIndex];
                    indices[index]  = index++;

                    vertexIndex++;
                    vertices[index] = VertexList[vertexIndex];
                    indices[index]  = index++;
                }
            }

            // Set up the description of the vertex buffer. no Need its static
            // Create the vertex buffer.
            node.VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);

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

            // Release the vertex and index arrays now that the data is stored in the buffers in the node.
            vertices = null;
            indices  = null;

            createdNode = node;

            return;
        }