| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "Resources.h" | 9 #include "Resources.h" |
| 10 | 10 |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
| 13 #include "SkLightingShader.h" | 13 #include "SkLightingShader.h" |
| 14 #include "SkPoint3.h" |
| 15 |
| 16 static const SkLightingShader::Lights* create_lights(SkScalar angle, SkScalar bl
ue) { |
| 17 |
| 18 const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_Scal
arPI*0.25f), |
| 19 SkScalarCos(angle)*SkScalarSin(SK_Scal
arPI*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 } |
| 14 | 29 |
| 15 //////////////////////////////////////////////////////////////////////////// | 30 //////////////////////////////////////////////////////////////////////////// |
| 16 | 31 |
| 17 class LightingView : public SampleView { | 32 class LightingView : public SampleView { |
| 18 public: | 33 public: |
| 19 SkAutoTUnref<SkShader> fShader; | 34 SkAutoTUnref<SkShader> fShader; |
| 20 SkBitmap fDiffuseBitmap; | 35 SkBitmap fDiffuseBitmap; |
| 21 SkBitmap fNormalBitmap; | 36 SkBitmap fNormalBitmap; |
| 22 SkScalar fLightAngle; | 37 SkScalar fLightAngle; |
| 23 SkScalar fColorFactor; | 38 SkScalar fColorFactor; |
| 24 SkColor3f fAmbientColor; | |
| 25 | 39 |
| 26 LightingView() { | 40 LightingView() { |
| 27 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); | 41 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); |
| 28 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); | 42 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); |
| 29 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); | 43 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); |
| 30 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); | 44 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); |
| 31 | 45 |
| 32 fLightAngle = 0.0f; | 46 fLightAngle = 0.0f; |
| 33 fColorFactor = 0.0f; | 47 fColorFactor = 0.0f; |
| 34 | 48 |
| 35 SkLightingShader::Light light; | 49 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLight
Angle, 1.0f)); |
| 36 light.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); | |
| 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 | |
| 41 fAmbientColor = SkColor3f::Make(0.1f, 0.1f, 0.1f); | |
| 42 | 50 |
| 43 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, | 51 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
| 44 light, fAmbientColor, nullptr)); | 52 lights, SkVector::Make(1.0f, 0.0f
), |
| 53 nullptr, nullptr)); |
| 45 } | 54 } |
| 46 | 55 |
| 47 virtual ~LightingView() {} | 56 virtual ~LightingView() {} |
| 48 | 57 |
| 49 protected: | 58 protected: |
| 50 // overrides from SkEventSink | 59 // overrides from SkEventSink |
| 51 bool onQuery(SkEvent* evt) override { | 60 bool onQuery(SkEvent* evt) override { |
| 52 if (SampleCode::TitleQ(*evt)) { | 61 if (SampleCode::TitleQ(*evt)) { |
| 53 SampleCode::TitleR(evt, "Lighting"); | 62 SampleCode::TitleR(evt, "Lighting"); |
| 54 return true; | 63 return true; |
| 55 } | 64 } |
| 56 return this->INHERITED::onQuery(evt); | 65 return this->INHERITED::onQuery(evt); |
| 57 } | 66 } |
| 58 | 67 |
| 59 void onDrawContent(SkCanvas* canvas) override { | 68 void onDrawContent(SkCanvas* canvas) override { |
| 60 fLightAngle += 0.015f; | 69 fLightAngle += 0.015f; |
| 61 fColorFactor += 0.01f; | 70 fColorFactor += 0.01f; |
| 62 if (fColorFactor > 1.0f) { | 71 if (fColorFactor > 1.0f) { |
| 63 fColorFactor = 0.0f; | 72 fColorFactor = 0.0f; |
| 64 } | 73 } |
| 65 | 74 |
| 66 SkLightingShader::Light light; | 75 SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLight
Angle, |
| 67 light.fColor = SkColor3f::Make(1.0f, 1.0f, fColorFactor); | 76 fColor
Factor)); |
| 68 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | |
| 69 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | |
| 70 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); | |
| 71 | 77 |
| 72 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, | 78 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
| 73 light, fAmbientColor, nullptr)); | 79 lights, SkVector::Make(1.0f, 0.0f
), |
| 80 nullptr, nullptr)); |
| 74 | 81 |
| 75 SkPaint paint; | 82 SkPaint paint; |
| 76 paint.setShader(fShader); | 83 paint.setShader(fShader); |
| 77 paint.setColor(SK_ColorBLACK); | 84 paint.setColor(SK_ColorBLACK); |
| 78 | 85 |
| 79 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(), | 86 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(), |
| 80 (SkScalar)fDiffuseBitmap.height()); | 87 (SkScalar)fDiffuseBitmap.height()); |
| 81 canvas->drawRect(r, paint); | 88 canvas->drawRect(r, paint); |
| 82 | 89 |
| 83 // so we're constantly updating | 90 // so we're constantly updating |
| 84 this->inval(NULL); | 91 this->inval(NULL); |
| 85 } | 92 } |
| 86 | 93 |
| 87 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { | 94 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { |
| 88 this->inval(NULL); | 95 this->inval(NULL); |
| 89 return this->INHERITED::onFindClickHandler(x, y, modi); | 96 return this->INHERITED::onFindClickHandler(x, y, modi); |
| 90 } | 97 } |
| 91 | 98 |
| 92 private: | 99 private: |
| 93 typedef SampleView INHERITED; | 100 typedef SampleView INHERITED; |
| 94 }; | 101 }; |
| 95 | 102 |
| 96 ////////////////////////////////////////////////////////////////////////////// | 103 ////////////////////////////////////////////////////////////////////////////// |
| 97 | 104 |
| 98 static SkView* MyFactory() { return new LightingView; } | 105 static SkView* MyFactory() { return new LightingView; } |
| 99 static SkViewRegister reg(MyFactory); | 106 static SkViewRegister reg(MyFactory); |
| OLD | NEW |