|
楼主 |
发表于 2010-12-31 12:31:48
|
显示全部楼层
4# array 目前这个自定义的Group类和基类osg::Group基本没区别,我把相关代码提取出来写了个简单的控制台测试程序,代码如下:
class CustomNode:public osg::Group
{
public:
CustomNode(float x,float y,float z):_x(x),_y(y),_z(z)
{
}
~CustomNode()
{
}
void Init()
{
osg::Geode *geo=new osg::Geode;
osg::Geometry *gem=new osg::Geometry;
geo->addDrawable(gem);
addChild(geo);
osg::Vec3Array *verticesArray=new osg::Vec3Array;
verticesArray->push_back(osg::Vec3(-_x/2.0,-_y/2.0,_z/2.0));
verticesArray->push_back(osg::Vec3(-_x/2.0,-_y/2.0,-_z/2.0));
verticesArray->push_back(osg::Vec3(_x/2.0,-_y/2.0,-_z/2.0));
verticesArray->push_back(osg::Vec3(_x/2.0,-_y/2.0,_z/2.0));
verticesArray->push_back(osg::Vec3(-_x/2.0,_y/2.0,_z/2.0));
verticesArray->push_back(osg::Vec3(-_x/2.0,_y/2.0,-_z/2.0));
verticesArray->push_back(osg::Vec3(_x/2.0,_y/2.0,-_z/2.0));
verticesArray->push_back(osg::Vec3(_x/2.0,_y/2.0,_z/2.0));
gem->setVertexArray(verticesArray);
osg::Vec4Array *colorArray=new osg::Vec4Array;
colorArray->push_back(osg::Vec4(0.0,0.0,0.0,1.0));
colorArray->push_back(osg::Vec4(1.0,0.0,0.0,1.0));
colorArray->push_back(osg::Vec4(0.0,1.0,0.0,1.0));
colorArray->push_back(osg::Vec4(0.0,0.0,1.0,1.0));
colorArray->push_back(osg::Vec4(1.0,1.0,0.0,1.0));
colorArray->push_back(osg::Vec4(1.0,0.0,1.0,1.0));
colorArray->push_back(osg::Vec4(0.0,1.0,1.0,1.0));
colorArray->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
gem->setColorArray(colorArray);
gem->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
osg:rawElementsUInt *back=new osg::DrawElementsUInt(osg:rimitiveSet:UADS,0);
back->push_back(3);
back->push_back(2);
back->push_back(1);
back->push_back(0);
osg::DrawElementsUInt *front=new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
front->push_back(4);
front->push_back(5);
front->push_back(6);
front->push_back(7);
osg::DrawElementsUInt *left=new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
left->push_back(0);
left->push_back(1);
left->push_back(5);
left->push_back(4);
osg::DrawElementsUInt *right=new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
right->push_back(7);
right->push_back(6);
right->push_back(2);
right->push_back(3);
osg::DrawElementsUInt *bottom=new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
bottom->push_back(5);
bottom->push_back(1);
bottom->push_back(2);
bottom->push_back(6);
osg::DrawElementsUInt *top=new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
top->push_back(0);
top->push_back(4);
top->push_back(7);
top->push_back(3);
gem->addPrimitiveSet(front);
gem->addPrimitiveSet(back);
gem->addPrimitiveSet(left);
gem->addPrimitiveSet(right);
gem->addPrimitiveSet(top);
gem->addPrimitiveSet(bottom);
}
private:
float _x;
float _y;
float _z;
};
class CustomHandler:public osgGA::GUIEventHandler
{
public:
CustomHandler(){}
~CustomHandler(){}
bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
{
osgViewer::Viewer *viewer=dynamic_cast<osgViewer::Viewer*>(&aa);
if(!viewer) return false;
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
if(ea.getButton()==1)
{
osgUtil:ineSegmentIntersector::Intersections intersections;
if(viewer->computeIntersections(ea.getX(),ea.getY(),intersections))
{
for(osgUtil::LineSegmentIntersector::Intersections::iterator hits=intersections.begin();
hits!=intersections.end();++hits)
{
if(!hits->nodePath.empty()&&!(hits->nodePath.back()->getName().empty()))
{
osg::NodePath np=hits->nodePath;
for(int i=np.size()-1;i>=0;--i)
{
CustomNode* sc=dynamic_cast<CustomNode*>(np);
if(sc!=NULL)
{
osg::Group *par_node=sc->getParent(0);
par_node->removeChild(sc);
return true;
}
}
}
}
}
}
}
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
osg::Group *root=new osg::Group;
CustomNode *cus_node=new CustomNode(10,10,10);
cus_node->Init();
osg::StateSet *ss=cus_node->getOrCreateStateSet();
ss->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
root->addChild(cus_node);
osgViewer::Viewer *viewer=new osgViewer::Viewer;
viewer->setSceneData(root);
viewer->run();
return 0;
}
运行结果画了个立方体,不过不管我怎么点击鼠标都没反应 |
|