| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CC_OUTPUT_BSP_TREE_H_ | |
| 6 #define CC_OUTPUT_BSP_TREE_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "cc/base/scoped_ptr_deque.h" | |
| 12 #include "cc/base/scoped_ptr_vector.h" | |
| 13 #include "cc/output/bsp_compare_result.h" | |
| 14 #include "cc/quads/draw_polygon.h" | |
| 15 | |
| 16 namespace cc { | |
| 17 | |
| 18 struct BspNode { | |
| 19 // This represents the splitting plane. | |
| 20 scoped_ptr<DrawPolygon> node_data; | |
| 21 // This represents any coplanar geometry we found while building the BSP. | |
| 22 ScopedPtrVector<DrawPolygon> coplanars_front; | |
| 23 ScopedPtrVector<DrawPolygon> coplanars_back; | |
| 24 | |
| 25 scoped_ptr<BspNode> back_child; | |
| 26 scoped_ptr<BspNode> front_child; | |
| 27 | |
| 28 explicit BspNode(scoped_ptr<DrawPolygon> data); | |
| 29 ~BspNode(); | |
| 30 }; | |
| 31 | |
| 32 class BspTree { | |
| 33 public: | |
| 34 explicit BspTree(ScopedPtrDeque<DrawPolygon>* list); | |
| 35 scoped_ptr<BspNode>& root() { return root_; } | |
| 36 | |
| 37 template <typename ActionHandlerType> | |
| 38 void TraverseWithActionHandler(ActionHandlerType* action_handler) const { | |
| 39 if (root_) { | |
| 40 WalkInOrderRecursion<ActionHandlerType>(action_handler, root_.get()); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 ~BspTree(); | |
| 45 | |
| 46 private: | |
| 47 scoped_ptr<BspNode> root_; | |
| 48 | |
| 49 void FromList(ScopedPtrVector<DrawPolygon>* list); | |
| 50 void BuildTree(BspNode* node, ScopedPtrDeque<DrawPolygon>* data); | |
| 51 | |
| 52 template <typename ActionHandlerType> | |
| 53 void WalkInOrderAction(ActionHandlerType* action_handler, | |
| 54 DrawPolygon* item) const { | |
| 55 (*action_handler)(item); | |
| 56 } | |
| 57 | |
| 58 template <typename ActionHandlerType> | |
| 59 void WalkInOrderVisitNodes( | |
| 60 ActionHandlerType* action_handler, | |
| 61 const BspNode* node, | |
| 62 const BspNode* first_child, | |
| 63 const BspNode* second_child, | |
| 64 const ScopedPtrVector<DrawPolygon>& first_coplanars, | |
| 65 const ScopedPtrVector<DrawPolygon>& second_coplanars) const { | |
| 66 if (first_child) { | |
| 67 WalkInOrderRecursion(action_handler, first_child); | |
| 68 } | |
| 69 for (size_t i = 0; i < first_coplanars.size(); i++) { | |
| 70 WalkInOrderAction(action_handler, first_coplanars[i]); | |
| 71 } | |
| 72 WalkInOrderAction(action_handler, node->node_data.get()); | |
| 73 for (size_t i = 0; i < second_coplanars.size(); i++) { | |
| 74 WalkInOrderAction(action_handler, second_coplanars[i]); | |
| 75 } | |
| 76 if (second_child) { | |
| 77 WalkInOrderRecursion(action_handler, second_child); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 template <typename ActionHandlerType> | |
| 82 void WalkInOrderRecursion(ActionHandlerType* action_handler, | |
| 83 const BspNode* node) const { | |
| 84 // If our view is in front of the the polygon | |
| 85 // in this node then walk back then front. | |
| 86 if (GetCameraPositionRelative(*(node->node_data)) == BSP_FRONT) { | |
| 87 WalkInOrderVisitNodes<ActionHandlerType>(action_handler, | |
| 88 node, | |
| 89 node->back_child.get(), | |
| 90 node->front_child.get(), | |
| 91 node->coplanars_front, | |
| 92 node->coplanars_back); | |
| 93 } else { | |
| 94 WalkInOrderVisitNodes<ActionHandlerType>(action_handler, | |
| 95 node, | |
| 96 node->front_child.get(), | |
| 97 node->back_child.get(), | |
| 98 node->coplanars_back, | |
| 99 node->coplanars_front); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 // Returns whether or not nodeA is on one or the other side of nodeB, | |
| 104 // coplanar, or whether it crosses nodeB's plane and needs to be split | |
| 105 static BspCompareResult GetNodePositionRelative(const DrawPolygon& node_a, | |
| 106 const DrawPolygon& node_b); | |
| 107 // Returns whether or not our viewer is in front of or behind the plane | |
| 108 // defined by this polygon/node | |
| 109 static BspCompareResult GetCameraPositionRelative(const DrawPolygon& node); | |
| 110 }; | |
| 111 | |
| 112 } // namespace cc | |
| 113 | |
| 114 #endif // CC_OUTPUT_BSP_TREE_H_ | |
| OLD | NEW |