72 lines
2.3 KiB
C++
72 lines
2.3 KiB
C++
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
|
*
|
|
* This library is open source and may be redistributed and/or modified under
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* OpenSceneGraph Public License for more details.
|
|
*/
|
|
#include <osgUtil/RenderToTextureStage>
|
|
|
|
using namespace osg;
|
|
using namespace osgUtil;
|
|
|
|
// register a RenderToTextureStage prototype with the RenderBin prototype list.
|
|
//RegisterRenderBinProxy<RenderToTextureStage> s_registerRenderToTextureStageProxy;
|
|
|
|
RenderToTextureStage::RenderToTextureStage()
|
|
{
|
|
_imageReadPixelFormat = GL_RGBA;
|
|
_imageReadPixelDataType = GL_UNSIGNED_BYTE;
|
|
}
|
|
|
|
RenderToTextureStage::~RenderToTextureStage()
|
|
{
|
|
}
|
|
|
|
void RenderToTextureStage::reset()
|
|
{
|
|
RenderStage::reset();
|
|
}
|
|
|
|
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;
|
|
bool fbo_supported = fbo_ext && fbo_ext->isSupported();
|
|
|
|
if (fbo_supported)
|
|
{
|
|
_fbo->apply(state);
|
|
}
|
|
|
|
|
|
RenderStage::draw(state,previous);
|
|
|
|
// now copy the rendered image to attached texture.
|
|
if (_texture.valid() && !fbo_supported)
|
|
{
|
|
//cout << " reading "<<this<< " "<<_viewport->x()<<","<< _viewport->y()<<","<< _viewport->width()<<","<< _viewport->height()<<std::endl;
|
|
|
|
_texture->copyTexImage2D(state,_viewport->x(),_viewport->y(),_viewport->width(),_viewport->height());
|
|
}
|
|
|
|
if (_image.valid())
|
|
_image->readPixels(_viewport->x(),_viewport->y(),_viewport->width(),_viewport->height(),_imageReadPixelFormat,_imageReadPixelDataType);
|
|
|
|
if (fbo_supported)
|
|
{
|
|
fbo_ext->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
}
|
|
|
|
}
|
|
|