SkCamera.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. // Inspired by Rob Johnson's most excellent QuickDraw GX sample code
  8. #ifndef SkCamera_DEFINED
  9. #define SkCamera_DEFINED
  10. #include "include/core/SkMatrix.h"
  11. #include "include/private/SkNoncopyable.h"
  12. class SkCanvas;
  13. struct SkUnit3D {
  14. SkScalar fX, fY, fZ;
  15. void set(SkScalar x, SkScalar y, SkScalar z) {
  16. fX = x; fY = y; fZ = z;
  17. }
  18. static SkScalar Dot(const SkUnit3D&, const SkUnit3D&);
  19. static void Cross(const SkUnit3D&, const SkUnit3D&, SkUnit3D* cross);
  20. };
  21. struct SkPoint3D {
  22. SkScalar fX, fY, fZ;
  23. void set(SkScalar x, SkScalar y, SkScalar z) {
  24. fX = x; fY = y; fZ = z;
  25. }
  26. SkScalar normalize(SkUnit3D*) const;
  27. };
  28. typedef SkPoint3D SkVector3D;
  29. struct SkMatrix3D {
  30. SkScalar fMat[3][4];
  31. void reset();
  32. void setRow(int row, SkScalar a, SkScalar b, SkScalar c, SkScalar d = 0) {
  33. SkASSERT((unsigned)row < 3);
  34. fMat[row][0] = a;
  35. fMat[row][1] = b;
  36. fMat[row][2] = c;
  37. fMat[row][3] = d;
  38. }
  39. void setRotateX(SkScalar deg);
  40. void setRotateY(SkScalar deg);
  41. void setRotateZ(SkScalar deg);
  42. void setTranslate(SkScalar x, SkScalar y, SkScalar z);
  43. void preRotateX(SkScalar deg);
  44. void preRotateY(SkScalar deg);
  45. void preRotateZ(SkScalar deg);
  46. void preTranslate(SkScalar x, SkScalar y, SkScalar z);
  47. void setConcat(const SkMatrix3D& a, const SkMatrix3D& b);
  48. void mapPoint(const SkPoint3D& src, SkPoint3D* dst) const;
  49. void mapVector(const SkVector3D& src, SkVector3D* dst) const;
  50. void mapPoint(SkPoint3D* v) const {
  51. this->mapPoint(*v, v);
  52. }
  53. void mapVector(SkVector3D* v) const {
  54. this->mapVector(*v, v);
  55. }
  56. };
  57. class SkPatch3D {
  58. public:
  59. SkPatch3D();
  60. void reset();
  61. void transform(const SkMatrix3D&, SkPatch3D* dst = nullptr) const;
  62. // dot a unit vector with the patch's normal
  63. SkScalar dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const;
  64. SkScalar dotWith(const SkVector3D& v) const {
  65. return this->dotWith(v.fX, v.fY, v.fZ);
  66. }
  67. // deprecated, but still here for animator (for now)
  68. void rotate(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
  69. void rotateDegrees(SkScalar /*x*/, SkScalar /*y*/, SkScalar /*z*/) {}
  70. private:
  71. public: // make public for SkDraw3D for now
  72. SkVector3D fU, fV;
  73. SkPoint3D fOrigin;
  74. friend class SkCamera3D;
  75. };
  76. class SkCamera3D {
  77. public:
  78. SkCamera3D();
  79. void reset();
  80. void update();
  81. void patchToMatrix(const SkPatch3D&, SkMatrix* matrix) const;
  82. SkPoint3D fLocation; // origin of the camera's space
  83. SkPoint3D fAxis; // view direction
  84. SkPoint3D fZenith; // up direction
  85. SkPoint3D fObserver; // eye position (may not be the same as the origin)
  86. private:
  87. mutable SkMatrix fOrientation;
  88. mutable bool fNeedToUpdate;
  89. void doUpdate() const;
  90. };
  91. class SK_API Sk3DView : SkNoncopyable {
  92. public:
  93. Sk3DView();
  94. ~Sk3DView();
  95. void save();
  96. void restore();
  97. void translate(SkScalar x, SkScalar y, SkScalar z);
  98. void rotateX(SkScalar deg);
  99. void rotateY(SkScalar deg);
  100. void rotateZ(SkScalar deg);
  101. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  102. void setCameraLocation(SkScalar x, SkScalar y, SkScalar z);
  103. SkScalar getCameraLocationX() const;
  104. SkScalar getCameraLocationY() const;
  105. SkScalar getCameraLocationZ() const;
  106. #endif
  107. void getMatrix(SkMatrix*) const;
  108. void applyToCanvas(SkCanvas*) const;
  109. SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const;
  110. private:
  111. struct Rec {
  112. Rec* fNext;
  113. SkMatrix3D fMatrix;
  114. };
  115. Rec* fRec;
  116. Rec fInitialRec;
  117. SkCamera3D fCamera;
  118. };
  119. #endif