blob: 5949f4961bc52267b235814f0680d11ce7d925a8 [file] [log] [blame]
jvanverthd17a3292015-07-09 09:04:16 -07001/*
2 * Copyright 2015 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 */
msarettd15750c2016-03-18 15:48:49 -07007#include "DecodeFile.h"
jvanverthd17a3292015-07-09 09:04:16 -07008#include "SampleCode.h"
9#include "Resources.h"
10
dvonbeck5b794fa2016-07-06 13:58:36 -070011#include "SkBitmapProcShader.h"
jvanverthd17a3292015-07-09 09:04:16 -070012#include "SkCanvas.h"
robertphillips5f865b92015-07-29 12:28:04 -070013#include "SkLightingShader.h"
dvonbeck5b794fa2016-07-06 13:58:36 -070014#include "SkNormalSource.h"
robertphillips2f0dbc72015-08-20 05:15:06 -070015#include "SkPoint3.h"
16
robertphillips71e05522016-05-31 12:08:25 -070017static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
robertphillips2f0dbc72015-08-20 05:15:06 -070018
19 const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
20 SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
21 SkScalarCos(SK_ScalarPI*0.25f));
22
robertphillips71e05522016-05-31 12:08:25 -070023 SkLights::Builder builder;
robertphillips2f0dbc72015-08-20 05:15:06 -070024
robertphillips71e05522016-05-31 12:08:25 -070025 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, blue), dir));
26 builder.add(SkLights::Light(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
robertphillips2f0dbc72015-08-20 05:15:06 -070027
28 return builder.finish();
29}
jvanverthd17a3292015-07-09 09:04:16 -070030
31////////////////////////////////////////////////////////////////////////////
32
33class LightingView : public SampleView {
34public:
reedfe630452016-03-25 09:08:00 -070035 SkBitmap fDiffuseBitmap;
36 SkBitmap fNormalBitmap;
37 SkScalar fLightAngle;
38 SkScalar fColorFactor;
jvanverthd17a3292015-07-09 09:04:16 -070039
40 LightingView() {
41 SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
msarettd15750c2016-03-18 15:48:49 -070042 decode_file(diffusePath.c_str(), &fDiffuseBitmap);
jvanverthd17a3292015-07-09 09:04:16 -070043 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
msarettd15750c2016-03-18 15:48:49 -070044 decode_file(normalPath.c_str(), &fNormalBitmap);
jvanverthd17a3292015-07-09 09:04:16 -070045
46 fLightAngle = 0.0f;
robertphillipsa9652ac2015-07-31 05:17:24 -070047 fColorFactor = 0.0f;
jvanverthd17a3292015-07-09 09:04:16 -070048 }
49
jvanverthd17a3292015-07-09 09:04:16 -070050protected:
51 // overrides from SkEventSink
52 bool onQuery(SkEvent* evt) override {
53 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "Lighting");
55 return true;
56 }
57 return this->INHERITED::onQuery(evt);
58 }
59
60 void onDrawContent(SkCanvas* canvas) override {
61 fLightAngle += 0.015f;
robertphillipsa9652ac2015-07-31 05:17:24 -070062 fColorFactor += 0.01f;
63 if (fColorFactor > 1.0f) {
64 fColorFactor = 0.0f;
65 }
jvanverthd17a3292015-07-09 09:04:16 -070066
robertphillips71e05522016-05-31 12:08:25 -070067 sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
reedfe630452016-03-25 09:08:00 -070068 SkPaint paint;
dvonbeck5b794fa2016-07-06 13:58:36 -070069 sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalBitmap,
70 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr, nullptr);
71 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(
72 std::move(normalMap), SkMatrix::I());
73 paint.setShader(SkLightingShader::Make(fDiffuseBitmap, std::move(lights), nullptr,
74 std::move(normalSource)));
jvanverthd17a3292015-07-09 09:04:16 -070075 paint.setColor(SK_ColorBLACK);
76
halcanary9d524f22016-03-29 09:03:52 -070077 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
jvanverthd17a3292015-07-09 09:04:16 -070078 (SkScalar)fDiffuseBitmap.height());
79 canvas->drawRect(r, paint);
80
81 // so we're constantly updating
halcanary96fcdcc2015-08-27 07:41:13 -070082 this->inval(nullptr);
jvanverthd17a3292015-07-09 09:04:16 -070083 }
84
85 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -070086 this->inval(nullptr);
jvanverthd17a3292015-07-09 09:04:16 -070087 return this->INHERITED::onFindClickHandler(x, y, modi);
88 }
89
90private:
91 typedef SampleView INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new LightingView; }
97static SkViewRegister reg(MyFactory);