OpenWalnut  1.4.0
WGEViewerEffectImageOverlay.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 <osg/Texture2D>
28 #include <osgDB/ReadFile>
29 
30 #include "../common/WPathHelper.h"
31 #include "../common/WProperties.h"
32 #include "../common/WLogger.h"
33 
34 #include "WGEViewer.h"
35 
36 #include "callbacks/WGEFunctorCallback.h"
37 #include "shaders/WGEShader.h"
38 #include "shaders/WGEPropertyUniform.h"
39 
40 #include "WGEViewerEffectImageOverlay.h"
41 
43  WGEViewerEffect( "Image Overlay", "Blend in some arbitrary image." )
44 {
45  m_image = m_properties->addProperty( "Image", "The Image to use.", WPathHelper::getSharePath() / "GE" / "overlay.png" );
46  WPropDouble scale = m_properties->addProperty( "Scale", "Scale the image in percent.", 50.0 );
47  scale->setMin( 0.0 );
48  scale->setMax( 200.0 );
49 
50  WPropBool moveToTop = m_properties->addProperty( "Move to Top", "Move the image to the top.", false );
51  WPropBool moveToRight = m_properties->addProperty( "Move to Right", "Move the image to the right.", true );
52 
53  WPropDouble opacity = m_properties->addProperty( "Opacity",
54  "Make the overlay transparent. Please be aware that the image itself might be transparent already.", 1.0 );
55  opacity->setMin( 0.0 );
56  opacity->setMax( 3.0 );
57 
58  osg::ref_ptr< WGEShader > overlayShader = new WGEShader( "WGECameraOverlayTexture" );
59  overlayShader->apply( m_geode );
60 
61  m_forceReload = true;
62 
63  // texture setup, Loading is done later in the update callback.
64  m_logoTexture = new osg::Texture2D;
65  m_logoTexture->setDataVariance( osg::Object::DYNAMIC );
66  // no wrapping
67  m_logoTexture->setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
68  m_logoTexture->setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
69 
70  // texture width and height (gets set by the update callback)
71  m_overlayWidth = new osg::Uniform( "u_overlayWidth", static_cast< float >( 1 ) );
72  m_overlayHeight = new osg::Uniform( "u_overlayHeight", static_cast< float >( 1 ) );
73  m_state->addUniform( m_overlayWidth );
74  m_state->addUniform( m_overlayHeight );
75  // NOTE: the values of these uniforms get updated by the updateViewport callback, so these values are only placeholders
76  m_viewportWidth = new osg::Uniform( "u_viewportWidth", static_cast< float >( 1024 ) );
77  m_viewportHeight = new osg::Uniform( "u_viewportHeight", static_cast< float >( 768 ) );
78  m_state->addUniform( m_viewportWidth );
79  m_state->addUniform( m_viewportHeight );
80 
81  m_state->addUniform( new WGEPropertyUniform< WPropDouble >( "u_overlayScalePerc", scale ) );
82  m_state->addUniform( new WGEPropertyUniform< WPropBool >( "u_toTop", moveToTop ) );
83  m_state->addUniform( new WGEPropertyUniform< WPropBool >( "u_toRight", moveToRight ) );
84 
85  m_state->addUniform( new WGEPropertyUniform< WPropDouble >( "u_overlayOpacity", opacity ) );
86 
87  // add a callback which handles changes in viewport size
88  m_updater = new Updater();
89  addUpdateCallback( m_updater );
90 
91  // bind
92  m_state->setTextureAttributeAndModes( 0, m_logoTexture, osg::StateAttribute::ON );
93 }
94 
96 {
97  // cleanup
98 }
99 
101 {
102  m_viewer = viewer;
103 }
104 
105 const boost::shared_ptr< WGEViewer > WGEViewerEffectImageOverlay::getReferenceViewer() const
106 {
107  return m_viewer;
108 }
109 
110 void WGEViewerEffectImageOverlay::Updater::operator() ( osg::Node* node, osg::NodeVisitor* nv )
111 {
112  WGEViewerEffectImageOverlay* effect = dynamic_cast< WGEViewerEffectImageOverlay* >( node );
113  if( effect )
114  {
115  // viewer set?
116  if( effect->m_viewer )
117  {
118  // valid camera? -> update viewport
119  WGECamera* cam = effect->m_viewer->getCamera();
120  if( cam )
121  {
122  // valid viewport?
123  if( cam->getViewport() )
124  {
125  effect->m_viewportWidth->set( static_cast< float >( cam->getViewport()->width() ) );
126  effect->m_viewportHeight->set( static_cast< float >( cam->getViewport()->height() ) );
127  }
128  }
129 
130  // update image if needed
131  if( effect->m_forceReload || effect->m_image->changed() )
132  {
133  effect->m_forceReload = false;
134  osg::Image* logoImage = osgDB::readImageFile( effect->m_image->get( true ).string() );
135  if( logoImage )
136  {
137  effect->m_logoTexture->setImage( logoImage );
138  effect->m_overlayWidth->set( static_cast< float >( logoImage->s() ) );
139  effect->m_overlayHeight->set( static_cast< float >( logoImage->t() ) );
140  }
141  }
142  }
143  }
144 
145  // call nested callbacks
146  traverse( node, nv );
147  return;
148 }
149