blob: 079cb68708a9d138608494e83a3ca4a76338d386 [file] [log] [blame]
jvanverthd17a3292015-07-09 09:04:16 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "Resources.h"
10
11#include "SkCanvas.h"
robertphillips5f865b92015-07-29 12:28:04 -070012#include "SkImageDecoder.h"
13#include "SkLightingShader.h"
jvanverthd17a3292015-07-09 09:04:16 -070014
15////////////////////////////////////////////////////////////////////////////
16
17class LightingView : public SampleView {
18public:
robertphillips5f865b92015-07-29 12:28:04 -070019 SkAutoTUnref<SkShader> fShader;
20 SkBitmap fDiffuseBitmap;
21 SkBitmap fNormalBitmap;
22 SkScalar fLightAngle;
23 int fColorFactor;
24 SkColor fAmbientColor;
jvanverthd17a3292015-07-09 09:04:16 -070025
26 LightingView() {
27 SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
28 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap);
29 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
30 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap);
31
32 fLightAngle = 0.0f;
33 fColorFactor = 0;
34
robertphillips5f865b92015-07-29 12:28:04 -070035 SkLightingShader::Light light;
jvanverthd17a3292015-07-09 09:04:16 -070036 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff);
37 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
38 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
39 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
40
robertphillips5f865b92015-07-29 12:28:04 -070041 fAmbientColor = SkColorSetRGB(0x1f, 0x1f, 0x1f);
jvanverthd17a3292015-07-09 09:04:16 -070042
robertphillips5f865b92015-07-29 12:28:04 -070043 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
44 light, fAmbientColor));
jvanverthd17a3292015-07-09 09:04:16 -070045 }
46
47 virtual ~LightingView() {}
48
49protected:
50 // overrides from SkEventSink
51 bool onQuery(SkEvent* evt) override {
52 if (SampleCode::TitleQ(*evt)) {
53 SampleCode::TitleR(evt, "Lighting");
54 return true;
55 }
56 return this->INHERITED::onQuery(evt);
57 }
58
59 void onDrawContent(SkCanvas* canvas) override {
60 fLightAngle += 0.015f;
61 fColorFactor++;
62
robertphillips5f865b92015-07-29 12:28:04 -070063 SkLightingShader::Light light;
jvanverthd17a3292015-07-09 09:04:16 -070064 light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff);
65 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
66 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
67 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
68
robertphillips5f865b92015-07-29 12:28:04 -070069 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
70 light, fAmbientColor));
jvanverthd17a3292015-07-09 09:04:16 -070071
72 SkPaint paint;
73 paint.setShader(fShader);
74 paint.setColor(SK_ColorBLACK);
75
76 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
77 (SkScalar)fDiffuseBitmap.height());
78 canvas->drawRect(r, paint);
79
80 // so we're constantly updating
81 this->inval(NULL);
82 }
83
84 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
85 this->inval(NULL);
86 return this->INHERITED::onFindClickHandler(x, y, modi);
87 }
88
89private:
90 typedef SampleView INHERITED;
91};
92
93//////////////////////////////////////////////////////////////////////////////
94
95static SkView* MyFactory() { return new LightingView; }
96static SkViewRegister reg(MyFactory);