I've been trying to solve this Linked List Oct tree problem for 5 hours...

Started by
11 comments, last by DevLiquidKnight 20 years ago
Heres how I delete my linked list nodes. Mayhaps it''ll help you out.

	// Deconstructor	~CLinkedList(void)	{		CListNode<T> *pNodeToDelete, *pCurrentNode = m_pHeadNode;		while(pCurrentNode)		{			pNodeToDelete = pCurrentNode;			pCurrentNode = pCurrentNode->GetNext();			delete pNodeToDelete;		}	}


Don''t worry about CListNode just change that to your Octree Node class.
Advertisement
I think I did that before and it failed..
Could it be the way im adding my nodes?
	nodes->pNextNode = new OctTreeNode(pos,min,max);	nodes->pNextNode->pNextNode = NULL;	iNumberOfNodes++;

Also the inlliense is all messed up on the add node part..
VERY MESSED UP
it tends to think its declared as somthing 100% differnt then what it is.

//header declariation:void			AddNode(D3DXVECTOR3 pos, D3DXVECTOR3 min, D3DXVECTOR3 max);


// codevoid COctTree::AddNode(D3DXVECTOR3 pos, D3DXVECTOR3 min, D3DXVECTOR3 max){	pHeadNode->pNextNode = new OctTreeNode(pos,min,max);	pHeadNode->pNextNode->pNextNode = NULL;	iNumberOfNodes++;}



Intellisense picks up this:
void COctTree::AddNode(D3DXVECTOR3 pos,D3DXVECTOR3(*)(D3DXVECTOR3 pos)){// and the code inside add node is all picked up as D3DXVECTOR3's which its not.....}


[edited by - DevLiquidKnight on March 27, 2004 1:24:45 AM]
I dont think this is a problem thats going to be solved with a quick one line fix. The way you're doing things is somewhat fubar'd and is going to need to be addressed on a bigger scale.

I'd first start off with Palidine's suggestion on the OctTreeNode object. You can use a linked list for the list of childnodes, but thats just going to make things unnecessarily complicated.

I'd personally use:

class OctTreeNode{public:    int numChildren;    OctTreeNode *children[8];}   



the constructor would look something like this

OctTreeNode::OctTreeNode(){    for (x=0; x<8; x++) {        children[x]=NULL;    }}   


and the destructor:

OctTreeNode::~OctTreeNode(){    for (x=0; x<8; x++) {        if (children[x]) {            delete children[x];            children[x]=NULL;        }    }} 




The OctTree class at its simplest level would look like this:

class OctTree{public:    OctTreeNode *root;};   


Don't forget to set root to NULL in the constructor!

then to create the tree and initilize the root node (which is basically the node that encapsulates your entire world) you'd do:

OctTree *tree = new OctTree;tree->root = new OctTreeNode(whateverparamsyouwanttopass);   


where "whateverparamsyouwanttopass" would prolly be the min and max coordinates of the bounding box.


Then to build the tree:

OctTree::BuildOctTree(OctTreeNode *node){     //Here you'd split the world up and figure out what polygons go where.        for (x=0; x<8; x++) {        children[x]= new OctTreeNode (whateverparamsyouwanttopass);        BuildOctTree(children[x]);    }}   


You may want to pass in a list of polygons to the BuildOctTree method, it really depends off how you're managing the dataset.

Finally after creating the root node above you'd basically call:

//recursive methodBuildOctTree(root); //maybe pass in the polylist here...   

or
//recursive methodtree->BuildOctTree(tree->root); //maybe pass in the polylist here...   

depending on where you actually call the BuildOctTree method.



Rendering the tree would be something like the following"

tree->RenderOctTree(tree->root);  //recursive method  


and the method to go along with it"

OctTree::RenderOctTree(OctTreeNode *node){     //Here you'd set various renderstates and stuff or whatever         for (x=0; x<8; x++) {        RenderOctTree(children[x]);    }}  




If you wanted to destroy the tree:
if (tree->root) {    delete tree->root;    tree->root = NULL;}delete tree;tree = NULL;   


This, due to the OctTreeNode destructor would set up a chain reaction that would destroy and free up the memory for every node in the octree.

Once you have a base foundation like this up and running, you can slowly start adding the DirectX, vertexbuffer and bounding box stuff back in.

This is by no means the best or most efficient way to do this, but hopefully it's enough to help you solve your problems and get you going.


-=[ Megahertz ]=-


Edit, seems using i as an array subscript was causing problems with italics.

[edited by - Megahertz on March 27, 2004 5:08:22 AM]
-=[Megahertz]=-

This topic is closed to new replies.

Advertisement