aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleShadowing.cpp
blob: 3907db30cc1a5ea6ac26fd9af7769adb1de02bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SampleCode.h"
#include "SkPictureRecorder.h"
#include "SkShadowPaintFilterCanvas.h"
#include "SkShadowShader.h"
#include "SkSurface.h"

#ifdef SK_EXPERIMENTAL_SHADOWING

static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth,
                                          sk_sp<SkImage> diffuse,
                                          sk_sp<SkLights> lights) {

    sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileMode,
                                                          SkShader::kClamp_TileMode);

    sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMode,
                                                        SkShader::kClamp_TileMode);

    return SkShadowShader::Make(std::move(povDepthShader),
                                std::move(diffuseShader),
                                std::move(lights),
                                diffuse->width(), diffuse->height());
}

class ShadowingView : public SampleView {
public:
    ShadowingView() {

        this->setBGColor(0xFFCCCCCC);
        SkLights::Builder builder;
        builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f),
                                    SkVector3::Make(0.27f, 0.07f, 1.0f)));
        builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f),
                                    SkVector3::Make(0.03f, 0.27f, 1.0f)));
        builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f)));
        fLights = builder.finish();

        fTestRects[0].fColor = 0xFFEE8888;
        fTestRects[0].fDepth = 80;
        fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300);

        fTestRects[1].fColor = 0xFF88EE88;
        fTestRects[1].fDepth = 160;
        fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350);

        fTestRects[2].fColor = 0xFF8888EE;
        fTestRects[2].fDepth = 240;
        fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250);

        fSceneChanged = true;
        fLightsChanged = true;

        fSelectedRect = -1;
        fMoveLight = false;
    }

