|
楼主 |
发表于 2008-9-22 11:07:07
|
显示全部楼层
将法线算法做了一些改进,取消了smoothingvisitor,但为什么会有2根黑黑的线呢?- osg::ref_ptr<osg::Node> CreateCylinder()
- {
- // Create an object to store geometry in.
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- // Create an array of four vertices.
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray( v.get() );
- //------------------关键代码,设置圆柱体顶点------------------
- float radius=2.0;//半径
- float radius2=1.0;
- float fHeight=4.0;//高
- //上底
- for (float a=-1.0*radius;a<=1.0*radius+0.0001;a+=radius/25.0)
- {
- v->push_back(osg::Vec3(a,0.0f,sqrt(fabs(pow(radius,2)-pow(a,2)))));
- v->push_back(osg::Vec3(a*(radius2/radius),0.0f,sqrt(fabs(pow(radius2,2)-pow(a*(radius2/radius),2))))+osg::Vec3(0.0,fHeight,0.0));
- }
- for (float a=1.0*radius;a>=-1.0*radius-0.0001;a-=radius/25.0)
- {
- v->push_back(osg::Vec3(a,0.0f,-1*sqrt(fabs(pow(radius,2)-pow(a,2)))));
- v->push_back(osg::Vec3(a*(radius2/radius),0.0f,-1*sqrt(fabs(pow(radius2,2)-pow(a*(radius2/radius),2))))+osg::Vec3(0.0,fHeight,0.0));
- }
- float fPoint=204;//总共使用的顶点数
-
- //设置法线
- osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
- geom->setNormalArray( n.get() );
- geom->setNormalBinding( osg::Geometry::BIND_PER_VERTEX);
- // n->push_back( osg::Vec3( 0.0f,0.0f, 1.0f ) );
- float x2=radius*fHeight*fHeight/(fHeight*fHeight+pow((radius-radius2),2));
- float x3=x2*(radius-radius2)/fHeight;
- for (float a=-1.0*radius;a<=1.0*radius+0.0001;a+=radius/25.0)
- {
- n->push_back(osg::Vec3(0.0,0.0,0.0)+(osg::Vec3(a*(x2/radius),0.0f,sqrt(fabs(pow(x2,2)-pow(a*(x2/radius),2))))+osg::Vec3(0.0,x3,0.0)));
- n->push_back(osg::Vec3(0.0,0.0,0.0)+(osg::Vec3(a*(x2/radius),0.0f,sqrt(fabs(pow(x2,2)-pow(a*(x2/radius),2))))+osg::Vec3(0.0,x3,0.0)));
- }
- for (float a=1.0*radius;a>=-1.0*radius-0.0001;a-=radius/25.0)
- {
- n->push_back(osg::Vec3(0.0,0.0,0.0)+(osg::Vec3(a*(x2/radius),0.0f,-1*sqrt(fabs(pow(x2,2)-pow(a*(x2/radius),2))))+osg::Vec3(0.0,x3,0.0)));
- n->push_back(osg::Vec3(0.0,0.0,0.0)+(osg::Vec3(a*(x2/radius),0.0f,-1*sqrt(fabs(pow(x2,2)-pow(a*(x2/radius),2))))+osg::Vec3(0.0,x3,0.0)));
- }
-
- //----------------------------------------------------------
- // Create an array of four colors.
- osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );
- geom->setColorArray( c.get() );
- geom->setColorBinding( osg::Geometry::BIND_OVERALL );
- // Draw a four-vertex quad from the stored data.
- geom->addPrimitiveSet(new osg::DrawArrays( osg::PrimitiveSet::TRIANGLE_STRIP, 0,fPoint) );
- //使用smoothingVisitor
- // osgUtil::SmoothingVisitor::smooth(*geom.get());
- // Add the Geometry (Drawable) to a Geode and
- // return the Geode.
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable(geom.get());
- osg::Matrix RoLast1;
- RoLast1.makeRotate(180.0*3.14159265/180.0,osg::Vec3f(1.0,0.0,0.0));
- osg::Matrix RoLast2;
- RoLast2.makeTranslate(osg::Vec3(0.0,2.75,0.0));
- osg::ref_ptr<osg::MatrixTransform> TransNode = new osg::MatrixTransform;
- TransNode->addChild(geode.get());
- TransNode->setMatrix(RoLast1*RoLast2);
- osg::StateSet* state4 = TransNode->getOrCreateStateSet();
- osg::ref_ptr<osg::Material> mat3 = new osg::Material;
- //漫反射光(决定物体颜色)
- mat3->setDiffuse( osg::Material::FRONT_AND_BACK,
- osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
- //镜面反射光
- mat3->setSpecular( osg::Material::FRONT_AND_BACK,
- osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );
- //环境反射光
- mat3->setShininess( osg::Material::FRONT_AND_BACK, 128.f );
- state4->setAttribute( mat3.get() );
- state4->setRenderingHint( osg::StateSet::OPAQUE_BIN );
- return TransNode.get();
- }
复制代码 |
|