OpenWalnut  1.5.0dev
WGEOffscreenTexturePass.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 
27 #include "../WGETextureHud.h"
28 #include "../WGEGeodeUtils.h"
29 
30 #include "WGEOffscreenTexturePass.h"
31 
32 WGEOffscreenTexturePass::WGEOffscreenTexturePass( size_t textureWidth, size_t textureHeight, int num ):
33  WGEOffscreenRenderPass( textureWidth, textureHeight, num )
34 {
35  // initialize members
36  setup();
37 }
38 
39 WGEOffscreenTexturePass::WGEOffscreenTexturePass( size_t textureWidth, size_t textureHeight, osg::ref_ptr< WGETextureHud > hud, std::string name,
40  int num ):
41  WGEOffscreenRenderPass( textureWidth, textureHeight, hud, name, num )
42 {
43  // initialize members
44  setup();
45 }
46 
48 {
49  // cleanup
50 }
51 
53 {
54  // we need to create a nice quad for texture processing spanning the whole texture space
55  osg::ref_ptr< osg::Geode > geode = wge::genFinitePlane( osg::Vec3( 0.0, 0.0, 0.0 ),
56  osg::Vec3( 1.0, 0.0, 0.0 ),
57  osg::Vec3( 0.0, 1.0, 0.0 ) );
58  geode->setName( "Offscreen Rendering Proxy Plane" );
59 
60 
61  // setup the texture matrix scaler to the geode
62  geode->addUpdateCallback( new TextureMatrixUpdateCallback( this ) );
63 
64  // add the slice to the geode
65  this->addChild( geode );
66 
67  // disable all annoying states as blending, lighting and so on is done via shaders
68  osg::StateSet* state = this->getOrCreateStateSet();
69  state->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
70  state->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED );
71  state->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
72  state->setMode( GL_BLEND, osg::StateAttribute::PROTECTED );
73  state->setMode( GL_BLEND, osg::StateAttribute::OFF );
74 
75  // avoid culling
76  setCullingActive( false );
77 
78  // setup 2D projection
79  this->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
80  this->setProjectionMatrixAsOrtho2D( 0.0, 1.0, 0.0, 1.0 );
81  this->setViewMatrix( osg::Matrixd::identity() );
82 
83  // enable texture coordinate manipulation via texture matrices
84  m_texMat = new osg::TexMat;
85  m_texMat->setMatrix( osg::Matrixd::identity() );
86  state->setTextureAttributeAndModes( 0, m_texMat, osg::StateAttribute::ON );
87 }
88 
89 void WGEOffscreenTexturePass::TextureMatrixUpdateCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
90 {
91  osg::ref_ptr< osg::TexMat > texMat = m_pass->m_texMat;
92 
93  // we need all this to scale the texture coordinates to match the viewport of the camera as the whole texture might be larger than the
94  // viewport.
95  unsigned int screenWidth = m_pass->getViewport()->width();
96  unsigned int screenHeight = m_pass->getViewport()->height();
97 
98  texMat->setMatrix( osg::Matrixd::scale( static_cast< float >( screenWidth ) / static_cast< float >( m_pass->getTextureWidth() ),
99  static_cast< float >( screenHeight )/ static_cast< float >( m_pass->getTextureHeight() ), 1.0 ) );
100 
101  // call nested callbacks
102  traverse( node, nv );
103 }
This class encapsulates an OSG Camera and a corresponding framebuffer object.
size_t getTextureWidth() const
Get the size of the underlying texture.
size_t getTextureHeight() const
Get the size of the underlying texture.
Callback which aligns and renders the textures.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
operator () - called during the update traversal.
WGEOffscreenTexturePass * m_pass
The pass used in conjunction with this callback.
osg::ref_ptr< osg::TexMat > m_texMat
The texture matrix for this pass.
virtual ~WGEOffscreenTexturePass()
Destructor.
void setup()
Sets the whole node up.
WGEOffscreenTexturePass(size_t textureWidth, size_t textureHeight, int num=0)
Creates a new offscreen rendering instance.
osg::ref_ptr< osg::Geode > genFinitePlane(double xSize, double ySize, const WPlane &p, const WColor &color=WColor(0.0, 0.7, 0.7, 1.0), bool border=false)
Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.