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