2015-07-09 16:04:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-08-05 19:07:41 +00:00
|
|
|
|
2016-03-18 22:48:49 +00:00
|
|
|
#include "DecodeFile.h"
|
2015-07-09 16:04:16 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
2015-07-29 19:28:04 +00:00
|
|
|
#include "SkLightingShader.h"
|
2016-07-06 20:58:36 +00:00
|
|
|
#include "SkNormalSource.h"
|
2015-08-20 12:15:06 +00:00
|
|
|
#include "SkPoint3.h"
|
|
|
|
|
2016-05-31 19:08:25 +00:00
|
|
|
static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
|
2015-08-20 12:15:06 +00:00
|
|
|
|
|
|
|
const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(SK_ScalarPI*0.25f));
|
|
|
|
|
2016-05-31 19:08:25 +00:00
|
|
|
SkLights::Builder builder;
|
2015-08-20 12:15:06 +00:00
|
|
|
|
2016-05-31 19:08:25 +00:00
|
|
|
builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, blue), dir));
|
|
|
|
builder.add(SkLights::Light(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
|
2015-08-20 12:15:06 +00:00
|
|
|
|
|
|
|
return builder.finish();
|
|
|
|
}
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class LightingView : public SampleView {
|
|
|
|
public:
|
2016-03-25 16:08:00 +00:00
|
|
|
SkBitmap fDiffuseBitmap;
|
|
|
|
SkBitmap fNormalBitmap;
|
|
|
|
SkScalar fLightAngle;
|
|
|
|
SkScalar fColorFactor;
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
LightingView() {
|
|
|
|
SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
|
2016-03-18 22:48:49 +00:00
|
|
|
decode_file(diffusePath.c_str(), &fDiffuseBitmap);
|
2015-07-09 16:04:16 +00:00
|
|
|
SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
|
2016-03-18 22:48:49 +00:00
|
|
|
decode_file(normalPath.c_str(), &fNormalBitmap);
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
fLightAngle = 0.0f;
|
2015-07-31 12:17:24 +00:00
|
|
|
fColorFactor = 0.0f;
|
2015-07-09 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Lighting");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
fLightAngle += 0.015f;
|
2015-07-31 12:17:24 +00:00
|
|
|
fColorFactor += 0.01f;
|
|
|
|
if (fColorFactor > 1.0f) {
|
|
|
|
fColorFactor = 0.0f;
|
|
|
|
}
|
2015-07-09 16:04:16 +00:00
|
|
|
|
2016-05-31 19:08:25 +00:00
|
|
|
sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
|
2016-03-25 16:08:00 +00:00
|
|
|
SkPaint paint;
|
2016-08-05 19:07:41 +00:00
|
|
|
sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(fNormalBitmap,
|
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr);
|
2016-07-06 20:58:36 +00:00
|
|
|
sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(
|
|
|
|
std::move(normalMap), SkMatrix::I());
|
2016-08-02 13:12:06 +00:00
|
|
|
sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(fDiffuseBitmap,
|
2016-07-11 01:38:33 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr);
|
|
|
|
paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
|
|
|
|
std::move(lights)));
|
2015-07-09 16:04:16 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
|
2015-07-09 16:04:16 +00:00
|
|
|
(SkScalar)fDiffuseBitmap.height());
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
// so we're constantly updating
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2015-07-09 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2015-07-09 16:04:16 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new LightingView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|