|
- class DynamicLineCallback : public osg::Drawable::UpdateCallback
- {
- public:
- DynamicLineCallback(){}
- virtual void update( osg::NodeVisitor* nv, osg::Drawable* drawable )
- {
- osg::Geometry* geom = dynamic_cast<osg::Geometry*>( drawable );
- if ( !geom )
- {
- return;
- }
- osg::Vec4Array* colors = dynamic_cast<osg::Vec4Array*>(geom->getColorArray());
- if (colors)
- {
- osg::Vec4Array::iterator itrd;
- itrd = colors->begin();
- osg::Vec4f* ptt = &(*itrd);
- ptt->_v[0] = rand() % 255 / 255.0;
- ptt->_v[1] = rand() % 255 / 255.0;
- ptt->_v[2] = rand() % 255 / 255.0;
- }
- osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
- if ( vertices )
- {
- double dddd = rand() % 100;
- for ( osg::Vec3Array::iterator itr=vertices->begin(); itr!=vertices->end(); ++itr )
- {
- osg::Vec3f* pt = &(*itr);
- itr->set(pt->_v[0], pt->_v[1], dddd);
- }
- vertices->dirty();
- }
- }
- };
- void createCuboid(const double& x, const double& y, const double& z,
- const double& xw, const double& yw,
- const osg::Vec4& color,
- osg::Group* pCuboidGroups)
- {
- // 先计算点
- double points[1][4][3];
- // 上面四点
- points[0][0][0] = x - 0.5 * xw;
- points[0][0][1] = y - 0.5 * yw;
- points[0][0][2] = z;
- points[0][1][0] = x + 0.5 * xw;
- points[0][1][1] = y - 0.5 * yw;
- points[0][1][2] = z;
- points[0][2][0] = x + 0.5 * xw;
- points[0][2][1] = y + 0.5 * yw;
- points[0][2][2] = z;
- points[0][3][0] = x - 0.5 * xw;
- points[0][3][1] = y + 0.5 * yw;
- points[0][3][2] = z;
- // 上面
- osg::ref_ptr<osg::Geode> topGeode = new osg::Geode;
- osg::ref_ptr<osg::Geometry> topGeom = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> topVec = new osg::Vec3Array;
- topVec->push_back(osg::Vec3(points[0][0][0], points[0][0][1], points[0][0][2]));
- topVec->push_back(osg::Vec3(points[0][1][0], points[0][1][1], points[0][1][2]));
- topVec->push_back(osg::Vec3(points[0][2][0], points[0][2][1], points[0][2][2]));
- topVec->push_back(osg::Vec3(points[0][3][0], points[0][3][1], points[0][3][2]));
- topGeom->setVertexArray(topVec.get());
- osg::ref_ptr<osg::Vec4Array> topColor = new osg::Vec4Array;
- topColor->push_back(color);
- topGeom->setColorArray(topColor.get());
- osg::ref_ptr<osg::Vec3Array> topNormal = new osg::Vec3Array;
- topNormal->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
- topGeom->setNormalArray(topNormal.get());
- topGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- topGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, topVec->size()));
- topGeode->addDrawable(topGeom.get());
- {
- //topGeom->setInitialBound( osg::BoundingBox(osg::Vec3(-100.0,-100.0,-100.0), osg::Vec3(100.0,100.0,100.0)) );
- topGeom->setUpdateCallback( new DynamicLineCallback );
- topGeom->setUseDisplayList( false );
- topGeom->setUseVertexBufferObjects( true );
- }
- pCuboidGroups->addChild(topGeode.get());
- }
- osg::Node* createCuboids(void)
- {
- osg::Group* cuboidGroups = new osg::Group;
- long ni = 20;// 这里设置大小,设置大了就慢了,比如200,我的机器fps只有1
- long nj = 20;// 这里设置大小,设置大了就慢了,比如200,我的机器fps只有1
- double xw = 100;// 每个矩形面的大小
- double yw = 100;// 每个矩形面的大小
- long i = 0;
- long j = 0;
- double x = 0.0f;
- double y = 0.0f;
- double z = 0.0f;
- for (i=0; i<ni; i++)
- {
- for (j=0; j<nj; j++)
- {
- x = xw * 0.5 + i * xw;// 矩形面的中心位置x
- y = yw * 0.5 + j * yw;// 矩形面的中心位置y
- z = 0.0f;
- createCuboid(x, y, z,
- xw, yw,
- osg::Vec4(1.0f, 0.5f, 1.0f, 1.0f), cuboidGroups);
- }
- }
- return cuboidGroups;
- }
- int main()
- {
- // 创建Viewer对象,场景浏览器
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
- // 添加显示fps内容
- viewer->addEventHandler(new osgViewer::StatsHandler);
- // 创建场景组节点
- osg::ref_ptr<osg::Group> root = new osg::Group;
- // 添加shape
- root->addChild(createCuboids());
- //优化场景数据
- osgUtil::Optimizer optimizer;
- optimizer.optimize(root.get());
- //设置场景数据
- viewer->setSceneData(root.get());
- //初始化并创建窗口
- viewer->realize();
- //开始渲染
- viewer->run();
- return 0;
- }
复制代码 |
|