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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkCamera.h" |
| 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 | |
| 22 | static SkScalar SkScalarDot(int count, const SkScalar a[], int step_a, |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 23 | const SkScalar b[], int step_b) { |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 24 | SkScalar prod = 0; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 25 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | prod += a[0] * b[0]; |
| 27 | a += step_a; |
| 28 | b += step_b; |
| 29 | } |
| 30 | return prod; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 35 | SkScalar SkPoint3D::normalize(SkUnit3D* unit) const { |
| 36 | SkScalar mag = SkScalarSqrt(fX*fX + fY*fY + fZ*fZ); |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 37 | if (mag) { |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 38 | SkScalar scale = SkScalarInvert(mag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | unit->fX = fX * scale; |
| 40 | unit->fY = fY * scale; |
| 41 | unit->fZ = fZ * scale; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 42 | } else { |
| 43 | unit->fX = unit->fY = unit->fZ = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | return mag; |
| 46 | } |
| 47 | |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 48 | SkScalar SkUnit3D::Dot(const SkUnit3D& a, const SkUnit3D& b) { |
| 49 | return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ; |
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 | void SkUnit3D::Cross(const SkUnit3D& a, const SkUnit3D& b, SkUnit3D* cross) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | SkASSERT(cross); |
| 54 | |
| 55 | // use x,y,z, in case &a == cross or &b == cross |
| 56 | |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 57 | SkScalar x = a.fY * b.fZ - a.fZ * b.fY; |
| 58 | SkScalar y = a.fZ * b.fX - a.fX * b.fY; |
| 59 | SkScalar z = a.fX * b.fY - a.fY * b.fX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
| 61 | cross->set(x, y, z); |
| 62 | } |
| 63 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 66 | SkPatch3D::SkPatch3D() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | this->reset(); |
| 68 | } |
| 69 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 70 | void SkPatch3D::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | fOrigin.set(0, 0, 0); |
| 72 | fU.set(SK_Scalar1, 0, 0); |
| 73 | fV.set(0, -SK_Scalar1, 0); |
| 74 | } |
| 75 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 76 | void SkPatch3D::transform(const SkMatrix3D& m, SkPatch3D* dst) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | if (dst == nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | dst = (SkPatch3D*)this; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 79 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | m.mapVector(fU, &dst->fU); |
| 81 | m.mapVector(fV, &dst->fV); |
| 82 | m.mapPoint(fOrigin, &dst->fOrigin); |
| 83 | } |
| 84 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 85 | SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const { |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 86 | SkScalar cx = fU.fY * fV.fZ - fU.fZ * fV.fY; |
| 87 | SkScalar cy = fU.fZ * fV.fX - fU.fX * fV.fY; |
| 88 | SkScalar cz = fU.fX * fV.fY - fU.fY * fV.fX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 90 | return cx * dx + cy * dy + cz * dz; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 95 | void SkMatrix3D::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | memset(fMat, 0, sizeof(fMat)); |
| 97 | fMat[0][0] = fMat[1][1] = fMat[2][2] = SK_Scalar1; |
| 98 | } |
| 99 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 100 | void SkMatrix3D::setTranslate(SkScalar x, SkScalar y, SkScalar z) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | memset(fMat, 0, sizeof(fMat)); |
| 102 | fMat[0][0] = x; |
| 103 | fMat[1][1] = y; |
| 104 | fMat[2][2] = z; |
| 105 | } |
| 106 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 107 | void SkMatrix3D::setRotateX(SkScalar degX) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | SkScalar s, c; |
| 109 | |
| 110 | s = SkScalarSinCos(SkDegreesToRadians(degX), &c); |
| 111 | this->setRow(0, SK_Scalar1, 0, 0); |
| 112 | this->setRow(1, 0, c, -s); |
| 113 | this->setRow(2, 0, s, c); |
| 114 | } |
| 115 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 116 | void SkMatrix3D::setRotateY(SkScalar degY) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | SkScalar s, c; |
| 118 | |
| 119 | s = SkScalarSinCos(SkDegreesToRadians(degY), &c); |
| 120 | this->setRow(0, c, 0, -s); |
| 121 | this->setRow(1, 0, SK_Scalar1, 0); |
| 122 | this->setRow(2, s, 0, c); |
| 123 | } |
| 124 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 125 | void SkMatrix3D::setRotateZ(SkScalar degZ) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | SkScalar s, c; |
| 127 | |
| 128 | s = SkScalarSinCos(SkDegreesToRadians(degZ), &c); |
| 129 | this->setRow(0, c, -s, 0); |
| 130 | this->setRow(1, s, c, 0); |
| 131 | this->setRow(2, 0, 0, SK_Scalar1); |
| 132 | } |
| 133 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 134 | void SkMatrix3D::preTranslate(SkScalar x, SkScalar y, SkScalar z) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | SkScalar col[3] = { x, y, z}; |
| 136 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 137 | for (int i = 0; i < 3; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | fMat[i][3] += SkScalarDot(3, &fMat[i][0], 1, col, 1); |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 142 | void SkMatrix3D::preRotateX(SkScalar degX) { |
| 143 | SkMatrix3D m; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | m.setRotateX(degX); |
| 145 | this->setConcat(*this, m); |
| 146 | } |
| 147 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 148 | void SkMatrix3D::preRotateY(SkScalar degY) { |
| 149 | SkMatrix3D m; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | m.setRotateY(degY); |
| 151 | this->setConcat(*this, m); |
| 152 | } |
| 153 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 154 | void SkMatrix3D::preRotateZ(SkScalar degZ) { |
| 155 | SkMatrix3D m; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | m.setRotateZ(degZ); |
| 157 | this->setConcat(*this, m); |
| 158 | } |
| 159 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 160 | void SkMatrix3D::setConcat(const SkMatrix3D& a, const SkMatrix3D& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | SkMatrix3D tmp; |
| 162 | SkMatrix3D* c = this; |
| 163 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 164 | if (this == &a || this == &b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | c = &tmp; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 166 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | for (int i = 0; i < 3; i++) { |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 168 | for (int j = 0; j < 3; j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | c->fMat[i][j] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][j], 4); |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 170 | } |
| 171 | c->fMat[i][3] = SkScalarDot(3, &a.fMat[i][0], 1, |
| 172 | &b.fMat[0][3], 4) + a.fMat[i][3]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | } |
| 174 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 175 | if (c == &tmp) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | *this = tmp; |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 177 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 180 | void SkMatrix3D::mapPoint(const SkPoint3D& src, SkPoint3D* dst) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1) + fMat[0][3]; |
| 182 | SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1) + fMat[1][3]; |
| 183 | SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1) + fMat[2][3]; |
| 184 | dst->set(x, y, z); |
| 185 | } |
| 186 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 187 | void SkMatrix3D::mapVector(const SkVector3D& src, SkVector3D* dst) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1); |
| 189 | SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1); |
| 190 | SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1); |
| 191 | dst->set(x, y, z); |
| 192 | } |
| 193 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 194 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 196 | SkCamera3D::SkCamera3D() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | this->reset(); |
| 198 | } |
| 199 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 200 | void SkCamera3D::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | fLocation.set(0, 0, -SkIntToScalar(576)); // 8 inches backward |
| 202 | fAxis.set(0, 0, SK_Scalar1); // forward |
| 203 | fZenith.set(0, -SK_Scalar1, 0); // up |
| 204 | |
| 205 | fObserver.set(0, 0, fLocation.fZ); |
| 206 | |
| 207 | fNeedToUpdate = true; |
| 208 | } |
| 209 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 210 | void SkCamera3D::update() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | fNeedToUpdate = true; |
| 212 | } |
| 213 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 214 | void SkCamera3D::doUpdate() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | SkUnit3D axis, zenith, cross; |
| 216 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 217 | // construct a orthonormal basis of cross (x), zenith (y), and axis (z) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | fAxis.normalize(&axis); |
| 219 | |
| 220 | { |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 221 | SkScalar dot = SkUnit3D::Dot(SkUnit3D{fZenith.fX, fZenith.fY, fZenith.fZ}, axis); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 223 | zenith.fX = fZenith.fX - dot * axis.fX; |
| 224 | zenith.fY = fZenith.fY - dot * axis.fY; |
| 225 | zenith.fZ = fZenith.fZ - dot * axis.fZ; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 227 | SkPoint3D{zenith.fX, zenith.fY, zenith.fZ}.normalize(&zenith); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | SkUnit3D::Cross(axis, zenith, &cross); |
| 231 | |
| 232 | { |
| 233 | SkMatrix* orien = &fOrientation; |
| 234 | SkScalar x = fObserver.fX; |
| 235 | SkScalar y = fObserver.fY; |
| 236 | SkScalar z = fObserver.fZ; |
| 237 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 238 | // Looking along the view axis we have: |
| 239 | // |
| 240 | // /|\ zenith |
| 241 | // | |
| 242 | // | |
| 243 | // | * observer (projected on XY plane) |
| 244 | // | |
| 245 | // |____________\ cross |
| 246 | // / |
| 247 | // |
| 248 | // So this does a z-shear along the view axis based on the observer's x and y values, |
| 249 | // and scales in x and y relative to the negative of the observer's z value |
| 250 | // (the observer is in the negative z direction). |
| 251 | |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 252 | orien->set(SkMatrix::kMScaleX, x * axis.fX - z * cross.fX); |
| 253 | orien->set(SkMatrix::kMSkewX, x * axis.fY - z * cross.fY); |
| 254 | orien->set(SkMatrix::kMTransX, x * axis.fZ - z * cross.fZ); |
| 255 | orien->set(SkMatrix::kMSkewY, y * axis.fX - z * zenith.fX); |
| 256 | orien->set(SkMatrix::kMScaleY, y * axis.fY - z * zenith.fY); |
| 257 | orien->set(SkMatrix::kMTransY, y * axis.fZ - z * zenith.fZ); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 258 | orien->set(SkMatrix::kMPersp0, axis.fX); |
| 259 | orien->set(SkMatrix::kMPersp1, axis.fY); |
| 260 | orien->set(SkMatrix::kMPersp2, axis.fZ); |
| 261 | } |
| 262 | } |
| 263 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 264 | void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const { |
| 265 | if (fNeedToUpdate) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | this->doUpdate(); |
| 267 | fNeedToUpdate = false; |
| 268 | } |
| 269 | |
| 270 | const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation; |
| 271 | const SkScalar* patchPtr; |
| 272 | SkPoint3D diff; |
| 273 | SkScalar dot; |
| 274 | |
| 275 | diff.fX = quilt.fOrigin.fX - fLocation.fX; |
| 276 | diff.fY = quilt.fOrigin.fY - fLocation.fY; |
| 277 | diff.fZ = quilt.fOrigin.fZ - fLocation.fZ; |
| 278 | |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 279 | dot = SkUnit3D::Dot(SkUnit3D{diff.fX, diff.fY, diff.fZ}, |
| 280 | SkUnit3D{mapPtr[6], mapPtr[7], mapPtr[8]}); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 282 | // This multiplies fOrientation by the matrix [quilt.fU quilt.fV diff] -- U, V, and diff are |
| 283 | // column vectors in the matrix -- then divides by the length of the projection of diff onto |
| 284 | // the view axis (which is 'dot'). This transforms the patch (which transforms from local path |
| 285 | // space to world space) into view space (since fOrientation transforms from world space to |
| 286 | // view space). |
| 287 | // |
| 288 | // The divide by 'dot' isn't strictly necessary as the homogeneous divide would do much the |
| 289 | // same thing (it's just scaling the entire matrix by 1/dot). It looks like it's normalizing |
| 290 | // the matrix into some canonical space. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | patchPtr = (const SkScalar*)&quilt; |
| 292 | matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); |
| 293 | matrix->set(SkMatrix::kMSkewY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot)); |
| 294 | matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot)); |
| 295 | |
| 296 | patchPtr += 3; |
| 297 | matrix->set(SkMatrix::kMSkewX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); |
| 298 | matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot)); |
| 299 | matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot)); |
| 300 | |
| 301 | patchPtr = (const SkScalar*)(const void*)&diff; |
| 302 | matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); |
| 303 | 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] | 304 | matrix->set(SkMatrix::kMPersp2, SK_Scalar1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | } |
| 306 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 307 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 309 | Sk3DView::Sk3DView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | fInitialRec.fMatrix.reset(); |
| 311 | fRec = &fInitialRec; |
| 312 | } |
| 313 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 314 | Sk3DView::~Sk3DView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | Rec* rec = fRec; |
| 316 | while (rec != &fInitialRec) { |
| 317 | Rec* next = rec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 318 | delete rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | rec = next; |
| 320 | } |
| 321 | } |
| 322 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 323 | void Sk3DView::save() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 324 | Rec* rec = new Rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | rec->fNext = fRec; |
| 326 | rec->fMatrix = fRec->fMatrix; |
| 327 | fRec = rec; |
| 328 | } |
| 329 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 330 | void Sk3DView::restore() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | SkASSERT(fRec != &fInitialRec); |
| 332 | Rec* next = fRec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 333 | delete fRec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | fRec = next; |
| 335 | } |
| 336 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 337 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 338 | void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 339 | // the camera location is passed in inches, set in pt |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 340 | SkScalar lz = z * 72.0f; |
| 341 | fCamera.fLocation.set(x * 72.0f, y * 72.0f, lz); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 342 | fCamera.fObserver.set(0, 0, lz); |
| 343 | fCamera.update(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 344 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 345 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 346 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 347 | SkScalar Sk3DView::getCameraLocationX() const { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 348 | return fCamera.fLocation.fX / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 351 | SkScalar Sk3DView::getCameraLocationY() const { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 352 | return fCamera.fLocation.fY / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 355 | SkScalar Sk3DView::getCameraLocationZ() const { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 356 | return fCamera.fLocation.fZ / 72.0f; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 357 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 358 | #endif |
| 359 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 360 | void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 361 | fRec->fMatrix.preTranslate(x, y, z); |
| 362 | } |
| 363 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 364 | void Sk3DView::rotateX(SkScalar deg) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | fRec->fMatrix.preRotateX(deg); |
| 366 | } |
| 367 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 368 | void Sk3DView::rotateY(SkScalar deg) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | fRec->fMatrix.preRotateY(deg); |
| 370 | } |
| 371 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 372 | void Sk3DView::rotateZ(SkScalar deg) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | fRec->fMatrix.preRotateZ(deg); |
| 374 | } |
| 375 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 376 | SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | SkPatch3D patch; |
| 378 | patch.transform(fRec->fMatrix); |
| 379 | return patch.dotWith(x, y, z); |
| 380 | } |
| 381 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 382 | void Sk3DView::getMatrix(SkMatrix* matrix) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 383 | if (matrix != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | SkPatch3D patch; |
| 385 | patch.transform(fRec->fMatrix); |
| 386 | fCamera.patchToMatrix(patch, matrix); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | #include "SkCanvas.h" |
| 391 | |
reed@google.com | 91408cc | 2011-04-18 18:47:46 +0000 | [diff] [blame] | 392 | void Sk3DView::applyToCanvas(SkCanvas* canvas) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | SkMatrix matrix; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 394 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | this->getMatrix(&matrix); |
| 396 | canvas->concat(matrix); |
| 397 | } |