Equalizer  1.4.1
seqPly/vertexBufferDist.cpp
00001 
00002 /* Copyright (c) 2008-2012, Stefan Eilemann <eile@equalizergraphics.com>
00003  *                    2010, Cedric Stalder <cedric.stalder@gmail.com>
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * - Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  * - Redistributions in binary form must reproduce the above copyright notice,
00011  *   this list of conditions and the following disclaimer in the documentation
00012  *   and/or other materials provided with the distribution.
00013  * - Neither the name of Eyescale Software GmbH nor the names of its
00014  *   contributors may be used to endorse or promote products derived from this
00015  *   software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028   
00029  *
00030  * co::Object to distribute a model. Has a VertexBufferBase node.
00031  */
00032 
00033 #include "vertexBufferDist.h"
00034 
00035 #include "vertexBufferLeaf.h"
00036 
00037 namespace eqPly 
00038 {
00039 
00040 VertexBufferDist::VertexBufferDist()
00041         : _root( 0 )
00042         , _node( 0 )
00043         , _left( 0 )
00044         , _right( 0 )
00045         , _isRoot( false )
00046 {}
00047 
00048 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root )
00049         : _root( root )
00050         , _node( root )
00051         , _left( 0 )
00052         , _right( 0 )
00053         , _isRoot( true )
00054 {
00055     if( root->getLeft( ))
00056         _left = new VertexBufferDist( root, root->getLeft( ));
00057 
00058     if( root->getRight( ))
00059         _right = new VertexBufferDist( root, root->getRight( ));
00060 }
00061 
00062 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root, 
00063                                     const mesh::VertexBufferBase* node )
00064         : _root( root )
00065         , _node( node )
00066         , _left( 0 )
00067         , _right( 0 )
00068         , _isRoot( false )
00069 {
00070     if( !node )
00071         return;
00072 
00073     if( node->getLeft( ))
00074         _left = new VertexBufferDist( root, node->getLeft( ));
00075 
00076     if( node->getRight( ))
00077         _right = new VertexBufferDist( root, node->getRight( ));
00078 }
00079 
00080 VertexBufferDist::~VertexBufferDist()
00081 {
00082     delete _left;
00083     _left = 0;
00084     delete _right;
00085     _right = 0;
00086 }
00087 
00088 void VertexBufferDist::registerTree( co::LocalNodePtr node )
00089 {
00090     LBASSERT( !isAttached() );
00091     LBCHECK( node->registerObject( this ));
00092 
00093     if( _left )
00094         _left->registerTree( node );
00095     
00096     if( _right )
00097         _right->registerTree( node );
00098 }
00099 
00100 void VertexBufferDist::deregisterTree()
00101 {
00102     LBASSERT( isAttached() );
00103     LBASSERT( isMaster( ));
00104 
00105     getLocalNode()->deregisterObject( this );
00106 
00107     if( _left )
00108         _left->deregisterTree();
00109     if( _right )
00110         _right->deregisterTree();
00111 }
00112 
00113 mesh::VertexBufferRoot* VertexBufferDist::loadModel( co::NodePtr master,
00114                                                      co::LocalNodePtr localNode,
00115                                                   const eq::uint128_t& modelID )
00116 {
00117     LBASSERT( !_root && !_node );
00118 
00119     const uint32_t req = localNode->mapObjectNB( this, modelID,
00120                                                  co::VERSION_OLDEST, master );
00121     if( !localNode->mapObjectSync( req ))
00122     {
00123         LBWARN << "Mapping of model failed" << std::endl;
00124         return 0;
00125     }
00126 
00127     _unmapTree();
00128     return const_cast< mesh::VertexBufferRoot* >( _root );
00129 }
00130 
00131 void VertexBufferDist::_unmapTree()
00132 {
00133     LBASSERT( isAttached() );
00134     LBASSERT( !isMaster( ));
00135 
00136     getLocalNode()->unmapObject( this );
00137 
00138     if( _left )
00139         _left->_unmapTree();
00140     if( _right )
00141         _right->_unmapTree();
00142 }
00143 
00144 void VertexBufferDist::getInstanceData( co::DataOStream& os )
00145 {
00146     LBASSERT( _node );
00147     os << _isRoot;
00148 
00149     if( _left && _right )
00150     {
00151         os << _left->getID() << _right->getID();
00152 
00153         if( _isRoot )
00154         {
00155             LBASSERT( _root );
00156             const mesh::VertexBufferData& data = _root->_data;
00157             
00158             os << data.vertices << data.colors << data.normals << data.indices 
00159                << _root->_name;
00160         }
00161     }
00162     else
00163     {
00164         os << lunchbox::UUID::ZERO << lunchbox::UUID::ZERO;
00165 
00166         LBASSERT( dynamic_cast< const mesh::VertexBufferLeaf* >( _node ));
00167         const mesh::VertexBufferLeaf* leaf = 
00168             static_cast< const mesh::VertexBufferLeaf* >( _node );
00169 
00170         os << leaf->_boundingBox[0] << leaf->_boundingBox[1]
00171            << uint64_t( leaf->_vertexStart ) << uint64_t( leaf->_indexStart )
00172            << uint64_t( leaf->_indexLength ) << leaf->_vertexLength;
00173     }
00174 
00175     os << _node->_boundingSphere << _node->_range;
00176 }
00177 
00178 void VertexBufferDist::applyInstanceData( co::DataIStream& is )
00179 {
00180     LBASSERT( !_node );
00181 
00182     mesh::VertexBufferNode* node = 0;
00183     mesh::VertexBufferBase* base = 0;
00184 
00185     lunchbox::UUID leftID, rightID;
00186     is >> _isRoot >> leftID >> rightID;
00187 
00188     if( leftID != lunchbox::UUID::ZERO && rightID != lunchbox::UUID::ZERO )
00189     {
00190         if( _isRoot )
00191         {
00192             mesh::VertexBufferRoot* root = new mesh::VertexBufferRoot;
00193             mesh::VertexBufferData& data = root->_data;
00194 
00195             is >> data.vertices >> data.colors >> data.normals >> data.indices
00196                >> root->_name;
00197 
00198             node  = root;
00199             _root = root;
00200         }
00201         else
00202         {
00203             LBASSERT( _root );
00204             node = new mesh::VertexBufferNode;
00205         }
00206 
00207         base   = node;
00208         _left  = new VertexBufferDist( _root, 0 );
00209         _right = new VertexBufferDist( _root, 0 );
00210         co::LocalNodePtr to = getLocalNode();
00211         co::NodePtr from = is.getMaster();
00212         const uint32_t sync1 = to->mapObjectNB( _left, leftID,
00213                                                 co::VERSION_OLDEST, from );
00214         const uint32_t sync2 = to->mapObjectNB( _right, rightID,
00215                                                 co::VERSION_OLDEST, from );
00216 
00217         LBCHECK( to->mapObjectSync( sync1 ));
00218         LBCHECK( to->mapObjectSync( sync2 ));
00219 
00220         node->_left  = const_cast< mesh::VertexBufferBase* >( _left->_node );
00221         node->_right = const_cast< mesh::VertexBufferBase* >( _right->_node );
00222     }
00223     else
00224     {
00225         LBASSERT( !_isRoot );
00226         mesh::VertexBufferData& data = 
00227             const_cast< mesh::VertexBufferData& >( _root->_data );
00228         mesh::VertexBufferLeaf* leaf = new mesh::VertexBufferLeaf( data );
00229 
00230         uint64_t i1, i2, i3;
00231         is >> leaf->_boundingBox[0] >> leaf->_boundingBox[1]
00232            >> i1 >> i2 >> i3 >> leaf->_vertexLength;
00233         leaf->_vertexStart = size_t( i1 );
00234         leaf->_indexStart = size_t( i2 );
00235         leaf->_indexLength = size_t( i3 );
00236 
00237         base = leaf;
00238     }
00239 
00240     LBASSERT( base );
00241     is >> base->_boundingSphere >> base->_range;
00242 
00243     _node = base;
00244 }
00245 
00246 }
Generated on Mon Nov 26 2012 14:41:49 for Equalizer 1.4.1 by  doxygen 1.7.6.1