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.
|
|
|
|
*/
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "Resources.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
2015-07-29 19:28:04 +00:00
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkLightingShader.h"
|
2015-08-20 12:15:06 +00:00
|
|
|
#include "SkPoint3.h"
|
|
|
|
|
|
|
|
static const SkLightingShader::Lights* create_lights(SkScalar angle, SkScalar blue) {
|
|
|
|
|
|
|
|
const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
|
|
|
|
SkScalarCos(SK_ScalarPI*0.25f));
|
|
|
|
|
|
|
|
SkLightingShader::Lights::Builder builder;
|
|
|
|
|
|
|
|
builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, blue), dir));
|
|
|
|
builder.add(SkLight(SkColor3f::Make(0.1f, 0.1f, 0.1f)));
|
|
|
|
|
|
|
|
return builder.finish();
|
|
|
|
}
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class LightingView : public SampleView {
|
|
|
|
public:
|
2015-07-29 19:28:04 +00:00
|
|
|
SkAutoTUnref<SkShader> fShader;
|
|
|
|
SkBitmap fDiffuseBitmap;
|
|
|
|
SkBitmap fNormalBitmap;
|
|
|
|
SkScalar fLightAngle;
|
2015-07-31 12:17:24 +00:00
|
|
|
SkScalar fColorFactor;
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
LightingView() {
|
|
|
|
SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
|
|
|
|
SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap);
|
|
|
|
SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
|
|
|
|
SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap);
|
|
|
|
|
|
|
|
fLightAngle = 0.0f;
|
2015-07-31 12:17:24 +00:00
|
|
|
fColorFactor = 0.0f;
|
2015-07-09 16:04:16 +00:00
|
|
|
|
2015-08-20 12:15:06 +00:00
|
|
|
SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle, 1.0f));
|
2015-07-09 16:04:16 +00:00
|
|
|
|
2015-07-29 19:28:04 +00:00
|
|
|
fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
|
2015-08-20 12:15:06 +00:00
|
|
|
lights, SkVector::Make(1.0f, 0.0f),
|
|
|
|
nullptr, nullptr));
|
2015-07-09 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~LightingView() {}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-20 12:15:06 +00:00
|
|
|
SkAutoTUnref<const SkLightingShader::Lights> lights(create_lights(fLightAngle,
|
|
|
|
fColorFactor));
|
2015-07-09 16:04:16 +00:00
|
|
|
|
2015-07-29 19:28:04 +00:00
|
|
|
fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
|
2015-08-20 12:15:06 +00:00
|
|
|
lights, SkVector::Make(1.0f, 0.0f),
|
|
|
|
nullptr, nullptr));
|
2015-07-09 16:04:16 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fShader);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
|
|
|
|
(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);
|