Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: cc/quads/draw_polygon.cc

Issue 1497153002: Replace inverse transform with Cross-product computation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved section and rebased again Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/quads/draw_polygon.h ('k') | cc/quads/draw_polygon_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
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
113 #if defined(OS_WIN)
114 //
115 // Allows the unittest to invoke this for the more general constructor.
116 //
117 void DrawPolygon::RecomputeNormalForTesting() {
118 ConstructNormal();
119 }
120 #endif
121
91 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { 122 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const {
92 return gfx::DotProduct(point - points_[0], normal_); 123 return gfx::DotProduct(point - points_[0], normal_);
93 } 124 }
94 125
95 // Checks whether or not shape a lies on the front or back side of b, or 126 // 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 127 // 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. 128 // say A_BEFORE_B because it should be drawn in that order.
98 // Assumes that layers are split and there are no intersecting planes. 129 // Assumes that layers are split and there are no intersecting planes.
99 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, 130 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a,
100 const DrawPolygon& b) { 131 const DrawPolygon& b) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 for (size_t i = 0; i < points_.size(); i++) { 234 for (size_t i = 0; i < points_.size(); i++) {
204 transform.TransformPoint(&points_[i]); 235 transform.TransformPoint(&points_[i]);
205 } 236 }
206 } 237 }
207 238
208 // TransformToScreenSpace assumes we're moving a layer from its layer space 239 // TransformToScreenSpace assumes we're moving a layer from its layer space
209 // into 3D screen space, which for sorting purposes requires the normal to 240 // into 3D screen space, which for sorting purposes requires the normal to
210 // be transformed along with the vertices. 241 // be transformed along with the vertices.
211 void DrawPolygon::TransformToScreenSpace(const gfx::Transform& transform) { 242 void DrawPolygon::TransformToScreenSpace(const gfx::Transform& transform) {
212 ApplyTransform(transform); 243 ApplyTransform(transform);
213 ApplyTransformToNormal(transform); 244 ConstructNormal();
214 } 245 }
215 246
216 // In the case of TransformToLayerSpace, we assume that we are giving the 247 // 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 248 // 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 249 // 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. 250 // since we know the normal will just reset to its original state.
220 void DrawPolygon::TransformToLayerSpace( 251 void DrawPolygon::TransformToLayerSpace(
221 const gfx::Transform& inverse_transform) { 252 const gfx::Transform& inverse_transform) {
222 ApplyTransform(inverse_transform); 253 ApplyTransform(inverse_transform);
223 normal_ = gfx::Vector3dF(0.0f, 0.0f, -1.0f); 254 normal_ = gfx::Vector3dF(0.0f, 0.0f, -1.0f);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 quads->push_back( 361 quads->push_back(
331 gfx::QuadF(first, 362 gfx::QuadF(first,
332 gfx::PointF(points_[offset].x(), points_[offset].y()), 363 gfx::PointF(points_[offset].x(), points_[offset].y()),
333 gfx::PointF(points_[op1].x(), points_[op1].y()), 364 gfx::PointF(points_[op1].x(), points_[op1].y()),
334 gfx::PointF(points_[op2].x(), points_[op2].y()))); 365 gfx::PointF(points_[op2].x(), points_[op2].y())));
335 offset = op2; 366 offset = op2;
336 } 367 }
337 } 368 }
338 369
339 } // namespace cc 370 } // namespace cc
OLDNEW
« no previous file with comments | « cc/quads/draw_polygon.h ('k') | cc/quads/draw_polygon_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698