• osg 八叉树可视化


    目录

    什么是八叉树

    八叉树算法过程

    八叉树的计算原理

    八叉树c++实现

    使用osg可视化八叉树


    什么是八叉树

    在描述三维场景的过程中常常用到一种名为八叉树的数据结构。描述三维空间的八叉树和描述二维空间的四叉树有相似之处,二维空间中正方形可以被分为四个相同形状的正方形,而三维空间中正方体可以被分为八个形状相同的正方体。

    八叉树的每个结点表示一个正方体的体积元素,每一个结点有八个子节点,这种用于描述三维空间的树装结构叫做八叉树。

    八叉树算法过程

    八叉树的计算原理

    1. 设定最大递归深度

    2. 找出场景的最大尺寸,并以此尺寸建立第一个立方体

    3. 依序将单位元元素丢入能包含且没有子节点的立方体

    4. 若没达到最大递归深度,就进行细分八等份,再讲该立方体所装的单位元元素全部分组给八个子立方体

    5. 若发现子立方体所分配到的单位元元素数量不为零且跟父立方体一样,则该子立方体停止细分

    6. 重复3,知道到达最大递归深度

    八叉树c++实现

    1. #include <iostream>
    2. using namespace std;
    3. //定义八叉树节点类
    4. template<class T>
    5. struct OctreeNode
    6. {
    7. T data; //节点数据
    8. T xmin,xmax; //节点坐标,即六面体个顶点的坐标
    9. T ymin,ymax;
    10. T zmin,zmax;
    11. OctreeNode <T>*top_left_front,*top_left_back; //该节点的个子结点
    12. OctreeNode <T>*top_right_front,*top_right_back;
    13. OctreeNode <T>*bottom_left_front,*bottom_left_back;
    14. OctreeNode <T>*bottom_right_front,*bottom_right_back;
    15. OctreeNode //节点类
    16. (T nodeValue = T(),
    17. T xminValue = T(),T xmaxValue = T(),
    18. T yminValue = T(),T ymaxValue = T(),
    19. T zminValue = T(),T zmaxValue = T(),
    20. OctreeNode<T>*top_left_front_Node = NULL,
    21. OctreeNode<T>*top_left_back_Node = NULL,
    22. OctreeNode<T>*top_right_front_Node = NULL,
    23. OctreeNode<T>*top_right_back_Node = NULL,
    24. OctreeNode<T>*bottom_left_front_Node = NULL,
    25. OctreeNode<T>*bottom_left_back_Node = NULL,
    26. OctreeNode<T>*bottom_right_front_Node = NULL,
    27. OctreeNode<T>*bottom_right_back_Node = NULL )
    28. :data(nodeValue),
    29. xmin(xminValue),xmax(xmaxValue),
    30. ymin(yminValue),ymax(ymaxValue),
    31. zmin(zminValue),zmax(zmaxValue),
    32. top_left_front(top_left_front_Node),
    33. top_left_back(top_left_back_Node),
    34. top_right_front(top_right_front_Node),
    35. top_right_back(top_right_back_Node),
    36. bottom_left_front(bottom_left_front_Node),
    37. bottom_left_back(bottom_left_back_Node),
    38. bottom_right_front(bottom_right_front_Node),
    39. bottom_right_back(bottom_right_back_Node){}
    40. };
    41. //创建八叉树
    42. template <class T>
    43. void createOctree(OctreeNode<T> * &root,int maxdepth,double xmin,double xmax,double ymin,double ymax,double zmin,double zmax)
    44. {
    45. //cout<<"处理中,请稍候……"<<endl;
    46. maxdepth=maxdepth-1; //每递归一次就将最大递归深度-1
    47. if(maxdepth>=0)
    48. {
    49. root=new OctreeNode<T>();
    50. cout<<"请输入节点值:";
    51. //root->data =9;//为节点赋值,可以存储节点信息,如物体可见性。由于是简单实现八叉树功能,简单赋值为9
    52. cin>>root->data; //为节点赋值
    53. root->xmin=xmin; //为节点坐标赋值
    54. root->xmax=xmax;
    55. root->ymin=ymin;
    56. root->ymax=ymax;
    57. root->zmin=zmin;
    58. root->zmax=zmax;
    59. double xm=(xmax-xmin)/2;//计算节点个维度上的半边长
    60. double ym=(ymax-ymin)/2;
    61. double zm=(ymax-ymin)/2;
    62. //递归创建子树,根据每一个节点所处(是几号节点)的位置决定其子结点的坐标。
    63. createOctree(root->top_left_front,maxdepth,xmin,xmax-xm,ymax-ym,ymax,zmax-zm,zmax);
    64. createOctree(root->top_left_back,maxdepth,xmin,xmax-xm,ymin,ymax-ym,zmax-zm,zmax);
    65. createOctree(root->top_right_front,maxdepth,xmax-xm,xmax,ymax-ym,ymax,zmax-zm,zmax);
    66. createOctree(root->top_right_back,maxdepth,xmax-xm,xmax,ymin,ymax-ym,zmax-zm,zmax);
    67. createOctree(root->bottom_left_front,maxdepth,xmin,xmax-xm,ymax-ym,ymax,zmin,zmax-zm);
    68. createOctree(root->bottom_left_back,maxdepth,xmin,xmax-xm,ymin,ymax-ym,zmin,zmax-zm);
    69. createOctree(root->bottom_right_front,maxdepth,xmax-xm,xmax,ymax-ym,ymax,zmin,zmax-zm);
    70. createOctree(root->bottom_right_back,maxdepth,xmax-xm,xmax,ymin,ymax-ym,zmin,zmax-zm);
    71. }
    72. }
    73. int i=1;
    74. //先序遍历八叉树
    75. template <class T>
    76. void preOrder( OctreeNode<T> * & p)
    77. {
    78. if(p)
    79. {
    80. cout<<i<<".当前节点的值为:"<<p->data<<"\n坐标为:";
    81. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    82. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    83. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    84. i+=1;
    85. cout<<endl;
    86. preOrder(p->top_left_front);
    87. preOrder(p->top_left_back);
    88. preOrder(p->top_right_front);
    89. preOrder(p->top_right_back);
    90. preOrder(p->bottom_left_front);
    91. preOrder(p->bottom_left_back);
    92. preOrder(p->bottom_right_front);
    93. preOrder(p->bottom_right_back);
    94. cout<<endl;
    95. }
    96. }
    97. //求八叉树的深度
    98. template<class T>
    99. int depth(OctreeNode<T> *& p)
    100. {
    101. if(p == NULL)
    102. return -1;
    103. int h =depth(p->top_left_front);
    104. return h+1;
    105. }
    106. //计算单位长度,为查找点做准备
    107. int cal(int num)
    108. {
    109. int result=1;
    110. if(1==num)
    111. result=1;
    112. else
    113. {
    114. for(int i=1;i<num;i++)
    115. result=2*result;
    116. }
    117. return result;
    118. }
    119. //查找点
    120. int maxdepth=0;
    121. int times=0;
    122. static double xmin=0,xmax=0,ymin=0,ymax=0,zmin=0,zmax=0;
    123. int tmaxdepth=0;
    124. double txm=1,tym=1,tzm=1;
    125. template<class T>
    126. void find(OctreeNode<T> *& p,double x,double y,double z)
    127. {
    128. double xm=(p->xmax-p->xmin)/2;
    129. double ym=(p->ymax-p->ymin)/2;
    130. double zm=(p->ymax-p->ymin)/2;
    131. times++;
    132. if(x>xmax || x<xmin|| y>ymax || y<ymin || z>zmax || z<zmin)
    133. {
    134. cout<<"该点不在场景中!"<<endl;
    135. return;
    136. }
    137. if(x<=p->xmin+txm&& x>=p->xmax-txm && y<=p->ymin+tym &&y>=p->ymax-tym && z<=p->zmin+tzm &&z>=p->zmax-tzm )
    138. {
    139. cout<<endl<<"找到该点!"<<"该点位于"<<endl;
    140. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    141. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    142. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    143. cout<<"节点内!"<<endl;
    144. cout<<"共经过"<<times<<"次递归!"<<endl;
    145. }
    146. else if(x<(p->xmax-xm) && y<(p->ymax-ym) &&z<(p->zmax-zm))
    147. {
    148. cout<<"当前经过节点坐标:"<<endl;
    149. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    150. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    151. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    152. cout<<endl;
    153. find(p->bottom_left_back,x,y,z);
    154. }
    155. else if(x<(p->xmax-xm) && y<(p->ymax-ym) &&z>(p->zmax-zm))
    156. {
    157. cout<<"当前经过节点坐标:"<<endl;
    158. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    159. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    160. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    161. cout<<endl;
    162. find(p->top_left_back,x,y,z);
    163. }
    164. else if(x>(p->xmax-xm) && y<(p->ymax-ym) &&z<(p->zmax-zm))
    165. {
    166. cout<<"当前经过节点坐标:"<<endl;
    167. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    168. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    169. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    170. cout<<endl;
    171. find(p->bottom_right_back,x,y,z);
    172. }
    173. else if(x>(p->xmax-xm) && y<(p->ymax-ym) &&z>(p->zmax-zm))
    174. {
    175. cout<<"当前经过节点坐标:"<<endl;
    176. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    177. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    178. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    179. cout<<endl;
    180. find(p->top_right_back,x,y,z);
    181. }
    182. else if(x<(p->xmax-xm) && y>(p->ymax-ym) &&z<(p->zmax-zm))
    183. {
    184. cout<<"当前经过节点坐标:"<<endl;
    185. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    186. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    187. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    188. cout<<endl;
    189. find(p->bottom_left_front,x,y,z);
    190. }
    191. else if(x<(p->xmax-xm) && y>(p->ymax-ym) &&z>(p->zmax-zm))
    192. {
    193. cout<<"当前经过节点坐标:"<<endl;
    194. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    195. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    196. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    197. cout<<endl;
    198. find(p->top_left_front,x,y,z);
    199. }
    200. else if(x>(p->xmax-xm) && y>(p->ymax-ym) &&z<(p->zmax-zm))
    201. {
    202. cout<<"当前经过节点坐标:"<<endl;
    203. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    204. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    205. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    206. cout<<endl;
    207. find(p->bottom_right_front,x,y,z);
    208. }
    209. else if(x>(p->xmax-xm) && y>(p->ymax-ym) &&z>(p->zmax-zm))
    210. {
    211. cout<<"当前经过节点坐标:"<<endl;
    212. cout<<"xmin: "<<p->xmin<<" xmax: "<<p->xmax;
    213. cout<<"ymin: "<<p->ymin<<" ymax: "<<p->ymax;
    214. cout<<"zmin: "<<p->zmin<<" zmax: "<<p->zmax;
    215. cout<<endl;
    216. find(p->top_right_front,x,y,z);
    217. }
    218. }
    219. //main函数
    220. int main ()
    221. {
    222. OctreeNode<double> *rootNode = NULL;
    223. int choiced = 0;
    224. while(true)
    225. {
    226. system("cls");
    227. cout<<"请选择操作:\n";
    228. cout<<"1.创建八叉树 2.先序遍历八叉树\n";
    229. cout<<"3.查看树深度 4.查找节点 \n";
    230. cout<<"0.退出\n\n";
    231. cin>>choiced;
    232. if(choiced == 0)
    233. return 0;
    234. else if(choiced == 1)
    235. {
    236. system("cls");
    237. cout<<"请输入最大递归深度:"<<endl;
    238. cin>>maxdepth;
    239. cout<<"请输入外包盒坐标,顺序如下:xmin,xmax,ymin,ymax,zmin,zmax"<<endl;
    240. cin>>xmin>>xmax>>ymin>>ymax>>zmin>>zmax;
    241. if(maxdepth>=0|| xmax>xmin || ymax>ymin || zmax>zmin || xmin>0 || ymin>0||zmin>0)
    242. {
    243. tmaxdepth=cal(maxdepth);
    244. txm=(xmax-xmin)/tmaxdepth;
    245. tym=(ymax-ymin)/tmaxdepth;
    246. tzm=(zmax-zmin)/tmaxdepth;
    247. createOctree(rootNode,maxdepth,xmin,xmax,ymin,ymax,zmin,zmax);
    248. }
    249. else
    250. {
    251. cout<<"输入错误!";
    252. return 0;
    253. }
    254. }
    255. else if(choiced == 2)
    256. {
    257. system("cls");
    258. cout<<"先序遍历八叉树结果:/n";
    259. i=1;
    260. preOrder(rootNode);
    261. cout<<endl;
    262. system("pause");
    263. }
    264. else if(choiced == 3)
    265. {
    266. system("cls");
    267. int dep =depth(rootNode);
    268. cout<<"此八叉树的深度为"<<dep+1<<endl;
    269. system("pause");
    270. }
    271. else if(choiced == 4)
    272. {
    273. system("cls");
    274. cout<<"请输入您希望查找的点的坐标,顺序如下:x,y,z\n";
    275. double x,y,z;
    276. cin>>x>>y>>z;
    277. times=0;
    278. cout<<endl<<"开始搜寻该点……"<<endl;
    279. find(rootNode,x,y,z);
    280. system("pause");
    281. }
    282. else
    283. {
    284. system("cls");
    285. cout<<"\n\n错误选择!\n";
    286. system("pause");
    287. }
    288. }
    289. }

    使用osg可视化八叉树

     main函数

    1. // osgPro227.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. #include <osg/Group>
    3. #include <osgDB/ReadFile>
    4. #include <osgUtil/PrintVisitor>
    5. #include <osgViewer/ViewerEventHandlers>
    6. #include <osgViewer/Viewer>
    7. #include <osgViewer/ViewerEventHandlers> //事件监听
    8. #include <osgGA/StateSetManipulator> //事件响应类,对渲染状态进行控制
    9. #include "OctreeBuilder.h"
    10. #pragma comment(lib, "OpenThreadsd.lib")
    11. #pragma comment(lib, "osgd.lib")
    12. #pragma comment(lib, "osgDBd.lib")
    13. #pragma comment(lib, "osgUtild.lib")
    14. #pragma comment(lib, "osgGAd.lib")
    15. #pragma comment(lib, "osgViewerd.lib")
    16. #pragma comment(lib, "osgTextd.lib")
    17. float randomValue(float min, float max)
    18. {
    19. return (min + (float)rand() / (RAND_MAX + 1.0f) * (max - min));
    20. }
    21. osg::Vec3 randomVector(float min, float max)
    22. {
    23. return osg::Vec3(randomValue(min, max),randomValue(min, max),randomValue(min, max));
    24. }
    25. class PrintNameVisitor : public osgUtil::PrintVisitor
    26. {
    27. public:
    28. PrintNameVisitor(std::ostream& out) : osgUtil::PrintVisitor(out) {}
    29. void apply(osg::Node& node)
    30. {
    31. if (!node.getName().empty())
    32. {
    33. output() << node.getName() << std::endl;
    34. enter();
    35. traverse(node);
    36. leave();
    37. }
    38. else osgUtil::PrintVisitor::apply(node);
    39. }
    40. };
    41. int main(int argc, char** argv)
    42. {
    43. osg::BoundingBox globalBound;
    44. std::vector<OctreeBuilder::ElementInfo> globalElements;
    45. for (unsigned int i = 0; i < 5000; ++i)
    46. {
    47. osg::Vec3 pos = randomVector(-500.0f, 500.0f);
    48. float radius = randomValue(0.5f, 20.0f);
    49. std::stringstream ss; ss << "Ball-" << i + 1;
    50. osg::Vec3 min = pos - osg::Vec3(radius, radius, radius);
    51. osg::Vec3 max = pos + osg::Vec3(radius, radius, radius);
    52. osg::BoundingBox region(min, max);
    53. globalBound.expandBy(region);
    54. globalElements.push_back(OctreeBuilder::ElementInfo(ss.str(), region));
    55. }
    56. OctreeBuilder octree;
    57. osg::ref_ptr<osg::Group> root = octree.build(0, globalBound, globalElements);
    58. std::ofstream out("octree_output.txt");
    59. PrintNameVisitor printer(out);
    60. root->accept(printer);
    61. osg::ref_ptr <osgViewer::Viewer> viewer = new osgViewer::Viewer;
    62. viewer->setSceneData(root.get());
    63. viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
    64. viewer->addEventHandler(new osgViewer::StatsHandler());//实现状态信息统计
    65. viewer->addEventHandler(new osgViewer::WindowSizeHandler());
    66. return viewer->run();
    67. }

    OctreeBuilder.h 头文件

    1. #ifndef H_COOKBOOK_CH8_OCTREEBUILDER
    2. #define H_COOKBOOK_CH8_OCTREEBUILDER
    3. #include <osg/Geode>
    4. #include <osg/LOD>
    5. class OctreeBuilder
    6. {
    7. public:
    8. OctreeBuilder() : _maxChildNumber(16), _maxTreeDepth(8), _maxLevel(0) {}
    9. int getMaxLevel() const { return _maxLevel; }
    10. void setMaxChildNumber( int max ) { _maxChildNumber = max; }
    11. int getMaxChildNumber() const { return _maxChildNumber; }
    12. void setMaxTreeDepth( int max ) { _maxTreeDepth = max; }
    13. int getMaxTreeDepth() const { return _maxTreeDepth; }
    14. typedef std::pair<std::string, osg::BoundingBox> ElementInfo;
    15. osg::Group* build( int depth, const osg::BoundingBox& total,
    16. std::vector<ElementInfo>& elements );
    17. protected:
    18. osg::LOD* createNewLevel( int level, const osg::Vec3& center, float radius );
    19. osg::Node* createElement( const std::string& id, const osg::Vec3& center, float radius );
    20. osg::Geode* createBoxForDebug( const osg::Vec3& max, const osg::Vec3& min );
    21. int _maxChildNumber;
    22. int _maxTreeDepth;
    23. int _maxLevel;
    24. };
    25. #endif

    OctreeBuilder.cpp 

    1. #include <windows.h>
    2. #include <iostream>
    3. #include <fstream>
    4. #include <sstream>
    5. using namespace std;
    6. #include <osg/ShapeDrawable>
    7. #include <osg/Geometry>
    8. #include <osg/PolygonMode>
    9. #include "OctreeBuilder.h"
    10. osg::Group* OctreeBuilder::build( int depth, const osg::BoundingBox& total,std::vector<ElementInfo>& elements )
    11. {
    12. int s[3]; // axis sides (0 or 1)
    13. //存放当前包围盒的最大、中间、最小点,以为划分八叉树做准备
    14. osg::Vec3 extentSet[3] = {
    15. total._min,
    16. (total._max + total._min) * 0.5f,
    17. total._max
    18. };
    19. std::vector<ElementInfo> childData;
    20. //遍历父结点的所有孩子,让包含在当前盒子里的,不完全包含但是中点在盒子里的,都压入到当前盒子的子结点
    21. for ( unsigned int i=0; i<elements.size(); ++i )
    22. {
    23. const ElementInfo& obj = elements[i];
    24. if ( total.contains(obj.second._min) && total.contains(obj.second._max) )
    25. childData.push_back( obj );
    26. else if ( total.intersects(obj.second) )
    27. {
    28. osg::Vec3 center = (obj.second._max + obj.second._min) * 0.5f;
    29. if (total.contains(center))
    30. {
    31. childData.push_back(obj);
    32. }
    33. }
    34. }
    35. //如果当前结点的孩子数量已经达标,或者层数已经达标,则认为是叶结点
    36. bool isLeafNode = false;
    37. if ((int)childData.size() <= _maxChildNumber || depth > _maxTreeDepth)
    38. {
    39. isLeafNode = true;
    40. }
    41. //当前八叉树根
    42. osg::ref_ptr<osg::Group> group = new osg::Group;
    43. //如果不是叶结点,继续分,把空间一分为八
    44. if ( !isLeafNode )
    45. {
    46. osg::ref_ptr<osg::Group> childNodes[8];
    47. //空间一分为八2*2*2
    48. for ( s[0]=0; s[0]<2; ++s[0] ) //x
    49. {
    50. for ( s[1]=0; s[1]<2; ++s[1] ) //y
    51. {
    52. for ( s[2]=0; s[2]<2; ++s[2] ) //z
    53. {
    54. // Calculate the child extent
    55. //extentSet 0是最小,1是中间,2是最大
    56. //下面这个小算法有点磨人,分别求出min和max的x, y, z自己好好推几个试试
    57. osg::Vec3 min, max;
    58. for ( int a=0; a<3; ++a )
    59. {
    60. min[a] = (extentSet[s[a] + 0])[a];
    61. max[a] = (extentSet[s[a] + 1])[a];
    62. }
    63. //这么求id是为了确保唯一性
    64. int id = s[0] + (2 * s[1]) + (4 * s[2]);
    65. childNodes[id] = build( depth+1, osg::BoundingBox(min, max), childData );
    66. }
    67. }
    68. }
    69. //八个子结点构建完毕后,加入到根结点当中
    70. for ( unsigned int i=0; i<8; ++i )
    71. {
    72. if ( childNodes[i] && childNodes[i]->getNumChildren() )
    73. group->addChild( childNodes[i] );
    74. }
    75. }
    76. else //找到叶结点,递归就结束了
    77. {
    78. for ( unsigned int i=0; i<childData.size(); ++i )
    79. {
    80. const ElementInfo& obj = childData[i];
    81. osg::Vec3 center = (obj.second._max + obj.second._min) * 0.5;
    82. float radius = (obj.second._max - obj.second._min).length() * 0.5f;
    83. //创建一个球
    84. group->addChild( createElement(obj.first, center, radius) );
    85. }
    86. }
    87. osg::Vec3 center = (total._max + total._min) * 0.5;
    88. float radius = (total._max - total._min).length() * 0.5f;
    89. //最后创建一个LOD,离的远了显示调试盒子,离的近了显示分的组
    90. osg::LOD* level = createNewLevel( depth, center, radius );
    91. level->insertChild( 0, createBoxForDebug(total._max, total._min) ); // For debug use
    92. level->insertChild( 1, group.get() );
    93. return level;
    94. }
    95. osg::LOD* OctreeBuilder::createNewLevel( int level, const osg::Vec3& center, float radius )
    96. {
    97. osg::ref_ptr<osg::LOD> lod = new osg::LOD;
    98. lod->setCenterMode( osg::LOD::USER_DEFINED_CENTER );
    99. lod->setCenter( center );
    100. lod->setRadius( radius );
    101. lod->setRange( 0, radius * 5.0f, FLT_MAX );
    102. lod->setRange( 1, 0.0f, radius * 5.0f );
    103. if ( _maxLevel<level ) _maxLevel = level;
    104. return lod.release();
    105. }
    106. osg::Node* OctreeBuilder::createElement( const std::string& id, const osg::Vec3& center, float radius )
    107. {
    108. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    109. geode->addDrawable( new osg::ShapeDrawable(new osg::Sphere(center, radius)) );
    110. geode->setName( id );
    111. return geode.release();
    112. }
    113. osg::Geode* OctreeBuilder::createBoxForDebug( const osg::Vec3& max, const osg::Vec3& min )
    114. {
    115. osg::Vec3 dir = max - min;
    116. osg::ref_ptr<osg::Vec3Array> va = new osg::Vec3Array(10);
    117. (*va)[0] = min + osg::Vec3(0.0f, 0.0f, 0.0f);
    118. (*va)[1] = min + osg::Vec3(0.0f, 0.0f, dir[2]);
    119. (*va)[2] = min + osg::Vec3(dir[0], 0.0f, 0.0f);
    120. (*va)[3] = min + osg::Vec3(dir[0], 0.0f, dir[2]);
    121. (*va)[4] = min + osg::Vec3(dir[0], dir[1], 0.0f);
    122. (*va)[5] = min + osg::Vec3(dir[0], dir[1], dir[2]);
    123. (*va)[6] = min + osg::Vec3(0.0f, dir[1], 0.0f);
    124. (*va)[7] = min + osg::Vec3(0.0f, dir[1], dir[2]);
    125. (*va)[8] = min + osg::Vec3(0.0f, 0.0f, 0.0f);
    126. (*va)[9] = min + osg::Vec3(0.0f, 0.0f, dir[2]);
    127. osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    128. geom->setVertexArray( va.get() );
    129. geom->addPrimitiveSet( new osg::DrawArrays(GL_QUAD_STRIP, 0, 10) );
    130. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    131. geode->addDrawable( geom.get() );
    132. geode->getOrCreateStateSet()->setAttribute(
    133. new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE) );
    134. geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    135. return geode.release();
    136. }

  • 相关阅读:
    【LeetCode】双指针题总结(持续更新)
    redis悲观锁和乐观锁
    springmvc复习(第一天)(黑马版)
    28.【继承与派生 (详解)】
    【如何学习CAN总线测试】——OSEK网络管理测试
    在windows7中运行pycharm报错误“无法定位程序输入点 CreateAppContainerProfile 于动态链接库 USERENV.dll 上
    R语言快速实现图片布局(1)
    JVM堆内存的结构,YGC,FGC的原理
    1003 Emergency
    vue 第三方组件按需引入,最后项目的包体积真的变小了吗?
  • 原文地址:https://blog.csdn.net/cangqiongxiaoye/article/details/134300643