Index: gm/lightingshader2.cpp |
diff --git a/gm/lightingshader2.cpp b/gm/lightingshader2.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..58c5a638900a84307680d54b76cbe34867e4765a |
--- /dev/null |
+++ b/gm/lightingshader2.cpp |
@@ -0,0 +1,153 @@ |
+/* |
+ * 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 "gm.h" |
+ |
+#include "SkBitmapProcShader.h" |
+#include "SkLightingShader.h" |
+#include "SkNormalSource.h" |
+#include "SkPoint3.h" |
+#include "SkShader.h" |
+ |
+// Create a truncated pyramid normal map |
+static SkBitmap make_frustum_normalmap(int texSize) { |
+ SkBitmap frustum; |
+ frustum.allocN32Pixels(texSize, texSize); |
+ |
+ sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize)); |
+ return frustum; |
+} |
+ |
+namespace skiagm { |
+ |
+// This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using |
+// normal maps, and paint transparency. |
+class LightingShader2GM : public GM { |
+public: |
+ LightingShader2GM() { |
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
+ } |
+ |
+protected: |
+ SkString onShortName() override { |
+ return SkString("lightingshader2"); |
+ } |
+ |
+ SkISize onISize() override { |
+ return SkISize::Make(kGMSize, kGMSize); |
+ } |
+ |
+ void onOnceBeforeDraw() override { |
+ SkLights::Builder builder; |
+ const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f); |
+ |
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
+ kLightFromUpperRight)); |
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
+ fLights = builder.finish(); |
+ |
+ fDiffuse = sk_tool_utils::create_checkerboard_bitmap( |
+ kTexSize, kTexSize, |
+ sk_tool_utils::color_to_565(0x0), |
+ sk_tool_utils::color_to_565(0xFF804020), |
+ 8); |
+ |
+ fNormalMap = make_frustum_normalmap(kTexSize); |
+ } |
+ |
+ // Scales shape around origin, rotates shape around origin, then translates shape to GM center |
+ void positionCTM(SkCanvas *canvas, SkScalar scale, SkScalar rotate) const { |
+ canvas->translate(kGMSize / 2.0f, kGMSize / 2.0f); |
+ canvas->scale(scale, scale); |
+ canvas->rotate(rotate); |
+ canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f); |
+ } |
+ |
+ void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scale, |
+ SkScalar rotate, bool useNormalSource, bool useDiffuseShader, |
+ bool useTransparentPaint) { |
+ this->positionCTM(canvas, scale, rotate); |
+ |
+ SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height()); |
+ |
+ SkMatrix matrix; |
+ matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
+ |
+ const SkMatrix& ctm = canvas->getTotalMatrix(); |
+ |
+ SkPaint paint; |
+ sk_sp<SkNormalSource> normalSource = nullptr; |
+ sk_sp<SkShader> diffuseShader = nullptr; |
+ |
+ if (useNormalSource) { |
+ sk_sp <SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kClamp_TileMode, |
+ SkShader::kClamp_TileMode, &matrix, |
+ nullptr); |
+ normalSource = SkNormalSource::MakeFromNormalMap( |
+ std::move(normalMap), |
+ ctm); |
+ } |
+ if (useDiffuseShader) { |
+ diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::kClamp_TileMode, |
+ SkShader::kClamp_TileMode, &matrix, nullptr); |
+ } else { |
+ paint.setColor(0xFF00FF00); |
+ } |
+ if (useTransparentPaint) { |
egdaniel
2016/07/14 17:10:35
Let's also add a case for when we have a diffuse s
dvonbeck
2016/07/19 21:31:12
Done.
|
+ paint.setAlpha(0x55); |
+ } |
+ paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource), |
+ fLights)); |
+ |
+ canvas->drawRect(r, paint); |
+ } |
+ |
+ void onDraw(SkCanvas* canvas) override { |
+ SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); |
egdaniel
2016/07/14 17:10:35
So I think what would give us the best coverage wo
dvonbeck
2016/07/19 21:31:12
Done.
|
+ |
+ canvas->save(); |
+ canvas->translate(0.0f, -SkScalarSqrt(2.0f) * kTexSize); |
+ this->drawRect(canvas, r, 1.0f, 45.f, true, true, true); |
+ canvas->restore(); |
+ |
+ canvas->save(); |
+ canvas->translate(-SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize); |
+ this->drawRect(canvas, r, 1.2f, 0.f, true, true, false); |
+ canvas->restore(); |
+ |
+ canvas->save(); |
+ canvas->translate(SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize); |
+ this->drawRect(canvas, r, 0.5f, 0.f, true , true, false); |
+ canvas->restore(); |
+ |
+ canvas->save(); |
+ canvas->translate(-SK_ScalarSqrt2 * kTexSize, 0.0f); |
+ this->drawRect(canvas, r, 1.0f, 0.f, true, false, true); |
+ canvas->restore(); |
+ |
+ canvas->save(); |
+ canvas->translate(SK_ScalarSqrt2 * kTexSize, 0.0f); |
+ this->drawRect(canvas, r, 1.0f, 0.f, false, true, false); |
+ canvas->restore(); |
+ } |
+ |
+private: |
+ static const int kTexSize = 128; |
+ static const int kGMSize = 512; |
+ |
+ SkBitmap fDiffuse; |
+ SkBitmap fNormalMap; |
+ |
+ sk_sp<SkLights> fLights; |
+ |
+ typedef GM INHERITED; |
+}; |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+DEF_GM(return new LightingShader2GM;) |
+} |