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