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 "SkShader.h" | 12 #include "SkShader.h" |
13 | 13 |
14 static SkBitmap make_checkerboard(int texSize) { | 14 static SkBitmap make_checkerboard(int texSize) { |
15 SkBitmap bitmap; | 15 SkBitmap bitmap; |
16 bitmap.allocN32Pixels(texSize, texSize); | 16 bitmap.allocN32Pixels(texSize, texSize); |
17 | 17 |
18 SkCanvas canvas(bitmap); | 18 SkCanvas canvas(bitmap); |
19 sk_tool_utils::draw_checkerboard(&canvas, | 19 sk_tool_utils::draw_checkerboard(&canvas, |
20 sk_tool_utils::color_to_565(0x0), | 20 sk_tool_utils::color_to_565(0x0), |
21 sk_tool_utils::color_to_565(0xFF804020), | 21 sk_tool_utils::color_to_565(0xFF804020), |
22 2); | 22 8); |
23 return bitmap; | 23 return bitmap; |
24 } | 24 } |
25 | 25 |
26 // Create a hemispherical normal map | 26 // Create a hemispherical normal map |
27 static SkBitmap make_hemi_normalmap(int texSize) { | 27 static SkBitmap make_hemi_normalmap(int texSize) { |
28 SkBitmap hemi; | 28 SkBitmap hemi; |
29 hemi.allocN32Pixels(texSize, texSize); | 29 hemi.allocN32Pixels(texSize, texSize); |
30 | 30 |
31 sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSiz
e)); | 31 sk_tool_utils::create_hemi_normal_map(&hemi, SkIRect::MakeWH(texSize, texSiz
e)); |
32 return hemi; | 32 return hemi; |
(...skipping 18 matching lines...) Expand all Loading... |
51 } | 51 } |
52 | 52 |
53 namespace skiagm { | 53 namespace skiagm { |
54 | 54 |
55 // This GM exercises lighting shaders. | 55 // This GM exercises lighting shaders. |
56 class LightingShaderGM : public GM { | 56 class LightingShaderGM : public GM { |
57 public: | 57 public: |
58 LightingShaderGM() { | 58 LightingShaderGM() { |
59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
60 | 60 |
61 fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f); | 61 SkLightingShader::Lights::Builder builder; |
62 fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f); | |
63 | 62 |
64 fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f); | 63 builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 64 SkVector3::Make(1.0f, 0.0f, 0.0f))); |
| 65 builder.add(SkLight(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
| 66 |
| 67 fLights.reset(builder.finish()); |
65 } | 68 } |
66 | 69 |
67 protected: | 70 protected: |
68 enum NormalMap { | 71 enum NormalMap { |
69 kHemi_NormalMap, | 72 kHemi_NormalMap, |
70 kFrustum_NormalMap, | 73 kFrustum_NormalMap, |
71 kTetra_NormalMap, | 74 kTetra_NormalMap, |
72 | 75 |
73 kLast_NormalMap = kTetra_NormalMap | 76 kLast_NormalMap = kTetra_NormalMap |
74 }; | 77 }; |
(...skipping 16 matching lines...) Expand all Loading... |
91 fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); | 94 fNormalMaps[kTetra_NormalMap] = make_tetra_normalmap(kTexSize); |
92 } | 95 } |
93 | 96 |
94 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { | 97 void drawRect(SkCanvas* canvas, const SkRect& r, NormalMap mapType) { |
95 | 98 |
96 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); | 99 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height(
)); |
97 | 100 |
98 SkMatrix matrix; | 101 SkMatrix matrix; |
99 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | 102 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); |
100 | 103 |
| 104 const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 105 |
| 106 // TODO: correctly pull out the pure rotation |
| 107 SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSk
ewY] }; |
| 108 |
101 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( | 109 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create( |
102 fDiffuse, | 110 fDiffuse, |
103 fNormalMaps[mapType], | 111 fNormalMaps[mapType], |
104 fLight, fAmbient, | 112 fLights, |
105 &matrix)); | 113 invNormRotation, &matrix
, &matrix)); |
106 | 114 |
107 SkPaint paint; | 115 SkPaint paint; |
108 paint.setShader(fShader); | 116 paint.setShader(fShader); |
109 | 117 |
110 canvas->drawRect(r, paint); | 118 canvas->drawRect(r, paint); |
111 } | 119 } |
112 | 120 |
113 void onDraw(SkCanvas* canvas) override { | 121 void onDraw(SkCanvas* canvas) override { |
114 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); | 122 SkMatrix m; |
115 this->drawRect(canvas, r, kHemi_NormalMap); | 123 SkRect r; |
116 | 124 |
117 r.offset(kGMSize - kTexSize, 0); | 125 { |
118 this->drawRect(canvas, r, kFrustum_NormalMap); | 126 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize))
; |
| 127 this->drawRect(canvas, r, kHemi_NormalMap); |
119 | 128 |
120 r.offset(0, kGMSize - kTexSize); | 129 canvas->save(); |
121 this->drawRect(canvas, r, kTetra_NormalMap); | 130 m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 131 m.postTranslate(kGMSize/2.0f - kTexSize/2.0f, 0.0f); |
| 132 canvas->setMatrix(m); |
| 133 this->drawRect(canvas, r, kHemi_NormalMap); |
| 134 canvas->restore(); |
| 135 } |
122 | 136 |
123 r.offset(kTexSize - kGMSize, 0); | 137 { |
124 this->drawRect(canvas, r, kHemi_NormalMap); | 138 r.offset(kGMSize - kTexSize, 0); |
| 139 this->drawRect(canvas, r, kFrustum_NormalMap); |
| 140 |
| 141 canvas->save(); |
| 142 m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 143 m.postTranslate(0.0f, kGMSize/2.0f - kTexSize/2.0f); |
| 144 canvas->setMatrix(m); |
| 145 this->drawRect(canvas, r, kFrustum_NormalMap); |
| 146 canvas->restore(); |
| 147 } |
| 148 |
| 149 { |
| 150 r.offset(0, kGMSize - kTexSize); |
| 151 this->drawRect(canvas, r, kTetra_NormalMap); |
| 152 |
| 153 canvas->save(); |
| 154 m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 155 m.postTranslate(-kGMSize/2.0f + kTexSize/2.0f, 0.0f); |
| 156 canvas->setMatrix(m); |
| 157 this->drawRect(canvas, r, kTetra_NormalMap); |
| 158 canvas->restore(); |
| 159 } |
| 160 |
| 161 { |
| 162 r.offset(kTexSize - kGMSize, 0); |
| 163 this->drawRect(canvas, r, kHemi_NormalMap); |
| 164 |
| 165 canvas->save(); |
| 166 m.setRotate(45.0f, r.centerX(), r.centerY()); |
| 167 m.postTranslate(0.0f, -kGMSize/2.0f + kTexSize/2.0f); |
| 168 canvas->setMatrix(m); |
| 169 this->drawRect(canvas, r, kHemi_NormalMap); |
| 170 canvas->restore(); |
| 171 } |
125 } | 172 } |
126 | 173 |
127 private: | 174 private: |
128 static const int kTexSize = 128; | 175 static const int kTexSize = 128; |
129 static const int kGMSize = 512; | 176 static const int kGMSize = 512; |
130 | 177 |
131 SkBitmap fDiffuse; | 178 SkBitmap fDiffuse; |
132 SkBitmap fNormalMaps[kNormalMapCount]; | 179 SkBitmap fNormalMaps[kNormalMapCount]; |
133 | 180 |
134 SkLightingShader::Light fLight; | 181 SkAutoTUnref<const SkLightingShader::Lights> fLights; |
135 SkColor3f fAmbient; | |
136 | 182 |
137 typedef GM INHERITED; | 183 typedef GM INHERITED; |
138 }; | 184 }; |
139 | 185 |
140 ////////////////////////////////////////////////////////////////////////////// | 186 ////////////////////////////////////////////////////////////////////////////// |
141 | 187 |
142 DEF_GM( return SkNEW(LightingShaderGM); ) | 188 DEF_GM( return SkNEW(LightingShaderGM); ) |
143 | 189 |
144 } | 190 } |
OLD | NEW |