OpenWalnut  1.5.0dev
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 <memory>
26 #include <string>
27 
28 #include <osg/Texture2D>
29 #include <osgDB/ReadFile>
30 
31 #include "../common/WLogger.h"
32 #include "../common/WPathHelper.h"
33 #include "../common/WProperties.h"
34 #include "WGEViewer.h"
35 #include "WGEViewerEffectImageOverlay.h"
36 #include "callbacks/WGEFunctorCallback.h"
37 #include "shaders/WGEPropertyUniform.h"
38 #include "shaders/WGEShader.h"
39 
41  WGEViewerEffect( "Image Overlay", "Blend in some arbitrary image." )
42 {
43  m_blendOutAuto = m_properties->addProperty( "Auto Blend-Out",
44  "Make the overlay blend out automatically after a given amount of time.", true );
45 
46  m_blendOutDuration = m_properties->addProperty( "Blend-Out Duration",
47  "Make the overlay blend out after this amount of time in seconds.", 4.0 );
48  m_blendOutDuration->setMin( 0.0 );
49  m_blendOutDuration->setMax( 60.0 );
50 
51  m_image = m_properties->addProperty( "Image", "The Image to use.", WPathHelper::getSharePath() / "GE" / "overlay.png" );
52  WPropDouble scale = m_properties->addProperty( "Scale", "Scale the image in percent.", 50.0 );
53  scale->setMin( 0.0 );
54  scale->setMax( 200.0 );
55 
56  WPropBool moveToTop = m_properties->addProperty( "Move to Top", "Move the image to the top.", false );
57  WPropBool moveToRight = m_properties->addProperty( "Move to Right", "Move the image to the right.", true );
58 
59  WPropDouble opacity = m_properties->addProperty( "Opacity",
60  "Make the overlay transparent. Please be aware that the image itself might be transparent already.", 1.0 );
61  opacity->setMin( 0.0 );
62  opacity->setMax( 3.0 );
63 
64  osg::ref_ptr< WGEShader > overlayShader = new WGEShader( "WGECameraOverlayTexture" );
65  overlayShader->apply( m_geode );
66 
67  m_forceReload = true;
68 
69  // texture setup, Loading is done later in the update callback.
70  m_logoTexture = new osg::Texture2D;
71  m_logoTexture->setDataVariance( osg::Object::DYNAMIC );
72  // no wrapping
73  m_logoTexture->setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
74  m_logoTexture->setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
75 
76  // texture width and height (gets set by the update callback)
77  m_overlayWidth = new osg::Uniform( "u_overlayWidth", static_cast< float >( 1 ) );
78  m_overlayHeight = new osg::Uniform( "u_overlayHeight", static_cast< float >( 1 ) );
79  m_state->addUniform( m_overlayWidth );
80  m_state->addUniform( m_overlayHeight );
81  // NOTE: the values of these uniforms get updated by the updateViewport callback, so these values are only placeholders
82  m_viewportWidth = new osg::Uniform( "u_viewportWidth", static_cast< float >( 1024 ) );
83  m_viewportHeight = new osg::Uniform( "u_viewportHeight", static_cast< float >( 768 ) );
84  m_state->addUniform( m_viewportWidth );
85  m_state->addUniform( m_viewportHeight );
86 
87  m_state->addUniform( new WGEPropertyUniform< WPropDouble >( "u_overlayScalePerc", scale ) );
88  m_state->addUniform( new WGEPropertyUniform< WPropBool >( "u_toTop", moveToTop ) );
89  m_state->addUniform( new WGEPropertyUniform< WPropBool >( "u_toRight", moveToRight ) );
90 
91  m_state->addUniform( new WGEPropertyUniform< WPropDouble >( "u_overlayOpacity", opacity ) );
92  m_state->addUniform( new WGEPropertyUniform< WPropBool >( "u_overlayAutoBlendOut", m_blendOutAuto ) );
93  m_state->addUniform( new WGEPropertyUniform< WPropDouble >( "u_overlayBlendOutDuration", m_blendOutDuration ) );
94 
95 
96  // add a callback which handles changes in viewport size
97  m_updater = new Updater();
98  addUpdateCallback( m_updater );
99 
100  // also add an animation timer
101  osg::Uniform* animationTime(
102  new osg::Uniform(
103  "u_blendOutTimer", // a name.
104  0 // initial value, type of this parameter defines the uniform type in GLSL!
105  )
106  );
107  // use the predefined timer callback:
108  osg::ref_ptr< WGEShaderAnimationCallback > m_animationCallback = new WGEShaderAnimationCallback();
109  animationTime->setUpdateCallback( m_animationCallback );
110 
111  // bind to geode
112  m_geode->getOrCreateStateSet()->addUniform( animationTime );
113 
114  // bind
115  m_state->setTextureAttributeAndModes( 0, m_logoTexture, osg::StateAttribute::ON );
116 }
117 
119 {
120  // cleanup
121 }
122 
124 {
125  m_viewer = viewer;
126 }
127 
128 const std::shared_ptr< WGEViewer > WGEViewerEffectImageOverlay::getReferenceViewer() const
129 {
130  return m_viewer;
131 }
132 
133 void WGEViewerEffectImageOverlay::Updater::operator() ( osg::Node* node, osg::NodeVisitor* nv )
134 {
135  WGEViewerEffectImageOverlay* effect = dynamic_cast< WGEViewerEffectImageOverlay* >( node );
136  if( effect )
137  {
138  // viewer set?
139  if( effect->m_viewer )
140  {
141  // valid camera? -> update viewport
142  WGECamera* cam = effect->m_viewer->getCamera();
143  if( cam )
144  {
145  // valid viewport?
146  if( cam->getViewport() )
147  {
148  effect->m_viewportWidth->set( static_cast< float >( cam->getViewport()->width() ) );
149  effect->m_viewportHeight->set( static_cast< float >( cam->getViewport()->height() ) );
150  }
151  }
152 
153  // update image if needed
154  if( effect->m_forceReload || effect->m_image->changed() )
155  {
156  effect->m_forceReload = false;
157  osg::Image* logoImage = osgDB::readImageFile( effect->m_image->get( true ).string() );
158  if( logoImage )
159  {
160  effect->m_logoTexture->setImage( logoImage );
161  effect->m_overlayWidth->set( static_cast< float >( logoImage->s() ) );
162  effect->m_overlayHeight->set( static_cast< float >( logoImage->t() ) );
163  }
164  }
165  }
166  }
167 
168  // call nested callbacks
169  traverse( node, nv );
170  return;
171 }
172 
Class for wrapping around the OSG Camera class.
Definition: WGECamera.h:36
Class implementing a uniform which can be controlled by a property instance.
This is a uniform callback setting the uniform to the current time in milliseconds,...
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.
Definition: WGEShader.h:48
Update the uniforms and textures if needed.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
Called before draw on update.
WPropBool m_blendOutAuto
Enable to blend out the overlay.
osg::ref_ptr< osg::Uniform > m_overlayWidth
Width of the image in pixels.
osg::ref_ptr< osg::Uniform > m_viewportHeight
Height of the viewport in pixels.
osg::ref_ptr< osg::Texture2D > m_logoTexture
The texture.
const std::shared_ptr< WGEViewer > getReferenceViewer() const
Query current reference viewer.
osg::ref_ptr< osg::Uniform > m_viewportWidth
Width of the viewport in pixels.
WPropFilename m_image
The filename to load.
WPropDouble m_blendOutDuration
Time to blend out in seconds.
std::shared_ptr< WGEViewer > m_viewer
The viewer used to query the current reference cam.
WGEViewerEffectImageOverlay()
Default constructor.
osg::ref_ptr< osg::Uniform > m_overlayHeight
Height of the image in pixels.
osg::ref_ptr< Updater > m_updater
Update callback for the viewport.
osg::ref_ptr< WGEShaderAnimationCallback > m_animationCallback
Animate blend out sequence.
bool m_forceReload
If true, the update callback is forced to reload the image.
void setReferenceViewer(std::shared_ptr< WGEViewer > viewer)
Set the reference viewer.
Base class for implementing basic fullscreen effects for the WGEViewer.
osg::ref_ptr< osg::StateSet > m_state
The stateset of the cam.
osg::ref_ptr< osg::Geode > m_geode
The fullscreen quad.
std::shared_ptr< WGEViewer > SPtr
Convenience typedef.
Definition: WGEViewer.h:76
WProperties::SPtr m_properties
the properties of the object.
Definition: WObjectNDIP.h:99
static boost::filesystem::path getSharePath()
The path where shared files reside in.