lighting.cpp revision c3bd8af6d5722e854feca70c40d92f4954c5b67b
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 440
13
14namespace skiagm {
15
16class ImageLightingGM : public GM {
17public:
18    ImageLightingGM() : fInitialized(false) {
19        this->setBGColor(0xFF000000);
20    }
21
22protected:
23    virtual SkString onShortName() {
24        return SkString("lighting");
25    }
26
27    void make_bitmap() {
28        fBitmap.allocN32Pixels(100, 100);
29        SkCanvas canvas(fBitmap);
30        canvas.clear(0x00000000);
31        SkPaint paint;
32        paint.setAntiAlias(true);
33        paint.setColor(0xFFFFFFFF);
34        paint.setTextSize(SkIntToScalar(96));
35        const char* str = "e";
36        canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
37    }
38
39    virtual SkISize onISize() {
40        return SkISize::Make(WIDTH, HEIGHT);
41    }
42
43    void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
44        canvas->save();
45        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
46        canvas->clipRect(SkRect::MakeWH(
47          SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
48        canvas->drawBitmap(fBitmap, 0, 0, &paint);
49        canvas->restore();
50    }
51
52    virtual void onDraw(SkCanvas* canvas) {
53        if (!fInitialized) {
54            make_bitmap();
55            fInitialized = true;
56        }
57        canvas->clear(0xFF101010);
58        SkPaint checkPaint;
59        checkPaint.setColor(0xFF202020);
60        for (int y = 0; y < HEIGHT; y += 16) {
61          for (int x = 0; x < WIDTH; x += 16) {
62            canvas->save();
63            canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
64            canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
65            canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
66            canvas->restore();
67          }
68        }
69        SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
70        SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
71        SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
72        SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
73                                  SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
74                                  SkScalarSin(elevationRad));
75        SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
76        SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
77        SkScalar spotExponent = SK_Scalar1;
78        SkScalar cutoffAngle = SkIntToScalar(15);
79        SkScalar kd = SkIntToScalar(2);
80        SkScalar ks = SkIntToScalar(1);
81        SkScalar shininess = SkIntToScalar(8);
82        SkScalar surfaceScale = SkIntToScalar(1);
83        SkColor white(0xFFFFFFFF);
84        SkPaint paint;
85
86        SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
87
88        int y = 0;
89        for (int i = 0; i < 2; i++) {
90            const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect;
91            paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref();
92            drawClippedBitmap(canvas, paint, 0, y);
93
94            paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref();
95            drawClippedBitmap(canvas, paint, 110, y);
96
97            paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref();
98            drawClippedBitmap(canvas, paint, 220, y);
99
100            y += 110;
101
102            paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref();
103            drawClippedBitmap(canvas, paint, 0, y);
104
105            paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref();
106            drawClippedBitmap(canvas, paint, 110, y);
107
108            paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref();
109            drawClippedBitmap(canvas, paint, 220, y);
110
111            y += 110;
112        }
113    }
114
115private:
116    typedef GM INHERITED;
117    SkBitmap fBitmap;
118    bool fInitialized;
119};
120
121//////////////////////////////////////////////////////////////////////////////
122
123static GM* MyFactory(void*) { return new ImageLightingGM; }
124static GMRegistry reg(MyFactory);
125
126}
127