epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| 10 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 11 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | // Inspired by Rob Johnson's most excellent QuickDraw GX sample code |
| 13 | |
| 14 | #ifndef SkCamera_DEFINED |
| 15 | #define SkCamera_DEFINED |
| 16 | |
| 17 | #include "Sk64.h" |
| 18 | #include "SkMatrix.h" |
| 19 | |
| 20 | class SkCanvas; |
| 21 | |
| 22 | #ifdef SK_SCALAR_IS_FIXED |
| 23 | typedef SkFract SkUnitScalar; |
| 24 | #define SK_UnitScalar1 SK_Fract1 |
| 25 | #define SkUnitScalarMul(a, b) SkFractMul(a, b) |
| 26 | #define SkUnitScalarDiv(a, b) SkFractDiv(a, b) |
| 27 | #else |
| 28 | typedef float SkUnitScalar; |
| 29 | #define SK_UnitScalar1 SK_Scalar1 |
| 30 | #define SkUnitScalarMul(a, b) SkScalarMul(a, b) |
| 31 | #define SkUnitScalarDiv(a, b) SkScalarDiv(a, b) |
| 32 | #endif |
| 33 | |
| 34 | struct SkUnit3D { |
| 35 | SkUnitScalar fX, fY, fZ; |
| 36 | |
| 37 | void set(SkUnitScalar x, SkUnitScalar y, SkUnitScalar z) |
| 38 | { |
| 39 | fX = x; fY = y; fZ = z; |
| 40 | } |
| 41 | static SkUnitScalar Dot(const SkUnit3D&, const SkUnit3D&); |
| 42 | static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross); |
| 43 | }; |
| 44 | |
| 45 | struct SkPoint3D { |
| 46 | SkScalar fX, fY, fZ; |
| 47 | |
| 48 | void set(SkScalar x, SkScalar y, SkScalar z) |
| 49 | { |
| 50 | fX = x; fY = y; fZ = z; |
| 51 | } |
| 52 | SkScalar normalize(SkUnit3D*) const; |
| 53 | }; |
| 54 | typedef SkPoint3D SkVector3D; |
| 55 | |
| 56 | struct SkMatrix3D { |
| 57 | SkScalar fMat[3][4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | void reset(); |
| 60 | |
| 61 | void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) |
| 62 | { |
| 63 | SkASSERT((unsigned)row < 3); |
| 64 | fMat[row][0] = a; |
| 65 | fMat[row][1] = b; |
| 66 | fMat[row][2] = c; |
| 67 | fMat[row][3] = d; |
| 68 | } |
| 69 | |
| 70 | void setRotateX(SkScalar deg); |
| 71 | void setRotateY(SkScalar deg); |
| 72 | void setRotateZ(SkScalar deg); |
| 73 | void setTranslate(SkScalar x, SkScalar y, SkScalar z); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | void preRotateX(SkScalar deg); |
| 76 | void preRotateY(SkScalar deg); |
| 77 | void preRotateZ(SkScalar deg); |
| 78 | void preTranslate(SkScalar x, SkScalar y, SkScalar z); |
| 79 | |
| 80 | void setConcat(const SkMatrix3D& a, const SkMatrix3D& b); |
| 81 | void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const; |
| 82 | void mapVector(const SkVector3D& src, SkVector3D* dst) const; |
| 83 | |
| 84 | void mapPoint(SkPoint3D* v) const |
| 85 | { |
| 86 | this->mapPoint(*v, v); |
| 87 | } |
| 88 | void mapVector(SkVector3D* v) const |
| 89 | { |
| 90 | this->mapVector(*v, v); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class SkPatch3D { |
| 95 | public: |
| 96 | SkPatch3D(); |
| 97 | |
| 98 | void reset(); |
| 99 | void transform(const SkMatrix3D&, SkPatch3D* dst = NULL) const; |
| 100 | |
| 101 | // dot a unit vector with the patch's normal |
| 102 | SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const; |
| 103 | SkScalar dotWith(const SkVector3D& v) const |
| 104 | { |
| 105 | return this->dotWith(v.fX, v.fY, v.fZ); |
| 106 | } |
| 107 | |
robertphillips@google.com | d414406 | 2012-05-31 15:29:44 +0000 | [diff] [blame] | 108 | // deprecated, but still here for animator (for now) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | void rotate(SkScalar x, SkScalar y, SkScalar z) {} |
| 110 | void rotateDegrees(SkScalar x, SkScalar y, SkScalar z) {} |
| 111 | |
| 112 | private: |
| 113 | public: // make public for SkDraw3D for now |
| 114 | SkVector3D fU, fV; |
| 115 | SkPoint3D fOrigin; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | friend class SkCamera3D; |
| 118 | }; |
| 119 | |
| 120 | class SkCamera3D { |
| 121 | public: |
| 122 | SkCamera3D(); |
| 123 | |
| 124 | void reset(); |
| 125 | void update(); |
| 126 | void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const; |
| 127 | |
| 128 | SkPoint3D fLocation; |
| 129 | SkPoint3D fAxis; |
| 130 | SkPoint3D fZenith; |
| 131 | SkPoint3D fObserver; |
| 132 | |
| 133 | private: |
| 134 | mutable SkMatrix fOrientation; |
| 135 | mutable bool fNeedToUpdate; |
| 136 | |
| 137 | void doUpdate() const; |
| 138 | }; |
| 139 | |
| 140 | class Sk3DView : SkNoncopyable { |
| 141 | public: |
| 142 | Sk3DView(); |
| 143 | ~Sk3DView(); |
| 144 | |
| 145 | void save(); |
| 146 | void restore(); |
| 147 | |
| 148 | void translate(SkScalar x, SkScalar y, SkScalar z); |
| 149 | void rotateX(SkScalar deg); |
| 150 | void rotateY(SkScalar deg); |
| 151 | void rotateZ(SkScalar deg); |
| 152 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 153 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 154 | void setCameraLocation(SkScalar x, SkScalar y, SkScalar z); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 155 | SkScalar getCameraLocationX(); |
| 156 | SkScalar getCameraLocationY(); |
| 157 | SkScalar getCameraLocationZ(); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | void getMatrix(SkMatrix*) const; |
| 161 | void applyToCanvas(SkCanvas*) const; |
| 162 | |
| 163 | SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 164 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | private: |
| 166 | struct Rec { |
| 167 | Rec* fNext; |
| 168 | SkMatrix3D fMatrix; |
| 169 | }; |
| 170 | Rec* fRec; |
| 171 | Rec fInitialRec; |
| 172 | SkCamera3D fCamera; |
| 173 | }; |
| 174 | |
| 175 | #endif |