senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 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 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 8 | #ifndef SkLightingImageFilter_DEFINED |
| 9 | #define SkLightingImageFilter_DEFINED |
| 10 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 11 | #include "SkImageFilter.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
| 13 | |
| 14 | class SK_API SkPoint3 { |
| 15 | public: |
| 16 | SkPoint3() {} |
| 17 | SkPoint3(SkScalar x, SkScalar y, SkScalar z) |
| 18 | : fX(x), fY(y), fZ(z) {} |
| 19 | SkScalar dot(const SkPoint3& other) const { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 20 | return fX * other.fX + fY * other.fY + fZ * other.fZ; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 21 | } |
| 22 | SkScalar maxComponent() const { |
| 23 | return fX > fY ? (fX > fZ ? fX : fZ) : (fY > fZ ? fY : fZ); |
| 24 | } |
| 25 | void normalize() { |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 26 | // Small epsilon is added to prevent division by 0. |
| 27 | SkScalar scale = SkScalarInvert(SkScalarSqrt(dot(*this)) + SK_ScalarNearlyZero); |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 28 | fX = fX * scale; |
| 29 | fY = fY * scale; |
| 30 | fZ = fZ * scale; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 31 | } |
| 32 | SkPoint3 operator*(SkScalar scalar) const { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 33 | return SkPoint3(fX * scalar, fY * scalar, fZ * scalar); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 34 | } |
| 35 | SkPoint3 operator-(const SkPoint3& other) const { |
| 36 | return SkPoint3(fX - other.fX, fY - other.fY, fZ - other.fZ); |
| 37 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 38 | bool operator==(const SkPoint3& other) const { |
| 39 | return fX == other.fX && fY == other.fY && fZ == other.fZ; |
| 40 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 41 | SkScalar fX, fY, fZ; |
| 42 | }; |
| 43 | |
| 44 | class SkLight; |
| 45 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 46 | class SK_API SkLightingImageFilter : public SkImageFilter { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 47 | public: |
| 48 | static SkImageFilter* CreateDistantLitDiffuse(const SkPoint3& direction, |
senorblanco@chromium.org | 254eae2 | 2012-10-05 17:38:00 +0000 | [diff] [blame] | 49 | SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 50 | SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 51 | static SkImageFilter* CreatePointLitDiffuse(const SkPoint3& location, |
senorblanco@chromium.org | 254eae2 | 2012-10-05 17:38:00 +0000 | [diff] [blame] | 52 | SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 53 | SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 54 | static SkImageFilter* CreateSpotLitDiffuse(const SkPoint3& location, |
| 55 | const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, |
senorblanco@chromium.org | 254eae2 | 2012-10-05 17:38:00 +0000 | [diff] [blame] | 56 | SkColor lightColor, SkScalar surfaceScale, SkScalar kd, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 57 | SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 58 | static SkImageFilter* CreateDistantLitSpecular(const SkPoint3& direction, |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 59 | SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 60 | SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 61 | static SkImageFilter* CreatePointLitSpecular(const SkPoint3& location, |
| 62 | SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 63 | SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 64 | static SkImageFilter* CreateSpotLitSpecular(const SkPoint3& location, |
| 65 | const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 66 | SkColor lightColor, SkScalar surfaceScale, SkScalar ks, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 67 | SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 68 | ~SkLightingImageFilter(); |
| 69 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 70 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 71 | |
| 72 | protected: |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 73 | SkLightingImageFilter(SkLight* light, |
| 74 | SkScalar surfaceScale, |
| 75 | SkImageFilter* input, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 76 | const CropRect* cropRect, |
| 77 | uint32_t uniqueID); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 78 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 79 | explicit SkLightingImageFilter(SkReadBuffer& buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 80 | #endif |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 81 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 82 | const SkLight* light() const { return fLight.get(); } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 83 | SkScalar surfaceScale() const { return fSurfaceScale; } |
| 84 | |
| 85 | private: |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 86 | typedef SkImageFilter INHERITED; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 87 | SkAutoTUnref<SkLight> fLight; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 88 | SkScalar fSurfaceScale; |
| 89 | }; |
| 90 | |
| 91 | #endif |