blob: ad74c81d2f7b3f96ede79f0d21091effed355fe1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
9
10
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@android.com8a1c16f2008-12-17 15:59:43 +000012// 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
20class SkCanvas;
21
reed@google.com8f4d2302013-12-17 16:44:46 +000022typedef float SkUnitScalar;
23#define SK_UnitScalar1 SK_Scalar1
24#define SkUnitScalarMul(a, b) SkScalarMul(a, b)
25#define SkUnitScalarDiv(a, b) SkScalarDiv(a, b)
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
27struct SkUnit3D {
28 SkUnitScalar fX, fY, fZ;
29
30 void set(SkUnitScalar x, SkUnitScalar y, SkUnitScalar z)
31 {
32 fX = x; fY = y; fZ = z;
33 }
34 static SkUnitScalar Dot(const SkUnit3D&, const SkUnit3D&);
35 static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross);
36};
37
38struct SkPoint3D {
39 SkScalar fX, fY, fZ;
40
41 void set(SkScalar x, SkScalar y, SkScalar z)
42 {
43 fX = x; fY = y; fZ = z;
44 }
45 SkScalar normalize(SkUnit3D*) const;
46};
47typedef SkPoint3D SkVector3D;
48
49struct SkMatrix3D {
50 SkScalar fMat[3][4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 void reset();
53
54 void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0)
55 {
56 SkASSERT((unsigned)row < 3);
57 fMat[row][0] = a;
58 fMat[row][1] = b;
59 fMat[row][2] = c;
60 fMat[row][3] = d;
61 }
62
63 void setRotateX(SkScalar deg);
64 void setRotateY(SkScalar deg);
65 void setRotateZ(SkScalar deg);
66 void setTranslate(SkScalar x, SkScalar y, SkScalar z);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 void preRotateX(SkScalar deg);
69 void preRotateY(SkScalar deg);
70 void preRotateZ(SkScalar deg);
71 void preTranslate(SkScalar x, SkScalar y, SkScalar z);
72
73 void setConcat(const SkMatrix3D& a, const SkMatrix3D& b);
74 void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const;
75 void mapVector(const SkVector3D& src, SkVector3D* dst) const;
76
77 void mapPoint(SkPoint3D* v) const
78 {
79 this->mapPoint(*v, v);
80 }
81 void mapVector(SkVector3D* v) const
82 {
83 this->mapVector(*v, v);
84 }
85};
86
87class SkPatch3D {
88public:
89 SkPatch3D();
90
91 void reset();
92 void transform(const SkMatrix3D&, SkPatch3D* dst = NULL) const;
93
94 // dot a unit vector with the patch's normal
95 SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const;
96 SkScalar dotWith(const SkVector3D& v) const
97 {
98 return this->dotWith(v.fX, v.fY, v.fZ);
99 }
100
robertphillips@google.comd4144062012-05-31 15:29:44 +0000101 // deprecated, but still here for animator (for now)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 void rotate(SkScalar x, SkScalar y, SkScalar z) {}
103 void rotateDegrees(SkScalar x, SkScalar y, SkScalar z) {}
104
105private:
106public: // make public for SkDraw3D for now
107 SkVector3D fU, fV;
108 SkPoint3D fOrigin;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 friend class SkCamera3D;
111};
112
113class SkCamera3D {
114public:
115 SkCamera3D();
116
117 void reset();
118 void update();
119 void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const;
120
121 SkPoint3D fLocation;
122 SkPoint3D fAxis;
123 SkPoint3D fZenith;
124 SkPoint3D fObserver;
125
126private:
127 mutable SkMatrix fOrientation;
128 mutable bool fNeedToUpdate;
129
130 void doUpdate() const;
131};
132
133class Sk3DView : SkNoncopyable {
134public:
135 Sk3DView();
136 ~Sk3DView();
137
138 void save();
139 void restore();
140
141 void translate(SkScalar x, SkScalar y, SkScalar z);
142 void rotateX(SkScalar deg);
143 void rotateY(SkScalar deg);
144 void rotateZ(SkScalar deg);
145
djsollen@google.com56c69772011-11-08 19:00:26 +0000146#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000147 void setCameraLocation(SkScalar x, SkScalar y, SkScalar z);
djsollen@google.come63793a2012-03-21 15:39:03 +0000148 SkScalar getCameraLocationX();
149 SkScalar getCameraLocationY();
150 SkScalar getCameraLocationZ();
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000151#endif
152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 void getMatrix(SkMatrix*) const;
154 void applyToCanvas(SkCanvas*) const;
155
156 SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158private:
159 struct Rec {
160 Rec* fNext;
161 SkMatrix3D fMatrix;
162 };
163 Rec* fRec;
164 Rec fInitialRec;
165 SkCamera3D fCamera;
166};
167
168#endif