Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkCamera.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
10static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a,
11 const SkScalar b[], int step_b,
12 SkScalar denom) {
13 SkScalar prod = 0;
14 for (int i = 0; i < count; i++) {
15 prod += a[0] * b[0];
16 a += step_a;
17 b += step_b;
18 }
19 return prod / denom;
20}
21
22///////////////////////////////////////////////////////////////////////////////
23
25 this->reset();
26}
27
29 fOrigin = {0, 0, 0};
30 fU = {SK_Scalar1, 0, 0};
31 fV = {0, -SK_Scalar1, 0};
32}
33
34void SkPatch3D::transform(const SkM44& m, SkPatch3D* dst) const {
35 if (dst == nullptr) {
36 dst = const_cast<SkPatch3D*>(this);
37 }
38 dst->fU = m * fU;
39 dst->fV = m * fV;
40 auto [x,y,z,_] = m.map(fOrigin.x, fOrigin.y, fOrigin.z, 1);
41 dst->fOrigin = {x, y, z};
42}
43
45 SkScalar cx = fU.y * fV.z - fU.z * fV.y;
46 SkScalar cy = fU.z * fV.x - fU.x * fV.y;
47 SkScalar cz = fU.x * fV.y - fU.y * fV.x;
48
49 return cx * dx + cy * dy + cz * dz;
50}
51
52///////////////////////////////////////////////////////////////////////////////
53
55 this->reset();
56}
57
59 fLocation = {0, 0, -SkIntToScalar(576)}; // 8 inches backward
60 fAxis = {0, 0, SK_Scalar1}; // forward
61 fZenith = {0, -SK_Scalar1, 0}; // up
62
63 fObserver = {0, 0, fLocation.z};
64
65 fNeedToUpdate = true;
66}
67
69 fNeedToUpdate = true;
70}
71
72void SkCamera3D::doUpdate() const {
73 SkV3 axis, zenith, cross;
74
75 // construct a orthonormal basis of cross (x), zenith (y), and axis (z)
76 axis = fAxis.normalize();
77
78 zenith = fZenith - (axis * fZenith) * axis;
79 zenith = zenith.normalize();
80
81 cross = axis.cross(zenith);
82
83 {
84 SkMatrix* orien = &fOrientation;
85 auto [x, y, z] = fObserver;
86
87 // Looking along the view axis we have:
88 //
89 // /|\ zenith
90 // |
91 // |
92 // | * observer (projected on XY plane)
93 // |
94 // |____________\ cross
95 // /
96 //
97 // So this does a z-shear along the view axis based on the observer's x and y values,
98 // and scales in x and y relative to the negative of the observer's z value
99 // (the observer is in the negative z direction).
100
101 orien->set(SkMatrix::kMScaleX, x * axis.x - z * cross.x);
102 orien->set(SkMatrix::kMSkewX, x * axis.y - z * cross.y);
103 orien->set(SkMatrix::kMTransX, x * axis.z - z * cross.z);
104 orien->set(SkMatrix::kMSkewY, y * axis.x - z * zenith.x);
105 orien->set(SkMatrix::kMScaleY, y * axis.y - z * zenith.y);
106 orien->set(SkMatrix::kMTransY, y * axis.z - z * zenith.z);
107 orien->set(SkMatrix::kMPersp0, axis.x);
108 orien->set(SkMatrix::kMPersp1, axis.y);
109 orien->set(SkMatrix::kMPersp2, axis.z);
110 }
111}
112
113void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
114 if (fNeedToUpdate) {
115 this->doUpdate();
116 fNeedToUpdate = false;
117 }
118
119 const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
120 const SkScalar* patchPtr;
121
122 SkV3 diff = quilt.fOrigin - fLocation;
123 SkScalar dot = diff.dot({mapPtr[6], mapPtr[7], mapPtr[8]});
124
125 // This multiplies fOrientation by the matrix [quilt.fU quilt.fV diff] -- U, V, and diff are
126 // column vectors in the matrix -- then divides by the length of the projection of diff onto
127 // the view axis (which is 'dot'). This transforms the patch (which transforms from local path
128 // space to world space) into view space (since fOrientation transforms from world space to
129 // view space).
130 //
131 // The divide by 'dot' isn't strictly necessary as the homogeneous divide would do much the
132 // same thing (it's just scaling the entire matrix by 1/dot). It looks like it's normalizing
133 // the matrix into some canonical space.
134 patchPtr = (const SkScalar*)&quilt;
135 matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
136 matrix->set(SkMatrix::kMSkewY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
137 matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
138
139 patchPtr += 3;
140 matrix->set(SkMatrix::kMSkewX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
141 matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
142 matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
143
144 patchPtr = (const SkScalar*)(const void*)&diff;
145 matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
146 matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
147 matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
153 fRec = &fInitialRec;
154}
155
157 Rec* rec = fRec;
158 while (rec != &fInitialRec) {
159 Rec* next = rec->fNext;
160 delete rec;
161 rec = next;
162 }
163}
164
166 Rec* rec = new Rec;
167 rec->fNext = fRec;
168 rec->fMatrix = fRec->fMatrix;
169 fRec = rec;
170}
171
173 SkASSERT(fRec != &fInitialRec);
174 Rec* next = fRec->fNext;
175 delete fRec;
176 fRec = next;
177}
178
179#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
180void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
181 // the camera location is passed in inches, set in pt
182 SkScalar lz = z * 72.0f;
183 fCamera.fLocation = {x * 72.0f, y * 72.0f, lz};
184 fCamera.fObserver = {0, 0, lz};
185 fCamera.update();
186
187}
188
189SkScalar Sk3DView::getCameraLocationX() const {
190 return fCamera.fLocation.x / 72.0f;
191}
192
193SkScalar Sk3DView::getCameraLocationY() const {
194 return fCamera.fLocation.y / 72.0f;
195}
196
197SkScalar Sk3DView::getCameraLocationZ() const {
198 return fCamera.fLocation.z / 72.0f;
199}
200#endif
201
203 fRec->fMatrix.preTranslate(x, y, z);
204}
205
207 fRec->fMatrix.preConcat(SkM44::Rotate({1, 0, 0}, deg * SK_ScalarPI / 180));
208}
209
211 fRec->fMatrix.preConcat(SkM44::Rotate({0,-1, 0}, deg * SK_ScalarPI / 180));
212}
213
215 fRec->fMatrix.preConcat(SkM44::Rotate({0, 0, 1}, deg * SK_ScalarPI / 180));
216}
217
219 SkPatch3D patch;
220 patch.transform(fRec->fMatrix);
221 return patch.dotWith(x, y, z);
222}
223
224void Sk3DView::getMatrix(SkMatrix* matrix) const {
225 if (matrix != nullptr) {
226 SkPatch3D patch;
227 patch.transform(fRec->fMatrix);
228 fCamera.patchToMatrix(patch, matrix);
229 }
230}
231
233
235 SkMatrix matrix;
236
237 this->getMatrix(&matrix);
238 canvas->concat(matrix);
239}
int count
static float next(float f)
#define SkASSERT(cond)
Definition SkAssert.h:116
static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a, const SkScalar b[], int step_b, SkScalar denom)
Definition SkCamera.cpp:10
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SK_ScalarPI
Definition SkScalar.h:21
void getMatrix(SkMatrix *) const
Definition SkCamera.cpp:224
void applyToCanvas(SkCanvas *) const
Definition SkCamera.cpp:234
void rotateZ(SkScalar deg)
Definition SkCamera.cpp:214
SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const
Definition SkCamera.cpp:218
void restore()
Definition SkCamera.cpp:172
void rotateX(SkScalar deg)
Definition SkCamera.cpp:206
void save()
Definition SkCamera.cpp:165
void translate(SkScalar x, SkScalar y, SkScalar z)
Definition SkCamera.cpp:202
void rotateY(SkScalar deg)
Definition SkCamera.cpp:210
void reset()
Definition SkCamera.cpp:58
void patchToMatrix(const SkPatch3D &, SkMatrix *matrix) const
Definition SkCamera.cpp:113
SkV3 fZenith
Definition SkCamera.h:63
void update()
Definition SkCamera.cpp:68
SkV3 fLocation
Definition SkCamera.h:61
SkV3 fObserver
Definition SkCamera.h:64
SkV3 fAxis
Definition SkCamera.h:62
void concat(const SkMatrix &matrix)
Definition SkM44.h:150
static SkM44 Rotate(SkV3 axis, SkScalar radians)
Definition SkM44.h:239
static constexpr int kMScaleX
horizontal scale factor
Definition SkMatrix.h:353
static constexpr int kMTransY
vertical translation
Definition SkMatrix.h:358
static constexpr int kMPersp1
input y perspective factor
Definition SkMatrix.h:360
SkMatrix & set(int index, SkScalar value)
Definition SkMatrix.h:489
static constexpr int kMPersp0
input x perspective factor
Definition SkMatrix.h:359
static constexpr int kMPersp2
perspective bias
Definition SkMatrix.h:361
static constexpr int kMTransX
horizontal translation
Definition SkMatrix.h:355
static constexpr int kMSkewY
vertical skew factor
Definition SkMatrix.h:356
static constexpr int kMScaleY
vertical scale factor
Definition SkMatrix.h:357
static constexpr int kMSkewX
horizontal skew factor
Definition SkMatrix.h:354
SkV3 fOrigin
Definition SkCamera.h:47
SkV3 fV
Definition SkCamera.h:46
void reset()
Definition SkCamera.cpp:28
SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const
Definition SkCamera.cpp:44
void transform(const SkM44 &, SkPatch3D *dst=nullptr) const
Definition SkCamera.cpp:34
SkV3 fU
Definition SkCamera.h:46
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct a[10]
double y
double x
Definition SkM44.h:56
SkV3 normalize() const
Definition SkM44.h:92
SkV3 cross(const SkV3 &v) const
Definition SkM44.h:91
SkScalar dot(const SkV3 &v) const
Definition SkM44.h:90
float y
Definition SkM44.h:57
float z
Definition SkM44.h:57
float x
Definition SkM44.h:57