|
本帖最后由 qingfeng5211 于 2011-3-21 11:40 编辑
由于需要动态添加节点,我给根节点加入了更新回调
- //mRoot是根节点 mRoot = new osg::Group;
- mRoot->setDataVariance(osg::Object::DYNAMIC);
- mAddRemoveCallback = new CAddRemoveCallback(); //CAddRemoveCallback继承自NodeCallback
- mRoot->setUpdateCallback(mAddRemoveCallback);
复制代码
我需要实现这样的功能:给一个Cylinder实现水效果的纹理,用的代码是Array的myocean,我在添加函数中设置了cylinder的stateset,添加了uniform,如下:
- osg::StateSet* stateset = geode->getOrCreateStateSet(); //geode是要添加的子节点Cylinder
- osg::Shader* vertexShader = new osg::Shader( osg::Shader::VERTEX );
- osg::Shader* fragmentShader = new osg::Shader( osg::Shader::FRAGMENT );
- vertexShader->loadShaderSourceFromFile( "D:\\Program Files\\OpenSceneGraph\\data\\ocean.vert" );
- fragmentShader->loadShaderSourceFromFile( "D:\\Program Files\\OpenSceneGraph\\data\\ocean.frag" );
- osg::Program* program = new osg::Program;
- program->addShader( vertexShader );
- program->addShader( fragmentShader );
- stateset->setAttributeAndModes( program, osg::StateAttribute::ON );
- osg::Uniform* param1 = new osg::Uniform( "LightPos", osg::Vec3(1.0, 1.0, 0.0) );
- stateset->addUniform( param1 );
- osg::Uniform* param2 = new osg::Uniform( "time", 0.0f );
- param2->setUpdateCallback( new updateShader );
- stateset->addUniform( param2 );
- osg::Uniform* param3 = new osg::Uniform( "normalMap", 0 );
- stateset->addUniform( param3 );
- osg::Uniform* param4 = new osg::Uniform( "cubeMap", 1 );
- stateset->addUniform( param4 );
- osg::Image* image = osgDB::readImageFile( "D:\\Program Files\\OpenSceneGraph\\data\\water.bmp" );
- osg::Texture2D* tex = new osg::Texture2D( image );
- tex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
- tex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
- tex->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
- tex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
- tex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
- stateset->setTextureAttributeAndModes( 0, tex, osg::StateAttribute::ON);
- stateset->setTextureAttributeAndModes( 1, readCubeMap(), osg::StateAttribute::ON);
复制代码
添加后,水的纹理效果出来了,但是水波没有变化,是固定的。我调试了一下,发现代码中设置的uniform的更新回调没有起作用——调试时updateShader类的operator()函数没有进入
- osg::Uniform* param2 = new osg::Uniform( "time", 0.0f );
- param2->setUpdateCallback( new updateShader );
复制代码
但是当我注释掉
- //mAddRemoveCallback = new CAddRemoveCallback();
- //mRoot->setUpdateCallback(mAddRemoveCallback);
复制代码 这两行代码,也就是去掉根节点的回调,就水纹就会变化,可以进入updateShader类的operator()函数,一切正常了。
考虑了半天也没想明白是什么原因?请大家指点指点。
|
|