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 "SkColorPriv.h" | |
11 #include "SkLightingShader.h" | 10 #include "SkLightingShader.h" |
| 11 #include "SkPoint3.h" |
| 12 #include "SkShader.h" |
12 | 13 |
13 static SkBitmap make_checkerboard(int texSize) { | 14 static SkBitmap make_checkerboard(int texSize) { |
14 SkBitmap bitmap; | 15 SkBitmap bitmap; |
15 bitmap.allocN32Pixels(texSize, texSize); | 16 bitmap.allocN32Pixels(texSize, texSize); |
16 | 17 |
17 SkCanvas canvas(bitmap); | 18 SkCanvas canvas(bitmap); |
18 sk_tool_utils::draw_checkerboard(&canvas, | 19 sk_tool_utils::draw_checkerboard(&canvas, |
19 sk_tool_utils::color_to_565(0x0), | 20 sk_tool_utils::color_to_565(0x0), |
20 sk_tool_utils::color_to_565(0xFF804020), | 21 sk_tool_utils::color_to_565(0xFF804020), |
21 2); | 22 2); |
22 return bitmap; | 23 return bitmap; |
23 } | 24 } |
24 | 25 |
25 // Create a hemispherical normal map | 26 // Create a hemispherical normal map |
26 static SkBitmap make_hemi_normalmap(int texSize) { | 27 static SkBitmap make_hemi_normalmap(int texSize) { |
27 SkBitmap hemi; | 28 SkBitmap hemi; |
28 hemi.allocN32Pixels(texSize, texSize); | 29 hemi.allocN32Pixels(texSize, texSize); |
29 | 30 |
30 for (int y = 0; y < texSize; ++y) { | 31 sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSiz
e)); |
31 for (int x = 0; x < texSize; ++x) { | |
32 SkScalar locX = (x + 0.5f - texSize/2.0f) / (texSize/2.0f); | |
33 SkScalar locY = (y + 0.5f - texSize/2.0f) / (texSize/2.0f); | |
34 | |
35 SkScalar locZ = locX * locX + locY * locY; | |
36 if (locZ >= 1.0f) { | |
37 locX = 0.0f; | |
38 locY = 0.0f; | |
39 locZ = 0.0f; | |
40 } | |
41 locZ = sqrtf(1.0f - locZ); | |
42 unsigned char r = static_cast<unsigned char>((0.5f * locX + 0.5f) *
255); | |
43 unsigned char g = static_cast<unsigned char>((-0.5f * locY + 0.5f) *
255); | |
44 unsigned char b = static_cast<unsigned char>((0.5f * locZ + 0.5f) *
255); | |
45 *hemi.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); | |
46 } | |
47 } | |
48 | |
49 return hemi; | 32 return hemi; |
50 } | 33 } |
51 | 34 |
52 // Create a truncated pyramid normal map | 35 // Create a truncated pyramid normal map |
53 static SkBitmap make_frustum_normalmap(int texSize) { | 36 static SkBitmap make_frustum_normalmap(int texSize) { |
54 SkBitmap frustum; | 37 SkBitmap frustum; |
55 frustum.allocN32Pixels(texSize, texSize); | 38 frustum.allocN32Pixels(texSize, texSize); |
56 | 39 |
57 SkIRect inner = SkIRect::MakeWH(texSize, texSize); | 40 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize,
texSize)); |
58 inner.inset(texSize/4, texSize/4); | 41 return frustum; |
| 42 } |
59 | 43 |
60 SkPoint3 norm; | 44 // Create a tetrahedral normal map |
61 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarR
oot2Over2); | 45 static SkBitmap make_tetra_normalmap(int texSize) { |
62 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarR
oot2Over2); | 46 SkBitmap tetra; |
63 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarR
oot2Over2); | 47 tetra.allocN32Pixels(texSize, texSize); |
64 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarR
oot2Over2); | |
65 | 48 |
66 for (int y = 0; y < texSize; ++y) { | 49 sk_tool_utils::create_tetra_normal_map(&tetra, SkIRect::MakeWH(texSize, texS
ize)); |
67 for (int x = 0; x < texSize; ++x) { | 50 return tetra; |
68 if (inner.contains(x, y)) { | |
69 norm.set(0.0f, 0.0f, 1.0f); | |
70 } else { | |
71 SkScalar locX = x + 0.5f - texSize/2.0f; | |
72 SkScalar locY = y + 0.5f - texSize/2.0f; | |
73 | |
74 if (locX >= 0.0f) { | |
75 if (locY > 0.0f) { | |
76 norm = locX >= locY ? right : down; // LR corner | |
77 } else { | |
78 norm = locX > -locY ? right : up; // UR corner | |
79 } | |
80 } else { | |
81 if (locY > 0.0f) { | |
82 norm = -locX > locY ? left : down; // LL corner | |
83 } else { | |
84 norm = locX > locY ? up : left; // UL corner | |
85 } | |
86 } | |
87 } | |
88 | |
89 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); | |
90 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f
) * 255); | |
91 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f
) * 255); | |
92 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f
) * 255); | |
93 *frustum.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); | |
94 } | |
95 } | |
96 | |
97 return frustum; | |
98 } | 51 } |
99 | 52 |
100 namespace skiagm { | 53 namespace skiagm { |
101 | 54 |
102 // This GM exercises lighting shaders. | 55 // This GM exercises lighting shaders. |
103 class LightingShaderGM : public GM { | 56 class LightingShaderGM : public GM { |
104 public: | 57 public: |
105 LightingShaderGM() { | 58 LightingShaderGM() { |
106 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
107 | 60 |
108 fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); | 61 fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); |
109 fLight.fDirection.fX = 0.0f; | 62 fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f); |
110 fLight.fDirection.fY = 0.0f; | |
111 fLight.fDirection.fZ = 1.0f; | |
112 | 63 |
113 fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); | 64 fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); |
114 } | 65 } |
115 | 66 |
116 protected: | 67 protected: |
| 68 enum NormalMap { |
| 69 kHemi_NormalMap, |
| 70 kFrustum_NormalMap, |
| 71 kTetra_NormalMap, |
| 72 |
| 73 kLast_NormalMap = kTetra_NormalMap |
| 74 }; |
| 75 |
| 76 static const int kNormalMapCount = kLast_NormalMap+1; |
117 | 77 |
118 SkString onShortName() override { | 78 SkString onShortName() override { |
119 return SkString("lightingshader"); | 79 return SkString("lightingshader"); |
120 } | 80 } |
121 | 81 |
122 SkISize onISize() override { | 82 SkISize onISize() override { |
123 return SkISize::Make(kGMSize, kGMSize); | 83 return SkISize::Make(kGMSize, kGMSize); |
124 } | 84 } |
125 | 85 |
126 void onOnceBeforeDraw() override { | 86 void onOnceBeforeDraw() override { |
127 fDiffuse = make_checkerboard(kTexSize); | 87 fDiffuse = make_checkerboard(kTexSize); |
128 fHemiNormalMap = make_hemi_normalmap(kTexSize); | 88 |
129 fFrustumNormalMap = make_frustum_normalmap(kTexSize); | 89 fNormalMaps[kHemi_NormalMap] = make_hemi_normalmap(kTexSize); |
| 90 fNormalMaps[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize); |
| 91 fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); |
130 } | 92 } |
131 | 93 |
132 void drawRect(SkCanvas* canvas, const SkRect& r, bool hemi) { | 94 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
133 | 95 |
134 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); | 96 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); |
135 | 97 |
136 SkMatrix matrix; | 98 SkMatrix matrix; |
137 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | 99 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
138 | 100 |
139 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( | 101 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
140 fDiffuse, | 102 fDiffuse, |
141 hemi ? fHemiNormalMap :
fFrustumNormalMap, | 103 fNormalMaps[mapType], |
142 fLight, fAmbient, | 104 fLight, fAmbient, |
143 &matrix)); | 105 &matrix)); |
144 | 106 |
145 SkPaint paint; | 107 SkPaint paint; |
146 paint.setShader(fShader); | 108 paint.setShader(fShader); |
147 | 109 |
148 canvas->drawRect(r, paint); | 110 canvas->drawRect(r, paint); |
149 } | 111 } |
150 | 112 |
151 void onDraw(SkCanvas* canvas) override { | 113 void onDraw(SkCanvas* canvas) override { |
152 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); | 114 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); |
153 this->drawRect(canvas, r, true); | 115 this->drawRect(canvas, r, kHemi_NormalMap); |
154 | 116 |
155 r.offset(kGMSize - kTexSize, 0); | 117 r.offset(kGMSize - kTexSize, 0); |
156 this->drawRect(canvas, r, false); | 118 this->drawRect(canvas, r, kFrustum_NormalMap); |
157 | 119 |
158 r.offset(0, kGMSize - kTexSize); | 120 r.offset(0, kGMSize - kTexSize); |
159 this->drawRect(canvas, r, true); | 121 this->drawRect(canvas, r, kTetra_NormalMap); |
160 | 122 |
161 r.offset(kTexSize - kGMSize, 0); | 123 r.offset(kTexSize - kGMSize, 0); |
162 this->drawRect(canvas, r, false); | 124 this->drawRect(canvas, r, kHemi_NormalMap); |
163 } | 125 } |
164 | 126 |
165 private: | 127 private: |
166 static const int kTexSize = 128; | 128 static const int kTexSize = 128; |
167 static const int kGMSize = 512; | 129 static const int kGMSize = 512; |
168 | 130 |
169 SkBitmap fDiffuse; | 131 SkBitmap fDiffuse; |
170 SkBitmap fHemiNormalMap; | 132 SkBitmap fNormalMaps[kNormalMapCount]; |
171 SkBitmap fFrustumNormalMap; | |
172 | 133 |
173 SkLightingShader::Light fLight; | 134 SkLightingShader::Light fLight; |
174 SkColor3f fAmbient; | 135 SkColor3f fAmbient; |
175 | 136 |
176 typedef GM INHERITED; | 137 typedef GM INHERITED; |
177 }; | 138 }; |
178 | 139 |
179 ////////////////////////////////////////////////////////////////////////////// | 140 ////////////////////////////////////////////////////////////////////////////// |
180 | 141 |
181 DEF_GM( return SkNEW(LightingShaderGM); ) | 142 DEF_GM( return SkNEW(LightingShaderGM); ) |
182 | 143 |
183 } | 144 } |
OLD | NEW |