aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/lightingshader.cpp
diff options
context:
space:
mode:
authorGravatar robertphillips <robertphillips@google.com>2015-08-20 05:15:06 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-08-20 05:15:07 -0700
commit2f0dbc761a626473c19db7de561c7072b12953c5 (patch)
tree5549b8e7359d6a55e171dd1b76634d93b343b491 /gm/lightingshader.cpp
parentd1c6b7c5007b5c609b44a9cdfe95ef64a5a8f29f (diff)
Update SkLightingShader to support rotation
This also: makes the SkLightingShader handle normal maps where the rects aren't aligned between the diffuse and normal maps. adds a light aggregating class (Lights) to SkLightingShader (along with a Builder nested class). Split out of https://codereview.chromium.org/1261433009/ (Add SkCanvas::drawLitAtlas call) Committed: https://skia.googlesource.com/skia/+/45b59ed6e4e231814dbdb9f707b3d2a7ee50de84 Review URL: https://codereview.chromium.org/1291783003
Diffstat (limited to 'gm/lightingshader.cpp')
-rw-r--r--gm/lightingshader.cpp84
1 files changed, 65 insertions, 19 deletions
diff --git a/gm/lightingshader.cpp b/gm/lightingshader.cpp
index 7e33504477..c73ad5b4c8 100644
--- a/gm/lightingshader.cpp
+++ b/gm/lightingshader.cpp
@@ -19,7 +19,7 @@ static SkBitmap make_checkerboard(int texSize) {
sk_tool_utils::draw_checkerboard(&canvas,
sk_tool_utils::color_to_565(0x0),
sk_tool_utils::color_to_565(0xFF804020),
- 2);
+ 8);
return bitmap;
}
@@ -58,10 +58,13 @@ public:
LightingShaderGM() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
- fLight.fColor = SkColor3f::Make(1.0f, 1.0f, 1.0f);
- fLight.fDirection = SkVector3::Make(0.0f, 0.0f, 1.0f);
+ SkLightingShader::Lights::Builder builder;
- fAmbient = SkColor3f::Make(0.1f, 0.1f, 0.1f);
+ builder.add(SkLight(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+ SkVector3::Make(1.0f, 0.0f, 0.0f)));
+ builder.add(SkLight(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+
+ fLights.reset(builder.finish());
}
protected:
@@ -98,11 +101,16 @@ protected:
SkMatrix matrix;
matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
+ const SkMatrix& ctm = canvas->getTotalMatrix();
+
+ // TODO: correctly pull out the pure rotation
+ SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSkewY] };
+
SkAutoTUnref<SkShader> fShader(SkLightingShader::Create(
fDiffuse,
fNormalMaps[mapType],
- fLight, fAmbient,
- &matrix));
+ fLights,
+ invNormRotation, &matrix, &matrix));
SkPaint paint;
paint.setShader(fShader);
@@ -111,17 +119,56 @@ protected:
}
void onDraw(SkCanvas* canvas) override {
- SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
- this->drawRect(canvas, r, kHemi_NormalMap);
-
- r.offset(kGMSize - kTexSize, 0);
- this->drawRect(canvas, r, kFrustum_NormalMap);
-
- r.offset(0, kGMSize - kTexSize);
- this->drawRect(canvas, r, kTetra_NormalMap);
-
- r.offset(kTexSize - kGMSize, 0);
- this->drawRect(canvas, r, kHemi_NormalMap);
+ SkMatrix m;
+ SkRect r;
+
+ {
+ r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
+ this->drawRect(canvas, r, kHemi_NormalMap);
+
+ canvas->save();
+ m.setRotate(45.0f, r.centerX(), r.centerY());
+ m.postTranslate(kGMSize/2.0f - kTexSize/2.0f, 0.0f);
+ canvas->setMatrix(m);
+ this->drawRect(canvas, r, kHemi_NormalMap);
+ canvas->restore();
+ }
+
+ {
+ r.offset(kGMSize - kTexSize, 0);
+ this->drawRect(canvas, r, kFrustum_NormalMap);
+
+ canvas->save();
+ m.setRotate(45.0f, r.centerX(), r.centerY());
+ m.postTranslate(0.0f, kGMSize/2.0f - kTexSize/2.0f);
+ canvas->setMatrix(m);
+ this->drawRect(canvas, r, kFrustum_NormalMap);
+ canvas->restore();
+ }
+
+ {
+ r.offset(0, kGMSize - kTexSize);
+ this->drawRect(canvas, r, kTetra_NormalMap);
+
+ canvas->save();
+ m.setRotate(45.0f, r.centerX(), r.centerY());
+ m.postTranslate(-kGMSize/2.0f + kTexSize/2.0f, 0.0f);
+ canvas->setMatrix(m);
+ this->drawRect(canvas, r, kTetra_NormalMap);
+ canvas->restore();
+ }
+
+ {
+ r.offset(kTexSize - kGMSize, 0);
+ this->drawRect(canvas, r, kHemi_NormalMap);
+
+ canvas->save();
+ m.setRotate(45.0f, r.centerX(), r.centerY());
+ m.postTranslate(0.0f, -kGMSize/2.0f + kTexSize/2.0f);
+ canvas->setMatrix(m);
+ this->drawRect(canvas, r, kHemi_NormalMap);
+ canvas->restore();
+ }
}
private:
@@ -131,8 +178,7 @@ private:
SkBitmap fDiffuse;
SkBitmap fNormalMaps[kNormalMapCount];
- SkLightingShader::Light fLight;
- SkColor3f fAmbient;
+ SkAutoTUnref<const SkLightingShader::Lights> fLights;
typedef GM INHERITED;
};