aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkLights.h
blob: 0b23cc14aa01fa742133e5dfdf332f24d8e6a334 (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

/*
 * 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 "SkPoint3.h"
#include "SkRefCnt.h"
#include "../private/SkTDArray.h"
#include "SkImage.h"

class SK_API SkLights  : public SkRefCnt {
public:
    class Light {
    public:
        enum LightType {
            kAmbient_LightType,       // only 'fColor' is used
            kDirectional_LightType
        };

        Light(const SkColor3f& color)
            : fType(kAmbient_LightType)
            , fColor(color) {
            fDirection.set(0.0f, 0.0f, 1.0f);
            fShadowMap.reset(nullptr);
        }

        Light(const SkColor3f& color, const SkVector3& dir)
            : fType(kDirectional_LightType)
            , fColor(color)
            , fDirection(dir) {
            if (!fDirection.normalize()) {
                fDirection.set(0.0f, 0.0f, 1.0f);
            }
            fShadowMap.reset(nullptr);
        }

        LightType type() const { return fType; }
        const SkColor3f& color() const { return fColor; }
        const SkVector3& dir() const { 
            SkASSERT(kAmbient_LightType != fType);
            return fDirection; 
        }

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

        sk_sp<SkImage> getShadowMap() const {
            return fShadowMap;
        }

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

            this->fColor = b.fColor;
            this->fType = b.fType;
            this->fDirection = b.fDirection;

            if (b.fShadowMap) {
                this->fShadowMap = b.fShadowMap;
            }

            return *this;
        }

    private:
        LightType   fType;
        SkColor3f   fColor;           // linear (unpremul) color. Range is 0..1 in each channel.
        SkVector3   fDirection;       // direction towards the light (+Z is out of the screen).
                                      // If degenerate, it will be replaced with (0, 0, 1).
        sk_sp<SkImage> fShadowMap;
    };

    class Builder {
    public:
        Builder() : fLights(new SkLights) { }
    
        void add(const Light& light) {
            if (fLights) {
                (void) fLights->fLights.append(1, &light);
            }
        }

        sk_sp<SkLights> finish() {
            return 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];
    }

private:
    SkLights() {}

    SkTDArray<Light> fLights;
};

#endif