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 "SkLightingShader.h" | 10 #include "SkLightingShader.h" |
11 #include "SkPoint3.h" | 11 #include "SkPoint3.h" |
| 12 #include "SkRSXform.h" |
12 #include "SkShader.h" | 13 #include "SkShader.h" |
13 | 14 |
14 static SkBitmap make_checkerboard(int texSize) { | 15 static SkBitmap make_checkerboard(int texSize) { |
15 SkBitmap bitmap; | 16 SkBitmap bitmap; |
16 bitmap.allocN32Pixels(texSize, texSize); | 17 bitmap.allocN32Pixels(texSize, texSize); |
17 | 18 |
18 SkCanvas canvas(bitmap); | 19 SkCanvas canvas(bitmap); |
19 sk_tool_utils::draw_checkerboard(&canvas, | 20 sk_tool_utils::draw_checkerboard(&canvas, |
20 sk_tool_utils::color_to_565(0x0), | 21 sk_tool_utils::color_to_565(0x0), |
21 sk_tool_utils::color_to_565(0xFF804020), | 22 sk_tool_utils::color_to_565(0xFF804020), |
(...skipping 29 matching lines...) Expand all Loading... |
51 } | 52 } |
52 | 53 |
53 namespace skiagm { | 54 namespace skiagm { |
54 | 55 |
55 // This GM exercises lighting shaders. | 56 // This GM exercises lighting shaders. |
56 class LightingShaderGM : public GM { | 57 class LightingShaderGM : public GM { |
57 public: | 58 public: |
58 LightingShaderGM() { | 59 LightingShaderGM() { |
59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 60 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
60 | 61 |
61 fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); | 62 SkLightingShader::Lights::Builder builder; |
62 fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f); | |
63 | 63 |
64 fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); | 64 builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 65 SkVector3::Make(0.0f, 0.0f, 1.0f))); |
| 66 builder.add(SkLight(SkColor3f::Make(0.1f, 0.1f, 0.1f))); |
| 67 |
| 68 fLights.reset(builder.finish()); |
65 } | 69 } |
66 | 70 |
67 protected: | 71 protected: |
68 enum NormalMap { | 72 enum NormalMap { |
69 kHemi_NormalMap, | 73 kHemi_NormalMap, |
70 kFrustum_NormalMap, | 74 kFrustum_NormalMap, |
71 kTetra_NormalMap, | 75 kTetra_NormalMap, |
72 | 76 |
73 kLast_NormalMap = kTetra_NormalMap | 77 kLast_NormalMap = kTetra_NormalMap |
74 }; | 78 }; |
(...skipping 16 matching lines...) Expand all Loading... |
91 fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); | 95 fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); |
92 } | 96 } |
93 | 97 |
94 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { | 98 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
95 | 99 |
96 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); | 100 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); |
97 | 101 |
98 SkMatrix matrix; | 102 SkMatrix matrix; |
99 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | 103 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
100 | 104 |
| 105 SkRSXform xform; |
| 106 xform.setIdentity(); |
| 107 |
101 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( | 108 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
102 fDiffuse, | 109 fDiffuse, |
103 fNormalMaps[mapType], | 110 fNormalMaps[mapType], |
104 fLight, fAmbient, | 111 fLights, |
105 &matrix)); | 112 xform, &matrix, &matrix)
); |
106 | 113 |
107 SkPaint paint; | 114 SkPaint paint; |
108 paint.setShader(fShader); | 115 paint.setShader(fShader); |
109 | 116 |
110 canvas->drawRect(r, paint); | 117 canvas->drawRect(r, paint); |
111 } | 118 } |
112 | 119 |
113 void onDraw(SkCanvas* canvas) override { | 120 void onDraw(SkCanvas* canvas) override { |
114 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); | 121 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); |
115 this->drawRect(canvas, r, kHemi_NormalMap); | 122 this->drawRect(canvas, r, kHemi_NormalMap); |
116 | 123 |
117 r.offset(kGMSize - kTexSize, 0); | 124 r.offset(kGMSize - kTexSize, 0); |
118 this->drawRect(canvas, r, kFrustum_NormalMap); | 125 this->drawRect(canvas, r, kFrustum_NormalMap); |
119 | 126 |
120 r.offset(0, kGMSize - kTexSize); | 127 r.offset(0, kGMSize - kTexSize); |
121 this->drawRect(canvas, r, kTetra_NormalMap); | 128 this->drawRect(canvas, r, kTetra_NormalMap); |
122 | 129 |
123 r.offset(kTexSize - kGMSize, 0); | 130 r.offset(kTexSize - kGMSize, 0); |
124 this->drawRect(canvas, r, kHemi_NormalMap); | 131 this->drawRect(canvas, r, kHemi_NormalMap); |
125 } | 132 } |
126 | 133 |
127 private: | 134 private: |
128 static const int kTexSize = 128; | 135 static const int kTexSize = 128; |
129 static const int kGMSize = 512; | 136 static const int kGMSize = 512; |
130 | 137 |
131 SkBitmap fDiffuse; | 138 SkBitmap fDiffuse; |
132 SkBitmap fNormalMaps[kNormalMapCount]; | 139 SkBitmap fNormalMaps[kNormalMapCount]; |
133 | 140 |
134 SkLightingShader::Light fLight; | 141 SkAutoTUnref<const SkLightingShader::Lights> fLights; |
135 SkColor3f fAmbient; | |
136 | 142 |
137 typedef GM INHERITED; | 143 typedef GM INHERITED; |
138 }; | 144 }; |
139 | 145 |
140 ////////////////////////////////////////////////////////////////////////////// | 146 ////////////////////////////////////////////////////////////////////////////// |
141 | 147 |
142 DEF_GM( return SkNEW(LightingShaderGM); ) | 148 DEF_GM( return SkNEW(LightingShaderGM); ) |
143 | 149 |
144 } | 150 } |
OLD | NEW |