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 {
7791408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com    if (dst == NULL) {
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 {
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar cx = SkScalarMul(fU.fY, fV.fZ) - SkScalarMul(fU.fZ, fV.fY);
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar cy = SkScalarMul(fU.fZ, fV.fX) - SkScalarMul(fU.fX, fV.fY);
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar cz = SkScalarMul(fU.fX, fV.fY) - SkScalarMul(fU.fY, fV.fX);
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarMul(cx, dx) + SkScalarMul(cy, dy) + SkScalarMul(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
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fAxis.normalize(&axis);
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
220373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com        SkScalar dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&fZenith), axis);
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
222d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        zenith.fX = fZenith.fX - dot * axis.fX;
223d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        zenith.fY = fZenith.fY - dot * axis.fY;
224d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        zenith.fZ = fZenith.fZ - dot * axis.fZ;
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
226373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com        SkTCast<SkPoint3D*>(&zenith)->normalize(&zenith);
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkUnit3D::Cross(axis, zenith, &cross);
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMatrix* orien = &fOrientation;
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar x = fObserver.fX;
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar y = fObserver.fY;
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar z = fObserver.fZ;
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
237d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMScaleX, x * axis.fX - z * cross.fX);
238d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMSkewX,  x * axis.fY - z * cross.fY);
239d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMTransX, x * axis.fZ - z * cross.fZ);
240d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMSkewY,  y * axis.fX - z * zenith.fX);
241d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMScaleY, y * axis.fY - z * zenith.fY);
242d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org        orien->set(SkMatrix::kMTransY, y * axis.fZ - z * zenith.fZ);
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        orien->set(SkMatrix::kMPersp0, axis.fX);
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        orien->set(SkMatrix::kMPersp1, axis.fY);
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        orien->set(SkMatrix::kMPersp2, axis.fZ);
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
25091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com    if (fNeedToUpdate) {
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->doUpdate();
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fNeedToUpdate = false;
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkScalar* patchPtr;
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint3D       diff;
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        dot;
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    diff.fX = quilt.fOrigin.fX - fLocation.fX;
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    diff.fY = quilt.fOrigin.fY - fLocation.fY;
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    diff.fZ = quilt.fOrigin.fZ - fLocation.fZ;
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
264373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com    dot = SkUnit3D::Dot(*SkTCast<const SkUnit3D*>(&diff),
265373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com                        *SkTCast<const SkUnit3D*>(SkTCast<const SkScalar*>(&fOrientation) + 6));
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    patchPtr = (const SkScalar*)&quilt;
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMSkewY,  SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    patchPtr += 3;
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMSkewX,  SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    patchPtr = (const SkScalar*)(const void*)&diff;
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
280d173b8760887f9b10cd43cc99ad6b8011e269370mike@reedtribe.org    matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
28391408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com///////////////////////////////////////////////////////////////////////////////
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
28591408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSk3DView::Sk3DView() {
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fInitialRec.fMatrix.reset();
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec = &fInitialRec;
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
29091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSk3DView::~Sk3DView() {
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec* rec = fRec;
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (rec != &fInitialRec) {
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Rec* next = rec->fNext;
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(rec);
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rec = next;
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
29991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::save() {
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec* rec = SkNEW(Rec);
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    rec->fNext = fRec;
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    rec->fMatrix = fRec->fMatrix;
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec = rec;
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
30691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::restore() {
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fRec != &fInitialRec);
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec* next = fRec->fNext;
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDELETE(fRec);
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec = next;
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
31356c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
31491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
315cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // the camera location is passed in inches, set in pt
3164b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    SkScalar lz = z * 72.0f;
3174b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    fCamera.fLocation.set(x * 72.0f, y * 72.0f, lz);
318cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    fCamera.fObserver.set(0, 0, lz);
319cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    fCamera.update();
320d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
321cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com}
322e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com
323e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationX() {
3244b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    return fCamera.fLocation.fX / 72.0f;
325e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com}
326e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com
327e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationY() {
3284b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    return fCamera.fLocation.fY / 72.0f;
329e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com}
330e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com
331e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.comSkScalar Sk3DView::getCameraLocationZ() {
3324b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    return fCamera.fLocation.fZ / 72.0f;
333e63793a2c8d2871bf7d95195be7b93ff669688d7djsollen@google.com}
334cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
335cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
33691408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) {
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec->fMatrix.preTranslate(x, y, z);
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
34091408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateX(SkScalar deg) {
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec->fMatrix.preRotateX(deg);
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
34491408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateY(SkScalar deg) {
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec->fMatrix.preRotateY(deg);
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
34891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::rotateZ(SkScalar deg) {
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fRec->fMatrix.preRotateZ(deg);
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
35291408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comSkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const {
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPatch3D   patch;
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    patch.transform(fRec->fMatrix);
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return patch.dotWith(x, y, z);
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
35891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::getMatrix(SkMatrix* matrix) const {
35991408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.com    if (matrix != NULL) {
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPatch3D   patch;
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        patch.transform(fRec->fMatrix);
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCamera.patchToMatrix(patch, matrix);
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
36891408cc296eb6d65ea14c34fb3d559b1c1f9aac9reed@google.comvoid Sk3DView::applyToCanvas(SkCanvas* canvas) const {
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
370d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->getMatrix(&matrix);
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    canvas->concat(matrix);
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
374