blob: 2218f31022322d79c29c3063d5864a213f5fb6c1 [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 */
reed1ec04d92016-08-05 12:07:41 -07007
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
jvanverthd17a3292015-07-09 09:04:16 -07009#include "SampleCode.h"
10#include "Resources.h"
jvanverthd17a3292015-07-09 09:04:16 -070011#include "SkCanvas.h"
robertphillips5f865b92015-07-29 12:28:04 -070012#include "SkLightingShader.h"
dvonbeck5b794fa2016-07-06 13:58:36 -070013#include "SkNormalSource.h"
robertphillips2f0dbc72015-08-20 05:15:06 -070014#include "SkPoint3.h"
15
robertphillips71e05522016-05-31 12:08:25 -070016static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
robertphillips2f0dbc72015-08-20 05:15:06 -070017
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
robertphillips71e05522016-05-31 12:08:25 -070022 SkLights::Builder builder;
robertphillips2f0dbc72015-08-20 05:15:06 -070023
vjiaoblack772b5ee2016-08-12 11:38:47 -070024 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, blue), dir));
vjiaoblacka8eabc42016-08-29 10:22:09 -070025 builder.setAmbientLightColor(SkColor3f::Make(0.1f, 0.1f, 0.1f));
robertphillips2f0dbc72015-08-20 05:15:06 -070026
27 return builder.finish();
28}
jvanverthd17a3292015-07-09 09:04:16 -070029
30////////////////////////////////////////////////////////////////////////////
31
32class LightingView : public SampleView {
33public:
reedfe630452016-03-25 09:08:00 -070034 SkBitmap fDiffuseBitmap;
35 SkBitmap fNormalBitmap;
36 SkScalar fLightAngle;
37 SkScalar fColorFactor;
jvanverthd17a3292015-07-09 09:04:16 -070038
39 LightingView() {
40 SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
msarettd15750c2016-03-18 15:48:49 -070041 decode_file(diffusePath.c_str(), &fDiffuseBitmap);
jvanverthd17a3292015-07-09 09:04:16 -070042 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
msarettd15750c2016-03-18 15:48:49 -070043 decode_file(normalPath.c_str(), &fNormalBitmap);
jvanverthd17a3292015-07-09 09:04:16 -070044
45 fLightAngle = 0.0f;
robertphillipsa9652ac2015-07-31 05:17:24 -070046 fColorFactor = 0.0f;
jvanverthd17a3292015-07-09 09:04:16 -070047 }
48
jvanverthd17a3292015-07-09 09:04:16 -070049protected:
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;
robertphillipsa9652ac2015-07-31 05:17:24 -070061 fColorFactor += 0.01f;
62 if (fColorFactor > 1.0f) {
63 fColorFactor = 0.0f;
64 }
jvanverthd17a3292015-07-09 09:04:16 -070065
robertphillips71e05522016-05-31 12:08:25 -070066 sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
reedfe630452016-03-25 09:08:00 -070067 SkPaint paint;
reed1ec04d92016-08-05 12:07:41 -070068 sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(fNormalBitmap,
69 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr);
dvonbeck5b794fa2016-07-06 13:58:36 -070070 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(
71 std::move(normalMap), SkMatrix::I());
reed320a40d2016-08-02 06:12:06 -070072 sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(fDiffuseBitmap,
dvonbeck6af677f2016-07-10 18:38:33 -070073 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr);
74 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
75 std::move(lights)));
jvanverthd17a3292015-07-09 09:04:16 -070076 paint.setColor(SK_ColorBLACK);
77
halcanary9d524f22016-03-29 09:03:52 -070078 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
jvanverthd17a3292015-07-09 09:04:16 -070079 (SkScalar)fDiffuseBitmap.height());
80 canvas->drawRect(r, paint);
81
82 // so we're constantly updating
halcanary96fcdcc2015-08-27 07:41:13 -070083 this->inval(nullptr);
jvanverthd17a3292015-07-09 09:04:16 -070084 }
85
86 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -070087 this->inval(nullptr);
jvanverthd17a3292015-07-09 09:04:16 -070088 return this->INHERITED::onFindClickHandler(x, y, modi);
89 }
90
91private:
92 typedef SampleView INHERITED;
93};
94
95//////////////////////////////////////////////////////////////////////////////
96
97static SkView* MyFactory() { return new LightingView; }
98static SkViewRegister reg(MyFactory);