aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/lighting.cpp
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 /gm/lighting.cpp
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 'gm/lighting.cpp')
-rw-r--r--gm/lighting.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/gm/lighting.cpp b/gm/lighting.cpp
new file mode 100644
index 0000000000..2b41528a38
--- /dev/null
+++ b/gm/lighting.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SkLightingImageFilter.h"
+
+#define WIDTH 330
+#define HEIGHT 220
+
+namespace skiagm {
+
+class ImageLightingGM : public GM {
+public:
+ ImageLightingGM() : fInitialized(false) {
+ this->setBGColor(0xFF000000);
+ }
+
+protected:
+ virtual SkString onShortName() {
+ return SkString("lighting");
+ }
+
+ void make_bitmap() {
+ fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
+ fBitmap.allocPixels();
+ SkDevice device(fBitmap);
+ SkCanvas canvas(&device);
+ canvas.clear(0x00000000);
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor(0xFFFFFFFF);
+ paint.setTextSize(SkIntToScalar(96));
+ const char* str = "e";
+ canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
+ }
+
+ virtual SkISize onISize() {
+ return make_isize(WIDTH, HEIGHT);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ if (!fInitialized) {
+ make_bitmap();
+ fInitialized = true;
+ }
+ SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
+ SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
+ SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
+ SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
+ SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
+ SkScalarSin(elevationRad));
+ SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
+ SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
+ SkScalar spotExponent = SK_Scalar1;
+ SkScalar cutoffAngle = SkIntToScalar(15);
+ SkScalar kd = SkIntToScalar(2);
+ SkScalar ks = SkIntToScalar(1);
+ SkScalar shininess = SkIntToScalar(8);
+ SkScalar surfaceScale = SkIntToScalar(1);
+ SkColor white(0xFFFFFFFF);
+ SkPaint paint;
+ paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd))->unref();
+ canvas->drawSprite(fBitmap, 0, 0, &paint);
+ paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd))->unref();
+ canvas->drawSprite(fBitmap, 110, 0, &paint);
+ paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd))->unref();
+ canvas->drawSprite(fBitmap, 220, 0, &paint);
+ paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess))->unref();
+ canvas->drawSprite(fBitmap, 0, 110, &paint);
+ paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess))->unref();
+ canvas->drawSprite(fBitmap, 110, 110, &paint);
+ paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess))->unref();
+ canvas->drawSprite(fBitmap, 220, 110, &paint);
+ }
+
+private:
+ typedef GM INHERITED;
+ SkBitmap fBitmap;
+ bool fInitialized;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new ImageLightingGM; }
+static GMRegistry reg(MyFactory);
+
+}