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