aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/effects/SkLightingImageFilter.h
diff options
context:
space:
mode:
authorGravatar senorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-22 21:01:23 +0000
committerGravatar senorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-22 21:01:23 +0000
commitf49b429ceface4f75f5f96570ea5a8b94896529d (patch)
tree15ec6b7d0f904ba949098f286c1588209e267e09 /include/effects/SkLightingImageFilter.h
parent7d6afdd795eb4c7ce8f5a327117cfdba5f957ddb (diff)
Raster implementation of diffuse and specular lighting filters. Externally,
the caller instantiates a light (distant, point or spot), and an SkDiffuseLightingFilter or SkSpecularLightingImageFilter with that light. A Sobel edge detection filter is applied to the alpha of the incoming bitmap, and the result is used as a height map for lighting calculations. Review URL: http://codereview.appspot.com/6302101/ git-svn-id: http://skia.googlecode.com/svn/trunk@4314 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/effects/SkLightingImageFilter.h')
-rw-r--r--include/effects/SkLightingImageFilter.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/include/effects/SkLightingImageFilter.h b/include/effects/SkLightingImageFilter.h
new file mode 100644
index 0000000000..f8a5ccf4af
--- /dev/null
+++ b/include/effects/SkLightingImageFilter.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2012 The Android Open Source Project
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#ifndef SkLightingImageFilter_DEFINED
+#define SkLightingImageFilter_DEFINED
+
+#include "SkImageFilter.h"
+#include "SkColor.h"
+
+class SK_API SkPoint3 {
+public:
+ SkPoint3() {}
+ SkPoint3(SkScalar x, SkScalar y, SkScalar z)
+ : fX(x), fY(y), fZ(z) {}
+ SkScalar dot(const SkPoint3& other) const {
+ return SkScalarMul(fX, other.fX)
+ + SkScalarMul(fY, other.fY)
+ + SkScalarMul(fZ, other.fZ);
+ }
+ SkScalar maxComponent() const {
+ return fX > fY ? (fX > fZ ? fX : fZ) : (fY > fZ ? fY : fZ);
+ }
+ void normalize() {
+ SkScalar scale = SkScalarInvert(SkScalarSqrt(dot(*this)));
+ fX = SkScalarMul(fX, scale);
+ fY = SkScalarMul(fY, scale);
+ fZ = SkScalarMul(fZ, scale);
+ }
+ SkPoint3 operator*(SkScalar scalar) const {
+ return SkPoint3(SkScalarMul(fX, scalar),
+ SkScalarMul(fY, scalar),
+ SkScalarMul(fZ, scalar));
+ }
+ SkPoint3 operator-(const SkPoint3& other) const {
+ return SkPoint3(fX - other.fX, fY - other.fY, fZ - other.fZ);
+ }
+ SkScalar fX, fY, fZ;
+};
+
+class SkLight;
+
+class SK_API SkLightingImageFilter : public SkImageFilter {
+public:
+ static SkImageFilter* CreateDistantLitDiffuse(const SkPoint3& direction,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar kd);
+ static SkImageFilter* CreatePointLitDiffuse(SkPoint3& location,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar kd);
+ static SkImageFilter* CreateSpotLitDiffuse(const SkPoint3& location,
+ const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar kd);
+ static SkImageFilter* CreateDistantLitSpecular(const SkPoint3& direction,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar ks,
+ SkScalar shininess);
+ static SkImageFilter* CreatePointLitSpecular(SkPoint3& location,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar ks,
+ SkScalar shininess);
+ static SkImageFilter* CreateSpotLitSpecular(const SkPoint3& location,
+ const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle,
+ const SkColor& lightColor, SkScalar surfaceScale, SkScalar ks,
+ SkScalar shininess);
+ ~SkLightingImageFilter();
+
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingImageFilter)
+
+protected:
+ SkLightingImageFilter(SkLight* light, const SkColor& lightColor,
+ SkScalar surfaceScale);
+ explicit SkLightingImageFilter(SkFlattenableReadBuffer& buffer);
+ virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
+ const SkLight* light() const { return fLight; }
+ const SkPoint3& lightColor() const { return fLightColor; }
+ SkScalar surfaceScale() const { return fSurfaceScale; }
+
+private:
+ typedef SkImageFilter INHERITED;
+ SkLight* fLight;
+ SkPoint3 fLightColor;
+ SkScalar fSurfaceScale;
+};
+
+#endif
+