Skip to main content
gamedev.net
🔒 Locked

Octree Iterator (Code sample inside :P)

Started by Max_Payne Feb 17, 2003 at 6:41 PM 0 replies 700+ views
Original Post
Max_Payne
Max_Payne

  
bool COctreeNode::Iterator::SeekNextNode()
{
	// Make sure we have a root node and that this node is not the root node

	if (!m_pRootNode || m_pCurrentNode == m_pRootNode)
		return false;

	// Go to higher levels of the octree

	while (true)
	{
		// Loop through our neighbour nodes

		for (int i = m_pCurrentNode->m_NodeIndex + 1; i < 8; i++)
		{
			// Get a pointer to this child node

			COctreeNode* pChildNode = m_pCurrentNode->m_pParent->m_pChildren[i];

			// If this child node does not exist, skip it

			if (!pChildNode)
				continue;

			// Verify if this child node intersects with the search area

			if (pChildNode->SphereIntersect(m_vSphereOrigin, m_fSphereRadius))
			{
				// Set the current node as this child node

				m_pCurrentNode = pChildNode;

				// Find the lowest level node within our search area

				for (int l = 0; m_pCurrentNode->m_NumChildren > 0 && l < OCTREE_MAX_SPLIT_LEVEL; l++)
				{
					// Loop through the children nodes

					for (int i = 0; i < 8; i++)
					{
						// If this child node does not exist, skip it

						if (!m_pCurrentNode->m_pChildren[i])
							continue;

						// Get a pointer to this child node

						COctreeNode* pChildNode = m_pCurrentNode->m_pChildren[i];

						// Verify if this child node intersects with the search area

						if (pChildNode->SphereIntersect(m_vSphereOrigin, m_fSphereRadius))
						{
							// Set the current node as this child node

							m_pCurrentNode = pChildNode;

							// Break out of this loop

							break;
						}
					}
				}

				// If this node has polygons in it

				if (m_pCurrentNode->m_NumPolygons > 0)
				{
					// We have found the next node

					return true;
				}
			}
		}
		
		// Verify if our parent node is the root node

		if (m_pCurrentNode->m_pParent != m_pRootNode)
		{
			// Go one level higher

			m_pCurrentNode = m_pCurrentNode->m_pParent; 
		}
		else
		{
			// Impossible to find next node

			return false;
		}
	}
}
  
This function is part of an octree iterator which finds the node that intersect with a spherical search area, its code is meant to find the next node. This code seem to be faulty past the third split level (the first being the root node). Also, i believe it could be more optimized, so i am submitting it to you guys :D [edited by - Max_Payne on February 17, 2003 7:48:21 PM]

Looking for a serious game project?
www.xgameproject.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.