| 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" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 // This allows for some imperfection in the normal comparison when checking if | 13 // This allows for some imperfection in the normal comparison when checking if |
| 14 // two pieces of geometry are coplanar. | 14 // two pieces of geometry are coplanar. |
| 15 static const float coplanar_dot_epsilon = 0.001f; | 15 static const float coplanar_dot_epsilon = 0.01f; |
| 16 // This threshold controls how "thick" a plane is. If a point's distance is | 16 // This threshold controls how "thick" a plane is. If a point's distance is |
| 17 // <= |compare_threshold|, then it is considered on the plane. Only when this | 17 // <= |compare_threshold|, then it is considered on the plane. Only when this |
| 18 // boundary is crossed do we consider doing splitting. | 18 // boundary is crossed do we consider doing splitting. |
| 19 static const float compare_threshold = 1.0f; | 19 static const float compare_threshold = 1.0f; |
| 20 // |split_threshold| is lower in this case because we want the points created | 20 // |split_threshold| is lower in this case because we want the points created |
| 21 // during splitting to be well within the range of |compare_threshold| for | 21 // during splitting to be well within the range of |compare_threshold| for |
| 22 // comparison purposes. The splitting operation will produce intersection points | 22 // comparison purposes. The splitting operation will produce intersection points |
| 23 // that fit within a tighter distance to the splitting plane as a result of this | 23 // that fit within a tighter distance to the splitting plane as a result of this |
| 24 // value. By using a value >= |compare_threshold| we run the risk of creating | 24 // value. By using a value >= |compare_threshold| we run the risk of creating |
| 25 // points that SHOULD be intersecting the "thick plane", but actually fail to | 25 // points that SHOULD be intersecting the "thick plane", but actually fail to |
| 26 // test positively for it because |split_threshold| allowed them to be outside | 26 // test positively for it because |split_threshold| allowed them to be outside |
| 27 // this range. | 27 // this range. |
| 28 static const float split_threshold = compare_threshold / 2.0f; | 28 static const float split_threshold = 0.5f; |
| 29 | |
| 30 static const float normalized_threshold = 0.001f; | |
| 31 } // namespace | 29 } // namespace |
| 32 | 30 |
| 33 namespace cc { | 31 namespace cc { |
| 34 | 32 |
| 35 gfx::Vector3dF DrawPolygon::default_normal = gfx::Vector3dF(0.0f, 0.0f, 1.0f); | 33 gfx::Vector3dF DrawPolygon::default_normal = gfx::Vector3dF(0.0f, 0.0f, -1.0f); |
| 36 | 34 |
| 37 DrawPolygon::DrawPolygon() { | 35 DrawPolygon::DrawPolygon() { |
| 38 } | 36 } |
| 39 | 37 |
| 40 DrawPolygon::DrawPolygon(const DrawQuad* original, | 38 DrawPolygon::DrawPolygon(DrawQuad* original, |
| 41 const std::vector<gfx::Point3F>& in_points, | 39 const std::vector<gfx::Point3F>& in_points, |
| 42 const gfx::Vector3dF& normal, | 40 const gfx::Vector3dF& normal, |
| 43 int draw_order_index) | 41 int draw_order_index) |
| 44 : order_index_(draw_order_index), original_ref_(original), is_split_(true) { | 42 : order_index_(draw_order_index), original_ref_(original) { |
| 45 for (size_t i = 0; i < in_points.size(); i++) { | 43 for (size_t i = 0; i < in_points.size(); i++) { |
| 46 points_.push_back(in_points[i]); | 44 points_.push_back(in_points[i]); |
| 47 } | 45 } |
| 48 normal_ = normal; | 46 normal_ = normal; |
| 49 } | 47 } |
| 50 | 48 |
| 51 // This takes the original DrawQuad that this polygon should be based on, | 49 // This takes the original DrawQuad that this polygon should be based on, |
| 52 // a visible content rect to make the 4 corner points from, and a transformation | 50 // a visible content rect to make the 4 corner points from, and a transformation |
| 53 // to move it and its normal into screen space. | 51 // to move it and its normal into screen space. |
| 54 DrawPolygon::DrawPolygon(const DrawQuad* original_ref, | 52 DrawPolygon::DrawPolygon(DrawQuad* original_ref, |
| 55 const gfx::RectF& visible_content_rect, | 53 const gfx::RectF& visible_content_rect, |
| 56 const gfx::Transform& transform, | 54 const gfx::Transform& transform, |
| 57 int draw_order_index) | 55 int draw_order_index) |
| 58 : normal_(default_normal), | 56 : order_index_(draw_order_index), original_ref_(original_ref) { |
| 59 order_index_(draw_order_index), | 57 normal_ = default_normal; |
| 60 original_ref_(original_ref), | |
| 61 is_split_(false) { | |
| 62 gfx::Point3F points[8]; | 58 gfx::Point3F points[8]; |
| 63 int num_vertices_in_clipped_quad; | 59 int num_vertices_in_clipped_quad; |
| 64 gfx::QuadF send_quad(visible_content_rect); | 60 gfx::QuadF send_quad(visible_content_rect); |
| 65 | 61 |
| 66 // Doing this mapping here is very important, since we can't just transform | 62 // Doing this mapping here is very important, since we can't just transform |
| 67 // the points without clipping and not run into strange geometry issues when | 63 // the points without clipping and not run into strange geometry issues when |
| 68 // crossing w = 0. At this point, in the constructor, we know that we're | 64 // crossing w = 0. At this point, in the constructor, we know that we're |
| 69 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d | 65 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d |
| 70 // function instead of writing a generic polygon version of it. | 66 // function instead of writing a generic polygon version of it. |
| 71 MathUtil::MapClippedQuad3d( | 67 MathUtil::MapClippedQuad3d( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 94 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { | 90 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { |
| 95 return gfx::DotProduct(point - points_[0], normal_); | 91 return gfx::DotProduct(point - points_[0], normal_); |
| 96 } | 92 } |
| 97 | 93 |
| 98 // Checks whether or not shape a lies on the front or back side of b, or | 94 // Checks whether or not shape a lies on the front or back side of b, or |
| 99 // whether they should be considered coplanar. If on the back side, we | 95 // whether they should be considered coplanar. If on the back side, we |
| 100 // say A_BEFORE_B because it should be drawn in that order. | 96 // say A_BEFORE_B because it should be drawn in that order. |
| 101 // Assumes that layers are split and there are no intersecting planes. | 97 // Assumes that layers are split and there are no intersecting planes. |
| 102 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, | 98 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, |
| 103 const DrawPolygon& b) { | 99 const DrawPolygon& b) { |
| 104 // Let's make sure that both of these are normalized. | |
| 105 DCHECK_GE(normalized_threshold, std::abs(a.normal_.LengthSquared() - 1.0f)); | |
| 106 DCHECK_GE(normalized_threshold, std::abs(b.normal_.LengthSquared() - 1.0f)); | |
| 107 // Right away let's check if they're coplanar | 100 // Right away let's check if they're coplanar |
| 108 double dot = gfx::DotProduct(a.normal_, b.normal_); | 101 double dot = gfx::DotProduct(a.normal_, b.normal_); |
| 109 float sign = 0.0f; | 102 float sign = 0.0f; |
| 110 bool normal_match = false; | 103 bool normal_match = false; |
| 111 // This check assumes that the normals are normalized. | 104 // This check assumes that the normals are normalized. |
| 112 if (std::abs(dot) >= 1.0f - coplanar_dot_epsilon) { | 105 if (std::abs(dot) >= 1.0f - coplanar_dot_epsilon) { |
| 113 normal_match = true; | 106 normal_match = true; |
| 114 // The normals are matching enough that we only have to test one point. | 107 // The normals are matching enough that we only have to test one point. |
| 115 sign = b.SignedPointDistance(a.points_[0]); | 108 sign = gfx::DotProduct(a.points_[0] - b.points_[0], b.normal_); |
| 116 // Is it on either side of the splitter? | 109 // Is it on either side of the splitter? |
| 117 if (sign < -compare_threshold) { | 110 if (sign < -compare_threshold) { |
| 118 return BSP_BACK; | 111 return BSP_BACK; |
| 119 } | 112 } |
| 120 | 113 |
| 121 if (sign > compare_threshold) { | 114 if (sign > compare_threshold) { |
| 122 return BSP_FRONT; | 115 return BSP_FRONT; |
| 123 } | 116 } |
| 124 | 117 |
| 125 // No it wasn't, so the sign of the dot product of the normals | 118 // No it wasn't, so the sign of the dot product of the normals |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 gfx::Point3F* intersection, | 161 gfx::Point3F* intersection, |
| 169 float distance_threshold) { | 162 float distance_threshold) { |
| 170 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; | 163 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; |
| 171 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; | 164 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; |
| 172 | 165 |
| 173 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); | 166 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); |
| 174 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); | 167 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); |
| 175 | 168 |
| 176 // The case where one vertex lies on the thick-plane and the other | 169 // The case where one vertex lies on the thick-plane and the other |
| 177 // is outside of it. | 170 // is outside of it. |
| 178 if (std::abs(start_distance) <= distance_threshold && | 171 if (std::abs(start_distance) < distance_threshold && |
| 179 std::abs(end_distance) > distance_threshold) { | 172 std::abs(end_distance) > distance_threshold) { |
| 180 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); | 173 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); |
| 181 return true; | 174 return true; |
| 182 } | 175 } |
| 183 | 176 |
| 184 // This is the case where we clearly cross the thick-plane. | 177 // This is the case where we clearly cross the thick-plane. |
| 185 if ((start_distance > distance_threshold && | 178 if ((start_distance > distance_threshold && |
| 186 end_distance < -distance_threshold) || | 179 end_distance < -distance_threshold) || |
| 187 (start_distance < -distance_threshold && | 180 (start_distance < -distance_threshold && |
| 188 end_distance > distance_threshold)) { | 181 end_distance > distance_threshold)) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 splitter.normal_, | 263 splitter.normal_, |
| 271 &intersections[current_intersection], | 264 &intersections[current_intersection], |
| 272 split_threshold)) { | 265 split_threshold)) { |
| 273 vertex_before[current_intersection] = current_vertex % points_size; | 266 vertex_before[current_intersection] = current_vertex % points_size; |
| 274 current_intersection++; | 267 current_intersection++; |
| 275 // We found both intersection points so we're done already. | 268 // We found both intersection points so we're done already. |
| 276 if (current_intersection == 2) { | 269 if (current_intersection == 2) { |
| 277 break; | 270 break; |
| 278 } | 271 } |
| 279 } | 272 } |
| 280 if (current_vertex++ > (points_size)) { | 273 if (current_vertex++ > points_size) { |
| 281 break; | 274 break; |
| 282 } | 275 } |
| 283 } | 276 } |
| 284 DCHECK_EQ(current_intersection, static_cast<size_t>(2)); | 277 DCHECK_EQ(current_intersection, static_cast<size_t>(2)); |
| 285 | 278 |
| 286 // Since we found both the intersection points, we can begin building the | 279 // Since we found both the intersection points, we can begin building the |
| 287 // vertex set for both our new polygons. | 280 // vertex set for both our new polygons. |
| 288 size_t start1 = (vertex_before[0] + 1) % points_size; | 281 size_t start1 = (vertex_before[0] + 1) % points_size; |
| 289 size_t start2 = (vertex_before[1] + 1) % points_size; | 282 size_t start2 = (vertex_before[1] + 1) % points_size; |
| 290 size_t points_remaining = points_size; | 283 size_t points_remaining = points_size; |
| 291 | 284 |
| 292 // First polygon. | 285 // First polygon. |
| 293 out_points[0].push_back(intersections[0]); | 286 out_points[0].push_back(intersections[0]); |
| 294 DCHECK_GE(vertex_before[1], start1); | |
| 295 for (size_t i = start1; i <= vertex_before[1]; i++) { | 287 for (size_t i = start1; i <= vertex_before[1]; i++) { |
| 296 out_points[0].push_back(points_[i]); | 288 out_points[0].push_back(points_[i]); |
| 297 --points_remaining; | 289 --points_remaining; |
| 298 } | 290 } |
| 299 out_points[0].push_back(intersections[1]); | 291 out_points[0].push_back(intersections[1]); |
| 300 | 292 |
| 301 // Second polygon. | 293 // Second polygon. |
| 302 out_points[1].push_back(intersections[1]); | 294 out_points[1].push_back(intersections[1]); |
| 303 size_t index = start2; | 295 size_t index = start2; |
| 304 for (size_t i = 0; i < points_remaining; i++) { | 296 for (size_t i = 0; i < points_remaining; i++) { |
| 305 out_points[1].push_back(points_[index % points_size]); | 297 out_points[1].push_back(points_[index % points_size]); |
| 306 ++index; | 298 ++index; |
| 307 } | 299 } |
| 308 out_points[1].push_back(intersections[0]); | 300 out_points[1].push_back(intersections[0]); |
| 309 | 301 |
| 310 // Give both polygons the original splitting polygon's ID, so that they'll | 302 // Give both polygons the original splitting polygon's ID, so that they'll |
| 311 // still be sorted properly in co-planar instances. | 303 // still be sorted properly in co-planar instances. |
| 312 scoped_ptr<DrawPolygon> poly1( | 304 scoped_ptr<DrawPolygon> poly1( |
| 313 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_)); | 305 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_)); |
| 314 scoped_ptr<DrawPolygon> poly2( | 306 scoped_ptr<DrawPolygon> poly2( |
| 315 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_)); | 307 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_)); |
| 316 | 308 |
| 317 DCHECK_GE(poly1->points().size(), 3u); | |
| 318 DCHECK_GE(poly2->points().size(), 3u); | |
| 319 | |
| 320 if (SideCompare(*poly1, splitter) == BSP_FRONT) { | 309 if (SideCompare(*poly1, splitter) == BSP_FRONT) { |
| 321 *front = poly1.Pass(); | 310 *front = poly1.Pass(); |
| 322 *back = poly2.Pass(); | 311 *back = poly2.Pass(); |
| 323 } else { | 312 } else { |
| 324 *front = poly2.Pass(); | 313 *front = poly2.Pass(); |
| 325 *back = poly1.Pass(); | 314 *back = poly1.Pass(); |
| 326 } | 315 } |
| 327 return true; | 316 return true; |
| 328 } | 317 } |
| 329 | 318 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 354 quads->push_back( | 343 quads->push_back( |
| 355 gfx::QuadF(first, | 344 gfx::QuadF(first, |
| 356 gfx::PointF(points_[offset].x(), points_[offset].y()), | 345 gfx::PointF(points_[offset].x(), points_[offset].y()), |
| 357 gfx::PointF(points_[op1].x(), points_[op1].y()), | 346 gfx::PointF(points_[op1].x(), points_[op1].y()), |
| 358 gfx::PointF(points_[op2].x(), points_[op2].y()))); | 347 gfx::PointF(points_[op2].x(), points_[op2].y()))); |
| 359 offset = op2; | 348 offset = op2; |
| 360 } | 349 } |
| 361 } | 350 } |
| 362 | 351 |
| 363 } // namespace cc | 352 } // namespace cc |
| OLD | NEW |