2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2016-08-11 00:30:29 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "BSPTree.h"
|
|
|
|
#include "mozilla/gfx/Polygon.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
void
|
2017-04-10 17:35:56 +03:00
|
|
|
BSPTree::BuildDrawOrder(BSPTreeNode* aNode,
|
2016-09-17 02:03:32 +03:00
|
|
|
nsTArray<LayerPolygon>& aLayers) const
|
|
|
|
{
|
2016-12-04 19:49:32 +03:00
|
|
|
const gfx::Point4D& normal = aNode->First().GetNormal();
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
BSPTreeNode* front = aNode->front;
|
|
|
|
BSPTreeNode* back = aNode->back;
|
2016-09-17 02:03:32 +03:00
|
|
|
|
|
|
|
// Since the goal is to return the draw order from back to front, we reverse
|
|
|
|
// the traversal order if the current polygon is facing towards the camera.
|
|
|
|
const bool reverseOrder = normal.z > 0.0f;
|
|
|
|
|
|
|
|
if (reverseOrder) {
|
|
|
|
std::swap(front, back);
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
if (front) {
|
|
|
|
BuildDrawOrder(front, aLayers);
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (LayerPolygon& layer : aNode->layers) {
|
2016-10-04 02:35:52 +03:00
|
|
|
MOZ_ASSERT(layer.geometry);
|
|
|
|
|
|
|
|
if (layer.geometry->GetPoints().Length() >= 3) {
|
|
|
|
aLayers.AppendElement(Move(layer));
|
|
|
|
}
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
if (back) {
|
|
|
|
BuildDrawOrder(back, aLayers);
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-10 17:35:56 +03:00
|
|
|
BSPTree::BuildTree(BSPTreeNode* aRoot,
|
|
|
|
std::list<LayerPolygon>& aLayers)
|
2016-09-17 02:03:32 +03:00
|
|
|
{
|
2017-04-10 17:35:56 +03:00
|
|
|
MOZ_ASSERT(!aLayers.empty());
|
|
|
|
|
|
|
|
aRoot->layers.push_back(Move(aLayers.front()));
|
|
|
|
aLayers.pop_front();
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
if (aLayers.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
const gfx::Polygon& plane = aRoot->First();
|
2017-04-04 05:51:29 +03:00
|
|
|
MOZ_ASSERT(!plane.IsEmpty());
|
|
|
|
|
|
|
|
const gfx::Point4D& planeNormal = plane.GetNormal();
|
|
|
|
const gfx::Point4D& planePoint = plane.GetPoints()[0];
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-10 17:35:56 +03:00
|
|
|
std::list<LayerPolygon> backLayers, frontLayers;
|
2016-09-17 02:03:32 +03:00
|
|
|
for (LayerPolygon& layerPolygon : aLayers) {
|
2017-04-04 05:51:29 +03:00
|
|
|
const nsTArray<gfx::Point4D>& geometry = layerPolygon.geometry->GetPoints();
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
// Calculate the plane-point distances for the polygon classification.
|
2016-10-04 02:35:52 +03:00
|
|
|
size_t pos = 0, neg = 0;
|
2017-04-04 05:51:29 +03:00
|
|
|
nsTArray<float> distances =
|
|
|
|
CalculatePointPlaneDistances(geometry, planeNormal, planePoint, pos, neg);
|
2016-09-17 02:03:32 +03:00
|
|
|
|
|
|
|
// Back polygon
|
|
|
|
if (pos == 0 && neg > 0) {
|
2016-10-04 02:35:52 +03:00
|
|
|
backLayers.push_back(Move(layerPolygon));
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
// Front polygon
|
|
|
|
else if (pos > 0 && neg == 0) {
|
2016-10-04 02:35:52 +03:00
|
|
|
frontLayers.push_back(Move(layerPolygon));
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
// Coplanar polygon
|
|
|
|
else if (pos == 0 && neg == 0) {
|
2016-10-04 02:35:52 +03:00
|
|
|
aRoot->layers.push_back(Move(layerPolygon));
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
// Polygon intersects with the splitting plane.
|
|
|
|
else if (pos > 0 && neg > 0) {
|
2016-12-04 19:49:32 +03:00
|
|
|
nsTArray<gfx::Point4D> backPoints, frontPoints;
|
2017-04-04 05:51:29 +03:00
|
|
|
// Clip the polygon against the plane. We reuse the previously calculated
|
|
|
|
// distances to find the plane-edge intersections.
|
|
|
|
ClipPointsWithPlane(geometry, planeNormal, distances,
|
|
|
|
backPoints, frontPoints);
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
const gfx::Point4D& normal = layerPolygon.geometry->GetNormal();
|
|
|
|
Layer* layer = layerPolygon.layer;
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
if (backPoints.Length() >= 3) {
|
2017-04-10 17:35:56 +03:00
|
|
|
backLayers.emplace_back(layer, Move(backPoints), normal);
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frontPoints.Length() >= 3) {
|
2017-04-10 17:35:56 +03:00
|
|
|
frontLayers.emplace_back(layer, Move(frontPoints), normal);
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backLayers.empty()) {
|
2017-04-10 17:35:56 +03:00
|
|
|
aRoot->back = new (mPool) BSPTreeNode(mListPointers);
|
2016-09-17 02:03:32 +03:00
|
|
|
BuildTree(aRoot->back, backLayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontLayers.empty()) {
|
2017-04-10 17:35:56 +03:00
|
|
|
aRoot->front = new (mPool) BSPTreeNode(mListPointers);
|
2016-09-17 02:03:32 +03:00
|
|
|
BuildTree(aRoot->front, frontLayers);
|
|
|
|
}
|
2016-08-11 00:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|