| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 SkVector3 fDirOrPos; // For directional lights, holds the direc
tion towards the | 111 SkVector3 fDirOrPos; // For directional lights, holds the direc
tion towards the |
| 117 // light (+Z is out of the screen). | 112 // light (+Z is out of the screen). |
| 118 // If degenerate, it will be replaced with
(0, 0, 1). | 113 // If degenerate, it will be replaced with
(0, 0, 1). |
| 119 // For point lights, holds location of poi
nt light | 114 // For point lights, holds location of poi
nt light |
| 120 | 115 |
| 121 SkScalar fIntensity; // For point lights, dictates the light in
tensity. | 116 SkScalar fIntensity; // For point lights, dictates the light in
tensity. |
| 122 // Simply a multiplier to the final light
output value. | 117 // Simply a multiplier to the final light
output value. |
| 123 sk_sp<SkImage> fShadowMap; | 118 sk_sp<SkImage> fShadowMap; |
| 124 | 119 |
| 125 Light(LightType type, const SkColor3f& color, | 120 Light(LightType type, const SkColor3f& color, |
| 126 const SkVector3& dir, SkScalar intensity = 0.0f) { | 121 const SkVector3& dirOrPos, SkScalar intensity = 0.0f) { |
| 127 fType = type; | 122 fType = type; |
| 128 fColor = color; | 123 fColor = color; |
| 129 fDirOrPos = dir; | 124 fDirOrPos = dirOrPos; |
| 130 fIntensity = intensity; | 125 fIntensity = intensity; |
| 131 } | 126 } |
| 132 }; | 127 }; |
| 133 | 128 |
| 134 class Builder { | 129 class Builder { |
| 135 public: | 130 public: |
| 136 Builder() : fLights(new SkLights) { } | 131 Builder() : fLights(new SkLights) {} |
| 137 | 132 |
| 138 void add(const Light& light) { | 133 void add(const Light& light) { |
| 139 if (fLights) { | 134 if (fLights) { |
| 140 fLights->fLights.push_back(light); | 135 fLights->fLights.push_back(light); |
| 141 } | 136 } |
| 142 } | 137 } |
| 143 | 138 |
| 144 void add(Light&& light) { | 139 void add(Light&& light) { |
| 145 if (fLights) { | 140 if (fLights) { |
| 146 fLights->fLights.push_back(std::move(light)); | 141 fLights->fLights.push_back(std::move(light)); |
| 147 } | 142 } |
| 148 } | 143 } |
| 149 | 144 |
| 145 void setAmbientLightColor(const SkColor3f& color) { |
| 146 if (fLights) { |
| 147 fLights->fAmbientLightColor = color; |
| 148 } |
| 149 } |
| 150 |
| 150 sk_sp<SkLights> finish() { | 151 sk_sp<SkLights> finish() { |
| 151 return std::move(fLights); | 152 return std::move(fLights); |
| 152 } | 153 } |
| 153 | 154 |
| 154 private: | 155 private: |
| 155 sk_sp<SkLights> fLights; | 156 sk_sp<SkLights> fLights; |
| 156 }; | 157 }; |
| 157 | 158 |
| 158 int numLights() const { | 159 int numLights() const { |
| 159 return fLights.count(); | 160 return fLights.count(); |
| 160 } | 161 } |
| 161 | 162 |
| 162 const Light& light(int index) const { | 163 const Light& light(int index) const { |
| 163 return fLights[index]; | 164 return fLights[index]; |
| 164 } | 165 } |
| 165 | 166 |
| 166 Light& light(int index) { | 167 Light& light(int index) { |
| 167 return fLights[index]; | 168 return fLights[index]; |
| 168 } | 169 } |
| 169 | 170 |
| 171 const SkColor3f& ambientLightColor() const { |
| 172 return fAmbientLightColor; |
| 173 } |
| 174 |
| 170 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); | 175 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); |
| 171 | 176 |
| 172 void flatten(SkWriteBuffer& buf) const; | 177 void flatten(SkWriteBuffer& buf) const; |
| 173 | 178 |
| 174 private: | 179 private: |
| 175 SkLights() {} | 180 SkLights() { |
| 181 fAmbientLightColor.set(0.0f, 0.0f, 0.0f); |
| 182 } |
| 176 SkTArray<Light> fLights; | 183 SkTArray<Light> fLights; |
| 184 SkColor3f fAmbientLightColor; |
| 177 typedef SkRefCnt INHERITED; | 185 typedef SkRefCnt INHERITED; |
| 178 }; | 186 }; |
| 179 | 187 |
| 180 #endif | 188 #endif |
| OLD | NEW |