//This goes through each node and then draws the end node's verticies //This function should be called by starting with the rood node. public void DrawOctree(cOctree pNode, cFrustum g_Frustum) { //Make sure a valid node was passed in; otherwise go back to the last node if (pNode == null) { return; } // Make sure its dimensions are within our frustum if (!g_Frustum.CubeInFrustum(pNode.m_vCenter.x, pNode.m_vCenter.y, pNode.m_vCenter.z, pNode.m_Width / 2)) { return; } //Check if this node is subdivided. If so, then we need to draw its nodes if (pNode.SubDivided) { //Recurse to the bottom of these nodes and draw //the end node’s vertices, Like creating the octree, //we need to recurse through each of the 8 nodes. DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.TOP_LEFT_FRONT], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.TOP_LEFT_BACK], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.TOP_RIGHT_BACK], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.TOP_RIGHT_FRONT], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.BOTTOM_LEFT_FRONT], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.BOTTOM_LEFT_BACK], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.BOTTOM_RIGHT_BACK], g_Frustum); DrawOctree(pNode.m_pOctreeNodes[(int)eOctreeNodes.BOTTOM_RIGHT_FRONT], g_Frustum); } else { //Make sure we have valid vertices assigned to this node if (pNode.m_pVertices.Length == 0) { return; } // Store the vertices in a local pointer to keep code more clean Vector3[] pVertices = pNode.m_pVertices; // Go through all of the vertices (the number of triangles * 3) for (int i = 0; i < pVertices.Length; i += 3) { Debug.DrawLine(pVertices[i], pVertices[i + 1]); Debug.DrawLine(pVertices[i + 1], pVertices[i + 2]); Debug.DrawLine(pVertices[i + 2], pVertices[i]); } } }
//Cleans up the subdivided node creation process, so our code isn't Huge private void CreateNewNode(Vector3[] pVertices, bool[] pList, int numberOfVerts, Vector3 vCenter, float width, int triangleCount, int nodeID) { //Check if the first node found some triangles in it, else, return if (triangleCount <= 0) { return; } //Allocate memory for the triangles found in this node Vector3[] pNodeVertices = new Vector3[triangleCount * 3]; //Create a counter to count the current index of the new node vertices int index = 0; //Go through all the vertices and assign the vertices to the node’s list for (int i = 0; i < numberOfVerts; i++) { //If this current triangle is in the node, assign its vertices to it if (pList[i / 3]) { pNodeVertices[index] = pVertices[i]; index++; } } //Allocate a new node for the octree m_pOctreeNodes[nodeID] = new cOctree(); //Get the new node’s center point depending on the nodeID //(nodeID: meaning, which of the 8 subdivided cubes). Vector3 vNodeCenter = GetNewNodeCenter(vCenter, width, nodeID); //Increase the current level of subdivision g_CurrentSubdivision++; //Recurse through this node and subdivide it if necessary m_pOctreeNodes[nodeID].CreateNode(pNodeVertices, triangleCount * 3, vNodeCenter, width / 2); m_pOctreeNodes[nodeID].g_CurrentSubdivision = g_CurrentSubdivision; //Decrease the current level of subdivision g_CurrentSubdivision--; }