Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #ifndef SkLights_DEFINED | 9 #ifndef SkLights_DEFINED |
| 10 #define SkLights_DEFINED | 10 #define SkLights_DEFINED |
| 11 | 11 |
| 12 #include "SkPoint3.h" | |
| 13 #include "SkRefCnt.h" | |
| 14 #include "../private/SkTArray.h" | 12 #include "../private/SkTArray.h" |
|
robertphillips
2016/08/11 14:56:18
Do we need to include SkImage or can we just prede
vjiaoblack
2016/08/11 19:18:54
Done.
| |
| 15 #include "SkImage.h" | 13 #include "SkImage.h" |
| 14 #include "SkPoint3.h" | |
|
robertphillips
2016/08/11 14:56:18
predeclare ReadBuffer and WriteBuffer and outline
vjiaoblack
2016/08/11 19:18:54
Done.
| |
| 15 #include "../../src/core/SkReadBuffer.h" | |
| 16 #include "SkRefCnt.h" | |
| 17 #include "SkWriteBuffer.h" | |
| 16 | 18 |
| 17 class SK_API SkLights : public SkRefCnt { | 19 class SK_API SkLights : public SkRefCnt { |
| 18 public: | 20 public: |
| 19 class Light { | 21 class Light { |
| 20 public: | 22 public: |
| 21 enum LightType { | 23 enum LightType { |
| 22 kAmbient_LightType, // only 'fColor' is used | 24 kAmbient_LightType, // only 'fColor' is used |
| 23 kDirectional_LightType | 25 kDirectional_LightType, |
| 26 kPoint_LightType | |
| 24 }; | 27 }; |
| 25 | 28 |
| 26 Light(const Light& other) | 29 Light(const Light& other) |
| 27 : fType(other.fType) | 30 : fType(other.fType) |
| 28 , fColor(other.fColor) | 31 , fColor(other.fColor) |
| 29 , fDirection(other.fDirection) | 32 , fDirection(other.fDirection) |
| 30 , fShadowMap(other.fShadowMap) { | 33 , fShadowMap(other.fShadowMap) { |
| 31 } | 34 } |
| 32 | 35 |
| 33 Light(Light&& other) | 36 Light(Light&& other) |
| 34 : fType(other.fType) | 37 : fType(other.fType) |
| 35 , fColor(other.fColor) | 38 , fColor(other.fColor) |
| 36 , fDirection(other.fDirection) | 39 , fDirection(other.fDirection) |
| 37 , fShadowMap(std::move(other.fShadowMap)) { | 40 , fShadowMap(std::move(other.fShadowMap)) { |
| 38 } | 41 } |
| 39 | 42 |
| 40 Light(const SkColor3f& color) | 43 static Light MakeAmbient(const SkColor3f& color) { |
| 41 : fType(kAmbient_LightType) | 44 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f)); |
| 42 , fColor(color) { | |
| 43 fDirection.set(0.0f, 0.0f, 1.0f); | |
| 44 } | 45 } |
| 45 | 46 |
| 46 Light(const SkColor3f& color, const SkVector3& dir) | 47 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) { |
| 47 : fType(kDirectional_LightType) | 48 Light light(kDirectional_LightType, color, dir); |
| 48 , fColor(color) | 49 if (!light.fDirection.normalize()) { |
| 49 , fDirection(dir) { | 50 light.fDirection.set(0.0f, 0.0f, 1.0f); |
| 50 if (!fDirection.normalize()) { | |
| 51 fDirection.set(0.0f, 0.0f, 1.0f); | |
| 52 } | 51 } |
| 52 return light; | |
| 53 } | |
| 54 | |
| 55 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) { | |
| 56 return Light(kPoint_LightType, color, pos); | |
| 53 } | 57 } |
| 54 | 58 |
| 55 LightType type() const { return fType; } | 59 LightType type() const { return fType; } |
| 56 const SkColor3f& color() const { return fColor; } | 60 const SkColor3f& color() const { return fColor; } |
| 57 const SkVector3& dir() const { | 61 const SkVector3& dir() const { |
| 58 SkASSERT(kAmbient_LightType != fType); | 62 SkASSERT(kDirectional_LightType == fType); |
| 59 return fDirection; | 63 return fDirection; |
| 60 } | 64 } |
| 65 const SkPoint3& pos() const { | |
| 66 SkASSERT(kPoint_LightType == fType); | |
| 67 return fDirection; | |
| 68 } | |
| 61 | 69 |
| 62 void setShadowMap(sk_sp<SkImage> shadowMap) { | 70 void setShadowMap(sk_sp<SkImage> shadowMap) { |
| 63 fShadowMap = std::move(shadowMap); | 71 fShadowMap = std::move(shadowMap); |
| 64 } | 72 } |
| 65 | 73 |
| 66 SkImage* getShadowMap() const { | 74 SkImage* getShadowMap() const { |
| 67 return fShadowMap.get(); | 75 return fShadowMap.get(); |
| 68 } | 76 } |
| 69 | 77 |
| 70 Light& operator= (const Light& b) { | 78 Light& operator= (const Light& b) { |
| 71 if (this == &b) { | 79 if (this == &b) { |
| 72 return *this; | 80 return *this; |
| 73 } | 81 } |
| 74 | 82 |
| 75 fColor = b.fColor; | 83 fColor = b.fColor; |
| 76 fType = b.fType; | 84 fType = b.fType; |
| 77 fDirection = b.fDirection; | 85 fDirection = b.fDirection; |
| 78 fShadowMap = b.fShadowMap; | 86 fShadowMap = b.fShadowMap; |
| 79 return *this; | 87 return *this; |
| 80 } | 88 } |
| 81 | 89 |
| 82 private: | 90 private: |
| 83 LightType fType; | 91 LightType fType; |
| 84 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. | 92 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. |
|
robertphillips
2016/08/11 14:56:18
// For directional lights, holds the direction tow
vjiaoblack
2016/08/11 19:18:54
Done.
| |
| 85 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). | 93 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). |
| 86 // If degenerate, it will be replaced with (0, 0, 1). | 94 // If degenerate, it will be replaced with (0, 0, 1). |
| 95 // For point lights, holds location of poi nt light | |
| 87 sk_sp<SkImage> fShadowMap; | 96 sk_sp<SkImage> fShadowMap; |
| 97 | |
| 98 Light(LightType type, const SkColor3f color, const SkVector3 dir) { | |
| 99 fType = type; | |
| 100 fColor = color; | |
| 101 fDirection = dir; | |
| 102 } | |
| 88 }; | 103 }; |
| 89 | 104 |
| 90 class Builder { | 105 class Builder { |
| 91 public: | 106 public: |
| 92 Builder() : fLights(new SkLights) { } | 107 Builder() : fLights(new SkLights) { } |
| 93 | 108 |
| 94 void add(const Light& light) { | 109 void add(const Light& light) { |
| 95 if (fLights) { | 110 if (fLights) { |
| 96 fLights->fLights.push_back(light); | 111 fLights->fLights.push_back(light); |
| 97 } | 112 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 115 return fLights.count(); | 130 return fLights.count(); |
| 116 } | 131 } |
| 117 | 132 |
| 118 const Light& light(int index) const { | 133 const Light& light(int index) const { |
| 119 return fLights[index]; | 134 return fLights[index]; |
| 120 } | 135 } |
| 121 | 136 |
| 122 Light& light(int index) { | 137 Light& light(int index) { |
| 123 return fLights[index]; | 138 return fLights[index]; |
| 124 } | 139 } |
| 125 | 140 |
|
robertphillips
2016/08/11 14:56:18
The pattern seems to be:
static sk_sp<SkLights> M
vjiaoblack
2016/08/11 19:18:53
Done.
| |
| 141 bool unFlatten(SkReadBuffer& buf) { | |
| 142 int numLights = buf.readInt(); | |
| 143 fLights.reset(); | |
| 144 fLights.reserve(numLights); | |
| 145 | |
| 146 for (int l = 0; l < numLights; ++l) { | |
| 147 bool isAmbient = buf.readBool(); | |
| 148 bool isPoint = buf.readBool(); | |
| 149 | |
| 150 SkColor3f color; | |
| 151 if (!buf.readScalarArray(&color.fX, 3)) { | |
| 152 return false; | |
| 153 } | |
| 154 | |
| 155 if (isAmbient) { | |
| 156 fLights[l] = Light::MakeAmbient(color); | |
| 157 } else { | |
| 158 SkVector3 dirOrPos; | |
| 159 if (!buf.readScalarArray(&dirOrPos.fX, 3)) { | |
| 160 return false; | |
| 161 } | |
| 162 if (isPoint) { | |
| 163 fLights[l] = Light::MakePoint(color, dirOrPos); | |
| 164 } else { | |
| 165 fLights[l] = Light::MakeDirectional(color, dirOrPos); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 return true; | |
| 171 } | |
| 172 | |
|
robertphillips
2016/08/11 14:56:18
Are you intentionally not serializing/deserializin
vjiaoblack
2016/08/11 19:18:53
Done.
| |
| 173 void flatten(SkWriteBuffer& buf) const { | |
| 174 | |
| 175 buf.writeInt(this->numLights()); | |
| 176 for (int l = 0; l < this->numLights(); ++l) { | |
| 177 const Light& light = this->light(l); | |
| 178 | |
| 179 bool isAmbient = Light::kAmbient_LightType == light.type(); | |
| 180 bool isPoint = Light::kPoint_LightType == light.type(); | |
| 181 | |
| 182 buf.writeBool(isAmbient); | |
| 183 buf.writeBool(isPoint); | |
| 184 buf.writeScalarArray(&light.color().fX, 3); | |
| 185 if (!isAmbient) { | |
| 186 buf.writeScalarArray(&light.dir().fX, 3); | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 | |
| 126 private: | 191 private: |
| 127 SkLights() {} | 192 SkLights() {} |
| 128 | 193 |
| 129 SkTArray<Light> fLights; | 194 SkTArray<Light> fLights; |
| 130 | 195 |
| 131 typedef SkRefCnt INHERITED; | 196 typedef SkRefCnt INHERITED; |
| 132 }; | 197 }; |
| 133 | 198 |
| 134 #endif | 199 #endif |
| OLD | NEW |