jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 1 | |
| 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" |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkLightingShader.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 14 | #include "SkPoint3.h" |
| 15 | |
| 16 | static 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 | } |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | class LightingView : public SampleView { |
| 33 | public: |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 34 | SkAutoTUnref<SkShader> fShader; |
| 35 | SkBitmap fDiffuseBitmap; |
| 36 | SkBitmap fNormalBitmap; |
| 37 | SkScalar fLightAngle; |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 38 | SkScalar fColorFactor; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 39 | |
| 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; |
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 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 49 | SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle, 1.0f)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 50 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 51 | fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 52 | lights, SkVector::Make(1.0f, 0.0f), |
| 53 | nullptr, nullptr)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | virtual ~LightingView() {} |
| 57 | |
| 58 | protected: |
| 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; |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 70 | fColorFactor += 0.01f; |
| 71 | if (fColorFactor > 1.0f) { |
| 72 | fColorFactor = 0.0f; |
| 73 | } |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 74 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 75 | SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle, |
| 76 | fColorFactor)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 77 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 78 | fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 79 | lights, SkVector::Make(1.0f, 0.0f), |
| 80 | nullptr, nullptr)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 81 | |
| 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 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | this->inval(nullptr); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | this->inval(nullptr); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 96 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | typedef SampleView INHERITED; |
| 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | static SkView* MyFactory() { return new LightingView; } |
| 106 | static SkViewRegister reg(MyFactory); |