epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkCamera.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a, |
| 11 | const SkScalar b[], int step_b, |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 12 | SkScalar denom) { |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 13 | SkScalar prod = 0; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 14 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | prod += a[0] * b[0]; |
| 16 | a += step_a; |
| 17 | b += step_b; |
| 18 | } |
| 19 | return prod / denom; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | } |
| 21 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 22 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 24 | SkPatch3D::SkPatch3D() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | this->reset(); |
| 26 | } |
| 27 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 28 | void SkPatch3D::reset() { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 29 | fOrigin = {0, 0, 0}; |
| 30 | fU = {SK_Scalar1, 0, 0}; |
| 31 | fV = {0, -SK_Scalar1, 0}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 34 | void SkPatch3D::transform(const SkM44& m, SkPatch3D* dst) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | if (dst == nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | dst = (SkPatch3D*)this; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 37 | } |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 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}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 44 | SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 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; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 49 | return cx * dx + cy * dy + cz * dz; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 52 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 54 | SkCamera3D::SkCamera3D() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | this->reset(); |
| 56 | } |
| 57 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 58 | void SkCamera3D::reset() { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 59 | fLocation = {0, 0, -SkIntToScalar(576)}; // 8 inches backward |
| 60 | fAxis = {0, 0, SK_Scalar1}; // forward |
| 61 | fZenith = {0, -SK_Scalar1, 0}; // up |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 63 | fObserver = {0, 0, fLocation.z}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | |
| 65 | fNeedToUpdate = true; |
| 66 | } |
| 67 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 68 | void SkCamera3D::update() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | fNeedToUpdate = true; |
| 70 | } |
| 71 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 72 | void SkCamera3D::doUpdate() const { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 73 | SkV3 axis, zenith, cross; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 75 | // construct a orthonormal basis of cross (x), zenith (y), and axis (z) |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 76 | axis = fAxis.normalize(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 78 | zenith = fZenith - (axis * fZenith) * axis; |
| 79 | zenith = zenith.normalize(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 81 | cross = axis.cross(zenith); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | SkMatrix* orien = &fOrientation; |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 85 | auto [x, y, z] = fObserver; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 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 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 113 | void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const { |
| 114 | if (fNeedToUpdate) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | this->doUpdate(); |
| 116 | fNeedToUpdate = false; |
| 117 | } |
| 118 | |
| 119 | const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation; |
| 120 | const SkScalar* patchPtr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 122 | SkV3 diff = quilt.fOrigin - fLocation; |
| 123 | SkScalar dot = diff.dot({mapPtr[6], mapPtr[7], mapPtr[8]}); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 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. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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)); |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 147 | matrix->set(SkMatrix::kMPersp2, SK_Scalar1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 150 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 152 | Sk3DView::Sk3DView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | fRec = &fInitialRec; |
| 154 | } |
| 155 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 156 | Sk3DView::~Sk3DView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | Rec* rec = fRec; |
| 158 | while (rec != &fInitialRec) { |
| 159 | Rec* next = rec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 160 | delete rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | rec = next; |
| 162 | } |
| 163 | } |
| 164 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 165 | void Sk3DView::save() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 166 | Rec* rec = new Rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | rec->fNext = fRec; |
| 168 | rec->fMatrix = fRec->fMatrix; |
| 169 | fRec = rec; |
| 170 | } |
| 171 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 172 | void Sk3DView::restore() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | SkASSERT(fRec != &fInitialRec); |
| 174 | Rec* next = fRec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 175 | delete fRec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | fRec = next; |
| 177 | } |
| 178 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 179 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 180 | void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 181 | // the camera location is passed in inches, set in pt |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 182 | SkScalar lz = z * 72.0f; |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 183 | fCamera.fLocation = {x * 72.0f, y * 72.0f, lz}; |
| 184 | fCamera.fObserver = {0, 0, lz}; |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 185 | fCamera.update(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 186 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 187 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 188 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 189 | SkScalar Sk3DView::getCameraLocationX() const { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 190 | return fCamera.fLocation.x / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 193 | SkScalar Sk3DView::getCameraLocationY() const { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 194 | return fCamera.fLocation.y / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 197 | SkScalar Sk3DView::getCameraLocationZ() const { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 198 | return fCamera.fLocation.z / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 199 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 200 | #endif |
| 201 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 202 | void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | fRec->fMatrix.preTranslate(x, y, z); |
| 204 | } |
| 205 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 206 | void Sk3DView::rotateX(SkScalar deg) { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 207 | fRec->fMatrix.preConcat(SkM44::Rotate({1, 0, 0}, deg * SK_ScalarPI / 180)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | } |
| 209 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 210 | void Sk3DView::rotateY(SkScalar deg) { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 211 | fRec->fMatrix.preConcat(SkM44::Rotate({0,-1, 0}, deg * SK_ScalarPI / 180)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | } |
| 213 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 214 | void Sk3DView::rotateZ(SkScalar deg) { |
Mike Reed | 4a9a2d1 | 2020-05-17 17:09:43 -0400 | [diff] [blame] | 215 | fRec->fMatrix.preConcat(SkM44::Rotate({0, 0, 1}, deg * SK_ScalarPI / 180)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | } |
| 217 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 218 | SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | SkPatch3D patch; |
| 220 | patch.transform(fRec->fMatrix); |
| 221 | return patch.dotWith(x, y, z); |
| 222 | } |
| 223 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 224 | void Sk3DView::getMatrix(SkMatrix* matrix) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 225 | if (matrix != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | SkPatch3D patch; |
| 227 | patch.transform(fRec->fMatrix); |
| 228 | fCamera.patchToMatrix(patch, matrix); |
| 229 | } |
| 230 | } |
| 231 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 232 | #include "include/core/SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 234 | void Sk3DView::applyToCanvas(SkCanvas* canvas) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | SkMatrix matrix; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | this->getMatrix(&matrix); |
| 238 | canvas->concat(matrix); |
| 239 | } |