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