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 "SkPoint3.h" | 13 #include "SkPoint3.h" |
13 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
14 #include "../private/SkTArray.h" | 15 |
15 #include "SkImage.h" | 16 class SkReadBuffer; |
| 17 class SkWriteBuffer; |
| 18 class SkImage; |
16 | 19 |
17 class SK_API SkLights : public SkRefCnt { | 20 class SK_API SkLights : public SkRefCnt { |
18 public: | 21 public: |
19 class Light { | 22 class Light { |
20 public: | 23 public: |
21 enum LightType { | 24 enum LightType { |
22 kAmbient_LightType, // only 'fColor' is used | 25 kAmbient_LightType, // only 'fColor' is used |
23 kDirectional_LightType | 26 kDirectional_LightType, |
| 27 kPoint_LightType |
24 }; | 28 }; |
25 | 29 |
26 Light(const Light& other) | 30 Light(const Light& other) |
27 : fType(other.fType) | 31 : fType(other.fType) |
28 , fColor(other.fColor) | 32 , fColor(other.fColor) |
29 , fDirection(other.fDirection) | 33 , fDirection(other.fDirection) |
30 , fShadowMap(other.fShadowMap) { | 34 , fShadowMap(other.fShadowMap) { |
31 } | 35 } |
32 | 36 |
33 Light(Light&& other) | 37 Light(Light&& other) |
34 : fType(other.fType) | 38 : fType(other.fType) |
35 , fColor(other.fColor) | 39 , fColor(other.fColor) |
36 , fDirection(other.fDirection) | 40 , fDirection(other.fDirection) |
37 , fShadowMap(std::move(other.fShadowMap)) { | 41 , fShadowMap(std::move(other.fShadowMap)) { |
38 } | 42 } |
39 | 43 |
40 Light(const SkColor3f& color) | 44 static Light MakeAmbient(const SkColor3f& color) { |
41 : fType(kAmbient_LightType) | 45 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f,
1.0f)); |
42 , fColor(color) { | |
43 fDirection.set(0.0f, 0.0f, 1.0f); | |
44 } | 46 } |
45 | 47 |
46 Light(const SkColor3f& color, const SkVector3& dir) | 48 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di
r) { |
47 : fType(kDirectional_LightType) | 49 Light light(kDirectional_LightType, color, dir); |
48 , fColor(color) | 50 if (!light.fDirection.normalize()) { |
49 , fDirection(dir) { | 51 light.fDirection.set(0.0f, 0.0f, 1.0f); |
50 if (!fDirection.normalize()) { | |
51 fDirection.set(0.0f, 0.0f, 1.0f); | |
52 } | 52 } |
| 53 return light; |
| 54 } |
| 55 |
| 56 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { |
| 57 return Light(kPoint_LightType, color, pos); |
53 } | 58 } |
54 | 59 |
55 LightType type() const { return fType; } | 60 LightType type() const { return fType; } |
56 const SkColor3f& color() const { return fColor; } | 61 const SkColor3f& color() const { return fColor; } |
57 const SkVector3& dir() const { | 62 const SkVector3& dir() const { |
58 SkASSERT(kAmbient_LightType != fType); | 63 SkASSERT(kDirectional_LightType == fType); |
59 return fDirection; | 64 return fDirection; |
| 65 } |
| 66 const SkPoint3& pos() const { |
| 67 SkASSERT(kPoint_LightType == fType); |
| 68 return fDirection; |
60 } | 69 } |
61 | 70 |
62 void setShadowMap(sk_sp<SkImage> shadowMap) { | 71 void setShadowMap(sk_sp<SkImage> shadowMap) { |
63 fShadowMap = std::move(shadowMap); | 72 fShadowMap = std::move(shadowMap); |
64 } | 73 } |
65 | 74 |
66 SkImage* getShadowMap() const { | 75 SkImage* getShadowMap() const { |
67 return fShadowMap.get(); | 76 return fShadowMap.get(); |
68 } | 77 } |
69 | 78 |
70 Light& operator= (const Light& b) { | 79 Light& operator= (const Light& b) { |
71 if (this == &b) { | 80 if (this == &b) { |
72 return *this; | 81 return *this; |
73 } | 82 } |
74 | 83 |
75 fColor = b.fColor; | 84 fColor = b.fColor; |
76 fType = b.fType; | 85 fType = b.fType; |
77 fDirection = b.fDirection; | 86 fDirection = b.fDirection; |
78 fShadowMap = b.fShadowMap; | 87 fShadowMap = b.fShadowMap; |
79 return *this; | 88 return *this; |
80 } | 89 } |
81 | |
82 private: | 90 private: |
83 LightType fType; | 91 LightType fType; |
84 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. | 92 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. |
85 SkVector3 fDirection; // direction towards the light (+Z is out
of the screen). | 93 SkVector3 fDirection; // For directional lights, holds the direc
tion towards the |
| 94 // light (+Z is out of the screen). |
86 // If degenerate, it will be replaced with
(0, 0, 1). | 95 // If degenerate, it will be replaced with
(0, 0, 1). |
| 96 // For point lights, holds location of poi
nt light |
87 sk_sp<SkImage> fShadowMap; | 97 sk_sp<SkImage> fShadowMap; |
| 98 |
| 99 Light(LightType type, const SkColor3f& color, const SkVector3& dir) { |
| 100 fType = type; |
| 101 fColor = color; |
| 102 fDirection = dir; |
| 103 } |
88 }; | 104 }; |
89 | 105 |
90 class Builder { | 106 class Builder { |
91 public: | 107 public: |
92 Builder() : fLights(new SkLights) { } | 108 Builder() : fLights(new SkLights) { } |
93 | 109 |
94 void add(const Light& light) { | 110 void add(const Light& light) { |
95 if (fLights) { | 111 if (fLights) { |
96 fLights->fLights.push_back(light); | 112 fLights->fLights.push_back(light); |
97 } | 113 } |
98 } | 114 } |
99 | 115 |
100 void add(Light&& light) { | 116 void add(Light&& light) { |
101 if (fLights) { | 117 if (fLights) { |
102 fLights->fLights.push_back(std::move(light)); | 118 fLights->fLights.push_back(std::move(light)); |
103 } | 119 } |
(...skipping 12 matching lines...) Expand all Loading... |
116 } | 132 } |
117 | 133 |
118 const Light& light(int index) const { | 134 const Light& light(int index) const { |
119 return fLights[index]; | 135 return fLights[index]; |
120 } | 136 } |
121 | 137 |
122 Light& light(int index) { | 138 Light& light(int index) { |
123 return fLights[index]; | 139 return fLights[index]; |
124 } | 140 } |
125 | 141 |
| 142 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf); |
| 143 |
| 144 void flatten(SkWriteBuffer& buf) const; |
| 145 |
126 private: | 146 private: |
127 SkLights() {} | 147 SkLights() {} |
128 | |
129 SkTArray<Light> fLights; | 148 SkTArray<Light> fLights; |
130 | |
131 typedef SkRefCnt INHERITED; | 149 typedef SkRefCnt INHERITED; |
132 }; | 150 }; |
133 | 151 |
134 #endif | 152 #endif |
OLD | NEW |