blob: 45e216edb271ce840a9ec61a6cb9c38040ebf1d3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/utils/SkCamera.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a,
11 const SkScalar b[], int step_b,
reed@google.com91408cc2011-04-18 18:47:46 +000012 SkScalar denom) {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000013 SkScalar prod = 0;
reed@google.com91408cc2011-04-18 18:47:46 +000014 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 prod += a[0] * b[0];
16 a += step_a;
17 b += step_b;
18 }
19 return prod / denom;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020}
21
reed@google.com91408cc2011-04-18 18:47:46 +000022///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
reed@google.com91408cc2011-04-18 18:47:46 +000024SkPatch3D::SkPatch3D() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 this->reset();
26}
27
reed@google.com91408cc2011-04-18 18:47:46 +000028void SkPatch3D::reset() {
Mike Reed4a9a2d12020-05-17 17:09:43 -040029 fOrigin = {0, 0, 0};
30 fU = {SK_Scalar1, 0, 0};
31 fV = {0, -SK_Scalar1, 0};
reed@android.com8a1c16f2008-12-17 15:59:43 +000032}
33
Mike Reed4a9a2d12020-05-17 17:09:43 -040034void SkPatch3D::transform(const SkM44& m, SkPatch3D* dst) const {
halcanary96fcdcc2015-08-27 07:41:13 -070035 if (dst == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 dst = (SkPatch3D*)this;
reed@google.com91408cc2011-04-18 18:47:46 +000037 }
Mike Reed4a9a2d12020-05-17 17:09:43 -040038 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};
reed@android.com8a1c16f2008-12-17 15:59:43 +000042}
43
reed@google.com91408cc2011-04-18 18:47:46 +000044SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const {
Mike Reed4a9a2d12020-05-17 17:09:43 -040045 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;
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
Mike Reeda99b6ce2017-02-04 11:04:26 -050049 return cx * dx + cy * dy + cz * dz;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050}
51
reed@google.com91408cc2011-04-18 18:47:46 +000052///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
reed@google.com91408cc2011-04-18 18:47:46 +000054SkCamera3D::SkCamera3D() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 this->reset();
56}
57
reed@google.com91408cc2011-04-18 18:47:46 +000058void SkCamera3D::reset() {
Mike Reed4a9a2d12020-05-17 17:09:43 -040059 fLocation = {0, 0, -SkIntToScalar(576)}; // 8 inches backward
60 fAxis = {0, 0, SK_Scalar1}; // forward
61 fZenith = {0, -SK_Scalar1, 0}; // up
reed@android.com8a1c16f2008-12-17 15:59:43 +000062
Mike Reed4a9a2d12020-05-17 17:09:43 -040063 fObserver = {0, 0, fLocation.z};
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
65 fNeedToUpdate = true;
66}
67
reed@google.com91408cc2011-04-18 18:47:46 +000068void SkCamera3D::update() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 fNeedToUpdate = true;
70}
71
reed@google.com91408cc2011-04-18 18:47:46 +000072void SkCamera3D::doUpdate() const {
Mike Reed4a9a2d12020-05-17 17:09:43 -040073 SkV3 axis, zenith, cross;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
Jim Van Verthda965502017-04-11 15:29:14 -040075 // construct a orthonormal basis of cross (x), zenith (y), and axis (z)
Mike Reed4a9a2d12020-05-17 17:09:43 -040076 axis = fAxis.normalize();
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
Mike Reed4a9a2d12020-05-17 17:09:43 -040078 zenith = fZenith - (axis * fZenith) * axis;
79 zenith = zenith.normalize();
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
Mike Reed4a9a2d12020-05-17 17:09:43 -040081 cross = axis.cross(zenith);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082
83 {
84 SkMatrix* orien = &fOrientation;
Mike Reed4a9a2d12020-05-17 17:09:43 -040085 auto [x, y, z] = fObserver;
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
Jim Van Verthda965502017-04-11 15:29:14 -040087 // 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
Mike Reed4a9a2d12020-05-17 17:09:43 -0400101 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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 }
111}
112
reed@google.com91408cc2011-04-18 18:47:46 +0000113void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
114 if (fNeedToUpdate) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 this->doUpdate();
116 fNeedToUpdate = false;
117 }
118
119 const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
120 const SkScalar* patchPtr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121
Mike Reed4a9a2d12020-05-17 17:09:43 -0400122 SkV3 diff = quilt.fOrigin - fLocation;
123 SkScalar dot = diff.dot({mapPtr[6], mapPtr[7], mapPtr[8]});
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
Jim Van Verthda965502017-04-11 15:29:14 -0400125 // 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.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 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));
mike@reedtribe.orgd173b872014-01-23 02:02:45 +0000147 matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148}
149
reed@google.com91408cc2011-04-18 18:47:46 +0000150///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151
reed@google.com91408cc2011-04-18 18:47:46 +0000152Sk3DView::Sk3DView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 fRec = &fInitialRec;
154}
155
reed@google.com91408cc2011-04-18 18:47:46 +0000156Sk3DView::~Sk3DView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 Rec* rec = fRec;
158 while (rec != &fInitialRec) {
159 Rec* next = rec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -0700160 delete rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 rec = next;
162 }
163}
164
reed@google.com91408cc2011-04-18 18:47:46 +0000165void Sk3DView::save() {
halcanary385fe4d2015-08-26 13:07:48 -0700166 Rec* rec = new Rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 rec->fNext = fRec;
168 rec->fMatrix = fRec->fMatrix;
169 fRec = rec;
170}
171
reed@google.com91408cc2011-04-18 18:47:46 +0000172void Sk3DView::restore() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkASSERT(fRec != &fInitialRec);
174 Rec* next = fRec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -0700175 delete fRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 fRec = next;
177}
178
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500179#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
reed@google.com91408cc2011-04-18 18:47:46 +0000180void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000181 // the camera location is passed in inches, set in pt
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000182 SkScalar lz = z * 72.0f;
Mike Reed4a9a2d12020-05-17 17:09:43 -0400183 fCamera.fLocation = {x * 72.0f, y * 72.0f, lz};
184 fCamera.fObserver = {0, 0, lz};
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000185 fCamera.update();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000187}
djsollen@google.come63793a2012-03-21 15:39:03 +0000188
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500189SkScalar Sk3DView::getCameraLocationX() const {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400190 return fCamera.fLocation.x / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000191}
192
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500193SkScalar Sk3DView::getCameraLocationY() const {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400194 return fCamera.fLocation.y / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000195}
196
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500197SkScalar Sk3DView::getCameraLocationZ() const {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400198 return fCamera.fLocation.z / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000199}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000200#endif
201
reed@google.com91408cc2011-04-18 18:47:46 +0000202void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 fRec->fMatrix.preTranslate(x, y, z);
204}
205
reed@google.com91408cc2011-04-18 18:47:46 +0000206void Sk3DView::rotateX(SkScalar deg) {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400207 fRec->fMatrix.preConcat(SkM44::Rotate({1, 0, 0}, deg * SK_ScalarPI / 180));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208}
209
reed@google.com91408cc2011-04-18 18:47:46 +0000210void Sk3DView::rotateY(SkScalar deg) {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400211 fRec->fMatrix.preConcat(SkM44::Rotate({0,-1, 0}, deg * SK_ScalarPI / 180));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212}
213
reed@google.com91408cc2011-04-18 18:47:46 +0000214void Sk3DView::rotateZ(SkScalar deg) {
Mike Reed4a9a2d12020-05-17 17:09:43 -0400215 fRec->fMatrix.preConcat(SkM44::Rotate({0, 0, 1}, deg * SK_ScalarPI / 180));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216}
217
reed@google.com91408cc2011-04-18 18:47:46 +0000218SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 SkPatch3D patch;
220 patch.transform(fRec->fMatrix);
221 return patch.dotWith(x, y, z);
222}
223
reed@google.com91408cc2011-04-18 18:47:46 +0000224void Sk3DView::getMatrix(SkMatrix* matrix) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 if (matrix != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 SkPatch3D patch;
227 patch.transform(fRec->fMatrix);
228 fCamera.patchToMatrix(patch, matrix);
229 }
230}
231
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500232#include "include/core/SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233
reed@google.com91408cc2011-04-18 18:47:46 +0000234void Sk3DView::applyToCanvas(SkCanvas* canvas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 this->getMatrix(&matrix);
238 canvas->concat(matrix);
239}