server/observer.cpp

00001 
00002 /* Copyright (c) 2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "observer.h"
00019 
00020 #include "config.h"
00021 #include "global.h"
00022 #include "paths.h"
00023 
00024 #include <eq/net/dataIStream.h>
00025 #include <eq/net/dataOStream.h>
00026 
00027 using namespace eq::base;
00028 
00029 namespace eq
00030 {
00031 namespace server
00032 {
00033 
00034 Observer::Observer()
00035         : _config( 0 )
00036         , _inverseHeadMatrix( Matrix4f::IDENTITY )
00037 {
00038 #ifdef EQ_USE_DEPRECATED
00039     setEyeBase(Global::instance()->getConfigFAttribute(Config::FATTR_EYE_BASE));
00040 #endif
00041     _updateEyes();
00042 }
00043 
00044 Observer::Observer( const Observer& from, Config* config )
00045         : eq::Observer( from )
00046         , _config( 0 )
00047         , _inverseHeadMatrix( from._inverseHeadMatrix )
00048 {
00049     config->addObserver( this );
00050     EQASSERT( _config );
00051     _updateEyes();
00052 }
00053 
00054 Observer::~Observer()
00055 {
00056 }
00057 
00058 void Observer::getInstanceData( net::DataOStream& os )
00059 {
00060     // This function is overwritten from eq::Object, since the class is
00061     // intended to be subclassed on the client side. When serializing a
00062     // server::Observer, we only transmit the effective bits, not all since that
00063     // potentially includes bits from subclassed eq::Observers.
00064     const uint64_t dirty = DIRTY_CUSTOM - 1;
00065     os << dirty;
00066     serialize( os, dirty );
00067 }
00068 
00069 void Observer::deserialize( net::DataIStream& is, const uint64_t dirtyBits )
00070 {
00071     eq::Observer::deserialize( is, dirtyBits );
00072 
00073     if( dirtyBits & ( DIRTY_EYE_BASE | DIRTY_HEAD ))
00074         _updateEyes();
00075     if( dirtyBits & DIRTY_HEAD )
00076         getHeadMatrix().inverse( _inverseHeadMatrix );
00077 }
00078 
00079 ObserverPath Observer::getPath() const
00080 {
00081     EQASSERT( _config );
00082     
00083     const ObserverVector&  observers = _config->getObservers();
00084     ObserverVector::const_iterator i = std::find( observers.begin(), 
00085                                                   observers.end(), this );
00086     EQASSERT( i != observers.end( ));
00087 
00088     ObserverPath path;
00089     path.observerIndex = std::distance( observers.begin(), i );
00090     return path;
00091 }
00092 
00093 void Observer::unmap()
00094 {
00095     net::Session* session = getSession();
00096     EQASSERT( session );
00097     EQASSERT( getID() != EQ_ID_INVALID );
00098 
00099     session->unmapObject( this );
00100 }
00101 
00102 void Observer::init()
00103 {
00104     _updateEyes();
00105     getHeadMatrix().inverse( _inverseHeadMatrix );
00106 }
00107     
00108 void Observer::_updateEyes()
00109 {
00110     const float eyeBase_2 = .5f * getEyeBase();
00111     const Matrix4f& head = getHeadMatrix();
00112 
00113     // eye_world = (+-eye_base/2., 0, 0 ) x head_matrix
00114     // OPT: don't use vector operator* due to possible simplification
00115 
00116     _eyes[eq::EYE_CYCLOP].x() = head.at( 0, 3 );
00117     _eyes[eq::EYE_CYCLOP].y() = head.at( 1, 3 );
00118     _eyes[eq::EYE_CYCLOP].z() = head.at( 2, 3 );
00119     _eyes[eq::EYE_CYCLOP]    /= head.at( 3, 3 );
00120 
00121     _eyes[eq::EYE_LEFT].x() = ( -eyeBase_2 * head.at( 0, 0 ) + head.at( 0, 3 ));
00122     _eyes[eq::EYE_LEFT].y() = ( -eyeBase_2 * head.at( 1, 0 ) + head.at( 1, 3 ));
00123     _eyes[eq::EYE_LEFT].z() = ( -eyeBase_2 * head.at( 2, 0 ) + head.at( 2, 3 ));
00124     _eyes[eq::EYE_LEFT]    /= ( -eyeBase_2 * head.at( 3, 0 ) + head.at( 3, 3 ));
00125 
00126     _eyes[eq::EYE_RIGHT].x() = ( eyeBase_2 * head.at( 0, 0 ) + head.at( 0, 3 ));
00127     _eyes[eq::EYE_RIGHT].y() = ( eyeBase_2 * head.at( 1, 0 ) + head.at( 1, 3 ));
00128     _eyes[eq::EYE_RIGHT].z() = ( eyeBase_2 * head.at( 2, 0 ) + head.at( 2, 3 ));
00129     _eyes[eq::EYE_RIGHT]    /= ( eyeBase_2 * head.at( 3, 0 ) + head.at( 3, 3 ));
00130 
00131     EQVERB << "Eye position: " << _eyes[ eq:: EYE_CYCLOP ] << std::endl;
00132 }
00133 
00134 
00135 }
00136 }
Generated on Mon Aug 10 18:58:40 2009 for Equalizer 0.9 by  doxygen 1.5.8