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