OLD | NEW |
---|---|
(Empty) | |
1 /* | |
robertphillips
2016/07/11 14:02:04
2016
dvonbeck
2016/07/11 19:37:29
Done.
| |
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 "SkBitmapProcShader.h" | |
11 #include "SkLightingShader.h" | |
12 #include "SkNormalSource.h" | |
13 #include "SkPoint3.h" | |
14 #include "SkShader.h" | |
15 | |
16 // Create a truncated pyramid normal map | |
17 static SkBitmap make_frustum_normalmap(int texSize) { | |
18 SkBitmap frustum; | |
19 frustum.allocN32Pixels(texSize, texSize); | |
20 | |
21 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize)); | |
22 return frustum; | |
23 } | |
24 | |
25 namespace skiagm { | |
26 | |
27 // This GM exercises lighting shaders. | |
robertphillips
2016/07/11 14:02:04
// more ...
// what does it do besides what the or
dvonbeck
2016/07/11 19:37:29
Done.
| |
28 class LightingShader2GM : public GM { | |
29 public: | |
30 LightingShader2GM() { | |
31 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | |
32 | |
33 SkLights::Builder builder; | |
34 | |
35 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
robertphillips
2016/07/11 14:02:04
normalize this so the reader has a better expectat
dvonbeck
2016/07/11 19:37:29
Done.
| |
36 SkVector3::Make(1.0f, 0.5f, 0.6f))); | |
37 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | |
38 | |
39 fLights = builder.finish(); | |
40 } | |
41 | |
42 protected: | |
robertphillips
2016/07/11 14:02:04
extra spaces ?
dvonbeck
2016/07/11 19:37:29
Done.
| |
43 | |
44 | |
45 SkString onShortName() override { | |
46 return SkString("lightingshader2"); | |
47 } | |
48 | |
49 SkISize onISize() override { | |
50 return SkISize::Make(kGMSize, kGMSize); | |
51 } | |
52 | |
53 void onOnceBeforeDraw() override { | |
robertphillips
2016/07/11 14:02:04
Can we create the lights in here too ?
dvonbeck
2016/07/11 19:37:28
Done. Why?
robertphillips
2016/07/11 20:40:55
The problem with doing stuff in the constructor is
| |
54 fDiffuse = sk_tool_utils::create_checkerboard_bitmap( | |
55 kTexSize, kTexSize, | |
56 sk_tool_utils::color_to_ 565(0x0), | |
57 sk_tool_utils::color_to_ 565(0xFF804020), | |
58 8); | |
59 | |
60 fNormalMap = make_frustum_normalmap(kTexSize); | |
61 } | |
62 | |
robertphillips
2016/07/11 14:02:04
// ... ?
dvonbeck
2016/07/11 19:37:29
Done.
| |
63 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const { | |
64 canvas->translate(kGMSize / 2.0f, kGMSize / 2.0f); | |
65 canvas->scale(scaleX, scaleY); | |
66 canvas->rotate(rotate); | |
67 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f); | |
68 } | |
69 | |
robertphillips
2016/07/11 14:02:03
Hmmm, there is a lot of duplicate code here. Can w
dvonbeck
2016/07/11 19:37:29
Done.
| |
70 void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar s caleY, | |
71 SkScalar rotate) { | |
robertphillips
2016/07/11 14:02:04
this->
dvonbeck
2016/07/11 19:37:29
Done.
| |
72 positionCTM(canvas, scaleX, scaleY, rotate); | |
73 | |
74 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( )); | |
75 | |
76 SkMatrix matrix; | |
77 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | |
78 | |
79 const SkMatrix& ctm = canvas->getTotalMatrix(); | |
80 | |
81 SkPaint paint; | |
82 sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kCl amp_TileMode, | |
83 SkShader::kClamp_TileMode , &matrix, nullptr); | |
84 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(s td::move(normalMap), | |
85 c tm); | |
86 sk_sp<SkShader> diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::k Clamp_TileMode, | |
87 SkShader::kClamp_TileMode, &matrix, nullptr); | |
88 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::mo ve(normalSource), | |
89 fLights)); | |
90 | |
91 canvas->drawRect(r, paint); | |
92 } | |
93 | |
94 void drawRectFromNullDiffuse(SkCanvas* canvas, const SkRect& r, SkScalar sca leX, | |
95 SkScalar scaleY, SkScalar rotate) { | |
96 positionCTM(canvas, scaleX, scaleY, rotate); | |
97 | |
98 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( )); | |
99 | |
100 SkMatrix matrix; | |
101 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | |
102 | |
103 const SkMatrix& ctm = canvas->getTotalMatrix(); | |
104 | |
105 SkPaint paint; | |
106 sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kCl amp_TileMode, | |
107 SkShader::kClamp_TileMode , &matrix, nullptr); | |
108 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(s td::move(normalMap), | |
109 c tm); | |
110 paint.setShader(SkLightingShader::Make(nullptr, std::move(normalSource), | |
111 fLights)); | |
112 paint.setColor(SK_ColorGREEN); | |
113 | |
114 canvas->drawRect(r, paint); | |
115 } | |
116 | |
117 void drawRectFromNullNormalSource(SkCanvas* canvas, const SkRect& r, SkScala r scaleX, | |
118 SkScalar scaleY, SkScalar rotate) { | |
119 positionCTM(canvas, scaleX, scaleY, rotate); | |
120 | |
121 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( )); | |
122 | |
123 SkMatrix matrix; | |
124 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | |
125 | |
126 SkPaint paint; | |
127 sk_sp<SkShader> diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::k Clamp_TileMode, | |
128 SkShader::kClamp_TileMode, &matrix, nullptr); | |
129 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), nullptr , fLights)); | |
130 | |
131 canvas->drawRect(r, paint); | |
132 } | |
133 | |
134 void onDraw(SkCanvas* canvas) override { | |
robertphillips
2016/07/11 14:02:04
one line ?
dvonbeck
2016/07/11 19:37:28
Done.
| |
135 SkRect r; | |
136 r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize)); | |
137 | |
138 canvas->save(); | |
robertphillips
2016/07/11 14:02:04
SK_ScalarSqrt2 ?
dvonbeck
2016/07/11 19:37:29
Done.
| |
139 canvas->translate(0.0f, -SkScalarSqrt(2.0f) * kTexSize); | |
140 this->drawRect(canvas, r, 1.0f, 1.0f, 45.f); | |
141 canvas->restore(); | |
142 | |
143 canvas->save(); | |
144 canvas->translate(-SkScalarSqrt(2.0f) * kTexSize, -SkScalarSqrt(2.0f) * kTexSize); | |
145 this->drawRect(canvas, r, 1.2f, 1.2f, 0.f); | |
146 canvas->restore(); | |
147 | |
148 canvas->save(); | |
149 canvas->translate(SkScalarSqrt(2.0f) * kTexSize, -SkScalarSqrt(2.0f) * k TexSize); | |
150 this->drawRect(canvas, r, 0.5f, 0.5f, 0.f); | |
151 canvas->restore(); | |
152 | |
153 canvas->save(); | |
154 canvas->translate(-SkScalarSqrt(2.0f) * kTexSize, 0.0f); | |
155 this->drawRectFromNullDiffuse(canvas, r, 1.0f, 1.0f, 0.f); | |
156 canvas->restore(); | |
157 | |
158 canvas->save(); | |
159 canvas->translate(SkScalarSqrt(2.0f) * kTexSize, 0.0f); | |
160 this->drawRectFromNullNormalSource(canvas, r, 1.0f, 1.0f, 0.f); | |
161 canvas->restore(); | |
162 } | |
163 | |
164 private: | |
165 static const int kTexSize = 128; | |
166 static const int kGMSize = 512; | |
167 | |
168 SkBitmap fDiffuse; | |
169 SkBitmap fNormalMap; | |
170 | |
171 sk_sp<SkLights> fLights; | |
172 | |
173 typedef GM INHERITED; | |
174 }; | |
175 | |
176 ////////////////////////////////////////////////////////////////////////////// | |
177 | |
178 DEF_GM(return new LightingShader2GM;) | |
179 } | |
OLD | NEW |