OpenWalnut  1.4.0
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  // setup the texture matrix scaler to the geode
59  geode->addUpdateCallback( new TextureMatrixUpdateCallback( this ) );
60 
61  // add the slice to the geode
62  this->addChild( geode );
63 
64  // disable all annoying states as blending, lighting and so on is done via shaders
65  osg::StateSet* state = this->getOrCreateStateSet();
66  state->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
67  state->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED );
68  state->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
69  state->setMode( GL_BLEND, osg::StateAttribute::PROTECTED );
70  state->setMode( GL_BLEND, osg::StateAttribute::OFF );
71 
72  // avoid culling
73  setCullingActive( false );
74 
75  // setup 2D projection
76  this->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
77  this->setProjectionMatrixAsOrtho2D( 0.0, 1.0, 0.0, 1.0 );
78  this->setViewMatrix( osg::Matrixd::identity() );
79 
80  // enable texture coordinate manipulation via texture matrices
81  m_texMat = new osg::TexMat;
82  m_texMat->setMatrix( osg::Matrixd::identity() );
83  state->setTextureAttributeAndModes( 0, m_texMat, osg::StateAttribute::ON );
84 }
85 
86 void WGEOffscreenTexturePass::TextureMatrixUpdateCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
87 {
88  osg::ref_ptr< osg::TexMat > texMat = m_pass->m_texMat;
89 
90  // 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
91  // viewport.
92  unsigned int screenWidth = m_pass->getViewport()->width();
93  unsigned int screenHeight = m_pass->getViewport()->height();
94 
95  texMat->setMatrix( osg::Matrixd::scale( static_cast< float >( screenWidth ) / static_cast< float >( m_pass->getTextureWidth() ),
96  static_cast< float >( screenHeight )/ static_cast< float >( m_pass->getTextureHeight() ), 1.0 ) );
97 
98  // call nested callbacks
99  traverse( node, nv );
100 }
101 
This class encapsulates an OSG Camera and a corresponding framebuffer object.
size_t getTextureHeight() const
Get the size of the underlying texture.
WGEOffscreenTexturePass * m_pass
The pass used in conjunction with this callback.
void setup()
Sets the whole node up.
virtual ~WGEOffscreenTexturePass()
Destructor.
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...
size_t getTextureWidth() const
Get the size of the underlying texture.
osg::ref_ptr< osg::TexMat > m_texMat
The texture matrix for this pass.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
operator () - called during the update traversal.
Callback which aligns and renders the textures.
WGEOffscreenTexturePass(size_t textureWidth, size_t textureHeight, int num=0)
Creates a new offscreen rendering instance.