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" |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 9 | #include "SkLightingShader.h" |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 10 | #include "SkNormalSource.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 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 14 | // Create a hemispherical normal map |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 15 | static SkBitmap make_hemi_normalmap(int texSize) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 16 | SkBitmap hemi; |
| 17 | hemi.allocN32Pixels(texSize, texSize); |
| 18 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 19 | sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSize)); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 20 | return hemi; |
| 21 | } |
| 22 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 23 | // Create a truncated pyramid normal map |
| 24 | static SkBitmap make_frustum_normalmap(int texSize) { |
| 25 | SkBitmap frustum; |
| 26 | frustum.allocN32Pixels(texSize, texSize); |
| 27 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 28 | sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize)); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 29 | return frustum; |
| 30 | } |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 31 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 32 | // Create a tetrahedral normal map |
| 33 | static SkBitmap make_tetra_normalmap(int texSize) { |
| 34 | SkBitmap tetra; |
| 35 | tetra.allocN32Pixels(texSize, texSize); |
| 36 | |
| 37 | sk_tool_utils::create_tetra_normal_map(&tetra, SkIRect::MakeWH(texSize, texSize)); |
| 38 | return tetra; |
| 39 | } |
| 40 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 41 | namespace skiagm { |
| 42 | |
| 43 | // This GM exercises lighting shaders. |
| 44 | class LightingShaderGM : public GM { |
| 45 | public: |
| 46 | LightingShaderGM() { |
| 47 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 48 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 49 | SkLights::Builder builder; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 50 | |
vjiaoblack | 772b5ee | 2016-08-12 11:38:47 -0700 | [diff] [blame] | 51 | builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 52 | SkVector3::Make(SK_ScalarRoot2Over2, |
| 53 | 0.0f, |
| 54 | SK_ScalarRoot2Over2))); |
vjiaoblack | a8eabc4 | 2016-08-29 10:22:09 -0700 | [diff] [blame] | 55 | builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 56 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 57 | fLights = builder.finish(); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 61 | enum NormalMap { |
| 62 | kHemi_NormalMap, |
| 63 | kFrustum_NormalMap, |
| 64 | kTetra_NormalMap, |
| 65 | |
| 66 | kLast_NormalMap = kTetra_NormalMap |
| 67 | }; |
| 68 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 69 | static constexpr int kNormalMapCount = kLast_NormalMap+1; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 70 | |
| 71 | SkString onShortName() override { |
| 72 | return SkString("lightingshader"); |
| 73 | } |
| 74 | |
| 75 | SkISize onISize() override { |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 76 | return SkISize::Make(kGMSize, kGMSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void onOnceBeforeDraw() override { |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 80 | fDiffuse = sk_tool_utils::create_checkerboard_bitmap( |
| 81 | kTexSize, kTexSize, |
| 82 | sk_tool_utils::color_to_565(0x0), |
| 83 | sk_tool_utils::color_to_565(0xFF804020), |
| 84 | 8); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 85 | |
| 86 | fNormalMaps[kHemi_NormalMap] = make_hemi_normalmap(kTexSize); |
| 87 | fNormalMaps[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize); |
| 88 | fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 89 | } |
| 90 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 91 | void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 92 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 93 | SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height()); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 94 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 95 | SkMatrix matrix; |
| 96 | matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 97 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 98 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 99 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 100 | SkPaint paint; |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 101 | sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(fDiffuse, |
| 102 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix); |
| 103 | sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(fNormalMaps[mapType], |
| 104 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix); |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 105 | sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), |
| 106 | ctm); |
dvonbeck | 6af677f | 2016-07-10 18:38:33 -0700 | [diff] [blame] | 107 | paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource), |
| 108 | fLights)); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 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 { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 114 | SkMatrix m; |
| 115 | SkRect r; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 116 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 117 | { |
| 118 | r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); |
| 119 | this->drawRect(canvas, r, kHemi_NormalMap); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 120 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 121 | canvas->save(); |
| 122 | m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 123 | m.postTranslate(kGMSize/2.0f - kTexSize/2.0f, 0.0f); |
| 124 | canvas->setMatrix(m); |
| 125 | this->drawRect(canvas, r, kHemi_NormalMap); |
| 126 | canvas->restore(); |
| 127 | } |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 128 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 129 | { |
| 130 | r.offset(kGMSize - kTexSize, 0); |
| 131 | this->drawRect(canvas, r, kFrustum_NormalMap); |
| 132 | |
| 133 | canvas->save(); |
| 134 | m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 135 | m.postTranslate(0.0f, kGMSize/2.0f - kTexSize/2.0f); |
| 136 | canvas->setMatrix(m); |
| 137 | this->drawRect(canvas, r, kFrustum_NormalMap); |
| 138 | canvas->restore(); |
| 139 | } |
| 140 | |
| 141 | { |
| 142 | r.offset(0, kGMSize - kTexSize); |
| 143 | this->drawRect(canvas, r, kTetra_NormalMap); |
| 144 | |
| 145 | canvas->save(); |
| 146 | m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 147 | m.postTranslate(-kGMSize/2.0f + kTexSize/2.0f, 0.0f); |
| 148 | canvas->setMatrix(m); |
| 149 | this->drawRect(canvas, r, kTetra_NormalMap); |
| 150 | canvas->restore(); |
| 151 | } |
| 152 | |
| 153 | { |
| 154 | r.offset(kTexSize - kGMSize, 0); |
| 155 | this->drawRect(canvas, r, kHemi_NormalMap); |
| 156 | |
| 157 | canvas->save(); |
| 158 | m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 159 | m.postTranslate(0.0f, -kGMSize/2.0f + kTexSize/2.0f); |
| 160 | canvas->setMatrix(m); |
| 161 | this->drawRect(canvas, r, kHemi_NormalMap); |
| 162 | canvas->restore(); |
| 163 | } |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 164 | } |
| 165 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 166 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 167 | static constexpr int kTexSize = 128; |
| 168 | static constexpr int kGMSize = 512; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 169 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 170 | SkBitmap fDiffuse; |
| 171 | SkBitmap fNormalMaps[kNormalMapCount]; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame] | 172 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 173 | sk_sp<SkLights> fLights; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 174 | |
| 175 | typedef GM INHERITED; |
| 176 | }; |
| 177 | |
| 178 | ////////////////////////////////////////////////////////////////////////////// |
| 179 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 180 | DEF_GM(return new LightingShaderGM;) |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 181 | } |