Further FBO support work.
This commit is contained in:
@@ -12,27 +12,6 @@
|
||||
|
||||
#include <osg/FrameBufferObject>
|
||||
|
||||
// This drawable class provides the means for clearing
|
||||
// the color and depth buffer and it is used in this
|
||||
// example to clear the offscreen buffers. Of course
|
||||
// this is just a trick, but I couldn't think of any
|
||||
// cleaner way to do that (ClearNode always affects
|
||||
// the main buffers).
|
||||
class ClearBuffer: public osg::Drawable
|
||||
{
|
||||
public:
|
||||
ClearBuffer() {}
|
||||
ClearBuffer(const ClearBuffer ©, const osg::CopyOp ©op = osg::CopyOp::SHALLOW_COPY): osg::Drawable(copy, copyop) {}
|
||||
META_Object(example, ClearBuffer);
|
||||
void drawImplementation(osg::State&) const
|
||||
{
|
||||
glPushAttrib(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(0.5f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glPopAttrib();
|
||||
}
|
||||
};
|
||||
|
||||
// This function builds a textured quad
|
||||
osg::Node *build_quad(osg::Texture2D *tex)
|
||||
{
|
||||
@@ -63,8 +42,8 @@ osg::Node *build_quad(osg::Texture2D *tex)
|
||||
|
||||
void build_world(osg::Group *root)
|
||||
{
|
||||
int width = 512;
|
||||
int height = 512;
|
||||
int width = 2048;
|
||||
int height = 2048;
|
||||
|
||||
// create and configure the texture that we're going
|
||||
// to use as target for render-to-texture
|
||||
@@ -84,8 +63,6 @@ void build_world(osg::Group *root)
|
||||
fbo->setAttachment(GL_COLOR_ATTACHMENT0_EXT, osg::FrameBufferAttachment(tex.get()));
|
||||
fbo->setAttachment(GL_DEPTH_ATTACHMENT_EXT, osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, GL_DEPTH_COMPONENT24)));
|
||||
|
||||
#if 0
|
||||
|
||||
osg::ref_ptr<osg::Node> subgraph = osgDB::readNodeFile("cow.osg");
|
||||
if (!subgraph) return;
|
||||
|
||||
@@ -121,51 +98,19 @@ void build_world(osg::Group *root)
|
||||
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));
|
||||
|
||||
#if 1
|
||||
// add a ClearBuffer drawable to the offscreen subgraph
|
||||
// in order to clear the color and depth buffers
|
||||
osg::ref_ptr<osg::Geode> cbuf = new osg::Geode;
|
||||
cbuf->addDrawable(new ClearBuffer);
|
||||
cbuf->getOrCreateStateSet()->setRenderBinDetails(-2, "RenderBin");
|
||||
camera->addChild(cbuf.get());
|
||||
|
||||
// attach the FBO.
|
||||
camera->getOrCreateStateSet()->setAttribute(fbo.get());
|
||||
#else
|
||||
// use glCopyTexSubImage
|
||||
// tell the camera to use OpenGL frame buffer object where supported.
|
||||
camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT);
|
||||
|
||||
// attach the texture and use it as the color buffer.
|
||||
camera->attach(osg::CameraNode::COLOR_BUFFER,tex.get());
|
||||
#endif
|
||||
|
||||
|
||||
// attach the subgraph
|
||||
camera->addChild(subgraph.get());
|
||||
|
||||
|
||||
|
||||
// attach the camera to the main scene graph.
|
||||
root->addChild(camera.get());
|
||||
|
||||
#else
|
||||
|
||||
// create a subgraph that will be rendered to texture.
|
||||
// We apply the previously created FBO and a Viewport
|
||||
// attribute to this subgraph.
|
||||
osg::ref_ptr<osg::MatrixTransform> offscreen = new osg::MatrixTransform;
|
||||
root->addChild(offscreen.get());
|
||||
offscreen->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
|
||||
offscreen->getOrCreateStateSet()->setAttribute(fbo.get());
|
||||
offscreen->getOrCreateStateSet()->setAttribute(new osg::Viewport(0, 0, width, height));
|
||||
offscreen->setMatrix(osg::Matrix::lookAt(osg::Vec3(0, -20, 0), osg::Vec3(0, 0, 0), osg::Vec3(0, 0, 1)));
|
||||
offscreen->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
||||
|
||||
// add a ClearBuffer drawable to the offscreen subgraph
|
||||
// in order to clear the color and depth buffers
|
||||
osg::ref_ptr<osg::Geode> cbuf = new osg::Geode;
|
||||
cbuf->addDrawable(new ClearBuffer);
|
||||
cbuf->getOrCreateStateSet()->setRenderBinDetails(-2, "RenderBin");
|
||||
offscreen->addChild(cbuf.get());
|
||||
|
||||
// add our beloved cow the offscreen subgraph
|
||||
offscreen->addChild(osgDB::readNodeFile("cow.osg"));
|
||||
#endif
|
||||
|
||||
// now create a simple quad that will be rendered
|
||||
// in the main framebuffers. The quad's texture
|
||||
|
||||
@@ -23,186 +23,6 @@
|
||||
|
||||
#include <osgProducer/Viewer>
|
||||
|
||||
#define USE_CAMERA_NODE
|
||||
|
||||
#ifndef USE_CAMERA_NODE
|
||||
|
||||
class MyUpdateCallback : public osg::NodeCallback
|
||||
{
|
||||
public:
|
||||
|
||||
MyUpdateCallback(osg::Node* subgraph):
|
||||
_subgraph(subgraph) {}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// 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();
|
||||
|
||||
// set the current renderbin to be the newly created stage.
|
||||
cv.setCurrentRenderBin(rtts.get());
|
||||
|
||||
|
||||
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;
|
||||
|
||||
znear *= 0.9f;
|
||||
zfar *= 1.1f;
|
||||
|
||||
// set up projection.
|
||||
osg::RefMatrix* projection = new osg::RefMatrix;
|
||||
projection->makeFrustum(-right,right,-top,top,znear,zfar);
|
||||
|
||||
cv.pushProjectionMatrix(projection);
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
// call back which cretes a deformation field to oscilate the model.
|
||||
class MyGeometryCallback :
|
||||
@@ -342,11 +162,19 @@ osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
|
||||
|
||||
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,vertices->size()));
|
||||
|
||||
|
||||
|
||||
unsigned int tex_width = 2048;
|
||||
unsigned int tex_height = 2048;
|
||||
|
||||
|
||||
// new we need to add the texture to the Drawable, we do so by creating a
|
||||
// StateSet to contain the Texture StateAttribute.
|
||||
osg::StateSet* stateset = new osg::StateSet;
|
||||
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setTextureSize(tex_width, tex_height);
|
||||
texture->setInternalFormat(GL_RGBA);
|
||||
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
|
||||
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
||||
stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
|
||||
@@ -358,20 +186,6 @@ 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));
|
||||
|
||||
parent->setCullCallback(new MyCullCallback(subgraph,texture));
|
||||
|
||||
parent->addChild(geode);
|
||||
|
||||
return parent;
|
||||
|
||||
#else
|
||||
|
||||
|
||||
osg::CameraNode* camera = new osg::CameraNode;
|
||||
|
||||
@@ -403,14 +217,18 @@ osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
|
||||
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->setViewport(0,0,tex_width,tex_height);
|
||||
|
||||
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);
|
||||
// tell the camera to use OpenGL frame buffer object where supported.
|
||||
camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT);
|
||||
|
||||
// attach the texture and use it as the color buffer.
|
||||
camera->attach(osg::CameraNode::COLOR_BUFFER, texture);
|
||||
|
||||
// add subgraph to render
|
||||
camera->addChild(subgraph);
|
||||
@@ -424,8 +242,6 @@ osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
|
||||
|
||||
|
||||
return parent;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
int main( int argc, char **argv )
|
||||
|
||||
Reference in New Issue
Block a user