1
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"
12 #include "SkImageDecoder.h"
13 #include "SkLightingShader.h"
14 #include "SkPoint3.h"
15
create_lights(SkScalar angle,SkScalar blue)16 static const SkLightingShader::Lights* create_lights(SkScalar angle, SkScalar blue) {
17
18 const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
19 SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
20 SkScalarCos(SK_ScalarPI*0.25f));
21
22 SkLightingShader::Lights::Builder builder;
23
24 builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, blue), dir));
25 builder.add(SkLight(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
26
27 return builder.finish();
28 }
29
30 ////////////////////////////////////////////////////////////////////////////
31
32 class LightingView : public SampleView {
33 public:
34 SkAutoTUnref<SkShader> fShader;
35 SkBitmap fDiffuseBitmap;
36 SkBitmap fNormalBitmap;
37 SkScalar fLightAngle;
38 SkScalar fColorFactor;
39
LightingView()40 LightingView() {
41 SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
42 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap);
43 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
44 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap);
45
46 fLightAngle = 0.0f;
47 fColorFactor = 0.0f;
48
49 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle, 1.0f));
50
51 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
52 lights, SkVector::Make(1.0f, 0.0f),
53 nullptr, nullptr));
54 }
55
~LightingView()56 virtual ~LightingView() {}
57
58 protected:
59 // overrides from SkEventSink
onQuery(SkEvent * evt)60 bool onQuery(SkEvent* evt) override {
61 if (SampleCode::TitleQ(*evt)) {
62 SampleCode::TitleR(evt, "Lighting");
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
67
onDrawContent(SkCanvas * canvas)68 void onDrawContent(SkCanvas* canvas) override {
69 fLightAngle += 0.015f;
70 fColorFactor += 0.01f;
71 if (fColorFactor > 1.0f) {
72 fColorFactor = 0.0f;
73 }
74
75 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle,
76 fColorFactor));
77
78 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
79 lights, SkVector::Make(1.0f, 0.0f),
80 nullptr, nullptr));
81
82 SkPaint paint;
83 paint.setShader(fShader);
84 paint.setColor(SK_ColorBLACK);
85
86 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
87 (SkScalar)fDiffuseBitmap.height());
88 canvas->drawRect(r, paint);
89
90 // so we're constantly updating
91 this->inval(nullptr);
92 }
93
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)94 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
95 this->inval(nullptr);
96 return this->INHERITED::onFindClickHandler(x, y, modi);
97 }
98
99 private:
100 typedef SampleView INHERITED;
101 };
102
103 //////////////////////////////////////////////////////////////////////////////
104
MyFactory()105 static SkView* MyFactory() { return new LightingView; }
106 static SkViewRegister reg(MyFactory);
107