Further FBO support work.
This commit is contained in:
parent
1641cd7b54
commit
c5cad6982e
@ -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 )
|
||||
|
@ -15,6 +15,7 @@
|
||||
#define OSGUTIL_RENDERTOTEXTURESTAGE 1
|
||||
|
||||
#include <osg/Texture2D>
|
||||
#include <osg/FrameBufferObject>
|
||||
|
||||
#include <osgUtil/RenderStage>
|
||||
|
||||
@ -51,6 +52,10 @@ class OSGUTIL_EXPORT RenderToTextureStage : public RenderStage
|
||||
|
||||
void setImageReadPixelDataType(GLenum type) { _imageReadPixelDataType = type; }
|
||||
GLenum getImageReadPixelDataType() const { return _imageReadPixelDataType; }
|
||||
|
||||
void setFrameBufferObject(osg::FrameBufferObject* fbo) { _fbo = fbo; }
|
||||
osg::FrameBufferObject* getFrameBufferObject() { return _fbo.get(); }
|
||||
const osg::FrameBufferObject* getFrameBufferObject() const { return _fbo.get(); }
|
||||
|
||||
virtual void draw(osg::State& state,RenderLeaf*& previous);
|
||||
|
||||
@ -61,10 +66,12 @@ class OSGUTIL_EXPORT RenderToTextureStage : public RenderStage
|
||||
|
||||
virtual ~RenderToTextureStage();
|
||||
|
||||
osg::ref_ptr<osg::Texture2D> _texture;
|
||||
osg::ref_ptr<osg::Image> _image;
|
||||
GLenum _imageReadPixelFormat;
|
||||
GLenum _imageReadPixelDataType;
|
||||
osg::ref_ptr<osg::Texture2D> _texture;
|
||||
osg::ref_ptr<osg::Image> _image;
|
||||
GLenum _imageReadPixelFormat;
|
||||
GLenum _imageReadPixelDataType;
|
||||
|
||||
osg::ref_ptr<osg::FrameBufferObject> _fbo;
|
||||
|
||||
};
|
||||
|
||||
|
@ -304,7 +304,7 @@ void FrameBufferObject::apply(State &state) const
|
||||
|
||||
if (_unsupported[contextID])
|
||||
return;
|
||||
|
||||
|
||||
FBOExtensions* ext = FBOExtensions::instance(contextID);
|
||||
if (!ext->isSupported())
|
||||
{
|
||||
|
@ -1053,8 +1053,18 @@ void CullVisitor::apply(osg::CameraNode& camera)
|
||||
{
|
||||
// use render to texture stage.
|
||||
// create the render to texture stage.
|
||||
osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage;
|
||||
|
||||
osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = dynamic_cast<osgUtil::RenderToTextureStage*>(camera.getRenderingCache());
|
||||
if (!rtts)
|
||||
{
|
||||
rtts = new osgUtil::RenderToTextureStage;
|
||||
camera.setRenderingCache(rtts.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
// reusing render to texture stage, so need to reset it to empty it from previous frames contents.
|
||||
rtts->reset();
|
||||
}
|
||||
|
||||
// set up lighting.
|
||||
// currently ignore lights in the scene graph itself..
|
||||
// will do later.
|
||||
@ -1064,8 +1074,10 @@ void CullVisitor::apply(osg::CameraNode& camera)
|
||||
rtts->setClearColor(camera.getClearColor());
|
||||
rtts->setClearMask(camera.getClearMask());
|
||||
|
||||
osg::Viewport* viewport = camera.getViewport()!=0 ? camera.getViewport() : previous_stage->getViewport();
|
||||
|
||||
// set up the viewport.
|
||||
rtts->setViewport( camera.getViewport()!=0 ? camera.getViewport() : previous_stage->getViewport());
|
||||
rtts->setViewport( viewport );
|
||||
|
||||
// set up to charge the same RenderStageLighting is the parent previous stage.
|
||||
rtts->setRenderStageLighting(previous_stage->getRenderStageLighting());
|
||||
@ -1104,7 +1116,9 @@ void CullVisitor::apply(osg::CameraNode& camera)
|
||||
default :
|
||||
getCurrentRenderBin()->getStage()->addPostRenderStage(rtts.get());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
osg::Texture2D* tex = 0;
|
||||
|
||||
osg::CameraNode::BufferAttachmentMap& bufferAttachements = camera.getBufferAttachmentMap();
|
||||
for(osg::CameraNode::BufferAttachmentMap::iterator itr = bufferAttachements.begin();
|
||||
@ -1114,11 +1128,31 @@ void CullVisitor::apply(osg::CameraNode& camera)
|
||||
|
||||
// if one exist attach texture to the RenderToTextureStage.
|
||||
osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(itr->second._texture.get());
|
||||
if (texture2D) rtts->setTexture(texture2D);
|
||||
if (texture2D)
|
||||
{
|
||||
tex = texture2D;
|
||||
rtts->setTexture(texture2D);
|
||||
}
|
||||
|
||||
|
||||
// if one exist attach image to the RenderToTextureStage.
|
||||
if (itr->second._image.valid()) rtts->setImage(itr->second._image.get());
|
||||
}
|
||||
|
||||
if (camera.getRenderTargetImplmentation()==osg::CameraNode::FRAME_BUFFER_OBJECT)
|
||||
{
|
||||
osg::ref_ptr<osg::FrameBufferObject> fbo = rtts->getFrameBufferObject();
|
||||
|
||||
if (!fbo)
|
||||
{
|
||||
fbo = new osg::FrameBufferObject;
|
||||
rtts->setFrameBufferObject(fbo.get());
|
||||
|
||||
fbo->setAttachment(GL_COLOR_ATTACHMENT0_EXT, osg::FrameBufferAttachment(tex));
|
||||
fbo->setAttachment(GL_DEPTH_ATTACHMENT_EXT, osg::FrameBufferAttachment(new osg::RenderBuffer(viewport->width(), viewport->height(), GL_DEPTH_COMPONENT24)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -39,11 +39,18 @@ void RenderToTextureStage::draw(osg::State& state,RenderLeaf*& previous)
|
||||
if (_stageDrawnThisFrame) return;
|
||||
|
||||
//cout << "begining RTTS draw "<<this<< " "<<_viewport->x()<<","<< _viewport->y()<<","<< _viewport->width()<<","<< _viewport->height()<<std::endl;
|
||||
|
||||
osg::FBOExtensions* fbo_ext = _fbo.valid() ? osg::FBOExtensions::instance(state.getContextID()) : 0;
|
||||
if (fbo_ext)
|
||||
{
|
||||
_fbo->apply(state);
|
||||
}
|
||||
|
||||
|
||||
RenderStage::draw(state,previous);
|
||||
|
||||
// now copy the rendered image to attached texture.
|
||||
if (_texture.valid())
|
||||
if (_texture.valid() && !fbo_ext)
|
||||
{
|
||||
//cout << " reading "<<this<< " "<<_viewport->x()<<","<< _viewport->y()<<","<< _viewport->width()<<","<< _viewport->height()<<std::endl;
|
||||
|
||||
@ -52,5 +59,10 @@ void RenderToTextureStage::draw(osg::State& state,RenderLeaf*& previous)
|
||||
|
||||
if (_image.valid())
|
||||
_image->readPixels(_viewport->x(),_viewport->y(),_viewport->width(),_viewport->height(),_imageReadPixelFormat,_imageReadPixelDataType);
|
||||
|
||||
if (fbo_ext)
|
||||
{
|
||||
fbo_ext->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user