aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkLights.h
blob: 954168de4d1ede3838f312e203b5bf1e192debab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkLights_DEFINED
#define SkLights_DEFINED

#include "../private/SkTArray.h"
#include "SkPoint3.h"
#include "SkRefCnt.h"

class SkReadBuffer;
class SkWriteBuffer;
class SkImage;

class SK_API SkLights  : public SkRefCnt {
public:
    class Light {
    public:
        enum LightType {
            kDirectional_LightType,
            kPoint_LightType
        };

        Light(const Light& other)
            : fType(other.fType)
            , fColor(other.fColor)
            , fDirOrPos(other.fDirOrPos)
            , fIntensity(other.fIntensity)
            , fShadowMap(other.fShadowMap)
            , fIsRadial(other.fIsRadial) {
        }

        Light(Light&& other)
            : fType(other.fType)
            , fColor(other.fColor)
            , fDirOrPos(other.fDirOrPos)
            , fIntensity(other.fIntensity)
            , fShadowMap(std::move(other.fShadowMap))
            , fIsRadial(other.fIsRadial)  {
        }

        static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir,
                                     bool isRadial = false) {
            Light light(kDirectional_LightType, color, dir, isRadial);
            if (!light.fDirOrPos.normalize()) {
                light.fDirOrPos.set(0.0f, 0.0f, 1.0f);
            }
            return light;
        }

        static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity,
                               bool isRadial = false) {
            return Light(kPoint_LightType, color, pos, intensity, isRadial);
        }

        LightType type() const { return fType; }
        const SkColor3f& color() const { return fColor; }
        const SkVector3& dir() const {
            SkASSERT(kDirectional_LightType == fType);
            return fDirOrPos;
        }
        const SkPoint3& pos() const {
            SkASSERT(kPoint_LightType == fType);
            return fDirOrPos;
        }
        SkScalar intensity() const {
            SkASSERT(kPoint_LightType == fType);
            return fIntensity;
        }

        void setShadowMap(sk_sp<SkImage> shadowMap) {
            fShadowMap = std::move(shadowMap);
        }

        SkImage* getShadowMap() const {
            return fShadowMap.get();
        }

        bool isRadial() const { return fIsRadial; }

        Light& operator= (const Light& b) {
            if (this == &b) {
                return *this;
            }

            fColor = b.fColor;
            fType = b.fType;
            fDirOrPos = b.fDirOrPos;
            fIntensity = b.fIntensity;
            fShadowMap = b.fShadowMap;
            fIsRadial = b.fIsRadial;
            return *this;
        }

        bool operator== (const Light& b) {
            if (this == &b) {
                return true;
            }

            return (fColor     == b.fColor) &&
                   (fType      == b.fType) &&
                   (fDirOrPos  == b.fDirOrPos) &&
                   (fShadowMap == b.fShadowMap) &&
                   (fIntensity == b.fIntensity) &&
                   (fIsRadial  == b.fIsRadial);
        }

        bool operator!= (const Light& b) { return !(this->operator==(b)); }

    private:
        LightType   fType;
        SkColor3f   fColor;           // linear (unpremul) color. Range is 0..1 in each channel.

        SkVector3   fDirOrPos;        // For directional lights, holds the direction towards the
                                      // light (+Z is out of the screen).
                                      // If degenerate, it will be replaced with (0, 0, 1).
                                      // For point lights, holds location of point light

        SkScalar    fIntensity;       // For point lights, dictates the light intensity.
                                      // Simply a multiplier to the final light output value.
        sk_sp<SkImage> fShadowMap;
        bool        fIsRadial;        // Whether the light is radial or not. Radial lights will
                                      // cast shadows and lights radially outwards.

        Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos,
              SkScalar intensity = 0.0f, bool isRadial = false) {
            fType = type;
            fColor = color;
            fDirOrPos = dirOrPos;
            fIntensity = intensity;
            fIsRadial = isRadial;
        }
    };

    class Builder {
    public:
        Builder() : fLights(new SkLights) {}

        void add(const Light& light) {
            if (fLights) {
                fLights->fLights.push_back(light);
            }
        }

        void add(Light&& light) {
            if (fLights) {
                fLights->fLights.push_back(std::move(light));
            }
        }

        void setAmbientLightColor(const SkColor3f& color) {
            if (fLights) {
                fLights->fAmbientLightColor = color;
            }
        }

        sk_sp<SkLights> finish() {
            return std::move(fLights);
        }

    private:
        sk_sp<SkLights> fLights;
    };

    int numLights() const {
        return fLights.count();
    }

    const Light& light(int index) const {
        return fLights[index];
    }

    Light& light(int index) {
        return fLights[index];
    }

    const SkColor3f& ambientLightColor() const {
        return fAmbientLightColor;
    }

    static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);

    void flatten(SkWriteBuffer& buf) const;

private:
    SkLights() {
        fAmbientLightColor.set(0.0f, 0.0f, 0.0f);
    }
    SkTArray<Light> fLights;
    SkColor3f fAmbientLightColor;
    typedef SkRefCnt INHERITED;
};

#endif