sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | #include "SkPerlinNoiseShader.h" |
| 10 | |
| 11 | namespace skiagm { |
| 12 | |
| 13 | class PerlinNoiseGM : public GM { |
| 14 | public: |
| 15 | PerlinNoiseGM() { |
| 16 | this->setBGColor(0xFF000000); |
| 17 | fSize = SkISize::Make(80, 80); |
| 18 | } |
| 19 | |
| 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("perlinnoise"); |
| 23 | } |
| 24 | |
| 25 | virtual SkISize onISize() { |
| 26 | return make_isize(500, 400); |
| 27 | } |
| 28 | |
| 29 | void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 30 | canvas->save(); |
| 31 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 32 | SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()))); |
| 33 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 34 | SkIntToScalar(fSize.width()), |
| 35 | SkIntToScalar(fSize.height())); |
| 36 | canvas->drawRect(r, paint); |
| 37 | canvas->restore(); |
| 38 | } |
| 39 | |
| 40 | void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type, |
| 41 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
| 42 | bool stitchTiles) |
| 43 | { |
| 44 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 45 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 46 | seed, stitchTiles ? &fSize : NULL) : |
| 47 | SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves, |
| 48 | seed, stitchTiles ? &fSize : NULL); |
| 49 | SkPaint paint; |
| 50 | paint.setShader(shader)->unref(); |
| 51 | drawClippedRect(canvas, x, y, paint); |
| 52 | } |
| 53 | |
| 54 | virtual void onDraw(SkCanvas* canvas) { |
| 55 | canvas->clear(0x00000000); |
| 56 | |
| 57 | test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 58 | 0.1f, 0.1f, 2, 0, false); |
| 59 | test(canvas, 100, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 60 | 0.4f, 0.2f, 3, 0, true); |
| 61 | test(canvas, 200, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 62 | 0.3f, 0.6f, 4, 0, false); |
| 63 | test(canvas, 300, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 64 | 0.2f, 0.4f, 5, 0, true); |
| 65 | test(canvas, 400, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 66 | 0.5f, 0.8f, 6, 0, false); |
| 67 | |
| 68 | test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 69 | 0.1f, 0.1f, 2, 0, true); |
| 70 | test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 71 | 0.4f, 0.2f, 3, 0, false); |
| 72 | test(canvas, 200, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 73 | 0.3f, 0.6f, 4, 0, true); |
| 74 | test(canvas, 300, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 75 | 0.2f, 0.4f, 5, 0, false); |
| 76 | test(canvas, 400, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 77 | 0.5f, 0.8f, 6, 0, true); |
| 78 | |
| 79 | test(canvas, 0, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 80 | 0.1f, 0.1f, 3, 1, false); |
| 81 | test(canvas, 100, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 82 | 0.1f, 0.1f, 3, 2, false); |
| 83 | test(canvas, 200, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 84 | 0.1f, 0.1f, 3, 3, false); |
| 85 | test(canvas, 300, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 86 | 0.1f, 0.1f, 3, 4, false); |
| 87 | test(canvas, 400, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 88 | 0.1f, 0.1f, 3, 5, false); |
| 89 | |
| 90 | canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f)); |
| 91 | |
| 92 | test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 93 | 0.1f, 0.1f, 2, 0, false); |
| 94 | test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 95 | 0.4f, 0.2f, 3, 0, true); |
| 96 | test(canvas, 200, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 97 | 0.3f, 0.6f, 4, 0, false); |
| 98 | test(canvas, 300, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 99 | 0.2f, 0.4f, 5, 0, true); |
| 100 | test(canvas, 400, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 101 | 0.5f, 0.8f, 6, 0, false); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | typedef GM INHERITED; |
| 106 | SkISize fSize; |
| 107 | }; |
| 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | static GM* MyFactory(void*) { return new PerlinNoiseGM; } |
| 112 | static GMRegistry reg(MyFactory); |
| 113 | |
| 114 | } |