jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 7 | #include "DecodeFile.h" |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "Resources.h" |
| 10 | |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 11 | #include "SkBitmapProcShader.h" |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 12 | #include "SkCanvas.h" |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 13 | #include "SkLightingShader.h" |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 14 | #include "SkNormalSource.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 15 | #include "SkPoint3.h" |
| 16 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 17 | static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 18 | |
| 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 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 23 | SkLights::Builder builder; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 24 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 25 | 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))); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 27 | |
| 28 | return builder.finish(); |
| 29 | } |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 30 | |
| 31 | //////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | class LightingView : public SampleView { |
| 34 | public: |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 35 | SkBitmap fDiffuseBitmap; |
| 36 | SkBitmap fNormalBitmap; |
| 37 | SkScalar fLightAngle; |
| 38 | SkScalar fColorFactor; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 39 | |
| 40 | LightingView() { |
| 41 | SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 42 | decode_file(diffusePath.c_str(), &fDiffuseBitmap); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 43 | SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 44 | decode_file(normalPath.c_str(), &fNormalBitmap); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 45 | |
| 46 | fLightAngle = 0.0f; |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 47 | fColorFactor = 0.0f; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 48 | } |
| 49 | |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 50 | protected: |
| 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; |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 62 | fColorFactor += 0.01f; |
| 63 | if (fColorFactor > 1.0f) { |
| 64 | fColorFactor = 0.0f; |
| 65 | } |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 66 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 67 | sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor)); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 68 | SkPaint paint; |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 69 | 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))); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 75 | paint.setColor(SK_ColorBLACK); |
| 76 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 77 | SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(), |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 78 | (SkScalar)fDiffuseBitmap.height()); |
| 79 | canvas->drawRect(r, paint); |
| 80 | |
| 81 | // so we're constantly updating |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | this->inval(nullptr); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | this->inval(nullptr); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 87 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef SampleView INHERITED; |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | static SkView* MyFactory() { return new LightingView; } |
| 97 | static SkViewRegister reg(MyFactory); |