OpenWalnut  1.5.0dev
WGEShaderAnimationCallback.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 <ctime>
26 #include <iostream>
27 
28 #include "boost/date_time/posix_time/posix_time.hpp"
29 
30 #include "WGEShaderAnimationCallback.h"
31 
33  osg::Uniform::Callback(),
34  m_start( boost::posix_time::microsec_clock::local_time() ),
35  m_ticksPerSec( ticksPerSecond ),
36  m_tickMillisecRatio( static_cast< double >( ticksPerSecond ) / 1000.0 )
37 {
38 }
39 
41 {
42  // cleanup
43 }
44 
45 void WGEShaderAnimationCallback::operator() ( osg::Uniform* uniform, osg::NodeVisitor* /*nv*/ )
46 {
47  // according to boost doc, this is available on windows too! From boost doc "Get the local time using a sub second resolution clock. On Unix
48  // systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve
49  // microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution.
50  // -> fortunately, millisecond resolution is enough here.
51  boost::posix_time::ptime t = boost::posix_time::microsec_clock::local_time();
52 
53  boost::posix_time::time_duration td = t - m_start;
54  int ticks = static_cast< int >( static_cast< double >( td.total_milliseconds() ) * m_tickMillisecRatio );
55 
56  uniform->set( ticks );
57 }
58 
boost::posix_time::ptime m_start
Timer that stops the time hopefully OS independent.
WGEShaderAnimationCallback(int ticksPerSecond=100)
Default constructor.
virtual void operator()(osg::Uniform *uniform, osg::NodeVisitor *nv)
Operator called on uniform update.
virtual ~WGEShaderAnimationCallback()
Destructor.
double m_tickMillisecRatio
Ratio between milliseconds and m_ticksPerSec.