|
我的思路是:定义一个从osgGA::GUIEventHandler继承的回调函数,按下某个键,触发更新节点,相关代码如下所示
void CEmendation:perator ()(osg::Node *node, osg::NodeVisitor *nv)
{
if(bAjust) //按下某键,此值变为true
{
node=AdjustToRectangle(node);
}
traverse(node, nv);
}
osg::Node* CEmendation::AdjustToRectangle(osg::Node* node)
{
osg::Group* distortionNode = new osg::Group;
//osg::ref_ptr<osg::Group> distortionNode = new osg::Group;
//osg::Texture2D* texture = new osg::Texture2D;
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setTextureSize(width, height);
texture->setWrap(osg::Texture::WRAP_S,osg::Texture::REPEAT);
texture->setWrap(osg::Texture::WRAP_T,osg::Texture::REPEAT);
texture->setInternalFormat(GL_RGBA);
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D:INEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
// set up the render to texture camera.
{
//osg::Camera* camera = new osg::Camera;
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
// set clear the color and depth buffer
camera->setClearColor(osg::Vec4(0.0,0.0,0.0,0.2));
camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// just inherit the main cameras view
camera->setReferenceFrame(osg::Transform::RELATIVE_RF);
camera->setProjectionMatrix(osg::Matrixd::identity());
camera->setViewMatrix(osg::Matrixd::identity());
// set viewport
camera->setViewport(0,0,width,height);
// set the camera to render before the main camera.
camera->setRenderOrder(osg::Camera:RE_RENDER);
// tell the camera to use OpenGL frame buffer object where supported.
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
// attach the texture and use it as the color buffer.
camera->attach(osg::Camera::COLOR_BUFFER, texture);
// add subgraph to render
camera->addChild(node);
distortionNode->addChild(camera);
}
{
//osg::Geometry* polyGeom = new osg::Geometry();
osg::ref_ptr<osg::Geometry> polyGeom = new osg::Geometry();
polyGeom->setSupportsDisplayList(false);
/*osg::Vec3Array* vertices = new osg::Vec3Array;
osg::Vec2Array* texcoords = new osg::Vec2Array;
osg::Vec4Array* colors = new osg::Vec4Array;*/
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
int nPiece = param.getColumnNum();
float deltaX1;
float deltaY1;
float deltaX2;
float deltaY2;
deltaX1 = (param.getPointD().PosX - param.getPointA().PosX)/nPiece;
deltaY1 = (param.getPointD().PosY - param.getPointA().PosY)/nPiece;
deltaX2 = (param.getPointC().PosX - param.getPointB().PosX)/nPiece;
deltaY2 = (param.getPointC().PosY - param.getPointB().PosY)/nPiece;
float x1;
float y1;
float x2;
float y2;
//osg:rawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP);
osg::ref_ptr<osg::DrawElementsUShort> elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP);
int k=0;
for(int i=0;i<=nPiece;i++)
{
x1 = param.getPointA().PosX + i * deltaX1;
y1 = param.getPointA().PosY + i * deltaY1;
x2 = param.getPointB().PosX + i * deltaX2;
y2 = param.getPointB().PosY + i * deltaY2;
vertices->push_back(osg::Vec3(x1,y1,param.getPointA().PosZ));
texcoords->push_back(osg::Vec2(0.0, i*1.0 / nPiece));
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
vertices->push_back(osg::Vec3(x2,y2,param.getPointB().PosZ));
texcoords->push_back(osg::Vec2(1.0, i*1.0 / nPiece));
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
elements->push_back(k);
k++;
elements->push_back(k);
k++;
}
polyGeom->setVertexArray(vertices);
polyGeom->setColorArray(colors);
polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
polyGeom->setTexCoordArray(0,texcoords);
polyGeom->addPrimitiveSet(elements);
//osg::Geode* geode = new osg::Geode();
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(polyGeom);
//osg::StateSet* stateset = polyGeom->getOrCreateStateSet();
osg::ref_ptr<osg::StateSet> stateset = polyGeom->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
// set up the camera to render the textured quad
//osg::Camera* camera = new osg::Camera;
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
// just inherit the main cameras view
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setProjectionMatrixAsPerspective(33.7157, 1.33333, 0.1, 1000);
camera->setViewMatrixAsLookAt(osg::Vec3(0,0,0),osg::Vec3(0,0,-1),osg::Vec3(0,1,0));
// set the camera to render before the main camera.
camera->setRenderOrder(osg::Camera::NESTED_RENDER);
camera->addChild(geode);
distortionNode->addChild(camera);
}
return distortionNode;
}
运行出错,中断在onexit.c函数中的
/*
* Put the new entry into the table and update the end-of-table
* pointer.
*/
*(onexitend++) = (_PVFV)_encode_pointer(func);
__onexitend = (_PVFV *)_encode_pointer(onexitend);
return func;
不知道是什么原因导致,觉得应该是和指针有关,但是我改为osg::ref_ptr的话,又会中断在traverse(node, nv);处。??? |
|