2016-05-31 13:56:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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"
|
2016-07-21 17:25:54 +00:00
|
|
|
#include "SkImage.h"
|
2016-05-31 13:56:28 +00:00
|
|
|
|
|
|
|
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);
|
2016-07-21 17:25:54 +00:00
|
|
|
fShadowMap.reset(nullptr);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2016-07-21 17:25:54 +00:00
|
|
|
fShadowMap.reset(nullptr);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LightType type() const { return fType; }
|
|
|
|
const SkColor3f& color() const { return fColor; }
|
|
|
|
const SkVector3& dir() const {
|
|
|
|
SkASSERT(kAmbient_LightType != fType);
|
|
|
|
return fDirection;
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:25:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-31 13:56:28 +00:00
|
|
|
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).
|
2016-07-21 17:25:54 +00:00
|
|
|
sk_sp<SkImage> fShadowMap;
|
2016-05-31 13:56:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
Builder() : fLights(new SkLights) { }
|
|
|
|
|
|
|
|
void add(const Light& light) {
|
|
|
|
if (fLights) {
|
2016-07-21 17:25:54 +00:00
|
|
|
(void) fLights->fLights.append(1, &light);
|
2016-05-31 13:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 17:12:08 +00:00
|
|
|
sk_sp<SkLights> finish() {
|
2016-05-31 13:56:28 +00:00
|
|
|
return fLights;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<SkLights> fLights;
|
|
|
|
};
|
|
|
|
|
|
|
|
int numLights() const {
|
|
|
|
return fLights.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Light& light(int index) const {
|
|
|
|
return fLights[index];
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:25:54 +00:00
|
|
|
Light& light(int index) {
|
|
|
|
return fLights[index];
|
|
|
|
}
|
|
|
|
|
2016-05-31 13:56:28 +00:00
|
|
|
private:
|
|
|
|
SkLights() {}
|
|
|
|
|
|
|
|
SkTDArray<Light> fLights;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|