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 | 14 |
15 //////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////// |
16 | 16 |
17 class LightingView : public SampleView { | 17 class LightingView : public SampleView { |
18 public: | 18 public: |
19 SkAutoTUnref<SkShader> fShader; | 19 SkAutoTUnref<SkShader> fShader; |
20 SkBitmap fDiffuseBitmap; | 20 SkBitmap fDiffuseBitmap; |
21 SkBitmap fNormalBitmap; | 21 SkBitmap fNormalBitmap; |
22 SkScalar fLightAngle; | 22 SkScalar fLightAngle; |
23 int fColorFactor; | 23 SkScalar fColorFactor; |
24 SkColor fAmbientColor; | 24 SkColor3f fAmbientColor; |
25 | 25 |
26 LightingView() { | 26 LightingView() { |
27 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); | 27 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); |
28 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); | 28 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); |
29 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); | 29 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); |
30 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); | 30 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); |
31 | 31 |
32 fLightAngle = 0.0f; | 32 fLightAngle = 0.0f; |
33 fColorFactor = 0; | 33 fColorFactor = 0.0f; |
34 | 34 |
35 SkLightingShader::Light light; | 35 SkLightingShader::Light light; |
36 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); | 36 light.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); |
37 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 37 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
38 light.fDirection.fY = SkScalarCos(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); | 39 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
40 | 40 |
41 fAmbientColor = SkColorSetRGB(0x1f, 0x1f, 0x1f); | 41 fAmbientColor = SkColor3f::Make(0.1f, 0.1f, 0.1f); |
42 | 42 |
43 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, | 43 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
44 light, fAmbientColor, nullptr)); | 44 light, fAmbientColor, nullptr)); |
45 } | 45 } |
46 | 46 |
47 virtual ~LightingView() {} | 47 virtual ~LightingView() {} |
48 | 48 |
49 protected: | 49 protected: |
50 // overrides from SkEventSink | 50 // overrides from SkEventSink |
51 bool onQuery(SkEvent* evt) override { | 51 bool onQuery(SkEvent* evt) override { |
52 if (SampleCode::TitleQ(*evt)) { | 52 if (SampleCode::TitleQ(*evt)) { |
53 SampleCode::TitleR(evt, "Lighting"); | 53 SampleCode::TitleR(evt, "Lighting"); |
54 return true; | 54 return true; |
55 } | 55 } |
56 return this->INHERITED::onQuery(evt); | 56 return this->INHERITED::onQuery(evt); |
57 } | 57 } |
58 | 58 |
59 void onDrawContent(SkCanvas* canvas) override { | 59 void onDrawContent(SkCanvas* canvas) override { |
60 fLightAngle += 0.015f; | 60 fLightAngle += 0.015f; |
61 fColorFactor++; | 61 fColorFactor += 0.01f; |
| 62 if (fColorFactor > 1.0f) { |
| 63 fColorFactor = 0.0f; |
| 64 } |
62 | 65 |
63 SkLightingShader::Light light; | 66 SkLightingShader::Light light; |
64 light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff); | 67 light.fColor = SkColor3f::Make(1.0f, 1.0f, fColorFactor); |
65 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 68 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
66 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 69 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
67 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); | 70 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
68 | 71 |
69 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, | 72 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap, |
70 light, fAmbientColor, nullptr)); | 73 light, fAmbientColor, nullptr)); |
71 | 74 |
72 SkPaint paint; | 75 SkPaint paint; |
73 paint.setShader(fShader); | 76 paint.setShader(fShader); |
74 paint.setColor(SK_ColorBLACK); | 77 paint.setColor(SK_ColorBLACK); |
(...skipping 12 matching lines...) Expand all Loading... |
87 } | 90 } |
88 | 91 |
89 private: | 92 private: |
90 typedef SampleView INHERITED; | 93 typedef SampleView INHERITED; |
91 }; | 94 }; |
92 | 95 |
93 ////////////////////////////////////////////////////////////////////////////// | 96 ////////////////////////////////////////////////////////////////////////////// |
94 | 97 |
95 static SkView* MyFactory() { return new LightingView; } | 98 static SkView* MyFactory() { return new LightingView; } |
96 static SkViewRegister reg(MyFactory); | 99 static SkViewRegister reg(MyFactory); |
OLD | NEW |