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