1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCamera.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a, 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const SkScalar b[], int step_b, 1291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com SkScalar denom) { 13d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar prod = 0; 1491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com for (int i = 0; i < count; i++) { 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com prod += a[0] * b[0]; 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a += step_a; 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b += step_b; 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prod / denom; 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar SkScalarDot(int count, const SkScalar a[], int step_a, 2391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com const SkScalar b[], int step_b) { 24d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar prod = 0; 2591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com for (int i = 0; i < count; i++) { 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com prod += a[0] * b[0]; 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a += step_a; 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b += step_b; 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prod; 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com/////////////////////////////////////////////////////////////////////////////// 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 35d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.orgSkScalar SkPoint3D::normalize(SkUnit3D* unit) const { 36d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar mag = SkScalarSqrt(fX*fX + fY*fY + fZ*fZ); 3791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com if (mag) { 38d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar scale = SkScalarInvert(mag); 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unit->fX = fX * scale; 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unit->fY = fY * scale; 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unit->fZ = fZ * scale; 4291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } else { 4391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com unit->fX = unit->fY = unit->fZ = 0; 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return mag; 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 48d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.orgSkScalar SkUnit3D::Dot(const SkUnit3D& a, const SkUnit3D& b) { 49d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ; 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkUnit3D::Cross(const SkUnit3D& a, const SkUnit3D& b, SkUnit3D* cross) { 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(cross); 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // use x,y,z, in case &a == cross or &b == cross 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 57d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar x = a.fY * b.fZ - a.fZ * b.fY; 58d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar y = a.fZ * b.fX - a.fX * b.fY; 59d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org SkScalar z = a.fX * b.fY - a.fY * b.fX; 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com cross->set(x, y, z); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com/////////////////////////////////////////////////////////////////////////////// 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSkPatch3D::SkPatch3D() { 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->reset(); 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkPatch3D::reset() { 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fOrigin.set(0, 0, 0); 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fU.set(SK_Scalar1, 0, 0); 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fV.set(0, -SK_Scalar1, 0); 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkPatch3D::transform(const SkMatrix3D& m, SkPatch3D* dst) const { 7796fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (dst == nullptr) { 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst = (SkPatch3D*)this; 7991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.mapVector(fU, &dst->fU); 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.mapVector(fV, &dst->fV); 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.mapPoint(fOrigin, &dst->fOrigin); 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const { 86a99b6ceff92183b424634f2e7276b9ea1d59e69dMike Reed SkScalar cx = fU.fY * fV.fZ - fU.fZ * fV.fY; 87a99b6ceff92183b424634f2e7276b9ea1d59e69dMike Reed SkScalar cy = fU.fZ * fV.fX - fU.fX * fV.fY; 88a99b6ceff92183b424634f2e7276b9ea1d59e69dMike Reed SkScalar cz = fU.fX * fV.fY - fU.fY * fV.fX; 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 90a99b6ceff92183b424634f2e7276b9ea1d59e69dMike Reed return cx * dx + cy * dy + cz * dz; 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com/////////////////////////////////////////////////////////////////////////////// 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::reset() { 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memset(fMat, 0, sizeof(fMat)); 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMat[0][0] = fMat[1][1] = fMat[2][2] = SK_Scalar1; 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::setTranslate(SkScalar x, SkScalar y, SkScalar z) { 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memset(fMat, 0, sizeof(fMat)); 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMat[0][0] = x; 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMat[1][1] = y; 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMat[2][2] = z; 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::setRotateX(SkScalar degX) { 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar s, c; 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com s = SkScalarSinCos(SkDegreesToRadians(degX), &c); 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(0, SK_Scalar1, 0, 0); 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(1, 0, c, -s); 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(2, 0, s, c); 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::setRotateY(SkScalar degY) { 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar s, c; 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com s = SkScalarSinCos(SkDegreesToRadians(degY), &c); 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(0, c, 0, -s); 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(1, 0, SK_Scalar1, 0); 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(2, s, 0, c); 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::setRotateZ(SkScalar degZ) { 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar s, c; 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com s = SkScalarSinCos(SkDegreesToRadians(degZ), &c); 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(0, c, -s, 0); 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(1, s, c, 0); 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setRow(2, 0, 0, SK_Scalar1); 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 13491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::preTranslate(SkScalar x, SkScalar y, SkScalar z) { 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar col[3] = { x, y, z}; 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 13791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com for (int i = 0; i < 3; i++) { 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMat[i][3] += SkScalarDot(3, &fMat[i][0], 1, col, 1); 13991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 14291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::preRotateX(SkScalar degX) { 14391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com SkMatrix3D m; 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.setRotateX(degX); 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setConcat(*this, m); 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 14891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::preRotateY(SkScalar degY) { 14991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com SkMatrix3D m; 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.setRotateY(degY); 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setConcat(*this, m); 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 15491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::preRotateZ(SkScalar degZ) { 15591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com SkMatrix3D m; 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com m.setRotateZ(degZ); 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->setConcat(*this, m); 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 16091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::setConcat(const SkMatrix3D& a, const SkMatrix3D& b) { 1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMatrix3D tmp; 1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMatrix3D* c = this; 1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 16491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com if (this == &a || this == &b) { 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com c = &tmp; 16691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int i = 0; i < 3; i++) { 16891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com for (int j = 0; j < 3; j++) { 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com c->fMat[i][j] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][j], 4); 17091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } 17191408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com c->fMat[i][3] = SkScalarDot(3, &a.fMat[i][0], 1, 17291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com &b.fMat[0][3], 4) + a.fMat[i][3]; 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 17591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com if (c == &tmp) { 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *this = tmp; 17791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com } 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 18091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::mapPoint(const SkPoint3D& src, SkPoint3D* dst) const { 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1) + fMat[0][3]; 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1) + fMat[1][3]; 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1) + fMat[2][3]; 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst->set(x, y, z); 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 18791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkMatrix3D::mapVector(const SkVector3D& src, SkVector3D* dst) const { 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1); 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1); 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1); 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst->set(x, y, z); 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 19491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com/////////////////////////////////////////////////////////////////////////////// 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 19691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSkCamera3D::SkCamera3D() { 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->reset(); 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 20091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkCamera3D::reset() { 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fLocation.set(0, 0, -SkIntToScalar(576)); // 8 inches backward 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fAxis.set(0, 0, SK_Scalar1); // forward 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fZenith.set(0, -SK_Scalar1, 0); // up 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fObserver.set(0, 0, fLocation.fZ); 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fNeedToUpdate = true; 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 21091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkCamera3D::update() { 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fNeedToUpdate = true; 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 21491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkCamera3D::doUpdate() const { 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkUnit3D axis, zenith, cross; 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2176f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // construct a orthonormal basis of cross (x), zenith (y), and axis (z) 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fAxis.normalize(&axis); 2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 221373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com SkScalar dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&fZenith), axis); 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 223d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org zenith.fX = fZenith.fX - dot * axis.fX; 224d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org zenith.fY = fZenith.fY - dot * axis.fY; 225d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org zenith.fZ = fZenith.fZ - dot * axis.fZ; 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 227373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com SkTCast<SkPoint3D*>(&zenith)->normalize(&zenith); 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkUnit3D::Cross(axis, zenith, &cross); 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMatrix* orien = &fOrientation; 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = fObserver.fX; 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y = fObserver.fY; 2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar z = fObserver.fZ; 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2386f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // Looking along the view axis we have: 2396f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // 2406f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // /|\ zenith 2416f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // | 2426f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // | 2436f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // | * observer (projected on XY plane) 2446f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // | 2456f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // |____________\ cross 2466f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // / 2476f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // 2486f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // So this does a z-shear along the view axis based on the observer's x and y values, 2496f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // and scales in x and y relative to the negative of the observer's z value 2506f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // (the observer is in the negative z direction). 2516f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth 252d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMScaleX, x * axis.fX - z * cross.fX); 253d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMSkewX, x * axis.fY - z * cross.fY); 254d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMTransX, x * axis.fZ - z * cross.fZ); 255d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMSkewY, y * axis.fX - z * zenith.fX); 256d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMScaleY, y * axis.fY - z * zenith.fY); 257d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org orien->set(SkMatrix::kMTransY, y * axis.fZ - z * zenith.fZ); 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com orien->set(SkMatrix::kMPersp0, axis.fX); 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com orien->set(SkMatrix::kMPersp1, axis.fY); 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com orien->set(SkMatrix::kMPersp2, axis.fZ); 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 26491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const { 26591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com if (fNeedToUpdate) { 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->doUpdate(); 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fNeedToUpdate = false; 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation; 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const SkScalar* patchPtr; 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint3D diff; 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar dot; 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff.fX = quilt.fOrigin.fX - fLocation.fX; 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff.fY = quilt.fOrigin.fY - fLocation.fY; 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff.fZ = quilt.fOrigin.fZ - fLocation.fZ; 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 279373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&diff), 280373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com *SkTCast<const SkUnit3D*>(SkTCast<const SkScalar*>(&fOrientation) + 6)); 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2826f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // This multiplies fOrientation by the matrix [quilt.fU quilt.fV diff] -- U, V, and diff are 2836f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // column vectors in the matrix -- then divides by the length of the projection of diff onto 2846f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // the view axis (which is 'dot'). This transforms the patch (which transforms from local path 2856f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // space to world space) into view space (since fOrientation transforms from world space to 2866f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // view space). 2876f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // 2886f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // The divide by 'dot' isn't strictly necessary as the homogeneous divide would do much the 2896f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // same thing (it's just scaling the entire matrix by 1/dot). It looks like it's normalizing 2906f01f5aab2d28e8d48dd86d41e6364a1e1a61b77Jim Van Verth // the matrix into some canonical space. 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com patchPtr = (const SkScalar*)&quilt; 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMSkewY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot)); 2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot)); 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com patchPtr += 3; 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMSkewX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot)); 2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot)); 3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com patchPtr = (const SkScalar*)(const void*)&diff; 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot)); 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot)); 304d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org matrix->set(SkMatrix::kMPersp2, SK_Scalar1); 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 30791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com/////////////////////////////////////////////////////////////////////////////// 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 30991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSk3DView::Sk3DView() { 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fInitialRec.fMatrix.reset(); 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec = &fInitialRec; 3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 31491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSk3DView::~Sk3DView() { 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Rec* rec = fRec; 3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com while (rec != &fInitialRec) { 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Rec* next = rec->fNext; 318385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary delete rec; 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com rec = next; 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 32391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::save() { 324385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary Rec* rec = new Rec; 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com rec->fNext = fRec; 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com rec->fMatrix = fRec->fMatrix; 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec = rec; 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 33091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::restore() { 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(fRec != &fInitialRec); 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Rec* next = fRec->fNext; 333385fe4d4b62d7d1dd76116dd570df3290a2f487bhalcanary delete fRec; 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec = next; 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 33756c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID 33891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) { 339cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com // the camera location is passed in inches, set in pt 3404b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org SkScalar lz = z * 72.0f; 3414b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org fCamera.fLocation.set(x * 72.0f, y * 72.0f, lz); 342cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com fCamera.fObserver.set(0, 0, lz); 343cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com fCamera.update(); 344d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 345cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com} 346e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com 347e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationX() { 3484b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org return fCamera.fLocation.fX / 72.0f; 349e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com} 350e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com 351e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationY() { 3524b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org return fCamera.fLocation.fY / 72.0f; 353e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com} 354e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com 355e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationZ() { 3564b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org return fCamera.fLocation.fZ / 72.0f; 357e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com} 358cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif 359cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com 36091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) { 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec->fMatrix.preTranslate(x, y, z); 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 36491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateX(SkScalar deg) { 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec->fMatrix.preRotateX(deg); 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 36891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateY(SkScalar deg) { 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec->fMatrix.preRotateY(deg); 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 37291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateZ(SkScalar deg) { 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fRec->fMatrix.preRotateZ(deg); 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 37691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const { 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPatch3D patch; 3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com patch.transform(fRec->fMatrix); 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return patch.dotWith(x, y, z); 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 38291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::getMatrix(SkMatrix* matrix) const { 38396fcdcc219d2a0d3579719b84b28bede76efba64halcanary if (matrix != nullptr) { 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPatch3D patch; 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com patch.transform(fRec->fMatrix); 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fCamera.patchToMatrix(patch, matrix); 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h" 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 39291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::applyToCanvas(SkCanvas* canvas) const { 3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMatrix matrix; 394d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->getMatrix(&matrix); 3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->concat(matrix); 3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 398