|
发表于 2013-3-23 21:08:25
|
显示全部楼层
本帖最后由 anheihb03dlj 于 2013-3-23 21:09 编辑
方法很多的,很多代码都是从osg源码中抽取出来的(没事多看看源码很有收获滴)- // 修改Geometry的颜色
- void setGeometryColor( osg::Geometry* geom, const osg::Vec4& newColor )
- {
- osg::Vec4Array* colorArrays = dynamic_cast< osg::Vec4Array*>( geom->getColorArray() );
- if ( colorArrays )
- {
- for ( unsigned int i = 0; i < colorArrays->size(); i++ )
- {
- osg::Vec4* color = &colorArrays->operator []( i );
- // could also use *color = newColor
- color->set( newColor._v[0], newColor._v[1], newColor._v[2], newColor._v[3] );
- }
- }
- }
复制代码- // 设置Shape的颜色
- void setShapeColor( osg::ShapeDrawable* shape, const osg::Vec4& newColor )
- {
- shape->setColor( newColor );
- }
复制代码- // 设置Drawable(包括Geometry和Shape)的颜色
- void setDrawableColor( osg::Drawable* drawable, const osg::Vec4& newColor )
- {
- osg::Geometry* curGeom = drawable->asGeometry();
- // Only process if the drawable is geometry
- if ( curGeom )
- {
- setGeometryColor( curGeom, newColor );
- }
- else
- {
- osg::ShapeDrawable* curShape = dynamic_cast< osg::ShapeDrawable*>( drawable );
- if( curShape )
- {
- setShapeColor( curShape, newColor );
- }
- }
- }
复制代码- // 设置叶子节点的颜色
- void setGeodeColor( osg::Geode* node, const osg::Vec4& newColor )
- {
- unsigned int numGeoms = node->getNumDrawables();
- for( unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++ )
- {
- setDrawableColor( node->getDrawable( geodeIdx ), newColor );
- }
- }
复制代码- // 通过材质设置颜色
- // 在Dragger.cpp中发现的该函数!!!
- void setMaterialColor( osg::Node* node, const osg::Vec4& color )
- {
- if( node == 0 ) return;
- osg::Material* mat =
- dynamic_cast<osg::Material*>( node->getOrCreateStateSet()->getAttribute( osg::StateAttribute::MATERIAL ) );
- if ( !mat )
- {
- mat = new osg::Material;
- mat->setDataVariance( osg::Object::DYNAMIC );
- node->getOrCreateStateSet()->setAttribute( mat, osg::StateAttribute::ON );
- }
- mat->setDiffuse( osg::Material::FRONT_AND_BACK, color );
- }
复制代码 |
|