Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: cc/output/bsp_tree.cc

Issue 384083002: WIP BSP Tree for 3D Layer Sorting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <list>
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 BspNode::BspNode(scoped_ptr<DrawPolygon> data) {
19 node_data = data.Pass();
enne (OOO) 2014/07/29 00:28:08 nit: use an initializer for this, i.e. BspNode::B
troyhildebrandt 2014/07/29 19:05:19 Done.
20 }
21
22 BspNode::~BspNode() {
23 }
24
25 BspTree::BspTree(ScopedPtrDeque<DrawPolygon>* list) {
26 if (list->size() == 0)
27 return;
28
29 root_ = scoped_ptr<BspNode>(new BspNode(list->take_front()));
30 BuildTree(root_.get(), list);
31 }
32
33 // The idea behind using a deque for BuildTree's input is that we want to be
34 // able to place polygons that we've decided aren't splitting plane candidates
35 // at the back of the queue while moving the candidate splitting planes to the
36 // front when the heuristic decides that they're a better choice. This way we
37 // can always simply just take from the front of the deque for our node's
38 // data.
39 void BspTree::BuildTree(BspNode* node,
40 ScopedPtrDeque<DrawPolygon>* polygon_list) {
41 ScopedPtrDeque<DrawPolygon> front_list;
42 ScopedPtrDeque<DrawPolygon> back_list;
43
44 // We take in a list of polygons at this level of the tree, and have to
45 // find a splitting plane, then classify polygons as either in front of
46 // or behind that splitting plane.
47 while (polygon_list->size() > 0) {
48 // Is this particular polygon in front of or behind our splitting polygon.
49 BspCompareResult comparer_result =
50 GetNodePositionRelative(*polygon_list->front(), *(node->node_data));
51
52 // If it's clearly behind or in front of the splitting plane, we use the
53 // heuristic to decide whether or not we should put it at the back
54 // or front of the list.
55 scoped_ptr<DrawPolygon> new_front;
56 scoped_ptr<DrawPolygon> new_back;
57 scoped_ptr<DrawPolygon> polygon;
58 bool split_result = false;
enne (OOO) 2014/07/29 00:28:08 Can you scope this closer to where it's used? I kn
troyhildebrandt 2014/07/29 19:05:19 Done.
59 switch (comparer_result) {
60 case BSP_FRONT:
61 front_list.push_back(polygon_list->take_front().Pass());
62 break;
63 case BSP_BACK:
64 back_list.push_back(polygon_list->take_front().Pass());
65 break;
66 case BSP_SPLIT:
67 // Time to split this geometry, *it needs to be split by node_data.
68 polygon = polygon_list->take_front();
69 split_result =
70 polygon->Split(*(node->node_data), &new_front, &new_back);
71 DCHECK(split_result);
72 if (!split_result) {
73 break;
74 }
75 front_list.push_back(new_front.Pass());
76 back_list.push_back(new_back.Pass());
77 break;
78 case BSP_COPLANAR_FRONT:
79 node->coplanars_front.push_back(polygon_list->take_front());
80 break;
81 case BSP_COPLANAR_BACK:
82 node->coplanars_back.push_back(polygon_list->take_front());
83 break;
84 default:
85 NOTREACHED();
86 break;
87 }
88 }
89
90 // Build the back subtree using the front of the back_list as our splitter.
91 if (back_list.size() > 0) {
92 node->back_child = scoped_ptr<BspNode>(new BspNode(back_list.take_front()));
93 BuildTree(node->back_child.get(), &back_list);
94 }
95
96 // Build the front subtree using the front of the front_list as our splitter.
97 if (front_list.size() > 0) {
98 node->front_child =
99 scoped_ptr<BspNode>(new BspNode(front_list.take_front()));
100 BuildTree(node->front_child.get(), &front_list);
101 }
102 }
103
104 BspCompareResult BspTree::GetNodePositionRelative(const DrawPolygon& node_a,
105 const DrawPolygon& node_b) {
106 return DrawPolygon::SideCompare(node_a, node_b);
107 }
108
109 // The base comparer with 0,0,0 as camera position facing forward
110 BspCompareResult BspTree::GetCameraPositionRelative(const DrawPolygon& node) {
111 if (node.normal().z() > 0.0f) {
112 return BSP_FRONT;
113 }
114 return BSP_BACK;
115 }
116
117 BspTree::~BspTree() {
118 }
119
120 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698