blob: 2efcebb02d984b6f299e4ba056c73e46cc217808 [file] [log] [blame]
dvonbeckc526da92016-07-20 11:20:30 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
dvonbeckc526da92016-07-20 11:20:30 -07009#include "SkLightingShader.h"
10#include "SkNormalSource.h"
11#include "SkPoint3.h"
12#include "SkShader.h"
13
14// Create a truncated pyramid normal map
15static SkBitmap make_frustum_normalmap(int texSize) {
16 SkBitmap frustum;
17 frustum.allocN32Pixels(texSize, texSize);
18
19 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
20 return frustum;
21}
22
23namespace skiagm {
24
25// This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
dvonbeck6ad75d12016-08-15 11:35:55 -070026// normal maps, paint transparency, zero directional lights, multiple directional lights.
dvonbeckc526da92016-07-20 11:20:30 -070027class LightingShader2GM : public GM {
28public:
29 LightingShader2GM() {
30 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
31 }
32
33protected:
34 SkString onShortName() override {
35 return SkString("lightingshader2");
36 }
37
38 SkISize onISize() override {
dvonbeck680e2e92016-07-21 12:19:54 -070039 return SkISize::Make(600, 740);
dvonbeckc526da92016-07-20 11:20:30 -070040 }
41
42 void onOnceBeforeDraw() override {
dvonbeckc526da92016-07-20 11:20:30 -070043 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f);
dvonbeck6ad75d12016-08-15 11:35:55 -070044 const SkVector3 kLightFromUpperLeft = SkVector3::Make(-0.788f, 0.394f, 0.473f);
dvonbeckc526da92016-07-20 11:20:30 -070045
dvonbeck6ad75d12016-08-15 11:35:55 -070046 // Standard set of lights
47 SkLights::Builder builder;
vjiaoblack772b5ee2016-08-12 11:38:47 -070048 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
49 kLightFromUpperRight));
vjiaoblacka8eabc42016-08-29 10:22:09 -070050 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
dvonbeckc526da92016-07-20 11:20:30 -070051 fLights = builder.finish();
52
dvonbeck6ad75d12016-08-15 11:35:55 -070053 // No directional lights
54 SkLights::Builder builderNoDir;
vjiaoblacka8eabc42016-08-29 10:22:09 -070055 builderNoDir.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
dvonbeck6ad75d12016-08-15 11:35:55 -070056 fLightsNoDir = builderNoDir.finish();
57
58 // Two directional lights
59 SkLights::Builder builderTwoDir;
60 builderTwoDir.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 0.0f, 1.0f),
61 kLightFromUpperRight));
62 builderTwoDir.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.0f, 1.0f, 1.0f),
63 kLightFromUpperLeft));
vjiaoblacka8eabc42016-08-29 10:22:09 -070064 builderTwoDir.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
dvonbeck6ad75d12016-08-15 11:35:55 -070065 fLightsTwoDir = builderTwoDir.finish();
66
dvonbeckc526da92016-07-20 11:20:30 -070067 fRect = SkRect::MakeIWH(kTexSize, kTexSize);
68 SkMatrix matrix;
69 SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize);
70 matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit);
71
72 SkBitmap opaqueDiffuseMap = sk_tool_utils::create_checkerboard_bitmap(
73 kTexSize, kTexSize,
74 sk_tool_utils::color_to_565(0x0),
75 sk_tool_utils::color_to_565(0xFF804020),
76 8);
reed1ec04d92016-08-05 12:07:41 -070077 fOpaqueDiffuse = SkShader::MakeBitmapShader(opaqueDiffuseMap, SkShader::kClamp_TileMode,
78 SkShader::kClamp_TileMode, &matrix);
dvonbeckc526da92016-07-20 11:20:30 -070079
80 SkBitmap translucentDiffuseMap = sk_tool_utils::create_checkerboard_bitmap(
81 kTexSize, kTexSize,
82 SkColorSetARGB(0x55, 0x00, 0x00, 0x00),
83 SkColorSetARGB(0x55, 0x80, 0x40, 0x20),
84 8);
reed1ec04d92016-08-05 12:07:41 -070085 fTranslucentDiffuse = SkShader::MakeBitmapShader(translucentDiffuseMap,
86 SkShader::kClamp_TileMode,
87 SkShader::kClamp_TileMode, &matrix);
dvonbeckc526da92016-07-20 11:20:30 -070088
89 SkBitmap normalMap = make_frustum_normalmap(kTexSize);
reed1ec04d92016-08-05 12:07:41 -070090 fNormalMapShader = SkShader::MakeBitmapShader(normalMap, SkShader::kClamp_TileMode,
91 SkShader::kClamp_TileMode, &matrix);
dvonbeckc526da92016-07-20 11:20:30 -070092
93 }
94
95 // Scales shape around origin, rotates shape around origin, then translates shape to origin
96 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const {
97 canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
98 canvas->scale(scaleX, scaleY);
99 canvas->rotate(rotate);
100 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
101 }
102
103 static constexpr int NUM_BOOLEAN_PARAMS = 4;
104 void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY,
105 SkScalar rotate, bool useNormalSource, bool useDiffuseShader,
dvonbeck6ad75d12016-08-15 11:35:55 -0700106 bool useTranslucentPaint, bool useTranslucentShader, sk_sp<SkLights> lights) {
dvonbeckc526da92016-07-20 11:20:30 -0700107 canvas->save();
108
109 this->positionCTM(canvas, scaleX, scaleY, rotate);
110
111 const SkMatrix& ctm = canvas->getTotalMatrix();
112
113 SkPaint paint;
114 sk_sp<SkNormalSource> normalSource = nullptr;
115 sk_sp<SkShader> diffuseShader = nullptr;
116
117 if (useNormalSource) {
118 normalSource = SkNormalSource::MakeFromNormalMap(fNormalMapShader, ctm);
119 }
120
121 if (useDiffuseShader) {
122 diffuseShader = (useTranslucentShader) ? fTranslucentDiffuse : fOpaqueDiffuse;
123 } else {
124 paint.setColor(0xFF00FF00);
125 }
126
127 if (useTranslucentPaint) {
128 paint.setAlpha(0x99);
129 }
130
131 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
dvonbeck6ad75d12016-08-15 11:35:55 -0700132 std::move(lights)));
dvonbeckc526da92016-07-20 11:20:30 -0700133 canvas->drawRect(fRect, paint);
134
135 canvas->restore();
136 }
137
138 void onDraw(SkCanvas* canvas) override {
139
140 constexpr SkScalar LABEL_SIZE = 10.0f;
141 SkPaint labelPaint;
142 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif",
143 SkFontStyle()));
144 labelPaint.setAntiAlias(true);
145 labelPaint.setTextSize(LABEL_SIZE);
146
147 constexpr int GRID_COLUMN_NUM = 4;
148 constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARAMS * LABEL_SIZE;
149
150 int gridNum = 0;
151
152 // Running through all possible bool parameter combinations
153 for (bool useNormalSource : {true, false}) {
154 for (bool useDiffuseShader : {true, false}) {
155 for (bool useTranslucentPaint : {true, false}) {
156 for (bool useTranslucentShader : {true, false}) {
157
158 // Determining position
159 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
160 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
161
162 canvas->save();
163
164 canvas->translate(xPos, yPos);
165 this->drawRect(canvas, 1.0f, 1.0f, 0.f, useNormalSource, useDiffuseShader,
dvonbeck6ad75d12016-08-15 11:35:55 -0700166 useTranslucentPaint, useTranslucentShader, fLights);
dvonbeckc526da92016-07-20 11:20:30 -0700167 // Drawing labels
168 canvas->translate(0.0f, SkIntToScalar(kTexSize));
169 {
170 canvas->translate(0.0f, LABEL_SIZE);
171 SkString label;
172 label.appendf("useNormalSource: %d", useNormalSource);
173 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
174 }
175 {
176 canvas->translate(0.0f, LABEL_SIZE);
177 SkString label;
178 label.appendf("useDiffuseShader: %d", useDiffuseShader);
179 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
180 }
181 {
182 canvas->translate(0.0f, LABEL_SIZE);
183 SkString label;
184 label.appendf("useTranslucentPaint: %d", useTranslucentPaint);
185 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
186 }
187 {
188 canvas->translate(0.0f, LABEL_SIZE);
189 SkString label;
190 label.appendf("useTranslucentShader: %d", useTranslucentShader);
191 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
192 }
193
194 canvas->restore();
195
196 gridNum++;
197 }
198 }
199 }
200 }
201
202
203 // Rotation/scale test
204 {
205 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
206 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
207
208 canvas->save();
209 canvas->translate(xPos, yPos);
dvonbeck6ad75d12016-08-15 11:35:55 -0700210 this->drawRect(canvas, 0.6f, 0.6f, 45.0f, true, true, true, true, fLights);
dvonbeckc526da92016-07-20 11:20:30 -0700211 canvas->restore();
212
213 gridNum++;
214 }
215
216 // Anisotropic scale test
217 {
218 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
219 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
220
221 canvas->save();
222 canvas->translate(xPos, yPos);
dvonbeck6ad75d12016-08-15 11:35:55 -0700223 this->drawRect(canvas, 0.6f, 0.4f, 30.0f, true, true, true, true, fLights);
224 canvas->restore();
225
226 gridNum++;
227 }
228
229 // No directional lights test
230 {
231 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
232 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
233
234 canvas->save();
235 canvas->translate(xPos, yPos);
236 this->drawRect(canvas, 1.0f, 1.0f, 0.0f, true, true, false, false, fLightsNoDir);
237 canvas->restore();
238
239 gridNum++;
240 }
241
242 // Two directional lights test
243 {
244 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
245 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
246
247 canvas->save();
248 canvas->translate(xPos, yPos);
249 this->drawRect(canvas, 1.0f, 1.0f, 0.0f, true, true, false, false, fLightsTwoDir);
dvonbeckc526da92016-07-20 11:20:30 -0700250 canvas->restore();
251
252 gridNum++;
253 }
254 }
255
256private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700257 static constexpr int kTexSize = 96;
dvonbeckc526da92016-07-20 11:20:30 -0700258
259 sk_sp<SkShader> fOpaqueDiffuse;
260 sk_sp<SkShader> fTranslucentDiffuse;
261 sk_sp<SkShader> fNormalMapShader;
262
263 SkRect fRect;
264 sk_sp<SkLights> fLights;
dvonbeck6ad75d12016-08-15 11:35:55 -0700265 sk_sp<SkLights> fLightsNoDir;
266 sk_sp<SkLights> fLightsTwoDir;
dvonbeckc526da92016-07-20 11:20:30 -0700267
268 typedef GM INHERITED;
269};
270
271//////////////////////////////////////////////////////////////////////////////
272
273DEF_GM(return new LightingShader2GM;)
274}