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