blob: 26848c50d7885158c2239ad7978c1cb064be9e0a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008// Inspired by Rob Johnson's most excellent QuickDraw GX sample code
9
10#ifndef SkCamera_DEFINED
11#define SkCamera_DEFINED
12
Ben Wagnerd5148e32018-07-16 17:44:06 -040013#include "../private/SkNoncopyable.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkMatrix.h"
15
16class SkCanvas;
17
reed@android.com8a1c16f2008-12-17 15:59:43 +000018struct SkUnit3D {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000019 SkScalar fX, fY, fZ;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000021 void set(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 fX = x; fY = y; fZ = z;
23 }
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000024 static SkScalar Dot(const SkUnit3D&, const SkUnit3D&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross);
26};
27
28struct SkPoint3D {
29 SkScalar fX, fY, fZ;
30
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000031 void set(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 fX = x; fY = y; fZ = z;
33 }
34 SkScalar normalize(SkUnit3D*) const;
35};
36typedef SkPoint3D SkVector3D;
37
38struct SkMatrix3D {
39 SkScalar fMat[3][4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 void reset();
42
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000043 void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkASSERT((unsigned)row < 3);
45 fMat[row][0] = a;
46 fMat[row][1] = b;
47 fMat[row][2] = c;
48 fMat[row][3] = d;
49 }
50
51 void setRotateX(SkScalar deg);
52 void setRotateY(SkScalar deg);
53 void setRotateZ(SkScalar deg);
54 void setTranslate(SkScalar x, SkScalar y, SkScalar z);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 void preRotateX(SkScalar deg);
57 void preRotateY(SkScalar deg);
58 void preRotateZ(SkScalar deg);
59 void preTranslate(SkScalar x, SkScalar y, SkScalar z);
60
61 void setConcat(const SkMatrix3D& a, const SkMatrix3D& b);
62 void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const;
63 void mapVector(const SkVector3D& src, SkVector3D* dst) const;
64
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000065 void mapPoint(SkPoint3D* v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 this->mapPoint(*v, v);
67 }
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000068
69 void mapVector(SkVector3D* v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 this->mapVector(*v, v);
71 }
72};
73
74class SkPatch3D {
75public:
76 SkPatch3D();
77
78 void reset();
Ben Wagnera93a14a2017-08-28 10:34:05 -040079 void transform(const SkMatrix3D&, SkPatch3D* dst = nullptr) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
81 // dot a unit vector with the patch's normal
82 SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const;
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000083 SkScalar dotWith(const SkVector3D& v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 return this->dotWith(v.fX, v.fY, v.fZ);
85 }
86
robertphillips@google.comd4144062012-05-31 15:29:44 +000087 // deprecated, but still here for animator (for now)
djsollenc87dd2c2014-11-14 11:11:46 -080088 void rotate(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
89 void rotateDegrees(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
91private:
92public: // make public for SkDraw3D for now
93 SkVector3D fU, fV;
94 SkPoint3D fOrigin;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 friend class SkCamera3D;
97};
98
99class SkCamera3D {
100public:
101 SkCamera3D();
102
103 void reset();
104 void update();
105 void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const;
106
Jim Van Verthda965502017-04-11 15:29:14 -0400107 SkPoint3D fLocation; // origin of the camera's space
108 SkPoint3D fAxis; // view direction
109 SkPoint3D fZenith; // up direction
110 SkPoint3D fObserver; // eye position (may not be the same as the origin)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111
112private:
113 mutable SkMatrix fOrientation;
114 mutable bool fNeedToUpdate;
115
116 void doUpdate() const;
117};
118
Derek Sollenberger2fbf1bc2017-09-20 15:51:08 -0400119class SK_API Sk3DView : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120public:
121 Sk3DView();
122 ~Sk3DView();
123
124 void save();
125 void restore();
126
127 void translate(SkScalar x, SkScalar y, SkScalar z);
128 void rotateX(SkScalar deg);
129 void rotateY(SkScalar deg);
130 void rotateZ(SkScalar deg);
131
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500132#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000133 void setCameraLocation(SkScalar x, SkScalar y, SkScalar z);
Leon Scroggins IIIc41a5f52018-11-15 15:54:59 -0500134 SkScalar getCameraLocationX() const;
135 SkScalar getCameraLocationY() const;
136 SkScalar getCameraLocationZ() const;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000137#endif
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void getMatrix(SkMatrix*) const;
140 void applyToCanvas(SkCanvas*) const;
141
142 SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144private:
145 struct Rec {
146 Rec* fNext;
147 SkMatrix3D fMatrix;
148 };
149 Rec* fRec;
150 Rec fInitialRec;
151 SkCamera3D fCamera;
152};
153
154#endif