Chromium Code Reviews| 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 20 matching lines...) Expand all Loading... | |
| 41 namespace skiagm { | 43 namespace skiagm { |
| 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), |
|
robertphillips
2016/07/06 16:36:33
I think you want this to be normalized:
(SK_Scal
dvonbeck
2016/07/06 18:07:48
It gets normalized by the constructor, should I st
robertphillips
2016/07/06 19:11:13
It will probably make more sense to the reader. Ot
dvonbeck
2016/07/06 19:34:36
Done.
| |
| 52 SkVector3::Make(1.0f, 0.0f, 0.0f))); | 54 SkVector3::Make(1.0f, 0.0f, 0.6f))); |
| 53 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | 55 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
| 54 | 56 |
| 55 fLights = builder.finish(); | 57 fLights = builder.finish(); |
| 56 } | 58 } |
| 57 | 59 |
| 58 protected: | 60 protected: |
| 59 enum NormalMap { | 61 enum NormalMap { |
| 60 kHemi_NormalMap, | 62 kHemi_NormalMap, |
| 61 kFrustum_NormalMap, | 63 kFrustum_NormalMap, |
| 62 kTetra_NormalMap, | 64 kTetra_NormalMap, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 88 | 90 |
| 89 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { | 91 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
| 90 | 92 |
| 91 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( )); | 93 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( )); |
| 92 | 94 |
| 93 SkMatrix matrix; | 95 SkMatrix matrix; |
| 94 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | 96 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
| 95 | 97 |
| 96 const SkMatrix& ctm = canvas->getTotalMatrix(); | 98 const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 97 | 99 |
| 98 // TODO: correctly pull out the pure rotation | |
| 99 SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSk ewY] }; | |
| 100 | |
| 101 SkPaint paint; | 100 SkPaint paint; |
| 102 paint.setShader(SkLightingShader::Make(fDiffuse, fNormalMaps[mapType], f Lights, | 101 sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMaps[mapType], |
| 103 invNormRotation, &matrix, &matrix )); | 102 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix, n ullptr); |
| 103 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(s td::move(normalMap), | |
| 104 c tm); | |
| 105 paint.setShader(SkLightingShader::Make(fDiffuse, fLights, &matrix, | |
| 106 std::move(normalSource))); | |
| 104 | 107 |
| 105 canvas->drawRect(r, paint); | 108 canvas->drawRect(r, paint); |
| 106 } | 109 } |
| 107 | 110 |
| 108 void onDraw(SkCanvas* canvas) override { | 111 void onDraw(SkCanvas* canvas) override { |
| 109 SkMatrix m; | 112 SkMatrix m; |
| 110 SkRect r; | 113 SkRect r; |
| 111 | 114 |
| 112 { | 115 { |
| 113 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)) ; | 116 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)) ; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 170 |
| 168 sk_sp<SkLights> fLights; | 171 sk_sp<SkLights> fLights; |
| 169 | 172 |
| 170 typedef GM INHERITED; | 173 typedef GM INHERITED; |
| 171 }; | 174 }; |
| 172 | 175 |
| 173 ////////////////////////////////////////////////////////////////////////////// | 176 ////////////////////////////////////////////////////////////////////////////// |
| 174 | 177 |
| 175 DEF_GM(return new LightingShaderGM;) | 178 DEF_GM(return new LightingShaderGM;) |
| 176 } | 179 } |
| OLD | NEW |