blob: 5b4f2e0931b1c43ca3892d7cb15ebdd7cc96e210 [file] [log] [blame]
jvanverthd17a3292015-07-09 09:04:16 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "Resources.h"
10
11#include "SkCanvas.h"
robertphillips5f865b92015-07-29 12:28:04 -070012#include "SkImageDecoder.h"
13#include "SkLightingShader.h"
jvanverthd17a3292015-07-09 09:04:16 -070014
15////////////////////////////////////////////////////////////////////////////
16
17class LightingView : public SampleView {
18public:
robertphillips5f865b92015-07-29 12:28:04 -070019 SkAutoTUnref<SkShader> fShader;
20 SkBitmap fDiffuseBitmap;
21 SkBitmap fNormalBitmap;
22 SkScalar fLightAngle;
robertphillipsa9652ac2015-07-31 05:17:24 -070023 SkScalar fColorFactor;
24 SkColor3f fAmbientColor;
jvanverthd17a3292015-07-09 09:04:16 -070025
26 LightingView() {
27 SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
28 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap);
29 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
30 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap);
31
32 fLightAngle = 0.0f;
robertphillipsa9652ac2015-07-31 05:17:24 -070033 fColorFactor = 0.0f;
jvanverthd17a3292015-07-09 09:04:16 -070034
robertphillips5f865b92015-07-29 12:28:04 -070035 SkLightingShader::Light light;
robertphillipsa9652ac2015-07-31 05:17:24 -070036 light.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f);
jvanverthd17a3292015-07-09 09:04:16 -070037 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
38 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
39 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
40
robertphillipsa9652ac2015-07-31 05:17:24 -070041 fAmbientColor = SkColor3f::Make(0.1f, 0.1f, 0.1f);
jvanverthd17a3292015-07-09 09:04:16 -070042
robertphillips5f865b92015-07-29 12:28:04 -070043 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
robertphillips640898f2015-07-30 05:09:17 -070044 light, fAmbientColor, nullptr));
jvanverthd17a3292015-07-09 09:04:16 -070045 }
46
47 virtual ~LightingView() {}
48
49protected:
50 // overrides from SkEventSink
51 bool onQuery(SkEvent* evt) override {
52 if (SampleCode::TitleQ(*evt)) {
53 SampleCode::TitleR(evt, "Lighting");
54 return true;
55 }
56 return this->INHERITED::onQuery(evt);
57 }
58
59 void onDrawContent(SkCanvas* canvas) override {
60 fLightAngle += 0.015f;
robertphillipsa9652ac2015-07-31 05:17:24 -070061 fColorFactor += 0.01f;
62 if (fColorFactor > 1.0f) {
63 fColorFactor = 0.0f;
64 }
jvanverthd17a3292015-07-09 09:04:16 -070065
robertphillips5f865b92015-07-29 12:28:04 -070066 SkLightingShader::Light light;
robertphillipsa9652ac2015-07-31 05:17:24 -070067 light.fColor = SkColor3f::Make(1.0f, 1.0f, fColorFactor);
jvanverthd17a3292015-07-09 09:04:16 -070068 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
69 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
70 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
71
robertphillips5f865b92015-07-29 12:28:04 -070072 fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
robertphillips640898f2015-07-30 05:09:17 -070073 light, fAmbientColor, nullptr));
jvanverthd17a3292015-07-09 09:04:16 -070074
75 SkPaint paint;
76 paint.setShader(fShader);
77 paint.setColor(SK_ColorBLACK);
78
79 SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
80 (SkScalar)fDiffuseBitmap.height());
81 canvas->drawRect(r, paint);
82
83 // so we're constantly updating
84 this->inval(NULL);
85 }
86
87 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
88 this->inval(NULL);
89 return this->INHERITED::onFindClickHandler(x, y, modi);
90 }
91
92private:
93 typedef SampleView INHERITED;
94};
95
96//////////////////////////////////////////////////////////////////////////////
97
98static SkView* MyFactory() { return new LightingView; }
99static SkViewRegister reg(MyFactory);