Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkLights.h
1 
2 /*
3  * Copyright 2015 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef SkLights_DEFINED
10 #define SkLights_DEFINED
11 
12 #include "../private/SkTArray.h"
13 #include "SkPoint3.h"
14 #include "SkRefCnt.h"
15 
16 class SkReadBuffer;
17 class SkWriteBuffer;
18 class SkImage;
19 
20 class SK_API SkLights : public SkRefCnt {
21 public:
22  class Light {
23  public:
24  enum LightType {
25  kAmbient_LightType, // only 'fColor' is used
26  kDirectional_LightType,
27  kPoint_LightType
28  };
29 
30  Light(const Light& other)
31  : fType(other.fType)
32  , fColor(other.fColor)
33  , fDirection(other.fDirection)
34  , fShadowMap(other.fShadowMap) {
35  }
36 
37  Light(Light&& other)
38  : fType(other.fType)
39  , fColor(other.fColor)
40  , fDirection(other.fDirection)
41  , fShadowMap(std::move(other.fShadowMap)) {
42  }
43 
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& dir) {
49  Light light(kDirectional_LightType, color, dir);
50  if (!light.fDirection.normalize()) {
51  light.fDirection.set(0.0f, 0.0f, 1.0f);
52  }
53  return light;
54  }
55 
56  static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) {
57  return Light(kPoint_LightType, color, pos);
58  }
59 
60  LightType type() const { return fType; }
61  const SkColor3f& color() const { return fColor; }
62  const SkVector3& dir() const {
63  SkASSERT(kDirectional_LightType == fType);
64  return fDirection;
65  }
66  const SkPoint3& pos() const {
67  SkASSERT(kPoint_LightType == fType);
68  return fDirection;
69  }
70 
71  void setShadowMap(sk_sp<SkImage> shadowMap) {
72  fShadowMap = std::move(shadowMap);
73  }
74 
75  SkImage* getShadowMap() const {
76  return fShadowMap.get();
77  }
78 
79  Light& operator= (const Light& b) {
80  if (this == &b) {
81  return *this;
82  }
83 
84  fColor = b.fColor;
85  fType = b.fType;
86  fDirection = b.fDirection;
87  fShadowMap = b.fShadowMap;
88  return *this;
89  }
90 
91  bool operator== (const Light& b) {
92  if (this == &b) {
93  return true;
94  }
95 
96  return (fColor == b.fColor) &&
97  (fType == b.fType) &&
98  (fDirection == b.fDirection) &&
99  (fShadowMap == b.fShadowMap);
100  }
101 
102  bool operator!= (const Light& b) { return !(this->operator==(b)); }
103 
104  private:
105  LightType fType;
106  SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
107  SkVector3 fDirection; // For directional lights, holds the direction towards the
108  // light (+Z is out of the screen).
109  // If degenerate, it will be replaced with (0, 0, 1).
110  // For point lights, holds location of point light
111  sk_sp<SkImage> fShadowMap;
112 
113  Light(LightType type, const SkColor3f& color, const SkVector3& dir) {
114  fType = type;
115  fColor = color;
116  fDirection = dir;
117  }
118  };
119 
120  class Builder {
121  public:
122  Builder() : fLights(new SkLights) { }
123 
124  void add(const Light& light) {
125  if (fLights) {
126  fLights->fLights.push_back(light);
127  }
128  }
129 
130  void add(Light&& light) {
131  if (fLights) {
132  fLights->fLights.push_back(std::move(light));
133  }
134  }
135 
136  sk_sp<SkLights> finish() {
137  return std::move(fLights);
138  }
139 
140  private:
141  sk_sp<SkLights> fLights;
142  };
143 
144  int numLights() const {
145  return fLights.count();
146  }
147 
148  const Light& light(int index) const {
149  return fLights[index];
150  }
151 
152  Light& light(int index) {
153  return fLights[index];
154  }
155 
156  static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
157 
158  void flatten(SkWriteBuffer& buf) const;
159 
160 private:
161  SkLights() {}
162  SkTArray<Light> fLights;
163  typedef SkRefCnt INHERITED;
164 };
165 
166 #endif
Definition: SkPoint3.h:13
Definition: SkRefCnt.h:135
Definition: SkLights.h:20
Definition: SkLights.h:120
Definition: SkLights.h:22
bool normalize()
Set the point (vector) to be unit-length in the same direction as it already points.
Definition: SkWriteBuffer.h:26
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:45