1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/utils/SkCamera.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* 1.5 + * Copyright 2006 The Android Open Source Project 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +// Inspired by Rob Johnson's most excellent QuickDraw GX sample code 1.12 + 1.13 +#ifndef SkCamera_DEFINED 1.14 +#define SkCamera_DEFINED 1.15 + 1.16 +#include "SkMatrix.h" 1.17 + 1.18 +class SkCanvas; 1.19 + 1.20 +struct SkUnit3D { 1.21 + SkScalar fX, fY, fZ; 1.22 + 1.23 + void set(SkScalar x, SkScalar y, SkScalar z) { 1.24 + fX = x; fY = y; fZ = z; 1.25 + } 1.26 + static SkScalar Dot(const SkUnit3D&, const SkUnit3D&); 1.27 + static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross); 1.28 +}; 1.29 + 1.30 +struct SkPoint3D { 1.31 + SkScalar fX, fY, fZ; 1.32 + 1.33 + void set(SkScalar x, SkScalar y, SkScalar z) { 1.34 + fX = x; fY = y; fZ = z; 1.35 + } 1.36 + SkScalar normalize(SkUnit3D*) const; 1.37 +}; 1.38 +typedef SkPoint3D SkVector3D; 1.39 + 1.40 +struct SkMatrix3D { 1.41 + SkScalar fMat[3][4]; 1.42 + 1.43 + void reset(); 1.44 + 1.45 + void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) { 1.46 + SkASSERT((unsigned)row < 3); 1.47 + fMat[row][0] = a; 1.48 + fMat[row][1] = b; 1.49 + fMat[row][2] = c; 1.50 + fMat[row][3] = d; 1.51 + } 1.52 + 1.53 + void setRotateX(SkScalar deg); 1.54 + void setRotateY(SkScalar deg); 1.55 + void setRotateZ(SkScalar deg); 1.56 + void setTranslate(SkScalar x, SkScalar y, SkScalar z); 1.57 + 1.58 + void preRotateX(SkScalar deg); 1.59 + void preRotateY(SkScalar deg); 1.60 + void preRotateZ(SkScalar deg); 1.61 + void preTranslate(SkScalar x, SkScalar y, SkScalar z); 1.62 + 1.63 + void setConcat(const SkMatrix3D& a, const SkMatrix3D& b); 1.64 + void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const; 1.65 + void mapVector(const SkVector3D& src, SkVector3D* dst) const; 1.66 + 1.67 + void mapPoint(SkPoint3D* v) const { 1.68 + this->mapPoint(*v, v); 1.69 + } 1.70 + 1.71 + void mapVector(SkVector3D* v) const { 1.72 + this->mapVector(*v, v); 1.73 + } 1.74 +}; 1.75 + 1.76 +class SkPatch3D { 1.77 +public: 1.78 + SkPatch3D(); 1.79 + 1.80 + void reset(); 1.81 + void transform(const SkMatrix3D&, SkPatch3D* dst = NULL) const; 1.82 + 1.83 + // dot a unit vector with the patch's normal 1.84 + SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const; 1.85 + SkScalar dotWith(const SkVector3D& v) const { 1.86 + return this->dotWith(v.fX, v.fY, v.fZ); 1.87 + } 1.88 + 1.89 + // deprecated, but still here for animator (for now) 1.90 + void rotate(SkScalar x, SkScalar y, SkScalar z) {} 1.91 + void rotateDegrees(SkScalar x, SkScalar y, SkScalar z) {} 1.92 + 1.93 +private: 1.94 +public: // make public for SkDraw3D for now 1.95 + SkVector3D fU, fV; 1.96 + SkPoint3D fOrigin; 1.97 + 1.98 + friend class SkCamera3D; 1.99 +}; 1.100 + 1.101 +class SkCamera3D { 1.102 +public: 1.103 + SkCamera3D(); 1.104 + 1.105 + void reset(); 1.106 + void update(); 1.107 + void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const; 1.108 + 1.109 + SkPoint3D fLocation; 1.110 + SkPoint3D fAxis; 1.111 + SkPoint3D fZenith; 1.112 + SkPoint3D fObserver; 1.113 + 1.114 +private: 1.115 + mutable SkMatrix fOrientation; 1.116 + mutable bool fNeedToUpdate; 1.117 + 1.118 + void doUpdate() const; 1.119 +}; 1.120 + 1.121 +class Sk3DView : SkNoncopyable { 1.122 +public: 1.123 + Sk3DView(); 1.124 + ~Sk3DView(); 1.125 + 1.126 + void save(); 1.127 + void restore(); 1.128 + 1.129 + void translate(SkScalar x, SkScalar y, SkScalar z); 1.130 + void rotateX(SkScalar deg); 1.131 + void rotateY(SkScalar deg); 1.132 + void rotateZ(SkScalar deg); 1.133 + 1.134 +#ifdef SK_BUILD_FOR_ANDROID 1.135 + void setCameraLocation(SkScalar x, SkScalar y, SkScalar z); 1.136 + SkScalar getCameraLocationX(); 1.137 + SkScalar getCameraLocationY(); 1.138 + SkScalar getCameraLocationZ(); 1.139 +#endif 1.140 + 1.141 + void getMatrix(SkMatrix*) const; 1.142 + void applyToCanvas(SkCanvas*) const; 1.143 + 1.144 + SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const; 1.145 + 1.146 +private: 1.147 + struct Rec { 1.148 + Rec* fNext; 1.149 + SkMatrix3D fMatrix; 1.150 + }; 1.151 + Rec* fRec; 1.152 + Rec fInitialRec; 1.153 + SkCamera3D fCamera; 1.154 +}; 1.155 + 1.156 +#endif