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