|
为什么我创建10000个四边形可以,但是创建大于100个六面体对象错误?- // 四边形
- osg::ref_ptr<osg::Node> createQuad()
- {
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray( v.get() );
- v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, -1.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, 1.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, 1.f ) );
- osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- geom->setColorArray( c.get() );
- //geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
- c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
- c->push_back( osg::Vec4( 0.f, 1.f, 1.f, 1.f ) );
- c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) );
- c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );
- osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
- geom->setNormalArray( n.get() );
- geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
- n->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );
- geom->addPrimitiveSet(new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable( geom.get() );
- return geode.get();
- }
- // 六面体
- osg::ref_ptr<osg::Node> createBox()
- {
- osg::ref_ptr<osg::Geode> box = new osg::Geode;
- osg::StateSet* state = box->getOrCreateStateSet();
- osg::PolygonMode* pm = new osg::PolygonMode(
- osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL );
- state->setAttributeAndModes( pm,
- osg::StateAttribute::ON | osg::StateAttribute::PROTECTED );
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray( v.get() );
- {
- const float x( 0.f );
- const float y( 0.f );
- const float z( 0.f );
- const float r( 0.1f );
- v->push_back( osg::Vec3( x-r, y-r, z-r ) ); //left -X
- v->push_back( osg::Vec3( x-r, y-r, z+r ) );
- v->push_back( osg::Vec3( x-r, y+r, z+r ) );
- v->push_back( osg::Vec3( x-r, y+r, z-r ) );
- 。。。 。。。//right +X 同上
-
- 。。。 。。。// bottom -Z 同上
-
- 。。。 。。。// top +Z 同上
-
- 。。。 。。。// back +Y 同上
- }
- osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- geom->setColorArray( c.get() );
- geom->setColorBinding( osg::Geometry::BIND_OVERALL );
- c->push_back( osg::Vec4( 0.f, 1.f, 1.f, 1.f ) );
- osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
- geom->setNormalArray( n.get() );
- geom->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
- n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) );
- ... ... // push多个法线
- geom->addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 20 ) );
- box->addDrawable( geom.get() );
- return box.get();
- }
- void creatObj()
- {
- for (int nIndex = 0;nIndex < 480;nIndex++)
- {
- //创建正方形
- //osg::ref_ptr<osg::Node> nodeQuad = createQuad(); // OK
- osg::ref_ptr<osg::Node> nodeQuad = createBox(); // Error
- // 优化模型
- osgUtil::Optimizer optimizer;
- optimizer.optimize(nodeQuad.get());
- optimizer.reset();
- getGroupRoot()->addChild(nodeQuad.get());
- }
- }
复制代码
[ 本帖最后由 xubin 于 2008-9-9 10:51 编辑 ] |
|