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