blob: ad8823d27d68d69c08c256c7c01e929e82d10509 [file] [log] [blame]
robertphillips5f865b92015-07-29 12:28:04 -07001/*
2 * Copyright 2015 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"
9
10#include "SkColorPriv.h"
11#include "SkLightingShader.h"
12
13static SkBitmap make_checkerboard(int texSize) {
14 SkBitmap bitmap;
15 bitmap.allocN32Pixels(texSize, texSize);
16
17 SkCanvas canvas(bitmap);
18 sk_tool_utils::draw_checkerboard(&canvas,
19 sk_tool_utils::color_to_565(0x0),
20 sk_tool_utils::color_to_565(0xFF804020),
robertphillips640898f2015-07-30 05:09:17 -070021 2);
robertphillips5f865b92015-07-29 12:28:04 -070022 return bitmap;
23}
24
25// Create a hemispherical normal map
robertphillips640898f2015-07-30 05:09:17 -070026static SkBitmap make_hemi_normalmap(int texSize) {
robertphillips5f865b92015-07-29 12:28:04 -070027 SkBitmap hemi;
28 hemi.allocN32Pixels(texSize, texSize);
29
30 for (int y = 0; y < texSize; ++y) {
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 = sqrt(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;
50}
51
robertphillips640898f2015-07-30 05:09:17 -070052// Create a truncated pyramid normal map
53static SkBitmap make_frustum_normalmap(int texSize) {
54 SkBitmap frustum;
55 frustum.allocN32Pixels(texSize, texSize);
56
57 SkIRect inner = SkIRect::MakeWH(texSize, texSize);
58 inner.inset(texSize/4, texSize/4);
59
60 SkPoint3 norm;
61 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
62 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
63 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
64 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
65
66 for (int y = 0; y < texSize; ++y) {
67 for (int x = 0; x < texSize; ++x) {
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}
robertphillips5f865b92015-07-29 12:28:04 -070099
100namespace skiagm {
101
102// This GM exercises lighting shaders.
103class LightingShaderGM : public GM {
104public:
105 LightingShaderGM() {
106 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
robertphillips640898f2015-07-30 05:09:17 -0700107
108 fLight.fColor = SkColorSetRGB(0xff, 0xff, 0xff);
109 fLight.fDirection.fX = 0.0f;
110 fLight.fDirection.fY = 0.0f;
111 fLight.fDirection.fZ = 1.0f;
112
113 fAmbient = SkColorSetRGB(0x1f, 0x1f, 0x1f);
robertphillips5f865b92015-07-29 12:28:04 -0700114 }
115
116protected:
117
118 SkString onShortName() override {
119 return SkString("lightingshader");
120 }
121
122 SkISize onISize() override {
robertphillips640898f2015-07-30 05:09:17 -0700123 return SkISize::Make(kGMSize, kGMSize);
robertphillips5f865b92015-07-29 12:28:04 -0700124 }
125
126 void onOnceBeforeDraw() override {
127 fDiffuse = make_checkerboard(kTexSize);
robertphillips640898f2015-07-30 05:09:17 -0700128 fHemiNormalMap = make_hemi_normalmap(kTexSize);
129 fFrustumNormalMap = make_frustum_normalmap(kTexSize);
robertphillips5f865b92015-07-29 12:28:04 -0700130 }
131
robertphillips640898f2015-07-30 05:09:17 -0700132 void drawRect(SkCanvas* canvas, const SkRect& r, bool hemi) {
robertphillips5f865b92015-07-29 12:28:04 -0700133
robertphillips640898f2015-07-30 05:09:17 -0700134 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height());
robertphillips5f865b92015-07-29 12:28:04 -0700135
robertphillips640898f2015-07-30 05:09:17 -0700136 SkMatrix matrix;
137 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
138
139 SkAutoTUnref<SkShader> fShader(SkLightingShader::Create(
140 fDiffuse,
141 hemi ? fHemiNormalMap : fFrustumNormalMap,
142 fLight, fAmbient,
143 &matrix));
robertphillips5f865b92015-07-29 12:28:04 -0700144
145 SkPaint paint;
146 paint.setShader(fShader);
147
robertphillips5f865b92015-07-29 12:28:04 -0700148 canvas->drawRect(r, paint);
149 }
150
robertphillips640898f2015-07-30 05:09:17 -0700151 void onDraw(SkCanvas* canvas) override {
152 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
153 this->drawRect(canvas, r, true);
154
155 r.offset(kGMSize - kTexSize, 0);
156 this->drawRect(canvas, r, false);
157
158 r.offset(0, kGMSize - kTexSize);
159 this->drawRect(canvas, r, true);
160
161 r.offset(kTexSize - kGMSize, 0);
162 this->drawRect(canvas, r, false);
163 }
164
robertphillips5f865b92015-07-29 12:28:04 -0700165private:
166 static const int kTexSize = 128;
robertphillips640898f2015-07-30 05:09:17 -0700167 static const int kGMSize = 512;
robertphillips5f865b92015-07-29 12:28:04 -0700168
robertphillips640898f2015-07-30 05:09:17 -0700169 SkBitmap fDiffuse;
170 SkBitmap fHemiNormalMap;
171 SkBitmap fFrustumNormalMap;
172
173 SkLightingShader::Light fLight;
174 SkColor fAmbient;
robertphillips5f865b92015-07-29 12:28:04 -0700175
176 typedef GM INHERITED;
177};
178
179//////////////////////////////////////////////////////////////////////////////
180
181DEF_GM( return SkNEW(LightingShaderGM); )
182
183}