Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/quads/draw_polygon.h" | 5 #include "cc/quads/draw_polygon.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "cc/output/bsp_compare_result.h" | 9 #include "cc/output/bsp_compare_result.h" |
| 10 #include "cc/quads/draw_quad.h" | 10 #include "cc/quads/draw_quad.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 // Doing this mapping here is very important, since we can't just transform | 63 // Doing this mapping here is very important, since we can't just transform |
| 64 // the points without clipping and not run into strange geometry issues when | 64 // the points without clipping and not run into strange geometry issues when |
| 65 // crossing w = 0. At this point, in the constructor, we know that we're | 65 // crossing w = 0. At this point, in the constructor, we know that we're |
| 66 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d | 66 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d |
| 67 // function instead of writing a generic polygon version of it. | 67 // function instead of writing a generic polygon version of it. |
| 68 MathUtil::MapClippedQuad3d( | 68 MathUtil::MapClippedQuad3d( |
| 69 transform, send_quad, points, &num_vertices_in_clipped_quad); | 69 transform, send_quad, points, &num_vertices_in_clipped_quad); |
| 70 for (int i = 0; i < num_vertices_in_clipped_quad; i++) { | 70 for (int i = 0; i < num_vertices_in_clipped_quad; i++) { |
| 71 points_.push_back(points[i]); | 71 points_.push_back(points[i]); |
| 72 } | 72 } |
| 73 ApplyTransformToNormal(transform); | 73 ConstructNormal(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 DrawPolygon::~DrawPolygon() { | 76 DrawPolygon::~DrawPolygon() { |
| 77 } | 77 } |
| 78 | 78 |
| 79 scoped_ptr<DrawPolygon> DrawPolygon::CreateCopy() { | 79 scoped_ptr<DrawPolygon> DrawPolygon::CreateCopy() { |
| 80 scoped_ptr<DrawPolygon> new_polygon(new DrawPolygon()); | 80 scoped_ptr<DrawPolygon> new_polygon(new DrawPolygon()); |
| 81 new_polygon->order_index_ = order_index_; | 81 new_polygon->order_index_ = order_index_; |
| 82 new_polygon->original_ref_ = original_ref_; | 82 new_polygon->original_ref_ = original_ref_; |
| 83 new_polygon->points_.reserve(points_.size()); | 83 new_polygon->points_.reserve(points_.size()); |
| 84 new_polygon->points_ = points_; | 84 new_polygon->points_ = points_; |
| 85 new_polygon->normal_.set_x(normal_.x()); | 85 new_polygon->normal_.set_x(normal_.x()); |
| 86 new_polygon->normal_.set_y(normal_.y()); | 86 new_polygon->normal_.set_y(normal_.y()); |
| 87 new_polygon->normal_.set_z(normal_.z()); | 87 new_polygon->normal_.set_z(normal_.z()); |
| 88 return new_polygon; | 88 return new_polygon; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // | |
| 92 // If this were to be more generally used and expected to be applicable | |
| 93 // replacing this with Newell's algorithm (or an improvement thereof) | |
| 94 // would be preferable, but usually this is coming in from a rectangle | |
| 95 // that has been transformed to screen space and clipped. | |
| 96 // Averaging a few near diagonal cross products is pretty good in that case. | |
| 97 // | |
| 98 void DrawPolygon::ConstructNormal() { | |
| 99 normal_.set_x(0.0f); | |
| 100 normal_.set_y(0.0f); | |
| 101 normal_.set_z(0.0f); | |
|
Peter Mayo
2015/12/04 16:40:41
What about saving the old normal and adding a fall
| |
| 102 int delta = points_.size() / 2; | |
| 103 for (size_t i = 1; i + delta < points_.size(); i++) { | |
| 104 normal_ += | |
| 105 CrossProduct(points_[i] - points_[0], points_[i + delta] - points_[0]); | |
| 106 } | |
| 107 float normal_magnitude = normal_.Length(); | |
| 108 if (normal_magnitude != 0 && normal_magnitude != 1) { | |
| 109 normal_.Scale(1.0f / normal_magnitude); | |
| 110 } | |
| 111 } | |
| 112 | |
| 91 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { | 113 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { |
| 92 return gfx::DotProduct(point - points_[0], normal_); | 114 return gfx::DotProduct(point - points_[0], normal_); |
| 93 } | 115 } |
| 94 | 116 |
| 95 // Checks whether or not shape a lies on the front or back side of b, or | 117 // Checks whether or not shape a lies on the front or back side of b, or |
| 96 // whether they should be considered coplanar. If on the back side, we | 118 // whether they should be considered coplanar. If on the back side, we |
| 97 // say A_BEFORE_B because it should be drawn in that order. | 119 // say A_BEFORE_B because it should be drawn in that order. |
| 98 // Assumes that layers are split and there are no intersecting planes. | 120 // Assumes that layers are split and there are no intersecting planes. |
| 99 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, | 121 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, |
| 100 const DrawPolygon& b) { | 122 const DrawPolygon& b) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 for (size_t i = 0; i < points_.size(); i++) { | 225 for (size_t i = 0; i < points_.size(); i++) { |
| 204 transform.TransformPoint(&points_[i]); | 226 transform.TransformPoint(&points_[i]); |
| 205 } | 227 } |
| 206 } | 228 } |
| 207 | 229 |
| 208 // TransformToScreenSpace assumes we're moving a layer from its layer space | 230 // TransformToScreenSpace assumes we're moving a layer from its layer space |
| 209 // into 3D screen space, which for sorting purposes requires the normal to | 231 // into 3D screen space, which for sorting purposes requires the normal to |
| 210 // be transformed along with the vertices. | 232 // be transformed along with the vertices. |
| 211 void DrawPolygon::TransformToScreenSpace(const gfx::Transform& transform) { | 233 void DrawPolygon::TransformToScreenSpace(const gfx::Transform& transform) { |
| 212 ApplyTransform(transform); | 234 ApplyTransform(transform); |
| 213 ApplyTransformToNormal(transform); | 235 ConstructNormal(); |
| 214 } | 236 } |
| 215 | 237 |
| 216 // In the case of TransformToLayerSpace, we assume that we are giving the | 238 // In the case of TransformToLayerSpace, we assume that we are giving the |
| 217 // inverse transformation back to the polygon to move it back into layer space | 239 // inverse transformation back to the polygon to move it back into layer space |
| 218 // but we can ignore the costly process of applying the inverse to the normal | 240 // but we can ignore the costly process of applying the inverse to the normal |
| 219 // since we know the normal will just reset to its original state. | 241 // since we know the normal will just reset to its original state. |
| 220 void DrawPolygon::TransformToLayerSpace( | 242 void DrawPolygon::TransformToLayerSpace( |
| 221 const gfx::Transform& inverse_transform) { | 243 const gfx::Transform& inverse_transform) { |
| 222 ApplyTransform(inverse_transform); | 244 ApplyTransform(inverse_transform); |
| 223 normal_ = gfx::Vector3dF(0.0f, 0.0f, -1.0f); | 245 normal_ = gfx::Vector3dF(0.0f, 0.0f, -1.0f); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 quads->push_back( | 352 quads->push_back( |
| 331 gfx::QuadF(first, | 353 gfx::QuadF(first, |
| 332 gfx::PointF(points_[offset].x(), points_[offset].y()), | 354 gfx::PointF(points_[offset].x(), points_[offset].y()), |
| 333 gfx::PointF(points_[op1].x(), points_[op1].y()), | 355 gfx::PointF(points_[op1].x(), points_[op1].y()), |
| 334 gfx::PointF(points_[op2].x(), points_[op2].y()))); | 356 gfx::PointF(points_[op2].x(), points_[op2].y()))); |
| 335 offset = op2; | 357 offset = op2; |
| 336 } | 358 } |
| 337 } | 359 } |
| 338 | 360 |
| 339 } // namespace cc | 361 } // namespace cc |
| OLD | NEW |