Equalizer  1.2.1
eqPly/vertexBufferDist.cpp
00001 
00002 /* Copyright (c) 2008-2010, 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 using namespace std;
00038 
00039 namespace eqPly 
00040 {
00041 
00042 VertexBufferDist::VertexBufferDist()
00043         : _root( 0 )
00044         , _node( 0 )
00045         , _isRoot( false )
00046         , _left( 0 )
00047         , _right( 0 )
00048 {}
00049 
00050 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root )
00051         : _root( root )
00052         , _node( root )
00053         , _isRoot( true )
00054         , _left( 0 )
00055         , _right( 0 )
00056 {
00057     if( root->getLeft( ))
00058         _left = new VertexBufferDist( root, root->getLeft( ));
00059 
00060     if( root->getRight( ))
00061         _right = new VertexBufferDist( root, root->getRight( ));
00062 }
00063 
00064 VertexBufferDist::VertexBufferDist( const mesh::VertexBufferRoot* root, 
00065                                     const mesh::VertexBufferBase* node )
00066         : _root( root )
00067         , _node( node )
00068         , _isRoot( false )
00069         , _left( 0 )
00070         , _right( 0 )
00071 {
00072     if( !node )
00073         return;
00074 
00075     if( node->getLeft( ))
00076         _left = new VertexBufferDist( root, node->getLeft( ));
00077 
00078     if( node->getRight( ))
00079         _right = new VertexBufferDist( root, node->getRight( ));
00080 }
00081 
00082 VertexBufferDist::~VertexBufferDist()
00083 {
00084     delete _left;
00085     _left = 0;
00086     delete _right;
00087     _right = 0;
00088 }
00089 
00090 void VertexBufferDist::registerTree( co::LocalNodePtr node )
00091 {
00092     EQASSERT( !isAttached() );
00093     EQCHECK( node->registerObject( this ));
00094 
00095     if( _left )
00096         _left->registerTree( node );
00097     
00098     if( _right )
00099         _right->registerTree( node );
00100 }
00101 
00102 void VertexBufferDist::deregisterTree()
00103 {
00104     EQASSERT( isAttached() );
00105     EQASSERT( isMaster( ));
00106 
00107     getLocalNode()->deregisterObject( this );
00108 
00109     if( _left )
00110         _left->deregisterTree();
00111     if( _right )
00112         _right->deregisterTree();
00113 }
00114 
00115 mesh::VertexBufferRoot* VertexBufferDist::mapModel( co::LocalNodePtr node,
00116                                                   const eq::uint128_t& modelID )
00117 {
00118     EQASSERT( !_root && !_node );
00119 
00120     if( !node->mapObject( this, modelID ))
00121     {
00122         EQWARN << "Mapping of model failed" << endl;
00123         return 0;
00124     }
00125 
00126     return const_cast< mesh::VertexBufferRoot* >( _root );
00127 }
00128 
00129 void VertexBufferDist::unmapTree()
00130 {
00131     EQASSERT( isAttached() );
00132     EQASSERT( !isMaster( ));
00133 
00134     getLocalNode()->unmapObject( this );
00135 
00136     if( _left )
00137         _left->unmapTree();
00138     if( _right )
00139         _right->unmapTree();
00140 }
00141 
00142 void VertexBufferDist::getInstanceData( co::DataOStream& os )
00143 {
00144     EQASSERT( _node );
00145     os << _isRoot;
00146 
00147     if( _left && _right )
00148     {
00149         os << _left->getID() << _right->getID();
00150 
00151         if( _isRoot )
00152         {
00153             EQASSERT( _root );
00154             const mesh::VertexBufferData& data = _root->_data;
00155             
00156             os << data.vertices << data.colors << data.normals << data.indices 
00157                << _root->_name;
00158         }
00159     }
00160     else
00161     {
00162         os << co::base::UUID::ZERO << co::base::UUID::ZERO;
00163 
00164         EQASSERT( dynamic_cast< const mesh::VertexBufferLeaf* >( _node ));
00165         const mesh::VertexBufferLeaf* leaf = 
00166             static_cast< const mesh::VertexBufferLeaf* >( _node );
00167 
00168         os << uint64_t( leaf->_vertexStart ) << uint64_t( leaf->_indexStart )
00169            << uint64_t( leaf->_indexLength ) << leaf->_vertexLength;
00170     }
00171 
00172     os << _node->_boundingSphere << _node->_range;
00173 }
00174 
00175 void VertexBufferDist::applyInstanceData( co::DataIStream& is )
00176 {
00177     EQASSERT( !_node );
00178 
00179     mesh::VertexBufferNode* node = 0;
00180     mesh::VertexBufferBase* base = 0;
00181 
00182     co::base::UUID leftID, rightID;
00183     is >> _isRoot >> leftID >> rightID;
00184 
00185     if( leftID != co::base::UUID::ZERO && rightID != co::base::UUID::ZERO )
00186     {
00187         if( _isRoot )
00188         {
00189             mesh::VertexBufferRoot* root = new mesh::VertexBufferRoot;
00190             mesh::VertexBufferData& data = root->_data;
00191 
00192             is >> data.vertices >> data.colors >> data.normals >> data.indices
00193                >> root->_name;
00194 
00195             node  = root;
00196             _root = root;
00197         }
00198         else
00199         {
00200             EQASSERT( _root );
00201             node = new mesh::VertexBufferNode;
00202         }
00203 
00204         base   = node;
00205         _left  = new VertexBufferDist( _root, 0 );
00206         _right = new VertexBufferDist( _root, 0 );
00207         co::LocalNodePtr localNode = getLocalNode();
00208         const uint32_t sync1 = localNode->mapObjectNB( _left, leftID );
00209         const uint32_t sync2 = localNode->mapObjectNB( _right, rightID );
00210 
00211         EQCHECK( localNode->mapObjectSync( sync1 ));
00212         EQCHECK( localNode->mapObjectSync( sync2 ));
00213 
00214         node->_left  = const_cast< mesh::VertexBufferBase* >( _left->_node );
00215         node->_right = const_cast< mesh::VertexBufferBase* >( _right->_node );
00216     }
00217     else
00218     {
00219         EQASSERT( !_isRoot );
00220         mesh::VertexBufferData& data = 
00221             const_cast< mesh::VertexBufferData& >( _root->_data );
00222         mesh::VertexBufferLeaf* leaf = new mesh::VertexBufferLeaf( data );
00223 
00224         uint64_t i1, i2, i3;
00225         is >> i1 >> i2 >> i3 >> leaf->_vertexLength;
00226         leaf->_vertexStart = size_t( i1 );
00227         leaf->_indexStart = size_t( i2 );
00228         leaf->_indexLength = size_t( i3 );
00229 
00230         base = leaf;
00231     }
00232 
00233     EQASSERT( base );
00234     is >> base->_boundingSphere >> base->_range;
00235 
00236     _node = base;
00237 }
00238 
00239 }
Generated on Fri Jun 8 2012 15:44:32 for Equalizer 1.2.1 by  doxygen 1.8.0