| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 | 9 |
| 10 #include "SkBitmapProcShader.h" |
| 10 #include "SkLightingShader.h" | 11 #include "SkLightingShader.h" |
| 12 #include "SkNormalSource.h" |
| 11 #include "SkPoint3.h" | 13 #include "SkPoint3.h" |
| 12 #include "SkShader.h" | 14 #include "SkShader.h" |
| 13 | 15 |
| 14 // Create a hemispherical normal map | 16 // Create a hemispherical normal map |
| 15 static SkBitmap make_hemi_normalmap(int texSize) { | 17 static SkBitmap make_hemi_normalmap(int texSize) { |
| 16 SkBitmap hemi; | 18 SkBitmap hemi; |
| 17 hemi.allocN32Pixels(texSize, texSize); | 19 hemi.allocN32Pixels(texSize, texSize); |
| 18 | 20 |
| 19 sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSiz
e)); | 21 sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSiz
e)); |
| 20 return hemi; | 22 return hemi; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 42 | 44 |
| 43 // This GM exercises lighting shaders. | 45 // This GM exercises lighting shaders. |
| 44 class LightingShaderGM : public GM { | 46 class LightingShaderGM : public GM { |
| 45 public: | 47 public: |
| 46 LightingShaderGM() { | 48 LightingShaderGM() { |
| 47 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 49 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 48 | 50 |
| 49 SkLights::Builder builder; | 51 SkLights::Builder builder; |
| 50 | 52 |
| 51 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | 53 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 52 SkVector3::Make(1.0f, 0.0f, 0.0f))); | 54 SkVector3::Make(SK_ScalarRoot2Over2, |
| 55 0.0f, |
| 56 SK_ScalarRoot2Over2))); |
| 53 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | 57 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
| 54 | 58 |
| 55 fLights = builder.finish(); | 59 fLights = builder.finish(); |
| 56 } | 60 } |
| 57 | 61 |
| 58 protected: | 62 protected: |
| 59 enum NormalMap { | 63 enum NormalMap { |
| 60 kHemi_NormalMap, | 64 kHemi_NormalMap, |
| 61 kFrustum_NormalMap, | 65 kFrustum_NormalMap, |
| 62 kTetra_NormalMap, | 66 kTetra_NormalMap, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 88 | 92 |
| 89 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { | 93 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
| 90 | 94 |
| 91 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); | 95 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); |
| 92 | 96 |
| 93 SkMatrix matrix; | 97 SkMatrix matrix; |
| 94 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | 98 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
| 95 | 99 |
| 96 const SkMatrix& ctm = canvas->getTotalMatrix(); | 100 const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 97 | 101 |
| 98 // TODO: correctly pull out the pure rotation | |
| 99 SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSk
ewY] }; | |
| 100 | |
| 101 SkPaint paint; | 102 SkPaint paint; |
| 102 paint.setShader(SkLightingShader::Make(fDiffuse, fNormalMaps[mapType], f
Lights, | 103 sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMaps[mapType], |
| 103 invNormRotation, &matrix, &matrix
)); | 104 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix, n
ullptr); |
| 105 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(s
td::move(normalMap), |
| 106 c
tm); |
| 107 paint.setShader(SkLightingShader::Make(fDiffuse, fLights, &matrix, |
| 108 std::move(normalSource))); |
| 104 | 109 |
| 105 canvas->drawRect(r, paint); | 110 canvas->drawRect(r, paint); |
| 106 } | 111 } |
| 107 | 112 |
| 108 void onDraw(SkCanvas* canvas) override { | 113 void onDraw(SkCanvas* canvas) override { |
| 109 SkMatrix m; | 114 SkMatrix m; |
| 110 SkRect r; | 115 SkRect r; |
| 111 | 116 |
| 112 { | 117 { |
| 113 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize))
; | 118 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize))
; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 172 |
| 168 sk_sp<SkLights> fLights; | 173 sk_sp<SkLights> fLights; |
| 169 | 174 |
| 170 typedef GM INHERITED; | 175 typedef GM INHERITED; |
| 171 }; | 176 }; |
| 172 | 177 |
| 173 ////////////////////////////////////////////////////////////////////////////// | 178 ////////////////////////////////////////////////////////////////////////////// |
| 174 | 179 |
| 175 DEF_GM(return new LightingShaderGM;) | 180 DEF_GM(return new LightingShaderGM;) |
| 176 } | 181 } |
| OLD | NEW |