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" |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | class LightingView : public SampleView { |
| 18 | public: |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 19 | SkAutoTUnref<SkShader> fShader; |
| 20 | SkBitmap fDiffuseBitmap; |
| 21 | SkBitmap fNormalBitmap; |
| 22 | SkScalar fLightAngle; |
| 23 | int fColorFactor; |
| 24 | SkColor fAmbientColor; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 25 | |
| 26 | LightingView() { |
| 27 | SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); |
| 28 | SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); |
| 29 | SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); |
| 30 | SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); |
| 31 | |
| 32 | fLightAngle = 0.0f; |
| 33 | fColorFactor = 0; |
| 34 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 35 | SkLightingShader::Light light; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 36 | light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); |
| 37 | light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f); |
| 38 | light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f); |
| 39 | light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
| 40 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 41 | fAmbientColor = SkColorSetRGB(0x1f, 0x1f, 0x1f); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 42 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 43 | fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 44 | light, fAmbientColor, nullptr)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | virtual ~LightingView() {} |
| 48 | |
| 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; |
| 61 | fColorFactor++; |
| 62 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 63 | SkLightingShader::Light light; |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 64 | light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff); |
| 65 | light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f); |
| 66 | light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f); |
| 67 | light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
| 68 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 69 | fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 70 | light, fAmbientColor, nullptr)); |
jvanverth | d17a329 | 2015-07-09 09:04:16 -0700 | [diff] [blame] | 71 | |
| 72 | SkPaint paint; |
| 73 | paint.setShader(fShader); |
| 74 | paint.setColor(SK_ColorBLACK); |
| 75 | |
| 76 | SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(), |
| 77 | (SkScalar)fDiffuseBitmap.height()); |
| 78 | canvas->drawRect(r, paint); |
| 79 | |
| 80 | // so we're constantly updating |
| 81 | this->inval(NULL); |
| 82 | } |
| 83 | |
| 84 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
| 85 | this->inval(NULL); |
| 86 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | typedef SampleView INHERITED; |
| 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | static SkView* MyFactory() { return new LightingView; } |
| 96 | static SkViewRegister reg(MyFactory); |