blob: 4b77ec685d38a18e6f32e0c3e300399676ee8f10 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkMatrix.h"
14
15class SkCanvas;
16
reed@android.com8a1c16f2008-12-17 15:59:43 +000017struct SkUnit3D {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000018 SkScalar fX, fY, fZ;
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000020 void set(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 fX = x; fY = y; fZ = z;
22 }
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000023 static SkScalar Dot(const SkUnit3D&, const SkUnit3D&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross);
25};
26
27struct SkPoint3D {
28 SkScalar fX, fY, fZ;
29
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000030 void set(SkScalar x, SkScalar y, SkScalar z) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 fX = x; fY = y; fZ = z;
32 }
33 SkScalar normalize(SkUnit3D*) const;
34};
35typedef SkPoint3D SkVector3D;
36
37struct SkMatrix3D {
38 SkScalar fMat[3][4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 void reset();
41
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000042 void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 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.comfbfcd562012-08-23 18:09:54 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 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.orgd173b872014-01-23 02:02:45 +000064 void mapPoint(SkPoint3D* v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 this->mapPoint(*v, v);
66 }
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000067
68 void mapVector(SkVector3D* v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 this->mapVector(*v, v);
70 }
71};
72
73class SkPatch3D {
74public:
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.orgd173b872014-01-23 02:02:45 +000082 SkScalar dotWith(const SkVector3D& v) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 return this->dotWith(v.fX, v.fY, v.fZ);
84 }
85
robertphillips@google.comd4144062012-05-31 15:29:44 +000086 // deprecated, but still here for animator (for now)
djsollenc87dd2c2014-11-14 11:11:46 -080087 void rotate(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
88 void rotateDegrees(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90private:
91public: // make public for SkDraw3D for now
92 SkVector3D fU, fV;
93 SkPoint3D fOrigin;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 friend class SkCamera3D;
96};
97
98class SkCamera3D {
99public:
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
111private:
112 mutable SkMatrix fOrientation;
113 mutable bool fNeedToUpdate;
114
115 void doUpdate() const;
116};
117
118class Sk3DView : SkNoncopyable {
119public:
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.com56c69772011-11-08 19:00:26 +0000131#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000132 void setCameraLocation(SkScalar x, SkScalar y, SkScalar z);
djsollen@google.come63793a2012-03-21 15:39:03 +0000133 SkScalar getCameraLocationX();
134 SkScalar getCameraLocationY();
135 SkScalar getCameraLocationZ();
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000136#endif
137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 void getMatrix(SkMatrix*) const;
139 void applyToCanvas(SkCanvas*) const;
140
141 SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143private:
144 struct Rec {
145 Rec* fNext;
146 SkMatrix3D fMatrix;
147 };
148 Rec* fRec;
149 Rec fInitialRec;
150 SkCamera3D fCamera;
151};
152
153#endif