SampleLighting.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkPoint3.h"
  9. #include "samplecode/DecodeFile.h"
  10. #include "samplecode/Sample.h"
  11. #include "src/core/SkNormalSource.h"
  12. #include "src/shaders/SkLightingShader.h"
  13. #include "tools/Resources.h"
  14. static sk_sp<SkLights> create_lights(SkScalar angle, SkScalar blue) {
  15. const SkVector3 dir = SkVector3::Make(SkScalarSin(angle)*SkScalarSin(SK_ScalarPI*0.25f),
  16. SkScalarCos(angle)*SkScalarSin(SK_ScalarPI*0.25f),
  17. SkScalarCos(SK_ScalarPI*0.25f));
  18. SkLights::Builder builder;
  19. builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, blue), dir));
  20. builder.setAmbientLightColor(SkColor3f::Make(0.1f, 0.1f, 0.1f));
  21. return builder.finish();
  22. }
  23. ////////////////////////////////////////////////////////////////////////////
  24. class LightingView : public Sample {
  25. public:
  26. LightingView() : fLightAngle(0.0f) , fColorFactor(0.0f) {
  27. {
  28. SkBitmap diffuseBitmap;
  29. SkAssertResult(GetResourceAsBitmap("images/brickwork-texture.jpg", &diffuseBitmap));
  30. fRect = SkRect::MakeIWH(diffuseBitmap.width(), diffuseBitmap.height());
  31. fDiffuseShader = diffuseBitmap.makeShader();
  32. }
  33. {
  34. SkBitmap normalBitmap;
  35. SkAssertResult(GetResourceAsBitmap("images/brickwork_normal-map.jpg", &normalBitmap));
  36. sk_sp<SkShader> normalMap = normalBitmap.makeShader();
  37. fNormalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap), SkMatrix::I());
  38. }
  39. }
  40. protected:
  41. SkString name() override { return SkString("Lighting"); }
  42. void onDrawContent(SkCanvas* canvas) override {
  43. sk_sp<SkLights> lights(create_lights(fLightAngle, fColorFactor));
  44. SkPaint paint;
  45. paint.setShader(SkLightingShader::Make(fDiffuseShader,
  46. fNormalSource,
  47. std::move(lights)));
  48. paint.setColor(SK_ColorBLACK);
  49. canvas->drawRect(fRect, paint);
  50. }
  51. Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
  52. return this->INHERITED::onFindClickHandler(x, y, modi);
  53. }
  54. bool onAnimate(double nanos) override {
  55. fLightAngle += 0.015f;
  56. fColorFactor += 0.01f;
  57. if (fColorFactor > 1.0f) {
  58. fColorFactor = 0.0f;
  59. }
  60. return true;
  61. }
  62. private:
  63. SkRect fRect;
  64. sk_sp<SkShader> fDiffuseShader;
  65. sk_sp<SkNormalSource> fNormalSource;
  66. SkScalar fLightAngle;
  67. SkScalar fColorFactor;
  68. typedef Sample INHERITED;
  69. };
  70. //////////////////////////////////////////////////////////////////////////////
  71. DEF_SAMPLE( return new LightingView(); )