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 "SkPoint3.h" | 12 #include "SkPoint3.h" |
13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
14 #include "../private/SkTArray.h" | 14 #include "../private/SkTArray.h" |
15 #include "SkImage.h" | 15 #include "SkImage.h" |
16 | 16 |
17 class SK_API SkLights : public SkRefCnt { | 17 class SK_API SkLights : public SkRefCnt { |
18 public: | 18 public: |
19 class Light { | 19 class Light { |
20 public: | 20 public: |
21 enum LightType { | 21 enum LightType { |
22 kAmbient_LightType, // only 'fColor' is used | 22 kAmbient_LightType, // only 'fColor' is used |
23 kDirectional_LightType | 23 kDirectional_LightType, |
24 kPoint_LightType | |
24 }; | 25 }; |
25 | 26 |
26 Light(const Light& other) | 27 Light(const Light& other) |
27 : fType(other.fType) | 28 : fType(other.fType) |
28 , fColor(other.fColor) | 29 , fColor(other.fColor) |
29 , fDirection(other.fDirection) | 30 , fDirection(other.fDirection) |
30 , fShadowMap(other.fShadowMap) { | 31 , fShadowMap(other.fShadowMap) { |
31 } | 32 } |
32 | 33 |
33 Light(Light&& other) | 34 Light(Light&& other) |
34 : fType(other.fType) | 35 : fType(other.fType) |
35 , fColor(other.fColor) | 36 , fColor(other.fColor) |
36 , fDirection(other.fDirection) | 37 , fDirection(other.fDirection) |
37 , fShadowMap(std::move(other.fShadowMap)) { | 38 , fShadowMap(std::move(other.fShadowMap)) { |
38 } | 39 } |
39 | 40 |
robertphillips
2016/08/10 16:34:37
We could have a private Light(type, color, vec3) c
| |
40 Light(const SkColor3f& color) | 41 static Light MakeAmbientLight(const SkColor3f& color) { |
41 : fType(kAmbient_LightType) | 42 Light light; |
42 , fColor(color) { | 43 light.fType = kAmbient_LightType; |
43 fDirection.set(0.0f, 0.0f, 1.0f); | 44 light.fColor = color; |
45 light.fDirection.set(0.0f, 0.0f, 1.0f); | |
46 return light; | |
44 } | 47 } |
45 | 48 |
46 Light(const SkColor3f& color, const SkVector3& dir) | 49 static Light MakeDirectionalLight(const SkColor3f& color, const SkVector 3& dir) { |
47 : fType(kDirectional_LightType) | 50 Light light; |
48 , fColor(color) | 51 light.fType = kDirectional_LightType; |
49 , fDirection(dir) { | 52 light.fColor = color; |
50 if (!fDirection.normalize()) { | 53 light.fDirection = dir; |
51 fDirection.set(0.0f, 0.0f, 1.0f); | 54 if (!light.fDirection.normalize()) { |
55 light.fDirection.set(0.0f, 0.0f, 1.0f); | |
52 } | 56 } |
57 return light; | |
58 } | |
59 | |
robertphillips
2016/08/10 16:34:37
SkPoint3 for pos. Right now they are typedefed to
| |
60 static Light MakePointLight(const SkColor3f& color, const SkVector3& pos ) { | |
61 Light light; | |
62 light.fType = kPoint_LightType; | |
63 light.fColor = color; | |
64 light.fDirection = pos; | |
65 return light; | |
53 } | 66 } |
54 | 67 |
55 LightType type() const { return fType; } | 68 LightType type() const { return fType; } |
56 const SkColor3f& color() const { return fColor; } | 69 const SkColor3f& color() const { return fColor; } |
57 const SkVector3& dir() const { | 70 const SkVector3& dir() const { |
58 SkASSERT(kAmbient_LightType != fType); | 71 SkASSERT(kDirectional_LightType == fType); |
59 return fDirection; | 72 return fDirection; |
60 } | 73 } |
robertphillips
2016/08/10 16:34:37
SkPoint3
| |
74 const SkVector3& pos() const { | |
75 SkASSERT(kPoint_LightType == fType); | |
76 return fDirection; | |
77 } | |
61 | 78 |
62 void setShadowMap(sk_sp<SkImage> shadowMap) { | 79 void setShadowMap(sk_sp<SkImage> shadowMap) { |
63 fShadowMap = std::move(shadowMap); | 80 fShadowMap = std::move(shadowMap); |
64 } | 81 } |
65 | 82 |
66 SkImage* getShadowMap() const { | 83 SkImage* getShadowMap() const { |
67 return fShadowMap.get(); | 84 return fShadowMap.get(); |
68 } | 85 } |
69 | 86 |
70 Light& operator= (const Light& b) { | 87 Light& operator= (const Light& b) { |
71 if (this == &b) { | 88 if (this == &b) { |
72 return *this; | 89 return *this; |
73 } | 90 } |
74 | 91 |
75 fColor = b.fColor; | 92 fColor = b.fColor; |
76 fType = b.fType; | 93 fType = b.fType; |
77 fDirection = b.fDirection; | 94 fDirection = b.fDirection; |
78 fShadowMap = b.fShadowMap; | 95 fShadowMap = b.fShadowMap; |
79 return *this; | 96 return *this; |
80 } | 97 } |
81 | 98 |
82 private: | 99 private: |
83 LightType fType; | 100 LightType fType; |
84 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. |
85 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). | 102 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). |
86 // If degenerate, it will be replaced with (0, 0, 1). | 103 // If degenerate, it will be replaced with (0, 0, 1). |
87 sk_sp<SkImage> fShadowMap; | 104 sk_sp<SkImage> fShadowMap; |
105 | |
106 Light() {} | |
88 }; | 107 }; |
89 | 108 |
90 class Builder { | 109 class Builder { |
91 public: | 110 public: |
92 Builder() : fLights(new SkLights) { } | 111 Builder() : fLights(new SkLights) { } |
93 | 112 |
94 void add(const Light& light) { | 113 void add(const Light& light) { |
95 if (fLights) { | 114 if (fLights) { |
96 fLights->fLights.push_back(light); | 115 fLights->fLights.push_back(light); |
97 } | 116 } |
(...skipping 27 matching lines...) Expand all Loading... | |
125 | 144 |
126 private: | 145 private: |
127 SkLights() {} | 146 SkLights() {} |
128 | 147 |
129 SkTArray<Light> fLights; | 148 SkTArray<Light> fLights; |
130 | 149 |
131 typedef SkRefCnt INHERITED; | 150 typedef SkRefCnt INHERITED; |
132 }; | 151 }; |
133 | 152 |
134 #endif | 153 #endif |
OLD | NEW |