OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  // setup 2D projection
73  this->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
74  this->setProjectionMatrixAsOrtho2D( 0.0, 1.0, 0.0, 1.0 );
75  this->setViewMatrix( osg::Matrixd::identity() );
76 
77  // enable texture coordinate manipulation via texture matrices
78  m_texMat = new osg::TexMat;
79  m_texMat->setMatrix( osg::Matrixd::identity() );
80  state->setTextureAttributeAndModes( 0, m_texMat, osg::StateAttribute::ON );
81 }
82 
83 void WGEOffscreenTexturePass::TextureMatrixUpdateCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
84 {
85  osg::ref_ptr< osg::TexMat > texMat = m_pass->m_texMat;
86 
87  // 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
88  // viewport.
89  unsigned int screenWidth = m_pass->getViewport()->width();
90  unsigned int screenHeight = m_pass->getViewport()->height();
91 
92  texMat->setMatrix( osg::Matrixd::scale( static_cast< float >( screenWidth ) / static_cast< float >( m_pass->getTextureWidth() ),
93  static_cast< float >( screenHeight )/ static_cast< float >( m_pass->getTextureHeight() ), 1.0 ) );
94 
95  // call nested callbacks
96  traverse( node, nv );
97 }
98