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 , fDirection(other.fDirection) | 32 , fDirOrPos(other.fDirOrPos) |
34 , fShadowMap(other.fShadowMap) { | 33 , fShadowMap(other.fShadowMap) { |
35 } | 34 } |
36 | 35 |
37 Light(Light&& other) | 36 Light(Light&& other) |
38 : fType(other.fType) | 37 : fType(other.fType) |
39 , fColor(other.fColor) | 38 , fColor(other.fColor) |
40 , fDirection(other.fDirection) | 39 , fDirOrPos(other.fDirOrPos) |
41 , fShadowMap(std::move(other.fShadowMap)) { | 40 , fShadowMap(std::move(other.fShadowMap)) { |
42 } | 41 } |
43 | 42 |
44 static Light MakeAmbient(const SkColor3f& color) { | |
45 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f)); | |
46 } | |
47 | |
48 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) { | 43 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) { |
49 Light light(kDirectional_LightType, color, dir); | 44 Light light(kDirectional_LightType, color, dir); |
50 if (!light.fDirection.normalize()) { | 45 if (!light.fDirOrPos.normalize()) { |
51 light.fDirection.set(0.0f, 0.0f, 1.0f); | 46 light.fDirOrPos.set(0.0f, 0.0f, 1.0f); |
52 } | 47 } |
53 return light; | 48 return light; |
54 } | 49 } |
55 | 50 |
56 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { | 51 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { |
57 return Light(kPoint_LightType, color, pos); | 52 return Light(kPoint_LightType, color, pos); |
58 } | 53 } |
59 | 54 |
60 LightType type() const { return fType; } | 55 LightType type() const { return fType; } |
61 const SkColor3f& color() const { return fColor; } | 56 const SkColor3f& color() const { return fColor; } |
62 const SkVector3& dir() const { | 57 const SkVector3& dir() const { |
robertphillips
2016/08/26 16:08:05
keep as yoda-speak. The changeable variable is put
| |
63 SkASSERT(kDirectional_LightType == fType); | 58 SkASSERT(fType == kDirectional_LightType); |
64 return fDirection; | 59 return fDirOrPos; |
65 } | 60 } |
66 const SkPoint3& pos() const { | 61 const SkPoint3& pos() const { |
robertphillips
2016/08/26 16:08:05
same here
| |
67 SkASSERT(kPoint_LightType == fType); | 62 SkASSERT(fType == kPoint_LightType); |
68 return fDirection; | 63 return fDirOrPos; |
69 } | 64 } |
70 | 65 |
71 void setShadowMap(sk_sp<SkImage> shadowMap) { | 66 void setShadowMap(sk_sp<SkImage> shadowMap) { |
72 fShadowMap = std::move(shadowMap); | 67 fShadowMap = std::move(shadowMap); |
73 } | 68 } |
74 | 69 |
75 SkImage* getShadowMap() const { | 70 SkImage* getShadowMap() const { |
76 return fShadowMap.get(); | 71 return fShadowMap.get(); |
77 } | 72 } |
78 | 73 |
79 Light& operator= (const Light& b) { | 74 Light& operator= (const Light& b) { |
80 if (this == &b) { | 75 if (this == &b) { |
81 return *this; | 76 return *this; |
82 } | 77 } |
83 | 78 |
84 fColor = b.fColor; | 79 fColor = b.fColor; |
85 fType = b.fType; | 80 fType = b.fType; |
86 fDirection = b.fDirection; | 81 fDirOrPos = b.fDirOrPos; |
87 fShadowMap = b.fShadowMap; | 82 fShadowMap = b.fShadowMap; |
88 return *this; | 83 return *this; |
89 } | 84 } |
90 | 85 |
91 bool operator== (const Light& b) { | 86 bool operator== (const Light& b) { |
92 if (this == &b) { | 87 if (this == &b) { |
93 return true; | 88 return true; |
94 } | 89 } |
95 | 90 |
96 return (fColor == b.fColor) && | 91 return (fColor == b.fColor) && |
97 (fType == b.fType) && | 92 (fType == b.fType) && |
98 (fDirection == b.fDirection) && | 93 (fDirOrPos == b.fDirOrPos) && |
99 (fShadowMap == b.fShadowMap); | 94 (fShadowMap == b.fShadowMap); |
100 } | 95 } |
101 | 96 |
102 bool operator!= (const Light& b) { return !(this->operator==(b)); } | 97 bool operator!= (const Light& b) { return !(this->operator==(b)); } |
103 | 98 |
104 private: | 99 private: |
105 LightType fType; | 100 LightType fType; |
106 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. | 101 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. |
robertphillips
2016/08/26 16:08:05
line up comment
| |
107 SkVector3 fDirection; // For directional lights, holds the direc tion towards the | 102 SkVector3 fDirOrPos; // For directional lights, holds the direct ion towards the |
108 // light (+Z is out of the screen). | 103 // light (+Z is out of the screen). |
109 // If degenerate, it will be replaced with (0, 0, 1). | 104 // If degenerate, it will be replaced with (0, 0, 1). |
110 // For point lights, holds location of poi nt light | 105 // For point lights, holds location of poi nt light |
111 sk_sp<SkImage> fShadowMap; | 106 sk_sp<SkImage> fShadowMap; |
112 | 107 |
113 Light(LightType type, const SkColor3f& color, const SkVector3& dir) { | 108 Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos) { |
114 fType = type; | 109 fType = type; |
115 fColor = color; | 110 fColor = color; |
116 fDirection = dir; | 111 fDirOrPos = dirOrPos; |
117 } | 112 } |
118 }; | 113 }; |
119 | 114 |
120 class Builder { | 115 class Builder { |
121 public: | 116 public: |
122 Builder() : fLights(new SkLights) { } | 117 Builder() : fLights(new SkLights) { } |
123 | 118 |
124 void add(const Light& light) { | 119 void add(const Light& light) { |
robertphillips
2016/08/26 16:08:05
keep if test in case the caller tries to add stuff
vjiaoblack
2016/08/26 17:24:25
Done.
| |
125 if (fLights) { | 120 fLights->fNonAmbLights.push_back(light); |
126 fLights->fLights.push_back(light); | |
127 } | |
128 } | 121 } |
129 | 122 |
130 void add(Light&& light) { | 123 void add(Light&& light) { |
robertphillips
2016/08/26 16:08:05
here too
vjiaoblack
2016/08/26 17:24:25
Done.
| |
131 if (fLights) { | 124 fLights->fNonAmbLights.push_back(std::move(light)); |
132 fLights->fLights.push_back(std::move(light)); | 125 } |
133 } | 126 |
127 void setAmbientLightColor(const SkColor3f& color) { | |
128 fLights->fAmbientLight = color; | |
134 } | 129 } |
135 | 130 |
136 sk_sp<SkLights> finish() { | 131 sk_sp<SkLights> finish() { |
137 return std::move(fLights); | 132 return std::move(fLights); |
138 } | 133 } |
139 | 134 |
140 private: | 135 private: |
141 sk_sp<SkLights> fLights; | 136 sk_sp<SkLights> fLights; |
142 }; | 137 }; |
143 | 138 |
144 int numLights() const { | 139 int numLights() const { |
145 return fLights.count(); | 140 return fNonAmbLights.count(); |
146 } | 141 } |
147 | 142 |
148 const Light& light(int index) const { | 143 const Light& light(int index) const { |
149 return fLights[index]; | 144 return fNonAmbLights[index]; |
150 } | 145 } |
151 | 146 |
152 Light& light(int index) { | 147 Light& light(int index) { |
153 return fLights[index]; | 148 return fNonAmbLights[index]; |
149 } | |
150 | |
151 const SkColor3f& ambientLightColor() const { | |
152 return fAmbientLight; | |
154 } | 153 } |
155 | 154 |
156 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); | 155 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); |
157 | 156 |
158 void flatten(SkWriteBuffer& buf) const; | 157 void flatten(SkWriteBuffer& buf) const; |
159 | 158 |
160 private: | 159 private: |
161 SkLights() {} | 160 SkLights() {} |
162 SkTArray<Light> fLights; | 161 SkTArray<Light> fNonAmbLights; |
robertphillips
2016/08/26 16:08:05
fAmbientLightColor
| |
162 SkColor3f fAmbientLight; | |
163 typedef SkRefCnt INHERITED; | 163 typedef SkRefCnt INHERITED; |
164 }; | 164 }; |
165 | 165 |
166 #endif | 166 #endif |
OLD | NEW |