blob: 4838cf818388842b72e044049ee5d648da358032 [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"
robertphillips2f0dbc72015-08-20 05:15:06 -070014#include "SkPoint3.h"
15
16static 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}
jvanverthd17a3292015-07-09 09:04:16 -070029
30////////////////////////////////////////////////////////////////////////////
31
32class LightingView : public SampleView {
33public:
robertphillips5f865b92015-07-29 12:28:04 -070034 SkAutoTUnref<SkShader> fShader;
35 SkBitmap fDiffuseBitmap;
36 SkBitmap fNormalBitmap;
37 SkScalar fLightAngle;
robertphillipsa9652ac2015-07-31 05:17:24 -070038 SkScalar fColorFactor;
jvanverthd17a3292015-07-09 09:04:16 -070039
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;
robertphillipsa9652ac2015-07-31 05:17:24 -070047 fColorFactor = 0.0f;
jvanverthd17a3292015-07-09 09:04:16 -070048
robertphillips2f0dbc72015-08-20 05:15:06 -070049 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle, 1.0f));
jvanverthd17a3292015-07-09 09:04:16 -070050
robertphillips5f865b92015-07-29 12:28:04 -070051 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
robertphillips2f0dbc72015-08-20 05:15:06 -070052 lights, SkVector::Make(1.0f, 0.0f),
53 nullptr, nullptr));
jvanverthd17a3292015-07-09 09:04:16 -070054 }
55
56 virtual ~LightingView() {}
57
58protected:
59 // overrides from SkEventSink
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
68 void onDrawContent(SkCanvas* canvas) override {
69 fLightAngle += 0.015f;
robertphillipsa9652ac2015-07-31 05:17:24 -070070 fColorFactor += 0.01f;
71 if (fColorFactor > 1.0f) {
72 fColorFactor = 0.0f;
73 }
jvanverthd17a3292015-07-09 09:04:16 -070074
robertphillips2f0dbc72015-08-20 05:15:06 -070075 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle,
76 fColorFactor));
jvanverthd17a3292015-07-09 09:04:16 -070077
robertphillips5f865b92015-07-29 12:28:04 -070078 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
robertphillips2f0dbc72015-08-20 05:15:06 -070079 lights, SkVector::Make(1.0f, 0.0f),
80 nullptr, nullptr));
jvanverthd17a3292015-07-09 09:04:16 -070081
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(NULL);
92 }
93
94 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
95 this->inval(NULL);
96 return this->INHERITED::onFindClickHandler(x, y, modi);
97 }
98
99private:
100 typedef SampleView INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
105static SkView* MyFactory() { return new LightingView; }
106static SkViewRegister reg(MyFactory);