Chromium Code Reviews| Index: cc/output/bsp_tree.cc |
| diff --git a/cc/output/bsp_tree.cc b/cc/output/bsp_tree.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa1dffbea893837a72be7aa247cf0c87551e5e91 |
| --- /dev/null |
| +++ b/cc/output/bsp_tree.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/output/bsp_tree.h" |
| + |
| +#include <list> |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "cc/base/scoped_ptr_deque.h" |
| +#include "cc/base/scoped_ptr_vector.h" |
| +#include "cc/output/bsp_compare_result.h" |
| +#include "cc/output/bsp_controller.h" |
| +#include "cc/quads/draw_polygon.h" |
| + |
| +namespace cc { |
| + |
| +BspNode::BspNode(scoped_ptr<DrawPolygon> data) { |
| + node_data = data.Pass(); |
| +} |
| + |
| +BspNode::~BspNode() { |
| +} |
| + |
| +BspTree::BspTree(BspController* bsp_controller, |
| + ScopedPtrVector<DrawPolygon>* list) |
| + : controller_(bsp_controller) { |
| + FromList(list); |
| +} |
| + |
| +void BspTree::FromList(ScopedPtrVector<DrawPolygon>* list) { |
| + if (list->size() <= 0) |
|
Ian Vollick
2014/07/16 20:54:32
Can size < 0?
troyhildebrandt
2014/07/18 21:48:26
Nope, it's unsigned, changed to == 0.
|
| + return; |
| + |
| + float split_weight = 0.0f; |
|
Ian Vollick
2014/07/16 20:54:33
nit: max_weight or max_split_weight conveys a bit
troyhildebrandt
2014/07/18 21:48:25
Done.
|
| + ScopedPtrDeque<DrawPolygon> list_data; |
| + for (ScopedPtrVector<DrawPolygon>::iterator it = list->begin(); |
| + it != list->end(); |
| + ++it) { |
| + float poly_weight = controller_->SplitWeight(*list->back()); |
| + if (poly_weight > split_weight) { |
| + list_data.push_front(list->take(it)); |
| + split_weight = poly_weight; |
| + } else { |
| + list_data.push_back(list->take(it)); |
| + } |
| + } |
| + |
| + root_ = scoped_ptr<BspNode>(new BspNode(list_data.take_front())); |
| + BuildTree(root_, &list_data); |
| +} |
| + |
| +void BspTree::BuildTree(const scoped_ptr<BspNode>& node, |
| + ScopedPtrDeque<DrawPolygon>* data) { |
| + ScopedPtrDeque<DrawPolygon> front_list; |
| + ScopedPtrDeque<DrawPolygon> back_list; |
| + // We keep track of the splitting weights (the "score" of a polygon that |
| + // chooses whether or not it's the splitting plane of a new node) for both |
| + // the front and back list independently so the better splitting plane is |
| + // chosen for both sides. |
| + float front_weight = 0.0f; |
|
Ian Vollick
2014/07/16 20:54:32
Could you choose names for these that involve "max
troyhildebrandt
2014/07/18 21:48:26
Done.
|
| + float back_weight = 0.0f; |
| + |
| + // We take in a list of polygons at this level of the tree, and have to |
| + // find a splitting plane, then classify polygons as either in front of |
| + // or behind that splitting plane. |
| + while (data->size() > 0) { |
| + // Is this particular polygon in front of or behind our splitting polygon. |
| + BspCompareResult comparer_result = controller_->GetNodePositionRelative( |
| + *data->front(), *(node->node_data)); |
| + float poly_weight = controller_->SplitWeight(*data->front()); |
| + |
| + // If it's clearly behind or in front of the splitting plane, we use the |
| + // heuristic to decide whether or not we should put it at the back |
| + // or front of the list. |
| + if (comparer_result == BSP_FRONT) { |
|
Ian Vollick
2014/07/16 20:54:32
switch?
troyhildebrandt
2014/07/18 21:48:26
Done.
|
| + if (poly_weight > front_weight) { |
| + front_list.push_front(data->take_front()); |
| + front_weight = poly_weight; |
| + } else { |
| + front_list.push_back(data->take_front()); |
| + } |
| + |
| + } else if (comparer_result == BSP_BACK) { |
| + if (poly_weight > back_weight) { |
| + back_list.push_front(data->take_front()); |
| + back_weight = poly_weight; |
| + } else { |
| + back_list.push_back(data->take_front()); |
|
Ian Vollick
2014/07/16 20:54:33
The front and back behavior is so symmetric that i
troyhildebrandt
2014/07/18 21:48:26
Done.
|
| + } |
| + |
| + } else if (comparer_result == BSP_SPLIT) { |
| + // Time to split this geometry, *it needs to be split by node_data. |
| + scoped_ptr<DrawPolygon> new_front; |
| + scoped_ptr<DrawPolygon> new_back; |
| + if (controller_->SplitPolygon( |
| + data->take_front(), *(node->node_data), &new_front, &new_back)) { |
| + // Now add new_front and new_back to their respective lists, again |
| + // using the heuristic in an attempt to choose the best splitting |
| + // plane, and keep track of the highest weighted polygon known so far |
| + // so if we find something better we can use that instead. |
| + float front_poly_weight = controller_->SplitWeight(*new_front); |
| + float back_poly_weight = controller_->SplitWeight(*new_back); |
| + |
| + if (front_poly_weight > front_weight) { |
| + front_list.push_front(new_front.Pass()); |
| + front_weight = front_poly_weight; |
| + } else { |
| + front_list.push_back(new_front.Pass()); |
|
Ian Vollick
2014/07/16 20:54:32
These two bits could use that helper, too.
troyhildebrandt
2014/07/18 21:48:26
Done.
|
| + } |
| + |
| + if (back_poly_weight > back_weight) { |
| + back_list.push_front(new_back.Pass()); |
| + back_weight = back_poly_weight; |
| + } else { |
| + back_list.push_back(new_back.Pass()); |
| + } |
| + } |
| + } else if (comparer_result == BSP_COPLANAR_FRONT) { |
| + node->coplanars_front.push_back(data->take_front()); |
| + } else if (comparer_result == BSP_COPLANAR_BACK) { |
| + node->coplanars_back.push_back(data->take_front()); |
| + } |
|
Ian Vollick
2014/07/16 20:54:33
else {
NOTREACHED(); // or equivalent for the de
troyhildebrandt
2014/07/18 21:48:26
Done.
|
| + } |
| + |
| + // Build the back subtree using the front of the back_list as our splitter. |
| + if (back_list.size() > 0) { |
| + node->back_child = scoped_ptr<BspNode>(new BspNode(back_list.take_front())); |
| + BuildTree(node->back_child, &back_list); |
| + } |
| + |
| + // Build the front subtree using the front of the front_list as our splitter. |
| + if (front_list.size() > 0) { |
| + node->front_child = |
| + scoped_ptr<BspNode>(new BspNode(front_list.take_front())); |
| + BuildTree(node->front_child, &front_list); |
| + } |
| +} |
| + |
| +void BspTree::Clear() { |
| + if (root_) { |
| + root_.reset(); |
| + } |
| +} |
| + |
| +BspTree::~BspTree() { |
| + Clear(); |
| +} |
| + |
| +} // namespace cc |