// credit where credit is due on this one, most of it was borrowed from the tutorial on gamedev.net with a few modifications. #include #include #include using std::cout; using std::cin; float *fVerts; unsigned int *uiList; #define LEAF_TYPE 0 #define NODE_TYPE 1 typedef struct vertex{ float x,y,z; }VERTEX; class NODE{ int bType; VERTEX vBoundingCoordinates[4]; unsigned int uiBranches[4]; unsigned int uiVertexStrip1[8]; unsigned int uiVertexStrip2[8]; unsigned int uiVertexStrip3[8]; unsigned int uiVertexStrip4[8]; unsigned int uiID; unsigned int uiParentID; } NODE *pNodeList; /* uiBoundingCoordinates[0] uiBoundingCoordinates[1] *---------------------------|--------------------------* *---------------------------|--------------------------* uiBoundingCoordinates[2] uiBoundingCoordinates[3] */ void CreateNode(unsigned int Bounding[4],unsigned int ParentID,unsigned int NodeID) { static unsigned int TotalTreeID = 0; unsigned int uiNodeType; float uiWidth,uiHeight; uiWidth = fVerts[(Bounding[1]*3)] - fVerts[(Bounding[0]*3)]; uiHeight = fVerts[(Bounding[2]*3)+2] - fVerts[(Bounding[0]*3)+2]; if(0.5*uiWidth==2) {uiNodeType = LEAF_TYPE;} else {uiNodeType = NODE_TYPE;} NODE *pNode = &pNodeList[NodeID]; // pointer to parrent is pNodeList's address malloc'd memfor quadtree pNode->uiID = NodeID; pNode->uiParentID = ParentID; for (int i=0; i < 4; i++) { //parrent NODE uiBounding coords pNode->vBoundingCoordinates[i].x = fVerts[(Bounding[i]*3)]; pNode->vBoundingCoordinates[i].y = fVerts[(Bounding[i]*3)+1]; pNode->vBoundingCoordinates[i].z = fVerts[(Bounding[i]*3)+2]; } pNode->bType = uiNodeType; if(uiNodeType == LEAF_TYPE) {return;} else { unsigned int BoundingBox[4]; TotalTreeID++; pNode->uiBranches[0] = TotalTreeID; BoundingBox[0] = Bounding[0]; //Top-Left i.e. b[0] BoundingBox[1] = Bounding[0]+((Bounding[1]-Bounding[0])/2); //Between b[0] and b[1] BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2); //[between b[0] and b[2] BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //middle of node CreateNode(BoundingBox,NodeID,TotalTreeID); //************************************************************************************************************ TotalTreeID++; pNode->uiBranches[1] = TotalTreeID; BoundingBox[0] = Bounding[0]+((Bounding[1]-Bounding[0])/2); // Between b[0] and b[1] BoundingBox[1] = Bounding[1]; //Top-Right i.e. b[1] BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //middle of node BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])); //between b[1] & b[3] CreateNode(BoundingBox,NodeID,TotalTreeID); //************************************************************************************************************ TotalTreeID++; pNode->uiBranches[2] = TotalTreeID; BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2); //between b[0] and b[2] BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //middle of node BoundingBox[2] = Bounding[2]; //Bottom-Left i.e. b[2] BoundingBox[3] = Bounding[2]+((Bounding[3]-Bounding[2])/2); //between b[2] and b[3] CreateNode(BoundingBox,NodeID,TotalTreeID); //************************************************************************************************************ TotalTreeID++; pNode->uiBranches[3] = TotalTreeID; BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //middle of node BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2) + uiWidth; //between b[1] and b[3] BoundingBox[2] = Bounding[2]+((Bounding[3]-Bounding[2])/2); //between b[2] and b[3] BoundingBox[3] = Bounding[3]; //Bottom-Right i.e. b[3] CreateNode(BoundingBox,NodeID,TotalTreeID); } } void ReportNode(NODE *pNode,int TotalNodes) { int child; cout<<"Root Node ID: "<>GridWidth; // GridWidth=8; cout<<"\nEnter Grid Height: "; // GridHeight=8; cin>>GridHeight; CreateGrid(GridWidth+1,GridHeight+1); unsigned int Temp =(GridWidth/4)*(GridWidth/4); // devided into 4 sections ?? unsigned int Temp2 = CalcNodeNum(Temp,4); // max , min cout<