2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Inspired by Rob Johnson's most excellent QuickDraw GX sample code
|
|
|
|
|
|
|
|
#ifndef SkCamera_DEFINED
|
|
|
|
#define SkCamera_DEFINED
|
|
|
|
|
|
|
|
#include "SkMatrix.h"
|
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
|
|
|
|
struct SkUnit3D {
|
2014-01-23 02:02:45 +00:00
|
|
|
SkScalar fX, fY, fZ;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-01-23 02:02:45 +00:00
|
|
|
void set(SkScalar x, SkScalar y, SkScalar z) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fX = x; fY = y; fZ = z;
|
|
|
|
}
|
2014-01-23 02:02:45 +00:00
|
|
|
static SkScalar Dot(const SkUnit3D&, const SkUnit3D&);
|
2008-12-17 15:59:43 +00:00
|
|
|
static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SkPoint3D {
|
|
|
|
SkScalar fX, fY, fZ;
|
|
|
|
|
2014-01-23 02:02:45 +00:00
|
|
|
void set(SkScalar x, SkScalar y, SkScalar z) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fX = x; fY = y; fZ = z;
|
|
|
|
}
|
|
|
|
SkScalar normalize(SkUnit3D*) const;
|
|
|
|
};
|
|
|
|
typedef SkPoint3D SkVector3D;
|
|
|
|
|
|
|
|
struct SkMatrix3D {
|
|
|
|
SkScalar fMat[3][4];
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void reset();
|
|
|
|
|
2014-01-23 02:02:45 +00:00
|
|
|
void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT((unsigned)row < 3);
|
|
|
|
fMat[row][0] = a;
|
|
|
|
fMat[row][1] = b;
|
|
|
|
fMat[row][2] = c;
|
|
|
|
fMat[row][3] = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRotateX(SkScalar deg);
|
|
|
|
void setRotateY(SkScalar deg);
|
|
|
|
void setRotateZ(SkScalar deg);
|
|
|
|
void setTranslate(SkScalar x, SkScalar y, SkScalar z);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void preRotateX(SkScalar deg);
|
|
|
|
void preRotateY(SkScalar deg);
|
|
|
|
void preRotateZ(SkScalar deg);
|
|
|
|
void preTranslate(SkScalar x, SkScalar y, SkScalar z);
|
|
|
|
|
|
|
|
void setConcat(const SkMatrix3D& a, const SkMatrix3D& b);
|
|
|
|
void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const;
|
|
|
|
void mapVector(const SkVector3D& src, SkVector3D* dst) const;
|
|
|
|
|
2014-01-23 02:02:45 +00:00
|
|
|
void mapPoint(SkPoint3D* v) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->mapPoint(*v, v);
|
|
|
|
}
|
2014-01-23 02:02:45 +00:00
|
|
|
|
|
|
|
void mapVector(SkVector3D* v) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->mapVector(*v, v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkPatch3D {
|
|
|
|
public:
|
|
|
|
SkPatch3D();
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
void transform(const SkMatrix3D&, SkPatch3D* dst = NULL) const;
|
|
|
|
|
|
|
|
// dot a unit vector with the patch's normal
|
|
|
|
SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const;
|
2014-01-23 02:02:45 +00:00
|
|
|
SkScalar dotWith(const SkVector3D& v) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->dotWith(v.fX, v.fY, v.fZ);
|
|
|
|
}
|
|
|
|
|
2012-05-31 15:29:44 +00:00
|
|
|
// deprecated, but still here for animator (for now)
|
2014-11-14 19:11:46 +00:00
|
|
|
void rotate(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
|
|
|
|
void rotateDegrees(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
public: // make public for SkDraw3D for now
|
|
|
|
SkVector3D fU, fV;
|
|
|
|
SkPoint3D fOrigin;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
friend class SkCamera3D;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkCamera3D {
|
|
|
|
public:
|
|
|
|
SkCamera3D();
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
void update();
|
|
|
|
void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const;
|
|
|
|
|
|
|
|
SkPoint3D fLocation;
|
|
|
|
SkPoint3D fAxis;
|
|
|
|
SkPoint3D fZenith;
|
|
|
|
SkPoint3D fObserver;
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable SkMatrix fOrientation;
|
|
|
|
mutable bool fNeedToUpdate;
|
|
|
|
|
|
|
|
void doUpdate() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Sk3DView : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
Sk3DView();
|
|
|
|
~Sk3DView();
|
|
|
|
|
|
|
|
void save();
|
|
|
|
void restore();
|
|
|
|
|
|
|
|
void translate(SkScalar x, SkScalar y, SkScalar z);
|
|
|
|
void rotateX(SkScalar deg);
|
|
|
|
void rotateY(SkScalar deg);
|
|
|
|
void rotateZ(SkScalar deg);
|
|
|
|
|
2011-11-08 19:00:26 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
2011-03-14 20:30:14 +00:00
|
|
|
void setCameraLocation(SkScalar x, SkScalar y, SkScalar z);
|
2012-03-21 15:39:03 +00:00
|
|
|
SkScalar getCameraLocationX();
|
|
|
|
SkScalar getCameraLocationY();
|
|
|
|
SkScalar getCameraLocationZ();
|
2011-03-14 20:30:14 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void getMatrix(SkMatrix*) const;
|
|
|
|
void applyToCanvas(SkCanvas*) const;
|
|
|
|
|
|
|
|
SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
struct Rec {
|
|
|
|
Rec* fNext;
|
|
|
|
SkMatrix3D fMatrix;
|
|
|
|
};
|
|
|
|
Rec* fRec;
|
|
|
|
Rec fInitialRec;
|
|
|
|
SkCamera3D fCamera;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|