robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 10 | #include "SkLightingShader.h" |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 11 | #include "SkPoint3.h" |
| 12 | #include "SkShader.h" |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 13 | |
| 14 | static SkBitmap make_checkerboard(int texSize) { |
| 15 | SkBitmap bitmap; |
| 16 | bitmap.allocN32Pixels(texSize, texSize); |
| 17 | |
| 18 | SkCanvas canvas(bitmap); |
| 19 | sk_tool_utils::draw_checkerboard(&canvas, |
| 20 | sk_tool_utils::color_to_565(0x0), |
| 21 | sk_tool_utils::color_to_565(0xFF804020), |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 22 | 2); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 23 | return bitmap; |
| 24 | } |
| 25 | |
| 26 | // Create a hemispherical normal map |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 27 | static SkBitmap make_hemi_normalmap(int texSize) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 28 | SkBitmap hemi; |
| 29 | hemi.allocN32Pixels(texSize, texSize); |
| 30 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 31 | sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSize)); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 32 | return hemi; |
| 33 | } |
| 34 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 35 | // Create a truncated pyramid normal map |
| 36 | static SkBitmap make_frustum_normalmap(int texSize) { |
| 37 | SkBitmap frustum; |
| 38 | frustum.allocN32Pixels(texSize, texSize); |
| 39 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 40 | sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize)); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 41 | return frustum; |
| 42 | } |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 43 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 44 | // Create a tetrahedral normal map |
| 45 | static SkBitmap make_tetra_normalmap(int texSize) { |
| 46 | SkBitmap tetra; |
| 47 | tetra.allocN32Pixels(texSize, texSize); |
| 48 | |
| 49 | sk_tool_utils::create_tetra_normal_map(&tetra, SkIRect::MakeWH(texSize, texSize)); |
| 50 | return tetra; |
| 51 | } |
| 52 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 53 | namespace skiagm { |
| 54 | |
| 55 | // This GM exercises lighting shaders. |
| 56 | class LightingShaderGM : public GM { |
| 57 | public: |
| 58 | LightingShaderGM() { |
| 59 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 60 | |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 61 | fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 62 | fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 63 | |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 64 | fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | protected: |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 68 | enum NormalMap { |
| 69 | kHemi_NormalMap, |
| 70 | kFrustum_NormalMap, |
| 71 | kTetra_NormalMap, |
| 72 | |
| 73 | kLast_NormalMap = kTetra_NormalMap |
| 74 | }; |
| 75 | |
| 76 | static const int kNormalMapCount = kLast_NormalMap+1; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 77 | |
| 78 | SkString onShortName() override { |
| 79 | return SkString("lightingshader"); |
| 80 | } |
| 81 | |
| 82 | SkISize onISize() override { |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 83 | return SkISize::Make(kGMSize, kGMSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void onOnceBeforeDraw() override { |
| 87 | fDiffuse = make_checkerboard(kTexSize); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 88 | |
| 89 | fNormalMaps[kHemi_NormalMap] = make_hemi_normalmap(kTexSize); |
| 90 | fNormalMaps[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize); |
| 91 | fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 92 | } |
| 93 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 94 | void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 95 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 96 | SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height()); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 97 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 98 | SkMatrix matrix; |
| 99 | matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
| 100 | |
| 101 | SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
| 102 | fDiffuse, |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 103 | fNormalMaps[mapType], |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 104 | fLight, fAmbient, |
| 105 | &matrix)); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 106 | |
| 107 | SkPaint paint; |
| 108 | paint.setShader(fShader); |
| 109 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 110 | canvas->drawRect(r, paint); |
| 111 | } |
| 112 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 113 | void onDraw(SkCanvas* canvas) override { |
| 114 | SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 115 | this->drawRect(canvas, r, kHemi_NormalMap); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 116 | |
| 117 | r.offset(kGMSize - kTexSize, 0); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 118 | this->drawRect(canvas, r, kFrustum_NormalMap); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 119 | |
| 120 | r.offset(0, kGMSize - kTexSize); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 121 | this->drawRect(canvas, r, kTetra_NormalMap); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 122 | |
| 123 | r.offset(kTexSize - kGMSize, 0); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 124 | this->drawRect(canvas, r, kHemi_NormalMap); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 125 | } |
| 126 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 127 | private: |
| 128 | static const int kTexSize = 128; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 129 | static const int kGMSize = 512; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 130 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 131 | SkBitmap fDiffuse; |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame^] | 132 | SkBitmap fNormalMaps[kNormalMapCount]; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 133 | |
| 134 | SkLightingShader::Light fLight; |
robertphillips | a9652ac | 2015-07-31 05:17:24 -0700 | [diff] [blame] | 135 | SkColor3f fAmbient; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 136 | |
| 137 | typedef GM INHERITED; |
| 138 | }; |
| 139 | |
| 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | DEF_GM( return SkNEW(LightingShaderGM); ) |
| 143 | |
| 144 | } |