public void Render(DeviceContext deviceContext, Frustrum frustrum, TerrainShader terrainShader) { // Reset the number of the triangles that are drawn for this frame. DrawCount = 0; // Render each node that is visible at the parent node and moving down the tree. RenderNode(deviceContext, ParentNode, frustrum, terrainShader); }
private void RenderNode(DeviceContext deviceContext, QuadTreeNodeType node, Frustrum frustrum, TerrainShader terrainShader) { // Check to see if the node can be viewed, height doesn't matter in a quad tree. // If it can't be seen then none of its children can either, so don't continue down the tree, this is where the speed is gained. if (!frustrum.CheckCube(new SharpDX.Vector3(node.positionX, 0.0f, node.positionZ), (node.width / 2.0f))) // 63.7506409 { return; } // If it can be seen then check all four child nodes to see if they can also be seen. int count = 0; for (int i = 0; i < 4; i++) { // parentNode.width > 0.0f && parentNode.Nodes[i].VertexBuffer != null if (node.Nodes[i].width > 0.0f) //node.Nodes != null/ parentNode.Nodes[i].width > 0.0f { count++; RenderNode(deviceContext, node.Nodes[i], frustrum, terrainShader); } } // If there were any children nodes then there is no need to continue as parent nodes won't contain any triangles to render. if (count != 0) { return; } int vertexCount = node.TriangleCount * 3; // Otherwise if this node can be seen and has triangles in it then render these triangles. // Set vertex buffer stride and offset. // Set the vertex buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(node.VertexBuffer, SharpDX.Utilities.SizeOf <PositionTextureNormalVertex>(), 0)); // Set the index buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetIndexBuffer(node.IndexBuffer, Format.R32_UInt, 0); // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles. deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; // Determine the number of indices in this node. int indexCount = node.TriangleCount * 3; // Call the terrain shader to render the polygons in this node. terrainShader.RenderShader(deviceContext, indexCount); // Increase the count of the number of polygons that have been rendered during this frame. DrawCount += node.TriangleCount; }