Work on the RenderToTexture usage of the new osg::CameraNode. Both osghud

and osgprerender now ported across to osg::CameraNode.
This commit is contained in:
Robert Osfield
2005-06-14 20:51:35 +00:00
parent 868d381528
commit 71122ff38f
9 changed files with 507 additions and 195 deletions

View File

@@ -23,180 +23,186 @@
#include <osgProducer/Viewer>
class MyUpdateCallback : public osg::NodeCallback
{
public:
MyUpdateCallback(osg::Node* subgraph):
_subgraph(subgraph) {}
#define USE_CAMERA_NODE
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// traverse the subgraph to update any nodes.
if (_subgraph.valid()) _subgraph->accept(*nv);
// must traverse the Node's subgraph
traverse(node,nv);
}
osg::ref_ptr<osg::Node> _subgraph;
};
#ifndef USE_CAMERA_NODE
class MyCullCallback : public osg::NodeCallback
{
public:
MyCullCallback(osg::Node* subgraph,osg::Texture2D* texture):
_subgraph(subgraph),
_texture(texture),
_localState(new osg::StateSet) {}
class MyUpdateCallback : public osg::NodeCallback
{
public:
MyCullCallback(osg::Node* subgraph,osg::Image* image):
_subgraph(subgraph),
_image(image),
_localState(new osg::StateSet) {}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
MyUpdateCallback(osg::Node* subgraph):
_subgraph(subgraph) {}
osgUtil::CullVisitor* cullVisitor = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cullVisitor && (_texture.valid()|| _image.valid()) && _subgraph.valid())
{
doPreRender(*node,*cullVisitor);
// must traverse the subgraph
traverse(node,nv);
}
else
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// must traverse the subgraph
// traverse the subgraph to update any nodes.
if (_subgraph.valid()) _subgraph->accept(*nv);
// must traverse the Node's subgraph
traverse(node,nv);
}
osg::ref_ptr<osg::Node> _subgraph;
};
class MyCullCallback : public osg::NodeCallback
{
public:
MyCullCallback(osg::Node* subgraph,osg::Texture2D* texture):
_subgraph(subgraph),
_texture(texture),
_localState(new osg::StateSet) {}
MyCullCallback(osg::Node* subgraph,osg::Image* image):
_subgraph(subgraph),
_image(image),
_localState(new osg::StateSet) {}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cullVisitor = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cullVisitor && (_texture.valid()|| _image.valid()) && _subgraph.valid())
{
doPreRender(*node,*cullVisitor);
// must traverse the subgraph
traverse(node,nv);
}
else
{
// must traverse the subgraph
traverse(node,nv);
}
}
void doPreRender(osg::Node& node, osgUtil::CullVisitor& cv);
osg::ref_ptr<osg::Node> _subgraph;
osg::ref_ptr<osg::Texture2D> _texture;
osg::ref_ptr<osg::Image> _image;
osg::ref_ptr<osg::StateSet> _localState;
};
void MyCullCallback::doPreRender(osg::Node&, osgUtil::CullVisitor& cv)
{
const osg::BoundingSphere& bs = _subgraph->getBound();
if (!bs.valid())
{
osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()<<std::endl;
return;
}
void doPreRender(osg::Node& node, osgUtil::CullVisitor& cv);
osg::ref_ptr<osg::Node> _subgraph;
osg::ref_ptr<osg::Texture2D> _texture;
osg::ref_ptr<osg::Image> _image;
osg::ref_ptr<osg::StateSet> _localState;
};
void MyCullCallback::doPreRender(osg::Node&, osgUtil::CullVisitor& cv)
{
const osg::BoundingSphere& bs = _subgraph->getBound();
if (!bs.valid())
{
osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()<<std::endl;
return;
}
// create the render to texture stage.
osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage;
// set up lighting.
// currently ignore lights in the scene graph itself..
// will do later.
osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->getStage();
// set up the background color and clear mask.
rtts->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
rtts->setClearMask(previous_stage->getClearMask());
// set up to charge the same RenderStageLighting is the parent previous stage.
rtts->setRenderStageLighting(previous_stage->getRenderStageLighting());
// record the render bin, to be restored after creation
// of the render to text
osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin();
// create the render to texture stage.
osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage;
// set the current renderbin to be the newly created stage.
cv.setCurrentRenderBin(rtts.get());
// set up lighting.
// currently ignore lights in the scene graph itself..
// will do later.
osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->getStage();
// set up the background color and clear mask.
rtts->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
rtts->setClearMask(previous_stage->getClearMask());
// set up to charge the same RenderStageLighting is the parent previous stage.
rtts->setRenderStageLighting(previous_stage->getRenderStageLighting());
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
// 2:1 aspect ratio as per flag geomtry below.
float top = 0.25f*znear;
float right = 0.5f*znear;
// record the render bin, to be restored after creation
// of the render to text
osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin();
znear *= 0.9f;
zfar *= 1.1f;
// set the current renderbin to be the newly created stage.
cv.setCurrentRenderBin(rtts.get());
// set up projection.
osg::RefMatrix* projection = new osg::RefMatrix;
projection->makeFrustum(-right,right,-top,top,znear,zfar);
cv.pushProjectionMatrix(projection);
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
osg::RefMatrix* matrix = new osg::RefMatrix;
matrix->makeLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
// 2:1 aspect ratio as per flag geomtry below.
float top = 0.25f*znear;
float right = 0.5f*znear;
cv.pushModelViewMatrix(matrix);
znear *= 0.9f;
zfar *= 1.1f;
cv.pushStateSet(_localState.get());
// set up projection.
osg::RefMatrix* projection = new osg::RefMatrix;
projection->makeFrustum(-right,right,-top,top,znear,zfar);
{
cv.pushProjectionMatrix(projection);
// traverse the subgraph
_subgraph->accept(cv);
osg::RefMatrix* matrix = new osg::RefMatrix;
matrix->makeLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
cv.pushModelViewMatrix(matrix);
cv.pushStateSet(_localState.get());
{
// traverse the subgraph
_subgraph->accept(cv);
}
cv.popStateSet();
// restore the previous model view matrix.
cv.popModelViewMatrix();
// restore the previous model view matrix.
cv.popProjectionMatrix();
// restore the previous renderbin.
cv.setCurrentRenderBin(previousRenderBin);
if (rtts->getRenderGraphList().size()==0 && rtts->getRenderBinList().size()==0)
{
// getting to this point means that all the subgraph has been
// culled by small feature culling or is beyond LOD ranges.
return;
}
int height = 256;
int width = 512;
const osg::Viewport& viewport = *cv.getViewport();
// offset the impostor viewport from the center of the main window
// viewport as often the edges of the viewport might be obscured by
// other windows, which can cause image/reading writing problems.
int center_x = viewport.x()+viewport.width()/2;
int center_y = viewport.y()+viewport.height()/2;
osg::Viewport* new_viewport = new osg::Viewport;
new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height);
rtts->setViewport(new_viewport);
_localState->setAttribute(new_viewport);
// and the render to texture stage to the current stages
// dependancy list.
cv.getCurrentRenderBin()->getStage()->addToDependencyList(rtts.get());
// if one exist attach texture to the RenderToTextureStage.
if (_texture.valid()) rtts->setTexture(_texture.get());
// if one exist attach image to the RenderToTextureStage.
if (_image.valid()) rtts->setImage(_image.get());
}
cv.popStateSet();
// restore the previous model view matrix.
cv.popModelViewMatrix();
// restore the previous model view matrix.
cv.popProjectionMatrix();
// restore the previous renderbin.
cv.setCurrentRenderBin(previousRenderBin);
if (rtts->getRenderGraphList().size()==0 && rtts->getRenderBinList().size()==0)
{
// getting to this point means that all the subgraph has been
// culled by small feature culling or is beyond LOD ranges.
return;
}
int height = 256;
int width = 512;
const osg::Viewport& viewport = *cv.getViewport();
// offset the impostor viewport from the center of the main window
// viewport as often the edges of the viewport might be obscured by
// other windows, which can cause image/reading writing problems.
int center_x = viewport.x()+viewport.width()/2;
int center_y = viewport.y()+viewport.height()/2;
osg::Viewport* new_viewport = new osg::Viewport;
new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height);
rtts->setViewport(new_viewport);
_localState->setAttribute(new_viewport);
// and the render to texture stage to the current stages
// dependancy list.
cv.getCurrentRenderBin()->getStage()->addToDependencyList(rtts.get());
// if one exist attach texture to the RenderToTextureStage.
if (_texture.valid()) rtts->setTexture(_texture.get());
// if one exist attach image to the RenderToTextureStage.
if (_image.valid()) rtts->setImage(_image.get());
}
#endif
// call back which cretes a deformation field to oscilate the model.
class MyGeometryCallback :
@@ -352,6 +358,8 @@ osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
osg::Geode* geode = new osg::Geode();
geode->addDrawable(polyGeom);
#ifndef USE_CAMERA_NODE
osg::Group* parent = new osg::Group;
parent->setUpdateCallback(new MyUpdateCallback(subgraph));
@@ -361,6 +369,63 @@ osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
parent->addChild(geode);
return parent;
#else
osg::CameraNode* camera = new osg::CameraNode;
// set up the background color and clear mask.
camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const osg::BoundingSphere& bs = subgraph->getBound();
if (!bs.valid())
{
return subgraph;
}
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
// 2:1 aspect ratio as per flag geomtry below.
float proj_top = 0.25f*znear;
float proj_right = 0.5f*znear;
znear *= 0.9f;
zfar *= 1.1f;
// set up projection.
camera->setProjectionMatrixAsFrustum(-proj_right,proj_right,-proj_top,proj_top,znear,zfar);
// set view
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrixAsLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
// set viewport
camera->setViewport(0,0,1024,512);
camera->getOrCreateStateSet()->setAttribute(camera->getViewport());
// set the camera to render before the main camera.
camera->setRenderOrder(osg::CameraNode::PRE_RENDER);
camera->attach(osg::CameraNode::COLOR_BUFFER,texture);
// add subgraph to render
camera->addChild(subgraph);
// create a group to contain the flag and the pre rendering camera.
osg::Group* parent = new osg::Group;
parent->addChild(camera);
parent->addChild(geode);
return parent;
#endif
}
int main( int argc, char **argv )