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 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 enum LightType { | 24 enum LightType { |
25 kAmbient_LightType, // only 'fColor' is used | 25 kAmbient_LightType, // only 'fColor' is used |
26 kDirectional_LightType, | 26 kDirectional_LightType, |
27 kPoint_LightType | 27 kPoint_LightType |
28 }; | 28 }; |
29 | 29 |
30 Light(const Light& other) | 30 Light(const Light& other) |
31 : fType(other.fType) | 31 : fType(other.fType) |
32 , fColor(other.fColor) | 32 , fColor(other.fColor) |
33 , fDirection(other.fDirection) | 33 , fDirection(other.fDirection) |
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 , fDirection(other.fDirection) | 41 , fDirection(other.fDirection) |
42 , fIntensity(other.fIntensity) | |
41 , fShadowMap(std::move(other.fShadowMap)) { | 43 , fShadowMap(std::move(other.fShadowMap)) { |
42 } | 44 } |
43 | 45 |
44 static Light MakeAmbient(const SkColor3f& color) { | 46 static Light MakeAmbient(const SkColor3f& color) { |
45 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f)); | 47 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f)); |
46 } | 48 } |
47 | 49 |
48 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) { | 50 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) { |
49 Light light(kDirectional_LightType, color, dir); | 51 Light light(kDirectional_LightType, color, dir); |
50 if (!light.fDirection.normalize()) { | 52 if (!light.fDirection.normalize()) { |
51 light.fDirection.set(0.0f, 0.0f, 1.0f); | 53 light.fDirection.set(0.0f, 0.0f, 1.0f); |
52 } | 54 } |
53 return light; | 55 return light; |
54 } | 56 } |
55 | 57 |
56 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { | 58 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkSc alar intensity) { |
57 return Light(kPoint_LightType, color, pos); | 59 Light light(kPoint_LightType, color, pos); |
60 light.fIntensity = intensity; | |
61 return light; | |
58 } | 62 } |
59 | 63 |
60 LightType type() const { return fType; } | 64 LightType type() const { return fType; } |
61 const SkColor3f& color() const { return fColor; } | 65 const SkColor3f& color() const { return fColor; } |
62 const SkVector3& dir() const { | 66 const SkVector3& dir() const { |
63 SkASSERT(kDirectional_LightType == fType); | 67 SkASSERT(kDirectional_LightType == fType); |
64 return fDirection; | 68 return fDirection; |
65 } | 69 } |
66 const SkPoint3& pos() const { | 70 const SkPoint3& pos() const { |
67 SkASSERT(kPoint_LightType == fType); | 71 SkASSERT(kPoint_LightType == fType); |
68 return fDirection; | 72 return fDirection; |
69 } | 73 } |
74 SkScalar intensity() const { | |
75 SkASSERT(kPoint_LightType == fType); | |
76 return fIntensity; | |
77 } | |
70 | 78 |
71 void setShadowMap(sk_sp<SkImage> shadowMap) { | 79 void setShadowMap(sk_sp<SkImage> shadowMap) { |
72 fShadowMap = std::move(shadowMap); | 80 fShadowMap = std::move(shadowMap); |
73 } | 81 } |
74 | 82 |
75 SkImage* getShadowMap() const { | 83 SkImage* getShadowMap() const { |
76 return fShadowMap.get(); | 84 return fShadowMap.get(); |
77 } | 85 } |
78 | 86 |
79 Light& operator= (const Light& b) { | 87 Light& operator= (const Light& b) { |
80 if (this == &b) { | 88 if (this == &b) { |
81 return *this; | 89 return *this; |
82 } | 90 } |
83 | 91 |
84 fColor = b.fColor; | 92 fColor = b.fColor; |
85 fType = b.fType; | 93 fType = b.fType; |
86 fDirection = b.fDirection; | 94 fDirection = b.fDirection; |
87 fShadowMap = b.fShadowMap; | 95 fShadowMap = b.fShadowMap; |
88 return *this; | 96 return *this; |
89 } | 97 } |
90 private: | 98 private: |
91 LightType fType; | 99 LightType fType; |
92 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. | 100 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. |
101 | |
93 SkVector3 fDirection; // For directional lights, holds the direc tion towards the | 102 SkVector3 fDirection; // For directional lights, holds the direc tion towards the |
jvanverth1
2016/08/17 19:59:02
Reusing the name fDirection for point lights seems
vjiaoblack
2016/08/18 14:52:18
Done.
| |
94 // light (+Z is out of the screen). | 103 // light (+Z is out of the screen). |
95 // If degenerate, it will be replaced with (0, 0, 1). | 104 // If degenerate, it will be replaced with (0, 0, 1). |
96 // For point lights, holds location of poi nt light | 105 // For point lights, holds location of poi nt light |
106 | |
107 SkScalar fIntensity; // Dictates the light intensity. Basically this divides | |
jvanverth1
2016/08/17 19:59:02
You need to mention that this is only used for poi
vjiaoblack
2016/08/18 14:52:18
That equation would also provide a slightly incorr
| |
108 // the dist^2 attenuation variable to incr ease brightness. | |
97 sk_sp<SkImage> fShadowMap; | 109 sk_sp<SkImage> fShadowMap; |
98 | 110 |
99 Light(LightType type, const SkColor3f& color, const SkVector3& dir) { | 111 Light(LightType type, const SkColor3f& color, const SkVector3& dir) { |
100 fType = type; | 112 fType = type; |
101 fColor = color; | 113 fColor = color; |
102 fDirection = dir; | 114 fDirection = dir; |
115 fIntensity = 0.0f; | |
jvanverth1
2016/08/17 19:59:02
SK_ScalarZero
vjiaoblack
2016/08/18 14:52:18
This does not exist. SK_ScalarNearlyZero exists, b
| |
103 } | 116 } |
104 }; | 117 }; |
105 | 118 |
106 class Builder { | 119 class Builder { |
107 public: | 120 public: |
108 Builder() : fLights(new SkLights) { } | 121 Builder() : fLights(new SkLights) { } |
109 | 122 |
110 void add(const Light& light) { | 123 void add(const Light& light) { |
111 if (fLights) { | 124 if (fLights) { |
112 fLights->fLights.push_back(light); | 125 fLights->fLights.push_back(light); |
(...skipping 30 matching lines...) Expand all Loading... | |
143 | 156 |
144 void flatten(SkWriteBuffer& buf) const; | 157 void flatten(SkWriteBuffer& buf) const; |
145 | 158 |
146 private: | 159 private: |
147 SkLights() {} | 160 SkLights() {} |
148 SkTArray<Light> fLights; | 161 SkTArray<Light> fLights; |
149 typedef SkRefCnt INHERITED; | 162 typedef SkRefCnt INHERITED; |
150 }; | 163 }; |
151 | 164 |
152 #endif | 165 #endif |
OLD | NEW |