Advertisement
Guest User

renderGraph.cpp

a guest
May 7th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <algorithm>
  2.  
  3. #include "renderGraph.hpp"
  4.  
  5. void RenderGraph::SetEntities( const std::vector< Entity* >& a_entities ) {
  6.     for ( unsigned int i = 0; i < a_entities.size(); ++i ) {
  7.         nodes_.push_back( Node( a_entities[ i ] ) );
  8.     };
  9. };
  10.  
  11. void RenderGraph::CalculateDependencies() {
  12.     for ( unsigned int j = 0; j < nodes_.size(); ++j ) {
  13.         AABB::MinMax a = nodes_[ j ].entity_->GetMinMax();
  14.  
  15.         for ( unsigned int k = 0; k < nodes_.size(); ++k ) {
  16.             if ( j != k ) {
  17.                 AABB::MinMax b = nodes_[ k ].entity_->GetMinMax();
  18.                 if ( b.min.x < a.max.x && b.min.y < a.max.y && b.min.z < a.max.z ) {
  19.                     nodes_[ j ].dependencies_.push_back( &nodes_[ k ] );
  20.                 };
  21.             };
  22.         };
  23.     };
  24. };
  25.  
  26. void RenderGraph::Sort( std::vector< Entity* >& a_output ) {
  27.     int sortDepth = 0;
  28.  
  29.     for ( unsigned int i = 0; i < nodes_.size(); ++i ) {
  30.         VisitNode( sortDepth, &nodes_[ i ] );
  31.     };
  32.  
  33.     std::sort( nodes_.begin(), nodes_.end(), Comparator() );
  34.  
  35.     a_output.clear();
  36.     for ( unsigned int i = 0; i < nodes_.size(); ++i ) {
  37.         a_output.push_back( nodes_[ i ].entity_ );
  38.     };
  39. };
  40.  
  41. void RenderGraph::VisitNode( int& sortDepth, Node* a_node ) {
  42.     if ( !a_node->visited_ ) {
  43.         a_node->visited_ = true;
  44.  
  45.         for ( unsigned int i = 0; i < a_node->dependencies_.size(); ++i ) {
  46.             if ( a_node->dependencies_[ i ] == nullptr ) {
  47.                 break;
  48.             } else {
  49.                 VisitNode( sortDepth, a_node->dependencies_[ i ] );
  50.                 a_node->dependencies_[ i ] = nullptr;
  51.             };
  52.         };
  53.  
  54.         a_node->isoDepth_ = sortDepth++;
  55.     };
  56. };
  57.  
  58. bool RenderGraph::Comparator::operator()( const Node& a_lhs, const Node& a_rhs ) const {
  59.     return a_lhs.isoDepth_ < a_rhs.isoDepth_;
  60. };
  61.  
  62. RenderGraph::Node::Node( Entity* a_entity ) {
  63.     entity_ = a_entity;
  64.     isoDepth_ = -1;
  65.     visited_ = false;
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement