Figure 3
如图三,我们现在有四个子地形,继续分下去,知道一个部分只有一个单元,,在下图中,我们把第一个 小部分分成了四个更小的部分。

详细介绍四叉树 Quadtrees

Figure 4

  
  然后继续:
详细介绍四叉树 Quadtrees

Figure 5

  
  然后继续:
详细介绍四叉树 Quadtrees

Figure 6
好了,现在第一个部件只有一个单元大小,我们告诉树停止分割第一个部件,分割下一个,直到 全部分割完毕。当然你也可以将树分割到合适的三角形数目停止,在我们的例子中为16个三角形,第 一,这个树是父子关系,每个子节点有一个父节点,每个父节点有四个子节点,叶节点例外,他只有 一个父节点没有子节点。叶节点是我们允许的最小的子节点,第二,每个树都有一个根节点,它没有 父节点,但有四个子节点。
  
  再看一下图,暗红的边界就是根节点,在图3中,我们分割根节点,分配给他的子节点。蓝线描绘 的正方形是根节点的四个子。称为节点2,3,4,5。在图4 ,我们把节点2分为四份,这些正方形是 节点的子,称为节点6,7,8,9。继续由节点6分割出10,11,12,13,由节点10分割为14,15, 16,17。这是他们的叶节点。停止分割。分割节点11,分完后是12和13,然后是7,8,和9。然后是 3,4,5。完成。
  
  quadtrees 使用一个节点的包围坐标工作,我们说我们的图形0-16在X轴上,0-16在Z轴上。由于 这个原因,我们整个地图的包围坐标为左上为(0,0,0)右上(16,0,0)左下(0,0,16)右下 (16,0,16)当我们分割父节点时,我们就分割他的包围坐标,于是节点2的包围坐标为:左上 (0,0,0)右上(8,0,0)左下(0,0,8)右下(8,0,8)如图7.
详细介绍四叉树 Quadtrees

Figure 7

  
  Test 1
  方法如下:我们从根节点开始问“摄象机是否在根节点的包围坐标内?”我们说是。我们知道摄 象机在根节点的一个子节点内,于是测试他们,“摄象机在节点2的包围坐标内吗?”这里回答不, 于是我们离开节点2和它的子节点。这样我们就可以不用测试节点2的64个单元了,不坏,不坏。
详细介绍四叉树 Quadtrees

Figure 8

  
  Test 2
  你可以看图8,我移出了节点2和它的子节点。继续测试,“摄象机在节点3的包围坐标内吗?” ,回答不,于是我们可以安全的离开节点3和它的子节点。

详细介绍四叉树 Quadtrees

Figure 9

  
  Test 3
  继续“摄象机在节点4的包围坐标内吗?”回答不测试节点5。
详细介绍四叉树 Quadtrees

Figure 10


Figure 14

  
  如同你看见的一样,索引0指向element[0],element[0]是顶点0的X部件,依次类推。 现在,我们说我们的叶节点是4*4的三角形,这意味着叶宽为4三角形,由于我们知道节点的宽度(存储 在uiWidth),如果我们分割宽度的结果为2,那么意味着这个宽度为4,这个节点就是一个叶节点,
  
  if(0.5*uiWidth==2)
  {
  uiNodeType = LEAF_TYPE;
  }
  else
  {
  uiNodeType = NODE_TYPE;
  }
  
  接着,我们想得到一个指向我们节点的指针,pNodeList包含所有我们的节点,我们需要选择一个。
  
  NODE *pNode = &pNodeList[NodeID];
  
  向节点内填充内容
  
  pNodeList[NodeID].uID = Whatever;
  
  我们可以简单的做:
  
  pNode->uiID = Whatever;
  
  用我们得到的值填充
  
  pNode->uiID = NodeID;
  pNode->uiParentID = ParentID;
  
  pNode->vBoundingCoordinates[0].x = fVerts[(Bounding[0]*3)];
  pNode->vBoundingCoordinates[0].y = fVerts[(Bounding[0]*3)+1];
  pNode->vBoundingCoordinates[0].z = fVerts[(Bounding[0]*3)+2];
  
  pNode->vBoundingCoordinates[1].x = fVerts[(Bounding[1]*3)];
  pNode->vBoundingCoordinates[1].y = fVerts[(Bounding[1]*3)+1];
  pNode->vBoundingCoordinates[1].z = fVerts[(Bounding[1]*3)+2];
  
  pNode->vBoundingCoordinates[2].x = fVerts[(Bounding[2]*3)];
  pNode->vBoundingCoordinates[2].y = fVerts[(Bounding[2]*3)+1];
  pNode->vBoundingCoordinates[2].z = fVerts[(Bounding[2]*3)+2];
 
现在我们还没有处理叶节点,一旦我们分配了叶节点,我们将返回调用函数,在真实的世界里,你 或许希望得到一个指向数组或三角形的叶节点指针,如果你仔细看过NODE结构,你将注意变量 uiVertexStrip1...4,如果你愿意的话,可以在里面填充三角形,.
  
  if(uiNodeType == LEAF_TYPE)
  {
  return;
  }
  else
  {
  
  下面,我们需要处理节点的子节点
  
  unsigned int BoundingBox[4];
  TotalTreeID++;
  pNode->uiBranches[0] = TotalTreeID;
  
  //Top-Left i.e. b[0]
  BoundingBox[0] = Bounding[0];
  //Between b[0] and b[1]
  BoundingBox[1] = Bounding[0]+((Bounding[1]-Bounding[0])/2);
  //[between b[0] and b[2]
  BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2);
  //middle of node
  BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2);
  
  CreateNode(BoundingBox,NodeID,TotalTreeID);
  
  很简单,自己看吧,
  
  //******************************************************************************
  
  TotalTreeID++;
  pNode->uiBranches[1] = TotalTreeID;
  
  // Between b[0] and b[1]
  BoundingBox[0] = Bounding[0]+((Bounding[1]-Bounding[0])/2);
  //Top-Right i.e. b[1]
  BoundingBox[1] = Bounding[1];
  //middle of node
  BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2);
  //between b[1] & b[3]
  BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0]));
  
  CreateNode(BoundingBox,NodeID,TotalTreeID);
  
  //******************************************************************************
  
  TotalTreeID++;
  pNode->uiBranches[2] = TotalTreeID;
  
  //between b[0] and b[2]
  BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2);
  //middle of node
  BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2);
  //Bottom-Left i.e. b[2]
  BoundingBox[2] = Bounding[2];
  //between b[2] and b[3]
  BoundingBox[3] = Bounding[2]+((Bounding[3]-Bounding[2])/2);
  
  CreateNode(BoundingBox,NodeID,TotalTreeID);
  
  //******************************************************************************
  
  TotalTreeID++;
  pNode->uiBranches[3] = TotalTreeID;
  
  //middle of node
  BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2);
  //between b[1] and b[3]
  BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2) + uiWidth;
  //between b[2] and b[3]
  BoundingBox[2] = Bounding[2]+((Bounding[3]-Bounding[2])/2);
  //Bottom-Right i.e. b[3]
  BoundingBox[3] = Bou

  
  pNode->vBoundingCoordinates[3].x = fVerts[(Bounding[3]*3)];
  pNode->vBoundingCoordinates[3].y = fVerts[(Bounding[3]*3)+1];
  pNode->vBoundingCoordinates[3].z = fVerts[(Bounding[3]*3)+2];
  
  pNode->bType = uiNodeType;

相关文章:

  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2021-06-27
  • 2021-07-10
  • 2021-12-12
  • 2021-04-12
猜你喜欢
  • 2021-12-04
  • 2021-11-20
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案