|
楼主 |
发表于 2010-12-6 16:50:07
|
显示全部楼层
本帖最后由 MoonLight 于 2010-12-6 16:56 编辑
恩,我要做的就是对场景中的水添加波纹和反射;
先是利用NodeVisitor获取水的节点:代码如下
node = osgDB::readNodeFile(addName);
//创建节点查找访问器
FindNodeVisitor find("03-GEODE", "shui1-GEODE", "shui2-GEODE");
//启动访问器,开始执行遍历
node->accept(find);
FindNodeVisitor.h:
#ifndef FIND_NODE_VISITOR_H
#define FIND_NODE_VISITOR_H
#include <osg/NodeVisitor>
#include <osg/Node>
#include <osg/TextureCubeMap>
#include <osg/Uniform>
#include <osgSim/DOFTransform>
#include <iostream>
#include <vector>
#include <string>
//节点查找访问器,继承自osg::NodeVisitor
class FindNodeVisitor : public osg::NodeVisitor
{
public:
//构造函数,参数为需要查找的节点名
FindNodeVisitor(const std::string &searchName, const std::string &searchName1, const std::string &searchName2) ;
//重载apply方法
virtual void apply(osg::Node &searchNode);
osg::ref_ptr<osg::Node> getNode()
{
return m_node;
}
private:
//节点名
std::string searchForName;
std::string searchForName1;
std::string searchForName2;
osg::ref_ptr<osg::Node> m_node ;
osg::ref_ptr<osg::Node> node;
};
#endif
FindNodeVisitor.cpp:
/**********************************************************
*Write by FlySky
*zzuxp@163.com http://www.OsgChina.org
**********************************************************/
#include "FindNodeVisitor.h"
#include <osg/StateSet>
#include <osg/Shader>
#include <osg/Program>
#include <osg/Uniform>
#include <osg/Texture2D>
#include <osg/TextureCubeMap>
#include <osgDB/ReadFile>
//构造函数,初始化并设置遍历所有的子节点
FindNodeVisitor::FindNodeVisitor(const std::string &searchName, const std::string &searchName1, const std::string &searchName2) :
osg::NodeVisitor(TRAVERSE_ALL_CHILDREN),
searchForName(searchName),
searchForName1(searchName1),
searchForName2(searchName2)
{
//
}
//重载apply方法
void FindNodeVisitor::apply(osg::Node &searchNode)
{
std::string strName = searchNode.getName();
//判断节点名称是否与查找的节点名称一样
if (strName == searchForName || strName ==searchForName1 || strName == searchForName2)
{
m_node = &searchNode;
node = &searchNode;
node->setNodeMask(0x00000001);
}
//继续遍历
traverse(searchNode);
}
再向这个获取到的水节点中添加效果:
viewer->getCamera()->setCullMask(~0x00000001);
osg::ref_ptr<osg::Group> model = new osg::Group;
model->addChild(node.get());
osg::ref_ptr<osg::Node> hl = find.getNode(); osg::ref_ptr<osg::Group> quad = new osg::Group;
quad->addChild(hl.get());
root->addChild(createWaterScene(quad.get(), model.get()));
其中quad是水的模型,model是水反射的模型;
在createWaterScene(quad.get(), model.get())函数中
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(water.get());
root->addChild(rttCamera.get());
root->addChild(model.get());
return root.get();
就是上面红字部分,所以当我用setNodeMask()时就没有水的模型,去掉setNodeMask()时就出现2个水的模型 |
|