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 "../private/SkTArray.h" | 12 #include "../private/SkTArray.h" |
13 #include "SkPoint3.h" | 13 #include "SkPoint3.h" |
14 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
15 | 15 |
16 class SkReadBuffer; | 16 class SkReadBuffer; |
17 class SkWriteBuffer; | 17 class SkWriteBuffer; |
18 class SkImage; | 18 class SkImage; |
19 | 19 |
20 class SK_API SkLights : public SkRefCnt { | 20 class SK_API SkLights : public SkRefCnt { |
21 public: | 21 public: |
22 class Light { | 22 class Light { |
23 public: | 23 public: |
24 enum LightType { | 24 enum LightType { |
25 kAmbient_LightType, // only 'fColor' is used | |
26 kDirectional_LightType, | 25 kDirectional_LightType, |
27 kPoint_LightType | 26 kPoint_LightType |
28 }; | 27 }; |
29 | 28 |
30 Light(const Light& other) | 29 Light(const Light& other) |
31 : fType(other.fType) | 30 : fType(other.fType) |
32 , fColor(other.fColor) | 31 , fColor(other.fColor) |
33 , fDirOrPos(other.fDirOrPos) | 32 , fDirOrPos(other.fDirOrPos) |
34 , fIntensity(other.fIntensity) | 33 , fIntensity(other.fIntensity) |
35 , fShadowMap(other.fShadowMap) { | 34 , fShadowMap(other.fShadowMap) { |
36 } | 35 } |
37 | 36 |
38 Light(Light&& other) | 37 Light(Light&& other) |
39 : fType(other.fType) | 38 : fType(other.fType) |
40 , fColor(other.fColor) | 39 , fColor(other.fColor) |
41 , fDirOrPos(other.fDirOrPos) | 40 , fDirOrPos(other.fDirOrPos) |
42 , fIntensity(other.fIntensity) | 41 , fIntensity(other.fIntensity) |
43 , fShadowMap(std::move(other.fShadowMap)) { | 42 , fShadowMap(std::move(other.fShadowMap)) { |
44 } | 43 } |
45 | 44 |
46 static Light MakeAmbient(const SkColor3f& color) { | |
47 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f,
1.0f)); | |
48 } | |
49 | |
50 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di
r) { | 45 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di
r) { |
51 Light light(kDirectional_LightType, color, dir); | 46 Light light(kDirectional_LightType, color, dir); |
52 if (!light.fDirOrPos.normalize()) { | 47 if (!light.fDirOrPos.normalize()) { |
53 light.fDirOrPos.set(0.0f, 0.0f, 1.0f); | 48 light.fDirOrPos.set(0.0f, 0.0f, 1.0f); |
54 } | 49 } |
55 return light; | 50 return light; |
56 } | 51 } |
57 | 52 |
58 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkSc
alar intensity) { | 53 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkSc
alar intensity) { |
59 return Light(kPoint_LightType, color, pos, intensity); | 54 return Light(kPoint_LightType, color, pos, intensity); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 (fDirOrPos == b.fDirOrPos) && | 100 (fDirOrPos == b.fDirOrPos) && |
106 (fShadowMap == b.fShadowMap) && | 101 (fShadowMap == b.fShadowMap) && |
107 (fIntensity == b.fIntensity); | 102 (fIntensity == b.fIntensity); |
108 } | 103 } |
109 | 104 |
110 bool operator!= (const Light& b) { return !(this->operator==(b)); } | 105 bool operator!= (const Light& b) { return !(this->operator==(b)); } |
111 | 106 |
112 private: | 107 private: |
113 LightType fType; | 108 LightType fType; |
114 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. | 109 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. |
115 | |
116 SkVector3 fDirOrPos; // For directional lights, holds the direc
tion towards the | 110 SkVector3 fDirOrPos; // For directional lights, holds the direc
tion towards the |
117 // light (+Z is out of the screen). | 111 // light (+Z is out of the screen). |
118 // If degenerate, it will be replaced with
(0, 0, 1). | 112 // If degenerate, it will be replaced with
(0, 0, 1). |
119 // For point lights, holds location of poi
nt light | 113 // For point lights, holds location of poi
nt light |
120 | 114 |
121 SkScalar fIntensity; // For point lights, dictates the light in
tensity. | 115 SkScalar fIntensity; // For point lights, dictates the light in
tensity. |
122 // Simply a multiplier to the final light
output value. | 116 // Simply a multiplier to the final light
output value. |
123 sk_sp<SkImage> fShadowMap; | 117 sk_sp<SkImage> fShadowMap; |
124 | 118 |
125 Light(LightType type, const SkColor3f& color, | 119 Light(LightType type, const SkColor3f& color, |
126 const SkVector3& dir, SkScalar intensity = 0.0f) { | 120 const SkVector3& dirOrPos, SkScalar intensity = 0.0f) { |
127 fType = type; | 121 fType = type; |
128 fColor = color; | 122 fColor = color; |
129 fDirOrPos = dir; | 123 fDirOrPos = dirOrPos; |
130 fIntensity = intensity; | 124 fIntensity = intensity; |
131 } | 125 } |
132 }; | 126 }; |
133 | 127 |
134 class Builder { | 128 class Builder { |
135 public: | 129 public: |
136 Builder() : fLights(new SkLights) { } | 130 Builder() : fLights(new SkLights) { } |
137 | 131 |
138 void add(const Light& light) { | 132 void add(const Light& light) { |
139 if (fLights) { | 133 if (fLights) { |
140 fLights->fLights.push_back(light); | 134 fLights->fLights.push_back(light); |
141 } | 135 } |
142 } | 136 } |
143 | 137 |
144 void add(Light&& light) { | 138 void add(Light&& light) { |
145 if (fLights) { | 139 if (fLights) { |
146 fLights->fLights.push_back(std::move(light)); | 140 fLights->fLights.push_back(std::move(light)); |
147 } | 141 } |
148 } | 142 } |
149 | 143 |
| 144 void setAmbientLightColor(const SkColor3f& color) { |
| 145 if (fLights) { |
| 146 fLights->fAmbientLightColor = color; |
| 147 } |
| 148 } |
| 149 |
150 sk_sp<SkLights> finish() { | 150 sk_sp<SkLights> finish() { |
151 return std::move(fLights); | 151 return std::move(fLights); |
152 } | 152 } |
153 | 153 |
154 private: | 154 private: |
155 sk_sp<SkLights> fLights; | 155 sk_sp<SkLights> fLights; |
156 }; | 156 }; |
157 | 157 |
158 int numLights() const { | 158 int numLights() const { |
159 return fLights.count(); | 159 return fLights.count(); |
160 } | 160 } |
161 | 161 |
162 const Light& light(int index) const { | 162 const Light& light(int index) const { |
163 return fLights[index]; | 163 return fLights[index]; |
164 } | 164 } |
165 | 165 |
166 Light& light(int index) { | 166 Light& light(int index) { |
167 return fLights[index]; | 167 return fLights[index]; |
168 } | 168 } |
169 | 169 |
| 170 const SkColor3f& ambientLightColor() const { |
| 171 return fAmbientLightColor; |
| 172 } |
| 173 |
170 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); | 174 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); |
171 | 175 |
172 void flatten(SkWriteBuffer& buf) const; | 176 void flatten(SkWriteBuffer& buf) const; |
173 | 177 |
174 private: | 178 private: |
175 SkLights() {} | 179 SkLights() {} |
176 SkTArray<Light> fLights; | 180 SkTArray<Light> fLights; |
| 181 SkColor3f fAmbientLightColor; |
177 typedef SkRefCnt INHERITED; | 182 typedef SkRefCnt INHERITED; |
178 }; | 183 }; |
179 | 184 |
180 #endif | 185 #endif |
OLD | NEW |