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 // This is really supposd to be compare_threshold / 2.0f, but that would | |
29 // create another static initializer. | |
30 static const float split_threshold = 0.5f; | 28 static const float split_threshold = 0.5f; |
31 | |
32 static const float normalized_threshold = 0.001f; | |
33 } // namespace | 29 } // namespace |
34 | 30 |
35 namespace cc { | 31 namespace cc { |
36 | 32 |
37 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); |
38 | 34 |
39 DrawPolygon::DrawPolygon() { | 35 DrawPolygon::DrawPolygon() { |
40 } | 36 } |
41 | 37 |
42 DrawPolygon::DrawPolygon(const DrawQuad* original, | 38 DrawPolygon::DrawPolygon(DrawQuad* original, |
43 const std::vector<gfx::Point3F>& in_points, | 39 const std::vector<gfx::Point3F>& in_points, |
44 const gfx::Vector3dF& normal, | 40 const gfx::Vector3dF& normal, |
45 int draw_order_index) | 41 int draw_order_index) |
46 : order_index_(draw_order_index), original_ref_(original), is_split_(true) { | 42 : order_index_(draw_order_index), original_ref_(original) { |
47 for (size_t i = 0; i < in_points.size(); i++) { | 43 for (size_t i = 0; i < in_points.size(); i++) { |
48 points_.push_back(in_points[i]); | 44 points_.push_back(in_points[i]); |
49 } | 45 } |
50 normal_ = normal; | 46 normal_ = normal; |
51 } | 47 } |
52 | 48 |
53 // 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, |
54 // 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 |
55 // to move it and its normal into screen space. | 51 // to move it and its normal into screen space. |
56 DrawPolygon::DrawPolygon(const DrawQuad* original_ref, | 52 DrawPolygon::DrawPolygon(DrawQuad* original_ref, |
57 const gfx::RectF& visible_content_rect, | 53 const gfx::RectF& visible_content_rect, |
58 const gfx::Transform& transform, | 54 const gfx::Transform& transform, |
59 int draw_order_index) | 55 int draw_order_index) |
60 : normal_(default_normal), | 56 : order_index_(draw_order_index), original_ref_(original_ref) { |
61 order_index_(draw_order_index), | 57 normal_ = default_normal; |
62 original_ref_(original_ref), | |
63 is_split_(false) { | |
64 gfx::Point3F points[8]; | 58 gfx::Point3F points[8]; |
65 int num_vertices_in_clipped_quad; | 59 int num_vertices_in_clipped_quad; |
66 gfx::QuadF send_quad(visible_content_rect); | 60 gfx::QuadF send_quad(visible_content_rect); |
67 | 61 |
68 // 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 |
69 // 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 |
70 // 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 |
71 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d | 65 // working with a quad, so we can reuse the MathUtil::MapClippedQuad3d |
72 // function instead of writing a generic polygon version of it. | 66 // function instead of writing a generic polygon version of it. |
73 MathUtil::MapClippedQuad3d( | 67 MathUtil::MapClippedQuad3d( |
(...skipping 22 matching lines...) Expand all Loading... |
96 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { | 90 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { |
97 return gfx::DotProduct(point - points_[0], normal_); | 91 return gfx::DotProduct(point - points_[0], normal_); |
98 } | 92 } |
99 | 93 |
100 // 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 |
101 // 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 |
102 // 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. |
103 // Assumes that layers are split and there are no intersecting planes. | 97 // Assumes that layers are split and there are no intersecting planes. |
104 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, | 98 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, |
105 const DrawPolygon& b) { | 99 const DrawPolygon& b) { |
106 // Let's make sure that both of these are normalized. | |
107 DCHECK_GE(normalized_threshold, std::abs(a.normal_.LengthSquared() - 1.0f)); | |
108 DCHECK_GE(normalized_threshold, std::abs(b.normal_.LengthSquared() - 1.0f)); | |
109 // Right away let's check if they're coplanar | 100 // Right away let's check if they're coplanar |
110 double dot = gfx::DotProduct(a.normal_, b.normal_); | 101 double dot = gfx::DotProduct(a.normal_, b.normal_); |
111 float sign = 0.0f; | 102 float sign = 0.0f; |
112 bool normal_match = false; | 103 bool normal_match = false; |
113 // This check assumes that the normals are normalized. | 104 // This check assumes that the normals are normalized. |
114 if (std::abs(dot) >= 1.0f - coplanar_dot_epsilon) { | 105 if (std::abs(dot) >= 1.0f - coplanar_dot_epsilon) { |
115 normal_match = true; | 106 normal_match = true; |
116 // 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. |
117 sign = b.SignedPointDistance(a.points_[0]); | 108 sign = gfx::DotProduct(a.points_[0] - b.points_[0], b.normal_); |
118 // Is it on either side of the splitter? | 109 // Is it on either side of the splitter? |
119 if (sign < -compare_threshold) { | 110 if (sign < -compare_threshold) { |
120 return BSP_BACK; | 111 return BSP_BACK; |
121 } | 112 } |
122 | 113 |
123 if (sign > compare_threshold) { | 114 if (sign > compare_threshold) { |
124 return BSP_FRONT; | 115 return BSP_FRONT; |
125 } | 116 } |
126 | 117 |
127 // 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... |
170 gfx::Point3F* intersection, | 161 gfx::Point3F* intersection, |
171 float distance_threshold) { | 162 float distance_threshold) { |
172 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; | 163 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; |
173 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; | 164 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; |
174 | 165 |
175 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); | 166 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); |
176 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); | 167 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); |
177 | 168 |
178 // 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 |
179 // is outside of it. | 170 // is outside of it. |
180 if (std::abs(start_distance) <= distance_threshold && | 171 if (std::abs(start_distance) < distance_threshold && |
181 std::abs(end_distance) > distance_threshold) { | 172 std::abs(end_distance) > distance_threshold) { |
182 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); | 173 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); |
183 return true; | 174 return true; |
184 } | 175 } |
185 | 176 |
186 // This is the case where we clearly cross the thick-plane. | 177 // This is the case where we clearly cross the thick-plane. |
187 if ((start_distance > distance_threshold && | 178 if ((start_distance > distance_threshold && |
188 end_distance < -distance_threshold) || | 179 end_distance < -distance_threshold) || |
189 (start_distance < -distance_threshold && | 180 (start_distance < -distance_threshold && |
190 end_distance > distance_threshold)) { | 181 end_distance > distance_threshold)) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 splitter.normal_, | 263 splitter.normal_, |
273 &intersections[current_intersection], | 264 &intersections[current_intersection], |
274 split_threshold)) { | 265 split_threshold)) { |
275 vertex_before[current_intersection] = current_vertex % points_size; | 266 vertex_before[current_intersection] = current_vertex % points_size; |
276 current_intersection++; | 267 current_intersection++; |
277 // We found both intersection points so we're done already. | 268 // We found both intersection points so we're done already. |
278 if (current_intersection == 2) { | 269 if (current_intersection == 2) { |
279 break; | 270 break; |
280 } | 271 } |
281 } | 272 } |
282 if (current_vertex++ > (points_size)) { | 273 if (current_vertex++ > points_size) { |
283 break; | 274 break; |
284 } | 275 } |
285 } | 276 } |
286 DCHECK_EQ(current_intersection, static_cast<size_t>(2)); | 277 DCHECK_EQ(current_intersection, static_cast<size_t>(2)); |
287 | 278 |
288 // 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 |
289 // vertex set for both our new polygons. | 280 // vertex set for both our new polygons. |
290 size_t start1 = (vertex_before[0] + 1) % points_size; | 281 size_t start1 = (vertex_before[0] + 1) % points_size; |
291 size_t start2 = (vertex_before[1] + 1) % points_size; | 282 size_t start2 = (vertex_before[1] + 1) % points_size; |
292 size_t points_remaining = points_size; | 283 size_t points_remaining = points_size; |
293 | 284 |
294 // First polygon. | 285 // First polygon. |
295 out_points[0].push_back(intersections[0]); | 286 out_points[0].push_back(intersections[0]); |
296 DCHECK_GE(vertex_before[1], start1); | |
297 for (size_t i = start1; i <= vertex_before[1]; i++) { | 287 for (size_t i = start1; i <= vertex_before[1]; i++) { |
298 out_points[0].push_back(points_[i]); | 288 out_points[0].push_back(points_[i]); |
299 --points_remaining; | 289 --points_remaining; |
300 } | 290 } |
301 out_points[0].push_back(intersections[1]); | 291 out_points[0].push_back(intersections[1]); |
302 | 292 |
303 // Second polygon. | 293 // Second polygon. |
304 out_points[1].push_back(intersections[1]); | 294 out_points[1].push_back(intersections[1]); |
305 size_t index = start2; | 295 size_t index = start2; |
306 for (size_t i = 0; i < points_remaining; i++) { | 296 for (size_t i = 0; i < points_remaining; i++) { |
307 out_points[1].push_back(points_[index % points_size]); | 297 out_points[1].push_back(points_[index % points_size]); |
308 ++index; | 298 ++index; |
309 } | 299 } |
310 out_points[1].push_back(intersections[0]); | 300 out_points[1].push_back(intersections[0]); |
311 | 301 |
312 // 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 |
313 // still be sorted properly in co-planar instances. | 303 // still be sorted properly in co-planar instances. |
314 scoped_ptr<DrawPolygon> poly1( | 304 scoped_ptr<DrawPolygon> poly1( |
315 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_)); | 305 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_)); |
316 scoped_ptr<DrawPolygon> poly2( | 306 scoped_ptr<DrawPolygon> poly2( |
317 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_)); | 307 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_)); |
318 | 308 |
319 DCHECK_GE(poly1->points().size(), 3u); | |
320 DCHECK_GE(poly2->points().size(), 3u); | |
321 | |
322 if (SideCompare(*poly1, splitter) == BSP_FRONT) { | 309 if (SideCompare(*poly1, splitter) == BSP_FRONT) { |
323 *front = poly1.Pass(); | 310 *front = poly1.Pass(); |
324 *back = poly2.Pass(); | 311 *back = poly2.Pass(); |
325 } else { | 312 } else { |
326 *front = poly2.Pass(); | 313 *front = poly2.Pass(); |
327 *back = poly1.Pass(); | 314 *back = poly1.Pass(); |
328 } | 315 } |
329 return true; | 316 return true; |
330 } | 317 } |
331 | 318 |
(...skipping 24 matching lines...) Expand all Loading... |
356 quads->push_back( | 343 quads->push_back( |
357 gfx::QuadF(first, | 344 gfx::QuadF(first, |
358 gfx::PointF(points_[offset].x(), points_[offset].y()), | 345 gfx::PointF(points_[offset].x(), points_[offset].y()), |
359 gfx::PointF(points_[op1].x(), points_[op1].y()), | 346 gfx::PointF(points_[op1].x(), points_[op1].y()), |
360 gfx::PointF(points_[op2].x(), points_[op2].y()))); | 347 gfx::PointF(points_[op2].x(), points_[op2].y()))); |
361 offset = op2; | 348 offset = op2; |
362 } | 349 } |
363 } | 350 } |
364 | 351 |
365 } // namespace cc | 352 } // namespace cc |
OLD | NEW |