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