vertexBufferDist.cpp

00001 /*  
00002  *   Copyright (c) 2008-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  * eq::net::Object to distribute a model. Has a VertexBufferBase node.
00019  */
00020 
00021 #include "vertexBufferDist.h"
00022 
00023 #include "vertexBufferLeaf.h"
00024 
00025 using namespace std;
00026 
00027 namespace eqPly 
00028 {
00029 
00030 VertexBufferDist::VertexBufferDist()
00031         : _root( 0 )
00032         , _node( 0 )
00033         , _isRoot( false )
00034         , _left( 0 )
00035         , _right( 0 )
00036 {}
00037 
00038 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root )
00039         : _root( root )
00040         , _node( root )
00041         , _isRoot( true )
00042         , _left( 0 )
00043         , _right( 0 )
00044 {
00045     if( root->getLeft( ))
00046         _left = new VertexBufferDist( root, root->getLeft( ));
00047 
00048     if( root->getRight( ))
00049         _right = new VertexBufferDist( root, root->getRight( ));
00050 }
00051 
00052 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root, 
00053                                     const mesh::VertexBufferBase* node )
00054         : _root( root )
00055         , _node( node )
00056         , _isRoot( false )
00057         , _left( 0 )
00058         , _right( 0 )
00059 {
00060     if( !node )
00061         return;
00062 
00063     if( node->getLeft( ))
00064         _left = new VertexBufferDist( root, node->getLeft( ));
00065 
00066     if( node->getRight( ))
00067         _right = new VertexBufferDist( root, node->getRight( ));
00068 }
00069 
00070 VertexBufferDist::~VertexBufferDist()
00071 {
00072     delete _left;
00073     _left = 0;
00074     delete _right;
00075     _right = 0;
00076 }
00077 
00078 void VertexBufferDist::registerTree( eq::net::Session* session )
00079 {
00080     EQASSERT( getID() == EQ_ID_INVALID );
00081     session->registerObject( this );
00082 
00083     if( _left )
00084         _left->registerTree( session );
00085     if( _right )
00086         _right->registerTree( session );
00087 }
00088 
00089 void VertexBufferDist::deregisterTree()
00090 {
00091     EQASSERT( getID() != EQ_ID_INVALID );
00092     EQASSERT( isMaster( ));
00093 
00094     getSession()->deregisterObject( this );
00095 
00096     if( _left )
00097         _left->deregisterTree();
00098     if( _right )
00099         _right->deregisterTree();
00100 }
00101 
00102 mesh::VertexBufferRoot* VertexBufferDist::mapModel( eq::net::Session* session,
00103                                                     const uint32_t modelID )
00104 {
00105     EQASSERT( !_root && !_node );
00106 
00107     if( !session->mapObject( this, modelID ))
00108     {
00109         EQWARN << "Mapping of model failed" << endl;
00110         return 0;
00111     }
00112 
00113     return const_cast< mesh::VertexBufferRoot* >( _root );
00114 }
00115 
00116 void VertexBufferDist::unmapTree()
00117 {
00118     EQASSERT( getID() != EQ_ID_INVALID );
00119     EQASSERT( !isMaster( ));
00120 
00121     getSession()->unmapObject( this );
00122 
00123     if( _left )
00124         _left->unmapTree();
00125     if( _right )
00126         _right->unmapTree();
00127 }
00128 
00129 void VertexBufferDist::getInstanceData( eq::net::DataOStream& os )
00130 {
00131     EQASSERT( _node );
00132     os << _isRoot;
00133 
00134     if( _left && _right )
00135     {
00136         os << _left->getID() << _right->getID();
00137 
00138         if( _isRoot )
00139         {
00140             EQASSERT( _root );
00141             const mesh::VertexBufferData& data = _root->_data;
00142             
00143             os << data.vertices << data.colors << data.normals << data.indices 
00144                << _root->_name;
00145         }
00146     }
00147     else
00148     {
00149         os << EQ_ID_INVALID << EQ_ID_INVALID;
00150 
00151         EQASSERT( dynamic_cast< const mesh::VertexBufferLeaf* >( _node ));
00152         const mesh::VertexBufferLeaf* leaf = 
00153             static_cast< const mesh::VertexBufferLeaf* >( _node );
00154 
00155         os << leaf->_vertexStart << leaf->_vertexLength << leaf->_indexStart
00156            << leaf->_indexLength;
00157     }
00158 
00159     os << _node->_boundingSphere << _node->_range;
00160 }
00161 
00162 void VertexBufferDist::applyInstanceData( eq::net::DataIStream& is )
00163 {
00164     EQASSERT( !_node );
00165 
00166     mesh::VertexBufferNode* node = 0;
00167     mesh::VertexBufferBase* base = 0;
00168 
00169     uint32_t leftID, rightID;
00170     is >> _isRoot >> leftID >> rightID;
00171 
00172     if( leftID != EQ_ID_INVALID && rightID != EQ_ID_INVALID )
00173     {
00174         if( _isRoot )
00175         {
00176             mesh::VertexBufferRoot* root = new mesh::VertexBufferRoot;
00177             mesh::VertexBufferData& data = root->_data;
00178 
00179             is >> data.vertices >> data.colors >> data.normals >> data.indices
00180                >> root->_name;
00181 
00182             node  = root;
00183             _root = root;
00184         }
00185         else
00186         {
00187             EQASSERT( _root );
00188             node = new mesh::VertexBufferNode;
00189         }
00190 
00191         base   = node;
00192         _left  = new VertexBufferDist( _root, 0 );
00193         _right = new VertexBufferDist( _root, 0 );
00194 
00195         eq::net::Session* session = getSession();
00196         const uint32_t sync1 = session->mapObjectNB( _left, leftID );
00197         const uint32_t sync2 = session->mapObjectNB( _right, rightID );
00198         EQASSERT( sync1 != EQ_ID_INVALID );
00199         EQASSERT( sync2 != EQ_ID_INVALID );
00200 
00201         EQCHECK( session->mapObjectSync( sync1 ));
00202         EQCHECK( session->mapObjectSync( sync2 ));
00203 
00204         node->_left  = const_cast< mesh::VertexBufferBase* >( _left->_node );
00205         node->_right = const_cast< mesh::VertexBufferBase* >( _right->_node );
00206     }
00207     else
00208     {
00209         EQASSERT( !_isRoot );
00210         mesh::VertexBufferData& data = 
00211             const_cast< mesh::VertexBufferData& >( _root->_data );
00212         mesh::VertexBufferLeaf* leaf = new mesh::VertexBufferLeaf( data );
00213 
00214         is >> leaf->_vertexStart >> leaf->_vertexLength >> leaf->_indexStart
00215            >> leaf->_indexLength;
00216 
00217         base = leaf;
00218     }
00219 
00220     EQASSERT( base );
00221     is >> base->_boundingSphere >> base->_range;
00222 
00223     _node = base;
00224 }
00225 
00226 }
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8