OLD | NEW |
(Empty) | |
| 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), |
| 21 16); |
| 22 return bitmap; |
| 23 } |
| 24 |
| 25 // Create a hemispherical normal map |
| 26 static SkBitmap make_normalmap(int texSize) { |
| 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 |
| 52 |
| 53 namespace skiagm { |
| 54 |
| 55 // This GM exercises lighting shaders. |
| 56 class LightingShaderGM : public GM { |
| 57 public: |
| 58 LightingShaderGM() { |
| 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 60 } |
| 61 |
| 62 protected: |
| 63 |
| 64 SkString onShortName() override { |
| 65 return SkString("lightingshader"); |
| 66 } |
| 67 |
| 68 SkISize onISize() override { |
| 69 return SkISize::Make(kTexSize, kTexSize); |
| 70 } |
| 71 |
| 72 void onOnceBeforeDraw() override { |
| 73 fDiffuse = make_checkerboard(kTexSize); |
| 74 fNormalMap = make_normalmap(kTexSize); |
| 75 } |
| 76 |
| 77 void onDraw(SkCanvas* canvas) override { |
| 78 |
| 79 SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f); |
| 80 |
| 81 SkLightingShader::Light light; |
| 82 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); |
| 83 light.fDirection.fX = 0.0f; |
| 84 light.fDirection.fY = 0.0f; |
| 85 light.fDirection.fZ = 1.0f; |
| 86 |
| 87 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create(fDiffuse, fNorma
lMap, |
| 88 light, ambient))
; |
| 89 |
| 90 SkPaint paint; |
| 91 paint.setShader(fShader); |
| 92 |
| 93 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); |
| 94 |
| 95 canvas->drawRect(r, paint); |
| 96 } |
| 97 |
| 98 private: |
| 99 static const int kTexSize = 128; |
| 100 |
| 101 SkBitmap fDiffuse; |
| 102 SkBitmap fNormalMap; |
| 103 |
| 104 typedef GM INHERITED; |
| 105 }; |
| 106 |
| 107 ////////////////////////////////////////////////////////////////////////////// |
| 108 |
| 109 DEF_GM( return SkNEW(LightingShaderGM); ) |
| 110 |
| 111 } |
OLD | NEW |