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 | |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkLightingShader.h" |
| 12 | |
| 13 | static SkBitmap make_checkerboard(int texSize) { |
| 14 | SkBitmap bitmap; |
| 15 | bitmap.allocN32Pixels(texSize, texSize); |
| 16 | |
| 17 | SkCanvas canvas(bitmap); |
| 18 | sk_tool_utils::draw_checkerboard(&canvas, |
| 19 | sk_tool_utils::color_to_565(0x0), |
| 20 | sk_tool_utils::color_to_565(0xFF804020), |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 21 | 2); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 22 | return bitmap; |
| 23 | } |
| 24 | |
| 25 | // Create a hemispherical normal map |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 26 | static SkBitmap make_hemi_normalmap(int texSize) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 27 | SkBitmap hemi; |
| 28 | hemi.allocN32Pixels(texSize, texSize); |
| 29 | |
| 30 | for (int y = 0; y < texSize; ++y) { |
| 31 | for (int x = 0; x < texSize; ++x) { |
| 32 | SkScalar locX = (x + 0.5f - texSize/2.0f) / (texSize/2.0f); |
| 33 | SkScalar locY = (y + 0.5f - texSize/2.0f) / (texSize/2.0f); |
| 34 | |
| 35 | SkScalar locZ = locX * locX + locY * locY; |
| 36 | if (locZ >= 1.0f) { |
| 37 | locX = 0.0f; |
| 38 | locY = 0.0f; |
| 39 | locZ = 0.0f; |
| 40 | } |
| 41 | locZ = sqrt(1.0f - locZ); |
| 42 | unsigned char r = static_cast<unsigned char>((0.5f * locX + 0.5f) * 255); |
| 43 | unsigned char g = static_cast<unsigned char>((-0.5f * locY + 0.5f) * 255); |
| 44 | unsigned char b = static_cast<unsigned char>((0.5f * locZ + 0.5f) * 255); |
| 45 | *hemi.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return hemi; |
| 50 | } |
| 51 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 52 | // Create a truncated pyramid normal map |
| 53 | static SkBitmap make_frustum_normalmap(int texSize) { |
| 54 | SkBitmap frustum; |
| 55 | frustum.allocN32Pixels(texSize, texSize); |
| 56 | |
| 57 | SkIRect inner = SkIRect::MakeWH(texSize, texSize); |
| 58 | inner.inset(texSize/4, texSize/4); |
| 59 | |
| 60 | SkPoint3 norm; |
| 61 | const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 62 | const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 63 | const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 64 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 65 | |
| 66 | for (int y = 0; y < texSize; ++y) { |
| 67 | for (int x = 0; x < texSize; ++x) { |
| 68 | if (inner.contains(x, y)) { |
| 69 | norm.set(0.0f, 0.0f, 1.0f); |
| 70 | } else { |
| 71 | SkScalar locX = x + 0.5f - texSize/2.0f; |
| 72 | SkScalar locY = y + 0.5f - texSize/2.0f; |
| 73 | |
| 74 | if (locX >= 0.0f) { |
| 75 | if (locY > 0.0f) { |
| 76 | norm = locX >= locY ? right : down; // LR corner |
| 77 | } else { |
| 78 | norm = locX > -locY ? right : up; // UR corner |
| 79 | } |
| 80 | } else { |
| 81 | if (locY > 0.0f) { |
| 82 | norm = -locX > locY ? left : down; // LL corner |
| 83 | } else { |
| 84 | norm = locX > locY ? up : left; // UL corner |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); |
| 90 | unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255); |
| 91 | unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255); |
| 92 | unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255); |
| 93 | *frustum.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return frustum; |
| 98 | } |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 99 | |
| 100 | namespace skiagm { |
| 101 | |
| 102 | // This GM exercises lighting shaders. |
| 103 | class LightingShaderGM : public GM { |
| 104 | public: |
| 105 | LightingShaderGM() { |
| 106 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 107 | |
| 108 | fLight.fColor = SkColorSetRGB(0xff, 0xff, 0xff); |
| 109 | fLight.fDirection.fX = 0.0f; |
| 110 | fLight.fDirection.fY = 0.0f; |
| 111 | fLight.fDirection.fZ = 1.0f; |
| 112 | |
| 113 | fAmbient = SkColorSetRGB(0x1f, 0x1f, 0x1f); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | protected: |
| 117 | |
| 118 | SkString onShortName() override { |
| 119 | return SkString("lightingshader"); |
| 120 | } |
| 121 | |
| 122 | SkISize onISize() override { |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 123 | return SkISize::Make(kGMSize, kGMSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void onOnceBeforeDraw() override { |
| 127 | fDiffuse = make_checkerboard(kTexSize); |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 128 | fHemiNormalMap = make_hemi_normalmap(kTexSize); |
| 129 | fFrustumNormalMap = make_frustum_normalmap(kTexSize); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 130 | } |
| 131 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 132 | void drawRect(SkCanvas* canvas, const SkRect& r, bool hemi) { |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 133 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 134 | SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height()); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 135 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 136 | SkMatrix matrix; |
| 137 | matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
| 138 | |
| 139 | SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
| 140 | fDiffuse, |
| 141 | hemi ? fHemiNormalMap : fFrustumNormalMap, |
| 142 | fLight, fAmbient, |
| 143 | &matrix)); |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 144 | |
| 145 | SkPaint paint; |
| 146 | paint.setShader(fShader); |
| 147 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 148 | canvas->drawRect(r, paint); |
| 149 | } |
| 150 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 151 | void onDraw(SkCanvas* canvas) override { |
| 152 | SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); |
| 153 | this->drawRect(canvas, r, true); |
| 154 | |
| 155 | r.offset(kGMSize - kTexSize, 0); |
| 156 | this->drawRect(canvas, r, false); |
| 157 | |
| 158 | r.offset(0, kGMSize - kTexSize); |
| 159 | this->drawRect(canvas, r, true); |
| 160 | |
| 161 | r.offset(kTexSize - kGMSize, 0); |
| 162 | this->drawRect(canvas, r, false); |
| 163 | } |
| 164 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 165 | private: |
| 166 | static const int kTexSize = 128; |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 167 | static const int kGMSize = 512; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 168 | |
robertphillips | 640898f | 2015-07-30 05:09:17 -0700 | [diff] [blame^] | 169 | SkBitmap fDiffuse; |
| 170 | SkBitmap fHemiNormalMap; |
| 171 | SkBitmap fFrustumNormalMap; |
| 172 | |
| 173 | SkLightingShader::Light fLight; |
| 174 | SkColor fAmbient; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 175 | |
| 176 | typedef GM INHERITED; |
| 177 | }; |
| 178 | |
| 179 | ////////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | DEF_GM( return SkNEW(LightingShaderGM); ) |
| 182 | |
| 183 | } |