2013-04-05 13:47:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkPerlinNoiseShader.h"
|
|
|
|
|
2014-05-12 17:18:45 +00:00
|
|
|
class PerlinNoiseGM : public skiagm::GM {
|
2013-04-05 13:47:09 +00:00
|
|
|
public:
|
|
|
|
PerlinNoiseGM() {
|
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
fSize = SkISize::Make(80, 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("perlinnoise");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
2014-05-12 17:18:45 +00:00
|
|
|
return SkISize::Make(200, 500);
|
2013-04-05 13:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
|
|
|
|
SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height())));
|
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
|
|
|
|
SkIntToScalar(fSize.width()),
|
|
|
|
SkIntToScalar(fSize.height()));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
|
|
|
|
float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
|
2013-04-17 13:46:56 +00:00
|
|
|
bool stitchTiles) {
|
2013-04-05 13:47:09 +00:00
|
|
|
SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
|
|
|
|
SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
|
|
|
|
seed, stitchTiles ? &fSize : NULL) :
|
2014-04-01 16:09:37 +00:00
|
|
|
SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
|
2013-04-05 13:47:09 +00:00
|
|
|
seed, stitchTiles ? &fSize : NULL);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(shader)->unref();
|
|
|
|
drawClippedRect(canvas, x, y, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
canvas->clear(0x00000000);
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
|
|
|
|
0.1f, 0.1f, 0, 0, false);
|
|
|
|
test(canvas, 100, 0, SkPerlinNoiseShader::kTurbulence_Type,
|
|
|
|
0.1f, 0.1f, 0, 0, false);
|
2013-04-06 07:01:10 +00:00
|
|
|
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.1f, 0.1f, 2, 0, false);
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.2f, 0.4f, 5, 0, true);
|
|
|
|
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.1f, 0.1f, 2, 0, true);
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.2f, 0.4f, 5, 0, false);
|
|
|
|
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.1f, 0.1f, 3, 1, false);
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.1f, 0.1f, 3, 4, false);
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->scale(0.75f, 1.0f);
|
2013-04-05 13:47:09 +00:00
|
|
|
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.1f, 0.1f, 2, 0, false);
|
2013-11-06 10:08:38 +00:00
|
|
|
test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type,
|
2013-04-05 13:47:09 +00:00
|
|
|
0.2f, 0.4f, 5, 0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkISize fSize;
|
|
|
|
};
|
|
|
|
|
2014-05-12 17:18:45 +00:00
|
|
|
class PerlinNoiseGM2 : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
PerlinNoiseGM2() {
|
|
|
|
fSize = SkISize::Make(80, 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("perlinnoise_localmatrix");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
|
|
|
void install(SkPaint* paint, SkPerlinNoiseShader::Type type,
|
|
|
|
float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
|
|
|
|
bool stitchTiles) {
|
|
|
|
SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
|
|
|
|
SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
|
|
|
|
seed, stitchTiles ? &fSize : NULL) :
|
|
|
|
SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
|
|
|
|
seed, stitchTiles ? &fSize : NULL);
|
|
|
|
paint->setShader(shader)->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
install(&paint, SkPerlinNoiseShader::kFractalNoise_Type, 0.1f, 0.1f, 2, 0, false);
|
|
|
|
|
|
|
|
const SkScalar w = SkIntToScalar(fSize.width());
|
|
|
|
const SkScalar h = SkIntToScalar(fSize.height());
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(w, h);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(w * 5/4, 0);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
2013-04-05 13:47:09 +00:00
|
|
|
|
2014-05-12 17:18:45 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, h + 10);
|
|
|
|
canvas->scale(2, 2);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(w + 100, h + 10);
|
|
|
|
canvas->scale(2, 2);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// The next row should draw the same as the previous, even though we are using a local
|
|
|
|
// matrix instead of the canvas.
|
|
|
|
|
|
|
|
canvas->translate(0, h * 2 + 10);
|
|
|
|
|
|
|
|
SkMatrix lm;
|
|
|
|
lm.setScale(2, 2);
|
2014-05-12 20:42:21 +00:00
|
|
|
paint.setShader(SkShader::CreateLocalMatrixShader(paint.getShader(), lm))->unref();
|
2014-05-12 17:18:45 +00:00
|
|
|
r.fRight += r.width();
|
|
|
|
r.fBottom += r.height();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, h + 10);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(w + 100, h + 10);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkISize fSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-04-05 13:47:09 +00:00
|
|
|
|
2014-05-12 17:18:45 +00:00
|
|
|
DEF_GM( return new PerlinNoiseGM; )
|
|
|
|
DEF_GM( return new PerlinNoiseGM2; )
|