protected:
    bool onQuery(SkEvent *evt) override {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "shadowing");
            return true;
        }

        SkUnichar uni;
        if (SampleCode::CharQ(*evt, &uni)) {
            switch (uni) {
                case 'L':
                    fMoveLight = !fMoveLight;
                    break;
                default:
                    break;
            }
        }
        return this->INHERITED::onQuery(evt);
    }

    sk_sp<SkPicture> makeTestPicture(int width, int height) {
        SkPictureRecorder recorder;

        // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory)
        SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height));

        SkASSERT(canvas->getTotalMatrix().isIdentity());
        SkPaint paint;
        paint.setColor(SK_ColorGRAY);

        // LONG RANGE TODO: tag occluders
        // LONG RANGE TODO: track number of IDs we need (hopefully less than 256)
        //                  and determinate the mapping from z to id

        // universal receiver, "ground"
        canvas->drawRect(SkRect::MakeIWH(width, height), paint);

        for (int i = 0; i < kNumTestRects; i++) {
            paint.setColor(fTestRects[i].fColor);
            if (i == 0) {
                canvas->translateZ(fTestRects[0].fDepth);
            } else {
                canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth);
            }
            canvas->drawRect(fTestRects[i].fGeometry, paint);
        }

        return recorder.finishRecordingAsPicture();
    }

    void updateDepthMaps(SkCanvas *canvas) {
        for (int i = 0; i < fLights->numLights(); ++i) {
            // skip over ambient lights; they don't cast shadows
            if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) {
                continue;
            }

            // TODO: maybe add a kDepth_8_SkColorType when vertices have depth
            // assume max depth is 255.
            // TODO: find actual max depth of picture
            SkISize shMapSize = SkShadowPaintFilterCanvas::
                                ComputeDepthMapSize(fLights->light(i), 255, kWidth, kHeight);

            SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight,
                                                 kBGRA_8888_SkColorType,
                                                 kOpaque_SkAlphaType);

            // Create a new surface (that matches the backend of canvas)
            // for each shadow map
            sk_sp<SkSurface> surf(canvas->makeSurface(info));

            // Wrap another SPFCanvas around the surface
            sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
                    sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());

            // set the depth map canvas to have the light we're drawing.
            SkLights::Builder builder;
            builder.add(fLights->light(i));
            sk_sp<SkLights> curLight = builder.finish();

            depthMapCanvas->setLights(std::move(curLight));
            depthMapCanvas->drawPicture(fPicture);

            fLights->light(i).setShadowMap(surf->makeImageSnapshot());
        }
    }

    void updatePovDepthMap(SkCanvas* canvas) {
        // TODO: pass the depth to the shader in vertices, or uniforms
        //       so we don't have to render depth and color separately

        SkLights::Builder builder;
        builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
                                    SkVector3::Make(0.0f, 0.0f, 1.0f)));
        sk_sp<SkLights> povLight = builder.finish();

        SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
                                             kBGRA_8888_SkColorType,
                                             kOpaque_SkAlphaType);

        // Create a new surface (that matches the backend of canvas)
        // to create the povDepthMap
        sk_sp<SkSurface> surf(canvas->makeSurface(info));

        // Wrap another SPFCanvas around the surface
        sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
                sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());

        // set the depth map canvas to have the light as the user's POV
        depthMapCanvas->setLights(std::move(povLight));

        depthMapCanvas->drawPicture(fPicture);

        fPovDepthMap = surf->makeImageSnapshot();
    }

    void updateDiffuseMap(SkCanvas* canvas) {
        SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
                                             kBGRA_8888_SkColorType,
                                             kOpaque_SkAlphaType);

        sk_sp<SkSurface> surf(canvas->makeSurface(info));
        surf->getCanvas()->drawPicture(fPicture);

        fDiffuseMap = surf->makeImageSnapshot();
    }

    void onDrawContent(SkCanvas *canvas) override {
        if (fSceneChanged || !fPovDepthMap) {
            fPicture = this->makeTestPicture(kWidth, kHeight);
            this->updateDepthMaps(canvas);
            this->updatePovDepthMap(canvas);
            this->updateDiffuseMap(canvas);
        }

        if (fLightsChanged) {
            this->updateDepthMaps(canvas);
        }

        if (fSceneChanged || fLightsChanged || !fShadowShader) {
            fShadowShader = make_shadow_shader(fPovDepthMap, fDiffuseMap, fLights);
        }

        SkPaint paint;
        paint.setShader(fShadowShader);

        canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
    }

    SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
        return new SkView::Click(this);
    }

    bool onClick(Click *click) override {
        SkScalar x = click->fCurr.fX;
        SkScalar y = click->fCurr.fY;

        SkScalar dx = x - click->fPrev.fX;
        SkScalar dy = y - click->fPrev.fY;

        if (fMoveLight) {
            if (dx != 0 || dy != 0) {
                float recipX = 1.0f / kWidth;
                float recipY = 1.0f / kHeight;

                SkLights::Builder builder;
                builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f),
                                            SkVector3::Make(0.12f + (200.0f - x) * recipX,
                                                            -0.08f + (200.0f - y) * recipY,
                                                            1.0f)));
                builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f),
                                            SkVector3::Make(-0.12f + (200.0f - x) * recipX,
                                                            0.12f + (200.0f - y) * recipY,
                                                            1.0f)));
                builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f)));
                fLights = builder.finish();

                fLightsChanged = true;
                this->inval(nullptr);
            }
            return true;
        }

        if (click->fState == Click::State::kUp_State) {
            fSelectedRect = -1;
            return true;
        }

        if (fSelectedRect > -1) {
            fTestRects[fSelectedRect].fGeometry.offset(dx, dy);

            fSceneChanged = true;
            this->inval(nullptr);
            return true;
        }

        // assume last elements are highest
        for (int i = kNumTestRects - 1; i >= 0; i--) {
            if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
                fSelectedRect = i;
                fTestRects[i].fGeometry.offset(dx, dy);

                fSceneChanged = true;
                this->inval(nullptr);
                break;
            }
        }

        return true;
    }

private:
    static constexpr int kNumTestRects = 3;

    static const int kWidth = 400;
    static const int kHeight = 400;

    struct {
        SkRect  fGeometry;
        int     fDepth;
        SkColor fColor;
    } fTestRects[kNumTestRects];

    int fSelectedRect;
    bool fMoveLight;

    sk_sp<SkImage>   fPovDepthMap;
    sk_sp<SkImage>   fDiffuseMap;
    sk_sp<SkPicture> fPicture;
    sk_sp<SkShader>  fShadowShader;

    bool fSceneChanged;
    bool fLightsChanged;

    sk_sp<SkLights> fLights;

    typedef SampleView INHERITED;
};

//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new ShadowingView; }
static SkViewRegister reg(MyFactory);

#endif