skia2/include/core/SkLights.h
vjiaoblack 84cddf6fa7 Revert of Moved ambient lights out of SkLight's light array (patchset #7 id:120001 of https://codereview.chromium.org/2287553002/ )
Reason for revert:
Made Deigo's GM miss their ambient lights

Original issue's description:
> Moved ambient lights out of SkLight's light array
>
> BUG=skia:
> GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2287553002
>
> Committed: https://skia.googlesource.com/skia/+/8f98f0aa2d3f7571a890b916c7c4b5ee831e9686

TBR=robertphillips@google.com,djsollen@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review-Url: https://codereview.chromium.org/2291663002
2016-08-29 08:38:04 -07:00

181 lines
5.0 KiB
C++

/*
* 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 {
kAmbient_LightType, // only 'fColor' is used
kDirectional_LightType,
kPoint_LightType
};
Light(const Light& other)
: fType(other.fType)
, fColor(other.fColor)
, fDirOrPos(other.fDirOrPos)
, fIntensity(other.fIntensity)
, fShadowMap(other.fShadowMap) {
}
Light(Light&& other)
: fType(other.fType)
, fColor(other.fColor)
, fDirOrPos(other.fDirOrPos)
, fIntensity(other.fIntensity)
, fShadowMap(std::move(other.fShadowMap)) {
}
static Light MakeAmbient(const SkColor3f& color) {
return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f));
}
static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir) {
Light light(kDirectional_LightType, color, dir);
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) {
return Light(kPoint_LightType, color, pos, intensity);
}
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();
}
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;
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);
}
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;
Light(LightType type, const SkColor3f& color,
const SkVector3& dir, SkScalar intensity = 0.0f) {
fType = type;
fColor = color;
fDirOrPos = dir;
fIntensity = intensity;
}
};
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));
}
}
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];
}
static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
void flatten(SkWriteBuffer& buf) const;
private:
SkLights() {}
SkTArray<Light> fLights;
typedef SkRefCnt INHERITED;
};
#endif