blob: 9ca6df2aa0175a542da5747dbcd3eb84df7496bb [file] [log] [blame]
Robert Phillipsa8cdbd72018-07-17 12:30:40 -04001/*
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 "SampleCode.h"
9#include "SkAnimTimer.h"
10#include "SkBitmapProcShader.h"
11#include "SkCanvas.h"
12#include "SkDrawable.h"
13#include "SkLightingShader.h"
14#include "SkLights.h"
15#include "SkNormalSource.h"
16#include "SkRandom.h"
17#include "SkRSXform.h"
18#include "SkView.h"
19
20#include "sk_tool_utils.h"
21
22// A crude normal mapped asteroids-like sample
23class DrawLitAtlasDrawable : public SkDrawable {
24public:
25 DrawLitAtlasDrawable(const SkRect& r)
26 : fBounds(r)
27 , fUseColors(false)
28 , fLightDir(SkVector3::Make(1.0f, 0.0f, 0.0f)) {
29 fAtlas = MakeAtlas();
30
31 SkRandom rand;
32 for (int i = 0; i < kNumAsteroids; ++i) {
33 fAsteroids[i].initAsteroid(&rand, fBounds, &fDiffTex[i], &fNormTex[i]);
34 }
35
36 fShip.initShip(fBounds, &fDiffTex[kNumAsteroids], &fNormTex[kNumAsteroids]);
37
38 this->updateLights();
39 }
40
41 void toggleUseColors() {
42 fUseColors = !fUseColors;
43 }
44
45 void rotateLight() {
46 SkScalar c;
47 SkScalar s = SkScalarSinCos(SK_ScalarPI/6.0f, &c);
48
49 SkScalar newX = c * fLightDir.fX - s * fLightDir.fY;
50 SkScalar newY = s * fLightDir.fX + c * fLightDir.fY;
51
52 fLightDir.set(newX, newY, 0.0f);
53
54 this->updateLights();
55 }
56
57 void left() {
58 SkScalar newRot = SkScalarMod(fShip.rot() + (2*SK_ScalarPI - SK_ScalarPI/32.0f),
59 2 * SK_ScalarPI);
60 fShip.setRot(newRot);
61 }
62
63 void right() {
64 SkScalar newRot = SkScalarMod(fShip.rot() + SK_ScalarPI/32.0f, 2 * SK_ScalarPI);
65 fShip.setRot(newRot);
66 }
67
68 void thrust() {
69 SkScalar c;
70 SkScalar s = SkScalarSinCos(fShip.rot(), &c);
71
72 SkVector newVel = fShip.velocity();
73 newVel.fX += s;
74 newVel.fY += -c;
75
76 SkScalar len = newVel.length();
77 if (len > kMaxShipSpeed) {
78 newVel.setLength(SkIntToScalar(kMaxShipSpeed));
79 }
80
81 fShip.setVelocity(newVel);
82 }
83
84protected:
85 void onDraw(SkCanvas* canvas) override {
86 SkRSXform xforms[kNumAsteroids+kNumShips];
87 SkColor colors[kNumAsteroids+kNumShips];
88
89 for (int i = 0; i < kNumAsteroids; ++i) {
90 fAsteroids[i].advance(fBounds);
91 xforms[i] = fAsteroids[i].asRSXform();
92 if (fUseColors) {
93 colors[i] = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
94 }
95 }
96
97 fShip.advance(fBounds);
98 xforms[kNumAsteroids] = fShip.asRSXform();
99 if (fUseColors) {
100 colors[kNumAsteroids] = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
101 }
102
103#ifdef SK_DEBUG
104 canvas->drawBitmap(fAtlas, 0, 0); // just to see the atlas
105
106 this->drawLightDir(canvas, fBounds.centerX(), fBounds.centerY());
107#endif
108
109#if 0
110 // TODO: revitalize when drawLitAtlas API lands
111 SkPaint paint;
112 paint.setFilterQuality(kLow_SkFilterQuality);
113
114 const SkRect cull = this->getBounds();
115 const SkColor* colorsPtr = fUseColors ? colors : NULL;
116
117 canvas->drawLitAtlas(fAtlas, xforms, fDiffTex, fNormTex, colorsPtr, kNumAsteroids+1,
118 SkXfermode::kModulate_Mode, &cull, &paint, fLights);
119#else
120 SkMatrix diffMat, normalMat;
121
122 for (int i = 0; i < kNumAsteroids+1; ++i) {
123 colors[i] = colors[i] & 0xFF000000; // to silence compilers
124 SkPaint paint;
125
126 SkRect r = fDiffTex[i];
127 r.offsetTo(0, 0);
128
129 diffMat.setRectToRect(fDiffTex[i], r, SkMatrix::kFill_ScaleToFit);
130 normalMat.setRectToRect(fNormTex[i], r, SkMatrix::kFill_ScaleToFit);
131
132 SkMatrix m;
133 m.setRSXform(xforms[i]);
134
135 sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(fAtlas, SkShader::kClamp_TileMode,
136 SkShader::kClamp_TileMode, &normalMat);
137 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(
138 std::move(normalMap), m);
139 sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(fAtlas,
140 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &diffMat);
141 paint.setShader(SkLightingShader::Make(std::move(diffuseShader),
142 std::move(normalSource), fLights));
143
144 canvas->save();
145 canvas->setMatrix(m);
146 canvas->drawRect(r, paint);
147 canvas->restore();
148 }
149#endif
150
151#ifdef SK_DEBUG
152 {
153 SkPaint paint;
154 paint.setColor(SK_ColorRED);
155
156 for (int i = 0; i < kNumAsteroids; ++i) {
157 canvas->drawCircle(fAsteroids[i].pos().x(), fAsteroids[i].pos().y(), 2, paint);
158 }
159 canvas->drawCircle(fShip.pos().x(), fShip.pos().y(), 2, paint);
160
161 paint.setStyle(SkPaint::kStroke_Style);
162 canvas->drawRect(this->getBounds(), paint);
163 }
164#endif
165 }
166
167 SkRect onGetBounds() override {
168 return fBounds;
169 }
170
171private:
172
173 enum ObjType {
174 kBigAsteroid_ObjType = 0,
175 kMedAsteroid_ObjType,
176 kSmAsteroid_ObjType,
177 kShip_ObjType,
178
179 kLast_ObjType = kShip_ObjType
180 };
181
182 static const int kObjTypeCount = kLast_ObjType + 1;
183
184 void updateLights() {
185 SkLights::Builder builder;
186
187 builder.add(SkLights::Light::MakeDirectional(
188 SkColor3f::Make(1.0f, 1.0f, 1.0f), fLightDir));
189 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
190
191 fLights = builder.finish();
192 }
193
194#ifdef SK_DEBUG
195 // Draw a vector to the light
196 void drawLightDir(SkCanvas* canvas, SkScalar centerX, SkScalar centerY) {
197 static const int kBgLen = 30;
198 static const int kSmLen = 5;
199
200 // TODO: change the lighting coordinate system to be right handed
201 SkPoint p1 = SkPoint::Make(centerX + kBgLen * fLightDir.fX,
202 centerY - kBgLen * fLightDir.fY);
203 SkPoint p2 = SkPoint::Make(centerX + (kBgLen-kSmLen) * fLightDir.fX,
204 centerY - (kBgLen-kSmLen) * fLightDir.fY);
205
206 SkPaint p;
207 canvas->drawLine(centerX, centerY, p1.fX, p1.fY, p);
208 canvas->drawLine(p1.fX, p1.fY,
209 p2.fX - kSmLen * fLightDir.fY, p2.fY - kSmLen * fLightDir.fX, p);
210 canvas->drawLine(p1.fX, p1.fY,
211 p2.fX + kSmLen * fLightDir.fY, p2.fY + kSmLen * fLightDir.fX, p);
212 }
213#endif
214
215 // Create the mixed diffuse & normal atlas
216 //
217 // big color circle | big normal hemi
218 // ------------------------------------
219 // med color circle | med normal pyra
220 // ------------------------------------
221 // sm color circle | sm normal hemi
222 // ------------------------------------
223 // big ship | big tetra normal
224 static SkBitmap MakeAtlas() {
225
226 SkBitmap atlas;
227 atlas.allocN32Pixels(kAtlasWidth, kAtlasHeight);
228
229 for (int y = 0; y < kAtlasHeight; ++y) {
230 int x = 0;
231 for ( ; x < kBigSize+kPad; ++x) {
232 *atlas.getAddr32(x, y) = SK_ColorTRANSPARENT;
233 }
234 for ( ; x < kAtlasWidth; ++x) {
235 *atlas.getAddr32(x, y) = SkPackARGB32(0xFF, 0x88, 0x88, 0xFF);
236 }
237 }
238
239 // big asteroid
240 {
241 SkPoint bigCenter = SkPoint::Make(kDiffXOff + kBigSize/2.0f, kBigYOff + kBigSize/2.0f);
242
243 for (int y = kBigYOff; y < kBigYOff+kBigSize; ++y) {
244 for (int x = kDiffXOff; x < kDiffXOff+kBigSize; ++x) {
245 SkScalar distSq = (x - bigCenter.fX) * (x - bigCenter.fX) +
246 (y - bigCenter.fY) * (y - bigCenter.fY);
247 if (distSq > kBigSize*kBigSize/4.0f) {
248 *atlas.getAddr32(x, y) = SkPreMultiplyARGB(0, 0, 0, 0);
249 } else {
250 *atlas.getAddr32(x, y) = SkPackARGB32(0xFF, 0xFF, 0, 0);
251 }
252 }
253 }
254
255 sk_tool_utils::create_hemi_normal_map(&atlas,
256 SkIRect::MakeXYWH(kNormXOff, kBigYOff,
257 kBigSize, kBigSize));
258 }
259
260 // medium asteroid
261 {
262 for (int y = kMedYOff; y < kMedYOff+kMedSize; ++y) {
263 for (int x = kDiffXOff; x < kDiffXOff+kMedSize; ++x) {
264 *atlas.getAddr32(x, y) = SkPackARGB32(0xFF, 0, 0xFF, 0);
265 }
266 }
267
268 sk_tool_utils::create_frustum_normal_map(&atlas,
269 SkIRect::MakeXYWH(kNormXOff, kMedYOff,
270 kMedSize, kMedSize));
271 }
272
273 // small asteroid
274 {
275 SkPoint smCenter = SkPoint::Make(kDiffXOff + kSmSize/2.0f, kSmYOff + kSmSize/2.0f);
276
277 for (int y = kSmYOff; y < kSmYOff+kSmSize; ++y) {
278 for (int x = kDiffXOff; x < kDiffXOff+kSmSize; ++x) {
279 SkScalar distSq = (x - smCenter.fX) * (x - smCenter.fX) +
280 (y - smCenter.fY) * (y - smCenter.fY);
281 if (distSq > kSmSize*kSmSize/4.0f) {
282 *atlas.getAddr32(x, y) = SkPreMultiplyARGB(0, 0, 0, 0);
283 } else {
284 *atlas.getAddr32(x, y) = SkPackARGB32(0xFF, 0, 0, 0xFF);
285 }
286 }
287 }
288
289 sk_tool_utils::create_hemi_normal_map(&atlas,
290 SkIRect::MakeXYWH(kNormXOff, kSmYOff,
291 kSmSize, kSmSize));
292 }
293
294 // ship
295 {
296 SkScalar shipMidLine = kDiffXOff + kMedSize/2.0f;
297
298 for (int y = kShipYOff; y < kShipYOff+kMedSize; ++y) {
299 SkScalar scaledY = (y - kShipYOff)/(float)kMedSize; // 0..1
300
301 for (int x = kDiffXOff; x < kDiffXOff+kMedSize; ++x) {
302 SkScalar scaledX;
303
304 if (x < shipMidLine) {
305 scaledX = 1.0f - (x - kDiffXOff)/(kMedSize/2.0f); // 0..1
306 } else {
307 scaledX = (x - shipMidLine)/(kMedSize/2.0f); // 0..1
308 }
309
310 if (scaledX < scaledY) {
311 *atlas.getAddr32(x, y) = SkPackARGB32(0xFF, 0, 0xFF, 0xFF);
312 } else {
313 *atlas.getAddr32(x, y) = SkPackARGB32(0, 0, 0, 0);
314 }
315 }
316 }
317
318 sk_tool_utils::create_tetra_normal_map(&atlas,
319 SkIRect::MakeXYWH(kNormXOff, kShipYOff,
320 kMedSize, kMedSize));
321 }
322
323 return atlas;
324 }
325
326 class ObjectRecord {
327 public:
328 void initAsteroid(SkRandom *rand, const SkRect& bounds,
329 SkRect* diffTex, SkRect* normTex) {
330 static const SkScalar gMaxSpeeds[3] = { 1, 2, 5 }; // smaller asteroids can go faster
331 static const SkScalar gYOffs[3] = { kBigYOff, kMedYOff, kSmYOff };
332 static const SkScalar gSizes[3] = { kBigSize, kMedSize, kSmSize };
333
334 static unsigned int asteroidType = 0;
335 fObjType = static_cast<ObjType>(asteroidType++ % 3);
336
337 fPosition.set(bounds.fLeft + rand->nextUScalar1() * bounds.width(),
338 bounds.fTop + rand->nextUScalar1() * bounds.height());
339 fVelocity.fX = rand->nextSScalar1();
340 fVelocity.fY = sqrt(1.0f - fVelocity.fX * fVelocity.fX);
341 SkASSERT(SkScalarNearlyEqual(fVelocity.length(), 1.0f));
342 fVelocity *= gMaxSpeeds[fObjType];
343 fRot = 0;
344 fDeltaRot = rand->nextSScalar1() / 32;
345
346 diffTex->setXYWH(SkIntToScalar(kDiffXOff), gYOffs[fObjType],
347 gSizes[fObjType], gSizes[fObjType]);
348 normTex->setXYWH(SkIntToScalar(kNormXOff), gYOffs[fObjType],
349 gSizes[fObjType], gSizes[fObjType]);
350 }
351
352 void initShip(const SkRect& bounds, SkRect* diffTex, SkRect* normTex) {
353 fObjType = kShip_ObjType;
354 fPosition.set(bounds.centerX(), bounds.centerY());
355 fVelocity = SkVector::Make(0.0f, 0.0f);
356 fRot = 0.0f;
357 fDeltaRot = 0.0f;
358
359 diffTex->setXYWH(SkIntToScalar(kDiffXOff), SkIntToScalar(kShipYOff),
360 SkIntToScalar(kMedSize), SkIntToScalar(kMedSize));
361 normTex->setXYWH(SkIntToScalar(kNormXOff), SkIntToScalar(kShipYOff),
362 SkIntToScalar(kMedSize), SkIntToScalar(kMedSize));
363 }
364
365 void advance(const SkRect& bounds) {
366 fPosition += fVelocity;
367 if (fPosition.fX > bounds.right()) {
368 SkASSERT(fVelocity.fX > 0);
369 fVelocity.fX = -fVelocity.fX;
370 } else if (fPosition.fX < bounds.left()) {
371 SkASSERT(fVelocity.fX < 0);
372 fVelocity.fX = -fVelocity.fX;
373 }
374 if (fPosition.fY > bounds.bottom()) {
375 if (fVelocity.fY > 0) {
376 fVelocity.fY = -fVelocity.fY;
377 }
378 } else if (fPosition.fY < bounds.top()) {
379 if (fVelocity.fY < 0) {
380 fVelocity.fY = -fVelocity.fY;
381 }
382 }
383
384 fRot += fDeltaRot;
385 fRot = SkScalarMod(fRot, 2 * SK_ScalarPI);
386 }
387
388 const SkPoint& pos() const { return fPosition; }
389
390 SkScalar rot() const { return fRot; }
391 void setRot(SkScalar rot) { fRot = rot; }
392
393 const SkPoint& velocity() const { return fVelocity; }
394 void setVelocity(const SkPoint& velocity) { fVelocity = velocity; }
395
396 SkRSXform asRSXform() const {
397 static const SkScalar gHalfSizes[kObjTypeCount] = {
398 SkScalarHalf(kBigSize),
399 SkScalarHalf(kMedSize),
400 SkScalarHalf(kSmSize),
401 SkScalarHalf(kMedSize),
402 };
403
404 return SkRSXform::MakeFromRadians(1.0f, fRot, fPosition.x(), fPosition.y(),
405 gHalfSizes[fObjType],
406 gHalfSizes[fObjType]);
407 }
408
409 private:
410 ObjType fObjType;
411 SkPoint fPosition;
412 SkVector fVelocity;
413 SkScalar fRot; // In radians.
414 SkScalar fDeltaRot; // In radiands. Not used by ship.
415 };
416
417private:
418 static const int kNumLights = 2;
419 static const int kNumAsteroids = 6;
420 static const int kNumShips = 1;
421
422 static const int kBigSize = 128;
423 static const int kMedSize = 64;
424 static const int kSmSize = 32;
425 static const int kPad = 1;
426 static const int kAtlasWidth = kBigSize + kBigSize + 2 * kPad; // 2 pads in the middle
427 static const int kAtlasHeight = kBigSize + kMedSize + kSmSize + kMedSize + 3 * kPad;
428
429 static const int kDiffXOff = 0;
430 static const int kNormXOff = kBigSize + 2 * kPad;
431
432 static const int kBigYOff = 0;
433 static const int kMedYOff = kBigSize + kPad;
434 static const int kSmYOff = kMedYOff + kMedSize + kPad;
435 static const int kShipYOff = kSmYOff + kSmSize + kPad;
436 static const int kMaxShipSpeed = 5;
437
438 SkBitmap fAtlas;
439 ObjectRecord fAsteroids[kNumAsteroids];
440 ObjectRecord fShip;
441 SkRect fDiffTex[kNumAsteroids+kNumShips];
442 SkRect fNormTex[kNumAsteroids+kNumShips];
443 SkRect fBounds;
444 bool fUseColors;
445 SkVector3 fLightDir;
446 sk_sp<SkLights> fLights;
447
448 typedef SkDrawable INHERITED;
449};
450
451class DrawLitAtlasView : public SampleView {
452public:
453 DrawLitAtlasView() : fDrawable(new DrawLitAtlasDrawable(SkRect::MakeWH(640, 480))) {}
454
455protected:
456 bool onQuery(SkEvent* evt) override {
457 if (SampleCode::TitleQ(*evt)) {
458 SampleCode::TitleR(evt, "DrawLitAtlas");
459 return true;
460 }
461 SkUnichar uni;
462 if (SampleCode::CharQ(*evt, &uni)) {
463 switch (uni) {
464 case 'C':
465 fDrawable->toggleUseColors();
466 return true;
467 case 'j':
468 fDrawable->left();
469 return true;
470 case 'k':
471 fDrawable->thrust();
472 return true;
473 case 'l':
474 fDrawable->right();
475 return true;
476 case 'o':
477 fDrawable->rotateLight();
478 return true;
479 default:
480 break;
481 }
482 }
483 return this->INHERITED::onQuery(evt);
484 }
485
486 void onDrawContent(SkCanvas* canvas) override {
487 canvas->drawDrawable(fDrawable.get());
488 }
489
490 bool onAnimate(const SkAnimTimer& timer) override {
491 return true;
492 }
493
494private:
495 sk_sp<DrawLitAtlasDrawable> fDrawable;
496
497 typedef SampleView INHERITED;
498};
499
500//////////////////////////////////////////////////////////////////////////////
501
502static SkView* MyFactory() { return new DrawLitAtlasView; }
503static SkViewRegister reg(MyFactory);