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 #include "cc/output/bsp_tree.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "cc/base/scoped_ptr_deque.h" | |
11 #include "cc/base/scoped_ptr_vector.h" | |
12 #include "cc/output/bsp_compare_result.h" | |
13 #include "cc/quads/draw_polygon.h" | |
14 | |
15 namespace cc { | |
16 | |
17 BspNode::BspNode(scoped_ptr<DrawPolygon> data) : node_data(data.Pass()) { | |
18 } | |
19 | |
20 BspNode::~BspNode() { | |
21 } | |
22 | |
23 BspTree::BspTree(ScopedPtrDeque<DrawPolygon>* list) { | |
24 if (list->size() == 0) | |
25 return; | |
26 | |
27 root_ = make_scoped_ptr(new BspNode(list->take_front())); | |
28 BuildTree(root_.get(), list); | |
29 } | |
30 | |
31 // The idea behind using a deque for BuildTree's input is that we want to be | |
32 // able to place polygons that we've decided aren't splitting plane candidates | |
33 // at the back of the queue while moving the candidate splitting planes to the | |
34 // front when the heuristic decides that they're a better choice. This way we | |
35 // can always simply just take from the front of the deque for our node's | |
36 // data. | |
37 void BspTree::BuildTree(BspNode* node, | |
38 ScopedPtrDeque<DrawPolygon>* polygon_list) { | |
39 ScopedPtrDeque<DrawPolygon> front_list; | |
40 ScopedPtrDeque<DrawPolygon> back_list; | |
41 | |
42 // We take in a list of polygons at this level of the tree, and have to | |
43 // find a splitting plane, then classify polygons as either in front of | |
44 // or behind that splitting plane. | |
45 while (polygon_list->size() > 0) { | |
46 // Is this particular polygon in front of or behind our splitting polygon. | |
47 BspCompareResult comparer_result = | |
48 GetNodePositionRelative(*polygon_list->front(), *(node->node_data)); | |
49 | |
50 // If it's clearly behind or in front of the splitting plane, we use the | |
51 // heuristic to decide whether or not we should put it at the back | |
52 // or front of the list. | |
53 switch (comparer_result) { | |
54 case BSP_FRONT: | |
55 front_list.push_back(polygon_list->take_front().Pass()); | |
56 break; | |
57 case BSP_BACK: | |
58 back_list.push_back(polygon_list->take_front().Pass()); | |
59 break; | |
60 case BSP_SPLIT: | |
61 { | |
62 scoped_ptr<DrawPolygon> polygon; | |
63 scoped_ptr<DrawPolygon> new_front; | |
64 scoped_ptr<DrawPolygon> new_back; | |
65 // Time to split this geometry, *it needs to be split by node_data. | |
66 polygon = polygon_list->take_front(); | |
67 bool split_result = | |
68 polygon->Split(*(node->node_data), &new_front, &new_back); | |
69 DCHECK(split_result); | |
70 if (!split_result) { | |
71 break; | |
72 } | |
73 front_list.push_back(new_front.Pass()); | |
74 back_list.push_back(new_back.Pass()); | |
75 break; | |
76 } | |
77 case BSP_COPLANAR_FRONT: | |
78 node->coplanars_front.push_back(polygon_list->take_front()); | |
79 break; | |
80 case BSP_COPLANAR_BACK: | |
81 node->coplanars_back.push_back(polygon_list->take_front()); | |
82 break; | |
83 default: | |
84 NOTREACHED(); | |
85 break; | |
86 } | |
87 } | |
88 | |
89 // Build the back subtree using the front of the back_list as our splitter. | |
90 if (back_list.size() > 0) { | |
91 node->back_child = make_scoped_ptr(new BspNode(back_list.take_front())); | |
92 BuildTree(node->back_child.get(), &back_list); | |
93 } | |
94 | |
95 // Build the front subtree using the front of the front_list as our splitter. | |
96 if (front_list.size() > 0) { | |
97 node->front_child = | |
98 scoped_ptr<BspNode>(new BspNode(front_list.take_front())); | |
99 BuildTree(node->front_child.get(), &front_list); | |
100 } | |
101 } | |
102 | |
103 BspCompareResult BspTree::GetNodePositionRelative(const DrawPolygon& node_a, | |
104 const DrawPolygon& node_b) { | |
105 return DrawPolygon::SideCompare(node_a, node_b); | |
106 } | |
107 | |
108 // The base comparer with 0,0,0 as camera position facing forward | |
109 BspCompareResult BspTree::GetCameraPositionRelative(const DrawPolygon& node) { | |
110 if (node.normal().z() > 0.0f) { | |
111 return BSP_FRONT; | |
112 } | |
113 return BSP_BACK; | |
114 } | |
115 | |
116 BspTree::~BspTree() { | |
117 } | |
118 | |
119 } // namespace cc | |
OLD | NEW |