| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 | 9 |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkLightingShader.h" | 11 #include "SkLightingShader.h" |
| 12 | 12 |
| 13 static SkBitmap make_checkerboard(int texSize) { | 13 static SkBitmap make_checkerboard(int texSize) { |
| 14 SkBitmap bitmap; | 14 SkBitmap bitmap; |
| 15 bitmap.allocN32Pixels(texSize, texSize); | 15 bitmap.allocN32Pixels(texSize, texSize); |
| 16 | 16 |
| 17 SkCanvas canvas(bitmap); | 17 SkCanvas canvas(bitmap); |
| 18 sk_tool_utils::draw_checkerboard(&canvas, | 18 sk_tool_utils::draw_checkerboard(&canvas, |
| 19 sk_tool_utils::color_to_565(0x0), | 19 sk_tool_utils::color_to_565(0x0), |
| 20 sk_tool_utils::color_to_565(0xFF804020), | 20 sk_tool_utils::color_to_565(0xFF804020), |
| 21 16); | 21 2); |
| 22 return bitmap; | 22 return bitmap; |
| 23 } | 23 } |
| 24 | 24 |
| 25 // Create a hemispherical normal map | 25 // Create a hemispherical normal map |
| 26 static SkBitmap make_normalmap(int texSize) { | 26 static SkBitmap make_hemi_normalmap(int texSize) { |
| 27 SkBitmap hemi; | 27 SkBitmap hemi; |
| 28 hemi.allocN32Pixels(texSize, texSize); | 28 hemi.allocN32Pixels(texSize, texSize); |
| 29 | 29 |
| 30 for (int y = 0; y < texSize; ++y) { | 30 for (int y = 0; y < texSize; ++y) { |
| 31 for (int x = 0; x < texSize; ++x) { | 31 for (int x = 0; x < texSize; ++x) { |
| 32 SkScalar locX = (x + 0.5f - texSize/2.0f) / (texSize/2.0f); | 32 SkScalar locX = (x + 0.5f - texSize/2.0f) / (texSize/2.0f); |
| 33 SkScalar locY = (y + 0.5f - texSize/2.0f) / (texSize/2.0f); | 33 SkScalar locY = (y + 0.5f - texSize/2.0f) / (texSize/2.0f); |
| 34 | 34 |
| 35 SkScalar locZ = locX * locX + locY * locY; | 35 SkScalar locZ = locX * locX + locY * locY; |
| 36 if (locZ >= 1.0f) { | 36 if (locZ >= 1.0f) { |
| 37 locX = 0.0f; | 37 locX = 0.0f; |
| 38 locY = 0.0f; | 38 locY = 0.0f; |
| 39 locZ = 0.0f; | 39 locZ = 0.0f; |
| 40 } | 40 } |
| 41 locZ = sqrt(1.0f - locZ); | 41 locZ = sqrt(1.0f - locZ); |
| 42 unsigned char r = static_cast<unsigned char>((0.5f * locX + 0.5f) *
255); | 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); | 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); | 44 unsigned char b = static_cast<unsigned char>((0.5f * locZ + 0.5f) *
255); |
| 45 *hemi.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); | 45 *hemi.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 return hemi; | 49 return hemi; |
| 50 } | 50 } |
| 51 | 51 |
| 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_ScalarR
oot2Over2); |
| 62 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarR
oot2Over2); |
| 63 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarR
oot2Over2); |
| 64 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarR
oot2Over2); |
| 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 } |
| 52 | 99 |
| 53 namespace skiagm { | 100 namespace skiagm { |
| 54 | 101 |
| 55 // This GM exercises lighting shaders. | 102 // This GM exercises lighting shaders. |
| 56 class LightingShaderGM : public GM { | 103 class LightingShaderGM : public GM { |
| 57 public: | 104 public: |
| 58 LightingShaderGM() { | 105 LightingShaderGM() { |
| 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 106 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 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); |
| 60 } | 114 } |
| 61 | 115 |
| 62 protected: | 116 protected: |
| 63 | 117 |
| 64 SkString onShortName() override { | 118 SkString onShortName() override { |
| 65 return SkString("lightingshader"); | 119 return SkString("lightingshader"); |
| 66 } | 120 } |
| 67 | 121 |
| 68 SkISize onISize() override { | 122 SkISize onISize() override { |
| 69 return SkISize::Make(kTexSize, kTexSize); | 123 return SkISize::Make(kGMSize, kGMSize); |
| 70 } | 124 } |
| 71 | 125 |
| 72 void onOnceBeforeDraw() override { | 126 void onOnceBeforeDraw() override { |
| 73 fDiffuse = make_checkerboard(kTexSize); | 127 fDiffuse = make_checkerboard(kTexSize); |
| 74 fNormalMap = make_normalmap(kTexSize); | 128 fHemiNormalMap = make_hemi_normalmap(kTexSize); |
| 129 fFrustumNormalMap = make_frustum_normalmap(kTexSize); |
| 75 } | 130 } |
| 76 | 131 |
| 77 void onDraw(SkCanvas* canvas) override { | 132 void drawRect(SkCanvas* canvas, const SkRect& r, bool hemi) { |
| 78 | 133 |
| 79 SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f); | 134 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); |
| 80 | 135 |
| 81 SkLightingShader::Light light; | 136 SkMatrix matrix; |
| 82 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); | 137 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
| 83 light.fDirection.fX = 0.0f; | 138 |
| 84 light.fDirection.fY = 0.0f; | 139 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
| 85 light.fDirection.fZ = 1.0f; | 140 fDiffuse, |
| 86 | 141 hemi ? fHemiNormalMap :
fFrustumNormalMap, |
| 87 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create(fDiffuse, fNorma
lMap, | 142 fLight, fAmbient, |
| 88 light, ambient))
; | 143 &matrix)); |
| 89 | 144 |
| 90 SkPaint paint; | 145 SkPaint paint; |
| 91 paint.setShader(fShader); | 146 paint.setShader(fShader); |
| 92 | 147 |
| 148 canvas->drawRect(r, paint); |
| 149 } |
| 150 |
| 151 void onDraw(SkCanvas* canvas) override { |
| 93 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); | 152 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); |
| 153 this->drawRect(canvas, r, true); |
| 94 | 154 |
| 95 canvas->drawRect(r, paint); | 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); |
| 96 } | 163 } |
| 97 | 164 |
| 98 private: | 165 private: |
| 99 static const int kTexSize = 128; | 166 static const int kTexSize = 128; |
| 167 static const int kGMSize = 512; |
| 100 | 168 |
| 101 SkBitmap fDiffuse; | 169 SkBitmap fDiffuse; |
| 102 SkBitmap fNormalMap; | 170 SkBitmap fHemiNormalMap; |
| 171 SkBitmap fFrustumNormalMap; |
| 172 |
| 173 SkLightingShader::Light fLight; |
| 174 SkColor fAmbient; |
| 103 | 175 |
| 104 typedef GM INHERITED; | 176 typedef GM INHERITED; |
| 105 }; | 177 }; |
| 106 | 178 |
| 107 ////////////////////////////////////////////////////////////////////////////// | 179 ////////////////////////////////////////////////////////////////////////////// |
| 108 | 180 |
| 109 DEF_GM( return SkNEW(LightingShaderGM); ) | 181 DEF_GM( return SkNEW(LightingShaderGM); ) |
| 110 | 182 |
| 111 } | 183 } |
| OLD | NEW |