blob: d9f9cf545038d2ab19500d548ecf8e2987ba76e8 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkCamera.h"
9
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
22static SkScalar SkScalarDot(int count, const SkScalar a[], int step_a,
reed@google.com91408cc2011-04-18 18:47:46 +000023 const SkScalar b[], int step_b) {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000024 SkScalar prod = 0;
reed@google.com91408cc2011-04-18 18:47:46 +000025 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 prod += a[0] * b[0];
27 a += step_a;
28 b += step_b;
29 }
30 return prod;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031}
32
reed@google.com91408cc2011-04-18 18:47:46 +000033///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000035SkScalar SkPoint3D::normalize(SkUnit3D* unit) const {
36 SkScalar mag = SkScalarSqrt(fX*fX + fY*fY + fZ*fZ);
reed@google.com91408cc2011-04-18 18:47:46 +000037 if (mag) {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000038 SkScalar scale = SkScalarInvert(mag);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 unit->fX = fX * scale;
40 unit->fY = fY * scale;
41 unit->fZ = fZ * scale;
reed@google.com91408cc2011-04-18 18:47:46 +000042 } else {
43 unit->fX = unit->fY = unit->fZ = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 return mag;
46}
47
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000048SkScalar SkUnit3D::Dot(const SkUnit3D& a, const SkUnit3D& b) {
49 return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050}
51
reed@google.com91408cc2011-04-18 18:47:46 +000052void SkUnit3D::Cross(const SkUnit3D& a, const SkUnit3D& b, SkUnit3D* cross) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 SkASSERT(cross);
54
55 // use x,y,z, in case &a == cross or &b == cross
56
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000057 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.com8a1c16f2008-12-17 15:59:43 +000060
61 cross->set(x, y, z);
62}
63
reed@google.com91408cc2011-04-18 18:47:46 +000064///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
reed@google.com91408cc2011-04-18 18:47:46 +000066SkPatch3D::SkPatch3D() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 this->reset();
68}
69
reed@google.com91408cc2011-04-18 18:47:46 +000070void SkPatch3D::reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 fOrigin.set(0, 0, 0);
72 fU.set(SK_Scalar1, 0, 0);
73 fV.set(0, -SK_Scalar1, 0);
74}
75
reed@google.com91408cc2011-04-18 18:47:46 +000076void SkPatch3D::transform(const SkMatrix3D& m, SkPatch3D* dst) const {
77 if (dst == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 dst = (SkPatch3D*)this;
reed@google.com91408cc2011-04-18 18:47:46 +000079 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 m.mapVector(fU, &dst->fU);
81 m.mapVector(fV, &dst->fV);
82 m.mapPoint(fOrigin, &dst->fOrigin);
83}
84
reed@google.com91408cc2011-04-18 18:47:46 +000085SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkScalar cx = SkScalarMul(fU.fY, fV.fZ) - SkScalarMul(fU.fZ, fV.fY);
87 SkScalar cy = SkScalarMul(fU.fZ, fV.fX) - SkScalarMul(fU.fX, fV.fY);
88 SkScalar cz = SkScalarMul(fU.fX, fV.fY) - SkScalarMul(fU.fY, fV.fX);
89
90 return SkScalarMul(cx, dx) + SkScalarMul(cy, dy) + SkScalarMul(cz, dz);
91}
92
reed@google.com91408cc2011-04-18 18:47:46 +000093///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
reed@google.com91408cc2011-04-18 18:47:46 +000095void SkMatrix3D::reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 memset(fMat, 0, sizeof(fMat));
97 fMat[0][0] = fMat[1][1] = fMat[2][2] = SK_Scalar1;
98}
99
reed@google.com91408cc2011-04-18 18:47:46 +0000100void SkMatrix3D::setTranslate(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 memset(fMat, 0, sizeof(fMat));
102 fMat[0][0] = x;
103 fMat[1][1] = y;
104 fMat[2][2] = z;
105}
106
reed@google.com91408cc2011-04-18 18:47:46 +0000107void SkMatrix3D::setRotateX(SkScalar degX) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 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.com91408cc2011-04-18 18:47:46 +0000116void SkMatrix3D::setRotateY(SkScalar degY) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 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.com91408cc2011-04-18 18:47:46 +0000125void SkMatrix3D::setRotateZ(SkScalar degZ) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 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.com91408cc2011-04-18 18:47:46 +0000134void SkMatrix3D::preTranslate(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkScalar col[3] = { x, y, z};
136
reed@google.com91408cc2011-04-18 18:47:46 +0000137 for (int i = 0; i < 3; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 fMat[i][3] += SkScalarDot(3, &fMat[i][0], 1, col, 1);
reed@google.com91408cc2011-04-18 18:47:46 +0000139 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140}
141
reed@google.com91408cc2011-04-18 18:47:46 +0000142void SkMatrix3D::preRotateX(SkScalar degX) {
143 SkMatrix3D m;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 m.setRotateX(degX);
145 this->setConcat(*this, m);
146}
147
reed@google.com91408cc2011-04-18 18:47:46 +0000148void SkMatrix3D::preRotateY(SkScalar degY) {
149 SkMatrix3D m;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 m.setRotateY(degY);
151 this->setConcat(*this, m);
152}
153
reed@google.com91408cc2011-04-18 18:47:46 +0000154void SkMatrix3D::preRotateZ(SkScalar degZ) {
155 SkMatrix3D m;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 m.setRotateZ(degZ);
157 this->setConcat(*this, m);
158}
159
reed@google.com91408cc2011-04-18 18:47:46 +0000160void SkMatrix3D::setConcat(const SkMatrix3D& a, const SkMatrix3D& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 SkMatrix3D tmp;
162 SkMatrix3D* c = this;
163
reed@google.com91408cc2011-04-18 18:47:46 +0000164 if (this == &a || this == &b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 c = &tmp;
reed@google.com91408cc2011-04-18 18:47:46 +0000166 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 for (int i = 0; i < 3; i++) {
reed@google.com91408cc2011-04-18 18:47:46 +0000168 for (int j = 0; j < 3; j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 c->fMat[i][j] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][j], 4);
reed@google.com91408cc2011-04-18 18:47:46 +0000170 }
171 c->fMat[i][3] = SkScalarDot(3, &a.fMat[i][0], 1,
172 &b.fMat[0][3], 4) + a.fMat[i][3];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 }
174
reed@google.com91408cc2011-04-18 18:47:46 +0000175 if (c == &tmp) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 *this = tmp;
reed@google.com91408cc2011-04-18 18:47:46 +0000177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178}
179
reed@google.com91408cc2011-04-18 18:47:46 +0000180void SkMatrix3D::mapPoint(const SkPoint3D& src, SkPoint3D* dst) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 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.com91408cc2011-04-18 18:47:46 +0000187void SkMatrix3D::mapVector(const SkVector3D& src, SkVector3D* dst) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 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.com91408cc2011-04-18 18:47:46 +0000194///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195
reed@google.com91408cc2011-04-18 18:47:46 +0000196SkCamera3D::SkCamera3D() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 this->reset();
198}
199
reed@google.com91408cc2011-04-18 18:47:46 +0000200void SkCamera3D::reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 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.com91408cc2011-04-18 18:47:46 +0000210void SkCamera3D::update() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 fNeedToUpdate = true;
212}
213
reed@google.com91408cc2011-04-18 18:47:46 +0000214void SkCamera3D::doUpdate() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 SkUnit3D axis, zenith, cross;
216
217 fAxis.normalize(&axis);
218
219 {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000220 SkScalar dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&fZenith), axis);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
mike@reedtribe.orgd173b872014-01-23 02:02:45 +0000222 zenith.fX = fZenith.fX - dot * axis.fX;
223 zenith.fY = fZenith.fY - dot * axis.fY;
224 zenith.fZ = fZenith.fZ - dot * axis.fZ;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000226 SkTCast<SkPoint3D*>(&zenith)->normalize(&zenith);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 }
228
229 SkUnit3D::Cross(axis, zenith, &cross);
230
231 {
232 SkMatrix* orien = &fOrientation;
233 SkScalar x = fObserver.fX;
234 SkScalar y = fObserver.fY;
235 SkScalar z = fObserver.fZ;
236
mike@reedtribe.orgd173b872014-01-23 02:02:45 +0000237 orien->set(SkMatrix::kMScaleX, x * axis.fX - z * cross.fX);
238 orien->set(SkMatrix::kMSkewX, x * axis.fY - z * cross.fY);
239 orien->set(SkMatrix::kMTransX, x * axis.fZ - z * cross.fZ);
240 orien->set(SkMatrix::kMSkewY, y * axis.fX - z * zenith.fX);
241 orien->set(SkMatrix::kMScaleY, y * axis.fY - z * zenith.fY);
242 orien->set(SkMatrix::kMTransY, y * axis.fZ - z * zenith.fZ);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 orien->set(SkMatrix::kMPersp0, axis.fX);
244 orien->set(SkMatrix::kMPersp1, axis.fY);
245 orien->set(SkMatrix::kMPersp2, axis.fZ);
246 }
247}
248
reed@google.com91408cc2011-04-18 18:47:46 +0000249void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
250 if (fNeedToUpdate) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 this->doUpdate();
252 fNeedToUpdate = false;
253 }
254
255 const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
256 const SkScalar* patchPtr;
257 SkPoint3D diff;
258 SkScalar dot;
259
260 diff.fX = quilt.fOrigin.fX - fLocation.fX;
261 diff.fY = quilt.fOrigin.fY - fLocation.fY;
262 diff.fZ = quilt.fOrigin.fZ - fLocation.fZ;
263
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000264 dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&diff),
265 *SkTCast<const SkUnit3D*>(SkTCast<const SkScalar*>(&fOrientation) + 6));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266
267 patchPtr = (const SkScalar*)&quilt;
268 matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
269 matrix->set(SkMatrix::kMSkewY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
270 matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
271
272 patchPtr += 3;
273 matrix->set(SkMatrix::kMSkewX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
274 matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
275 matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
276
277 patchPtr = (const SkScalar*)(const void*)&diff;
278 matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
279 matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
mike@reedtribe.orgd173b872014-01-23 02:02:45 +0000280 matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281}
282
reed@google.com91408cc2011-04-18 18:47:46 +0000283///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
reed@google.com91408cc2011-04-18 18:47:46 +0000285Sk3DView::Sk3DView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 fInitialRec.fMatrix.reset();
287 fRec = &fInitialRec;
288}
289
reed@google.com91408cc2011-04-18 18:47:46 +0000290Sk3DView::~Sk3DView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 Rec* rec = fRec;
292 while (rec != &fInitialRec) {
293 Rec* next = rec->fNext;
294 SkDELETE(rec);
295 rec = next;
296 }
297}
298
reed@google.com91408cc2011-04-18 18:47:46 +0000299void Sk3DView::save() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 Rec* rec = SkNEW(Rec);
301 rec->fNext = fRec;
302 rec->fMatrix = fRec->fMatrix;
303 fRec = rec;
304}
305
reed@google.com91408cc2011-04-18 18:47:46 +0000306void Sk3DView::restore() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 SkASSERT(fRec != &fInitialRec);
308 Rec* next = fRec->fNext;
309 SkDELETE(fRec);
310 fRec = next;
311}
312
djsollen@google.com56c69772011-11-08 19:00:26 +0000313#ifdef SK_BUILD_FOR_ANDROID
reed@google.com91408cc2011-04-18 18:47:46 +0000314void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000315 // the camera location is passed in inches, set in pt
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000316 SkScalar lz = z * 72.0f;
317 fCamera.fLocation.set(x * 72.0f, y * 72.0f, lz);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000318 fCamera.fObserver.set(0, 0, lz);
319 fCamera.update();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000320
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000321}
djsollen@google.come63793a2012-03-21 15:39:03 +0000322
323SkScalar Sk3DView::getCameraLocationX() {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000324 return fCamera.fLocation.fX / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000325}
326
327SkScalar Sk3DView::getCameraLocationY() {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000328 return fCamera.fLocation.fY / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000329}
330
331SkScalar Sk3DView::getCameraLocationZ() {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000332 return fCamera.fLocation.fZ / 72.0f;
djsollen@google.come63793a2012-03-21 15:39:03 +0000333}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000334#endif
335
reed@google.com91408cc2011-04-18 18:47:46 +0000336void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 fRec->fMatrix.preTranslate(x, y, z);
338}
339
reed@google.com91408cc2011-04-18 18:47:46 +0000340void Sk3DView::rotateX(SkScalar deg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 fRec->fMatrix.preRotateX(deg);
342}
343
reed@google.com91408cc2011-04-18 18:47:46 +0000344void Sk3DView::rotateY(SkScalar deg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 fRec->fMatrix.preRotateY(deg);
346}
347
reed@google.com91408cc2011-04-18 18:47:46 +0000348void Sk3DView::rotateZ(SkScalar deg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 fRec->fMatrix.preRotateZ(deg);
350}
351
reed@google.com91408cc2011-04-18 18:47:46 +0000352SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 SkPatch3D patch;
354 patch.transform(fRec->fMatrix);
355 return patch.dotWith(x, y, z);
356}
357
reed@google.com91408cc2011-04-18 18:47:46 +0000358void Sk3DView::getMatrix(SkMatrix* matrix) const {
359 if (matrix != NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 SkPatch3D patch;
361 patch.transform(fRec->fMatrix);
362 fCamera.patchToMatrix(patch, matrix);
363 }
364}
365
366#include "SkCanvas.h"
367
reed@google.com91408cc2011-04-18 18:47:46 +0000368void Sk3DView::applyToCanvas(SkCanvas* canvas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000370
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 this->getMatrix(&matrix);
372 canvas->concat(matrix);
373}