• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkPoint3.h"
10 #include "samplecode/DecodeFile.h"
11 #include "samplecode/Sample.h"
12 #include "src/core/SkNormalSource.h"
13 #include "src/shaders/SkLightingShader.h"
14 #include "tools/Resources.h"
15 
create_lights(SkScalar angle,SkScalar blue)16 static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
17 
18     const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
19                                           SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
20                                           SkScalarCos(SK_ScalarPI*0.25f));
21 
22     SkLights::Builder builder;
23 
24     builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, blue), dir));
25     builder.setAmbientLightColor(SkColor3f::Make(0.1f, 0.1f, 0.1f));
26 
27     return builder.finish();
28 }
29 
30 ////////////////////////////////////////////////////////////////////////////
31 
32 class LightingView : public Sample {
33 public:
LightingView()34     LightingView() : fLightAngle(0.0f), fColorFactor(0.0f) {}
35 
36 protected:
name()37     SkString name() override { return SkString("Lighting"); }
38 
onOnceBeforeDraw()39     void onOnceBeforeDraw() override {
40         {
41             SkBitmap diffuseBitmap;
42             SkAssertResult(GetResourceAsBitmap("images/brickwork-texture.jpg", &diffuseBitmap));
43 
44             fRect = SkRect::MakeIWH(diffuseBitmap.width(), diffuseBitmap.height());
45 
46             fDiffuseShader = diffuseBitmap.makeShader();
47         }
48 
49         {
50             SkBitmap normalBitmap;
51             SkAssertResult(GetResourceAsBitmap("images/brickwork_normal-map.jpg", &normalBitmap));
52 
53             sk_sp<SkShader> normalMap = normalBitmap.makeShader();
54             fNormalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), SkMatrix::I());
55         }
56     }
57 
onDrawContent(SkCanvas * canvas)58     void onDrawContent(SkCanvas* canvas) override {
59         sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
60 
61         SkPaint paint;
62         paint.setShader(SkLightingShader::Make(fDiffuseShader,
63                                                fNormalSource,
64                                                std::move(lights)));
65         paint.setColor(SK_ColorBLACK);
66 
67         canvas->drawRect(fRect, paint);
68     }
69 
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)70     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
71         return this->INHERITED::onFindClickHandler(x, y, modi);
72     }
73 
onAnimate(double nanos)74     bool onAnimate(double nanos) override {
75         fLightAngle += 0.015f;
76         fColorFactor += 0.01f;
77         if (fColorFactor > 1.0f) {
78             fColorFactor = 0.0f;
79         }
80 
81         return true;
82     }
83 
84 private:
85     SkRect                fRect;
86     sk_sp<SkShader>       fDiffuseShader;
87     sk_sp<SkNormalSource> fNormalSource;
88 
89     SkScalar              fLightAngle;
90     SkScalar              fColorFactor;
91 
92     typedef Sample INHERITED;
93 };
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 DEF_SAMPLE( return new LightingView(); )
98