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 | 8 |
9 #ifndef SkLights_DEFINED | 9 #ifndef SkLights_DEFINED |
10 #define SkLights_DEFINED | 10 #define SkLights_DEFINED |
11 | 11 |
12 #include "SkPoint3.h" | 12 #include "SkPoint3.h" |
13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
14 #include "../private/SkTDArray.h" | 14 #include "../private/SkTDArray.h" |
| 15 #include "SkImage.h" |
15 | 16 |
16 class SK_API SkLights : public SkRefCnt { | 17 class SK_API SkLights : public SkRefCnt { |
17 public: | 18 public: |
18 class Light { | 19 class Light { |
19 public: | 20 public: |
20 enum LightType { | 21 enum LightType { |
21 kAmbient_LightType, // only 'fColor' is used | 22 kAmbient_LightType, // only 'fColor' is used |
22 kDirectional_LightType | 23 kDirectional_LightType |
23 }; | 24 }; |
24 | 25 |
25 Light(const SkColor3f& color) | 26 Light(const SkColor3f& color) |
26 : fType(kAmbient_LightType) | 27 : fType(kAmbient_LightType) |
27 , fColor(color) { | 28 , fColor(color) { |
28 fDirection.set(0.0f, 0.0f, 1.0f); | 29 fDirection.set(0.0f, 0.0f, 1.0f); |
| 30 fShadowMap.reset(nullptr); |
29 } | 31 } |
30 | 32 |
31 Light(const SkColor3f& color, const SkVector3& dir) | 33 Light(const SkColor3f& color, const SkVector3& dir) |
32 : fType(kDirectional_LightType) | 34 : fType(kDirectional_LightType) |
33 , fColor(color) | 35 , fColor(color) |
34 , fDirection(dir) { | 36 , fDirection(dir) { |
35 if (!fDirection.normalize()) { | 37 if (!fDirection.normalize()) { |
36 fDirection.set(0.0f, 0.0f, 1.0f); | 38 fDirection.set(0.0f, 0.0f, 1.0f); |
37 } | 39 } |
| 40 fShadowMap.reset(nullptr); |
38 } | 41 } |
39 | 42 |
40 LightType type() const { return fType; } | 43 LightType type() const { return fType; } |
41 const SkColor3f& color() const { return fColor; } | 44 const SkColor3f& color() const { return fColor; } |
42 const SkVector3& dir() const { | 45 const SkVector3& dir() const { |
43 SkASSERT(kAmbient_LightType != fType); | 46 SkASSERT(kAmbient_LightType != fType); |
44 return fDirection; | 47 return fDirection; |
45 } | 48 } |
46 | 49 |
| 50 void setShadowMap(sk_sp<SkImage> shadowMap) { |
| 51 fShadowMap = std::move(shadowMap); |
| 52 } |
| 53 |
| 54 sk_sp<SkImage> getShadowMap() const { |
| 55 return fShadowMap; |
| 56 } |
| 57 |
| 58 Light& operator= (const Light& b) { |
| 59 if (this == &b) |
| 60 return *this; |
| 61 |
| 62 this->fColor = b.fColor; |
| 63 this->fType = b.fType; |
| 64 this->fDirection = b.fDirection; |
| 65 |
| 66 if (b.fShadowMap) { |
| 67 this->fShadowMap = b.fShadowMap; |
| 68 } |
| 69 |
| 70 return *this; |
| 71 } |
| 72 |
47 private: | 73 private: |
48 LightType fType; | 74 LightType fType; |
49 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. | 75 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. |
50 SkVector3 fDirection; // direction towards the light (+Z is out
of the screen). | 76 SkVector3 fDirection; // direction towards the light (+Z is out
of the screen). |
51 // If degenerate, it will be replaced with
(0, 0, 1). | 77 // If degenerate, it will be replaced with
(0, 0, 1). |
| 78 sk_sp<SkImage> fShadowMap; |
52 }; | 79 }; |
53 | 80 |
54 class Builder { | 81 class Builder { |
55 public: | 82 public: |
56 Builder() : fLights(new SkLights) { } | 83 Builder() : fLights(new SkLights) { } |
57 | 84 |
58 void add(const Light& light) { | 85 void add(const Light& light) { |
59 if (fLights) { | 86 if (fLights) { |
60 *fLights->fLights.push() = light; | 87 (void) fLights->fLights.append(1, &light); |
61 } | 88 } |
62 } | 89 } |
63 | 90 |
64 sk_sp<SkLights> finish() { | 91 sk_sp<SkLights> finish() { |
65 return fLights; | 92 return fLights; |
66 } | 93 } |
67 | 94 |
68 private: | 95 private: |
69 sk_sp<SkLights> fLights; | 96 sk_sp<SkLights> fLights; |
70 }; | 97 }; |
71 | 98 |
72 int numLights() const { | 99 int numLights() const { |
73 return fLights.count(); | 100 return fLights.count(); |
74 } | 101 } |
75 | 102 |
76 const Light& light(int index) const { | 103 const Light& light(int index) const { |
77 return fLights[index]; | 104 return fLights[index]; |
78 } | 105 } |
79 | 106 |
| 107 Light& light(int index) { |
| 108 return fLights[index]; |
| 109 } |
| 110 |
80 private: | 111 private: |
81 SkLights() {} | 112 SkLights() {} |
82 | 113 |
83 SkTDArray<Light> fLights; | 114 SkTDArray<Light> fLights; |
84 }; | 115 }; |
85 | 116 |
86 #endif | 117 #endif |
OLD | NEW |