Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Unified Diff: src/core/SkLights.cpp

Issue 2237493002: Added PointLights to SkLights::Light (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Added SkLights.cpp Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkLights.cpp
diff --git a/src/core/SkLights.cpp b/src/core/SkLights.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5558cb09c94c9821be861859e74f19fa9393160e
--- /dev/null
+++ b/src/core/SkLights.cpp
@@ -0,0 +1,105 @@
+
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkLights.h"
+#include "SkReadBuffer.h"
+
+SkLights::Light::Light(LightType type, const SkColor3f color, const SkVector3 dir) {
+ fType = type;
+ fColor = color;
+ fDirection = dir;
+}
+
+void SkLights::Builder::add(const SkLights::Light& light) {
+ if (fLights) {
+ fLights->fLights.push_back(light);
+ }
+}
+
+void SkLights::Builder::add(SkLights::Light&& light) {
+ if (fLights) {
+ fLights->fLights.push_back(std::move(light));
+ }
+}
+
+sk_sp<SkLights> SkLights::Builder::finish() {
+ return std::move(fLights);
+}
+
+int SkLights::numLights() const {
+ return fLights.count();
+}
+
+const SkLights::Light& SkLights::light(int index) const {
+ return fLights[index];
+}
+
+SkLights::Light& SkLights::light(int index) {
+ return fLights[index];
+}
+
+sk_sp<SkLights> SkLights::MakeFromBuffer(SkReadBuffer& buf) {
+ int numLights = buf.readInt();
+
+ Builder builder;
+ for (int l = 0; l < numLights; ++l) {
+ bool isAmbient = buf.readBool();
+ bool isPoint = buf.readBool();
+
+ SkColor3f color;
+ if (!buf.readScalarArray(&color.fX, 3)) {
+ return nullptr;
+ }
+
+ if (isAmbient) {
+ builder.add(Light::MakeAmbient(color));
+ } else {
+ SkVector3 dirOrPos;
+ if (!buf.readScalarArray(&dirOrPos.fX, 3)) {
+ return nullptr;
+ }
+
+ sk_sp<SkImage> depthMap;
robertphillips 2016/08/12 13:34:48 Not all lights need to have a shadow map!
vjiaoblack 2016/08/12 14:12:10 Done.
+ if (!(depthMap = sk_ref_sp<SkImage>(buf.readImage()))) {
+ return nullptr;
+ }
+
+ if (isPoint) {
+ Light light = Light::MakePoint(color, dirOrPos);
+ light.setShadowMap(depthMap);
+ builder.add(light);
+ } else {
+ Light light = Light::MakeDirectional(color, dirOrPos);
+ light.setShadowMap(depthMap);
+ builder.add(light);
+ }
+ }
+ }
+
+ return builder.finish();
+}
+
+void SkLights::flatten(SkWriteBuffer& buf) const {
+
+ buf.writeInt(this->numLights());
+ for (int l = 0; l < this->numLights(); ++l) {
+ const Light& light = this->light(l);
+
+ bool isAmbient = Light::kAmbient_LightType == light.type();
+ bool isPoint = Light::kPoint_LightType == light.type();
+
+ buf.writeBool(isAmbient);
+ buf.writeBool(isPoint);
+ buf.writeScalarArray(&light.color().fX, 3);
+ if (!isAmbient) {
+ buf.writeScalarArray(&light.dir().fX, 3);
+ }
+
+ buf.writeImage(light.getShadowMap());
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698