2012-06-22 21:01:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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 "SkLightingImageFilter.h"
|
|
|
|
|
|
|
|
#define WIDTH 330
|
2013-07-26 00:10:07 +00:00
|
|
|
#define HEIGHT 440
|
2012-06-22 21:01:23 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageLightingGM : public GM {
|
|
|
|
public:
|
|
|
|
ImageLightingGM() : fInitialized(false) {
|
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("lighting");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(100, 100);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fBitmap);
|
2012-06-22 21:01:23 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-07-31 12:58:44 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2012-06-22 21:01:23 +00:00
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(SkIntToScalar(96));
|
|
|
|
const char* str = "e";
|
|
|
|
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
2014-02-13 17:14:46 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2012-06-22 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 16:27:50 +00:00
|
|
|
void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
|
|
|
|
canvas->save();
|
2013-08-27 21:37:01 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(
|
|
|
|
SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
2013-01-14 16:27:50 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fInitialized) {
|
|
|
|
make_bitmap();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
2012-10-29 19:47:06 +00:00
|
|
|
canvas->clear(0xFF101010);
|
|
|
|
SkPaint checkPaint;
|
|
|
|
checkPaint.setColor(0xFF202020);
|
|
|
|
for (int y = 0; y < HEIGHT; y += 16) {
|
|
|
|
for (int x = 0; x < WIDTH; x += 16) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
2012-06-22 21:01:23 +00:00
|
|
|
SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
|
|
|
|
SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
|
|
|
|
SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
|
|
|
|
SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
|
|
|
|
SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
|
|
|
|
SkScalarSin(elevationRad));
|
|
|
|
SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
|
|
|
|
SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
|
|
|
|
SkScalar spotExponent = SK_Scalar1;
|
|
|
|
SkScalar cutoffAngle = SkIntToScalar(15);
|
|
|
|
SkScalar kd = SkIntToScalar(2);
|
|
|
|
SkScalar ks = SkIntToScalar(1);
|
|
|
|
SkScalar shininess = SkIntToScalar(8);
|
|
|
|
SkScalar surfaceScale = SkIntToScalar(1);
|
|
|
|
SkColor white(0xFFFFFFFF);
|
|
|
|
SkPaint paint;
|
2013-07-26 00:10:07 +00:00
|
|
|
|
2013-10-10 13:51:19 +00:00
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
|
2013-07-26 00:10:07 +00:00
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
2013-10-10 13:51:19 +00:00
|
|
|
const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect;
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 0, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 110, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 220, y);
|
|
|
|
|
|
|
|
y += 110;
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 0, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 110, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref();
|
|
|
|
drawClippedBitmap(canvas, paint, 220, y);
|
2013-08-27 21:37:01 +00:00
|
|
|
|
2013-07-26 00:10:07 +00:00
|
|
|
y += 110;
|
|
|
|
}
|
2012-06-22 21:01:23 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-06-22 21:01:23 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
bool fInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ImageLightingGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|