1 /* 2 * Copyright 2012 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 8 9 #ifndef SkLightingImageFilter_DEFINED 10 #define SkLightingImageFilter_DEFINED 11 12 #include "SkSingleInputImageFilter.h" 13 #include "SkColor.h" 14 15 class SK_API SkPoint3 { 16 public: 17 SkPoint3() {} 18 SkPoint3(SkScalar x, SkScalar y, SkScalar z) 19 : fX(x), fY(y), fZ(z) {} 20 SkScalar dot(const SkPoint3& other) const { 21 return SkScalarMul(fX, other.fX) 22 + SkScalarMul(fY, other.fY) 23 + SkScalarMul(fZ, other.fZ); 24 } 25 SkScalar maxComponent() const { 26 return fX > fY ? (fX > fZ ? fX : fZ) : (fY > fZ ? fY : fZ); 27 } 28 void normalize() { 29 SkScalar scale = SkScalarInvert(SkScalarSqrt(dot(*this))); 30 fX = SkScalarMul(fX, scale); 31 fY = SkScalarMul(fY, scale); 32 fZ = SkScalarMul(fZ, scale); 33 } 34 SkPoint3 operator*(SkScalar scalar) const { 35 return SkPoint3(SkScalarMul(fX, scalar), 36 SkScalarMul(fY, scalar), 37 SkScalarMul(fZ, scalar)); 38 } 39 SkPoint3 operator-(const SkPoint3& other) const { 40 return SkPoint3(fX - other.fX, fY - other.fY, fZ - other.fZ); 41 } 42 bool operator==(const SkPoint3& other) const { 43 return fX == other.fX && fY == other.fY && fZ == other.fZ; 44 } 45 SkScalar fX, fY, fZ; 46 }; 47 48 class SkLight; 49 50 class SK_API SkLightingImageFilter : public SkSingleInputImageFilter { 51 public: 52 static SkImageFilter* CreateDistantLitDiffuse(const SkPoint3& direction, 53 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 54 SkImageFilter* input = NULL); 55 static SkImageFilter* CreatePointLitDiffuse(const SkPoint3& location, 56 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 57 SkImageFilter* input = NULL); 58 static SkImageFilter* CreateSpotLitDiffuse(const SkPoint3& location, 59 const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, 60 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 61 SkImageFilter* input = NULL); 62 static SkImageFilter* CreateDistantLitSpecular(const SkPoint3& direction, 63 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 64 SkScalar shininess, SkImageFilter* input = NULL); 65 static SkImageFilter* CreatePointLitSpecular(const SkPoint3& location, 66 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 67 SkScalar shininess, SkImageFilter* input = NULL); 68 static SkImageFilter* CreateSpotLitSpecular(const SkPoint3& location, 69 const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, 70 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 71 SkScalar shininess, SkImageFilter* input = NULL); 72 ~SkLightingImageFilter(); 73 74 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 75 76 protected: 77 SkLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkImageFilter* input); 78 explicit SkLightingImageFilter(SkFlattenableReadBuffer& buffer); 79 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; 80 const SkLight* light() const { return fLight; } 81 SkScalar surfaceScale() const { return fSurfaceScale; } 82 83 private: 84 typedef SkSingleInputImageFilter INHERITED; 85 SkLight* fLight; 86 SkScalar fSurfaceScale; 87 }; 88 89 #endif 90