senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 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 | #include "gm.h" |
| 9 | #include "SkLightingImageFilter.h" |
| 10 | |
| 11 | #define WIDTH 330 |
| 12 | #define HEIGHT 220 |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class ImageLightingGM : public GM { |
| 17 | public: |
| 18 | ImageLightingGM() : fInitialized(false) { |
| 19 | this->setBGColor(0xFF000000); |
| 20 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 21 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 22 | protected: |
| 23 | virtual SkString onShortName() { |
| 24 | return SkString("lighting"); |
| 25 | } |
| 26 | |
| 27 | void make_bitmap() { |
| 28 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 29 | fBitmap.allocPixels(); |
| 30 | SkDevice device(fBitmap); |
| 31 | SkCanvas canvas(&device); |
| 32 | canvas.clear(0x00000000); |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
| 35 | paint.setColor(0xFFFFFFFF); |
| 36 | paint.setTextSize(SkIntToScalar(96)); |
| 37 | const char* str = "e"; |
| 38 | canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
| 39 | } |
| 40 | |
| 41 | virtual SkISize onISize() { |
| 42 | return make_isize(WIDTH, HEIGHT); |
| 43 | } |
| 44 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 45 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 46 | canvas->save(); |
| 47 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 48 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 49 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 50 | canvas->restore(); |
| 51 | } |
| 52 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 53 | virtual void onDraw(SkCanvas* canvas) { |
| 54 | if (!fInitialized) { |
| 55 | make_bitmap(); |
| 56 | fInitialized = true; |
| 57 | } |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 58 | canvas->clear(0xFF101010); |
| 59 | SkPaint checkPaint; |
| 60 | checkPaint.setColor(0xFF202020); |
| 61 | for (int y = 0; y < HEIGHT; y += 16) { |
| 62 | for (int x = 0; x < WIDTH; x += 16) { |
| 63 | canvas->save(); |
| 64 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 65 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 66 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 67 | canvas->restore(); |
| 68 | } |
| 69 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 70 | SkPoint3 pointLocation(0, 0, SkIntToScalar(10)); |
| 71 | SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); |
| 72 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
| 73 | SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)), |
| 74 | SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)), |
| 75 | SkScalarSin(elevationRad)); |
| 76 | SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20)); |
| 77 | SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 78 | SkScalar spotExponent = SK_Scalar1; |
| 79 | SkScalar cutoffAngle = SkIntToScalar(15); |
| 80 | SkScalar kd = SkIntToScalar(2); |
| 81 | SkScalar ks = SkIntToScalar(1); |
| 82 | SkScalar shininess = SkIntToScalar(8); |
| 83 | SkScalar surfaceScale = SkIntToScalar(1); |
| 84 | SkColor white(0xFFFFFFFF); |
| 85 | SkPaint paint; |
| 86 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 87 | drawClippedBitmap(canvas, paint, 0, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 88 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 89 | drawClippedBitmap(canvas, paint, 110, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 90 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 91 | drawClippedBitmap(canvas, paint, 220, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 92 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 93 | drawClippedBitmap(canvas, paint, 0, 110); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 94 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 95 | drawClippedBitmap(canvas, paint, 110, 110); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 96 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 97 | drawClippedBitmap(canvas, paint, 220, 110); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 100 | private: |
| 101 | typedef GM INHERITED; |
| 102 | SkBitmap fBitmap; |
| 103 | bool fInitialized; |
| 104 | }; |
| 105 | |
| 106 | ////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | static GM* MyFactory(void*) { return new ImageLightingGM; } |
| 109 | static GMRegistry reg(MyFactory); |
| 110 | |
| 111 | } |