aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar robertphillips <robertphillips@google.com>2016-06-03 15:48:16 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-06-03 15:48:16 -0700
commit862dae548a8ef40804bc9146d5c7b9f360efa960 (patch)
tree0cef8b474b9ff8bb55ae4d65ce7c0e65f19fddaa
parent69df0206252ce0e6293c60274a1dfc363bb2e3b5 (diff)
Add ability to rotate light direction in drawLitAtlas Sample slide
-rw-r--r--samplecode/SampleLitAtlas.cpp63
-rw-r--r--src/core/SkLightingShader.cpp4
2 files changed, 56 insertions, 11 deletions
diff --git a/samplecode/SampleLitAtlas.cpp b/samplecode/SampleLitAtlas.cpp
index 4f989af1ae..f1d67e5c79 100644
--- a/samplecode/SampleLitAtlas.cpp
+++ b/samplecode/SampleLitAtlas.cpp
@@ -21,7 +21,8 @@ class DrawLitAtlasDrawable : public SkDrawable {
public:
DrawLitAtlasDrawable(const SkRect& r)
: fBounds(r)
- , fUseColors(false) {
+ , fUseColors(false)
+ , fLightDir(SkVector3::Make(1.0f, 0.0f, 0.0f)) {
fAtlas = MakeAtlas();
SkRandom rand;
@@ -31,19 +32,25 @@ public:
fShip.initShip(fBounds, &fDiffTex[kNumAsteroids], &fNormTex[kNumAsteroids]);
- SkLights::Builder builder;
-
- builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
- SkVector3::Make(1.0f, 0.0f, 0.0f)));
- builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
-
- fLights = builder.finish();
+ this->updateLights();
}
void toggleUseColors() {
fUseColors = !fUseColors;
}
+ void rotateLight() {
+ SkScalar c;
+ SkScalar s = SkScalarSinCos(SK_ScalarPI/6.0f, &c);
+
+ SkScalar newX = c * fLightDir.fX - s * fLightDir.fY;
+ SkScalar newY = s * fLightDir.fX + c * fLightDir.fY;
+
+ fLightDir.set(newX, newY, 0.0f);
+
+ this->updateLights();
+ }
+
void left() {
SkScalar newRot = SkScalarMod(fShip.rot() + (2*SK_ScalarPI - SK_ScalarPI/32.0f),
2 * SK_ScalarPI);
@@ -91,7 +98,9 @@ protected:
#ifdef SK_DEBUG
canvas->drawBitmap(fAtlas, 0, 0); // just to see the atlas
-#endif
+
+ this->drawLightDir(canvas, fBounds.centerX(), fBounds.centerY());
+#endif
#if 0
// TODO: revitalize when drawLitAtlas API lands
@@ -121,6 +130,7 @@ protected:
// TODO: correctly pull out the pure rotation
SkVector invNormRotation = { m[SkMatrix::kMScaleX], m[SkMatrix::kMSkewY] };
+ SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
paint.setShader(SkLightingShader::Make(fAtlas, fAtlas, fLights,
invNormRotation, &diffMat, &normalMat));
@@ -165,6 +175,36 @@ private:
static const int kObjTypeCount = kLast_ObjType + 1;
+ void updateLights() {
+ SkLights::Builder builder;
+
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), fLightDir));
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+
+ fLights = builder.finish();
+ }
+
+#ifdef SK_DEBUG
+ // Draw a vector to the light
+ void drawLightDir(SkCanvas* canvas, SkScalar centerX, SkScalar centerY) {
+ static const int kBgLen = 30;
+ static const int kSmLen = 5;
+
+ // TODO: change the lighting coordinate system to be right handed
+ SkPoint p1 = SkPoint::Make(centerX + kBgLen * fLightDir.fX,
+ centerY - kBgLen * fLightDir.fY);
+ SkPoint p2 = SkPoint::Make(centerX + (kBgLen-kSmLen) * fLightDir.fX,
+ centerY - (kBgLen-kSmLen) * fLightDir.fY);
+
+ SkPaint p;
+ canvas->drawLine(centerX, centerY, p1.fX, p1.fY, p);
+ canvas->drawLine(p1.fX, p1.fY,
+ p2.fX - kSmLen * fLightDir.fY, p2.fY - kSmLen * fLightDir.fX, p);
+ canvas->drawLine(p1.fX, p1.fY,
+ p2.fX + kSmLen * fLightDir.fY, p2.fY + kSmLen * fLightDir.fX, p);
+ }
+#endif
+
// Create the mixed diffuse & normal atlas
//
// big color circle | big normal hemi
@@ -398,6 +438,7 @@ private:
SkRect fNormTex[kNumAsteroids+kNumShips];
SkRect fBounds;
bool fUseColors;
+ SkVector3 fLightDir;
sk_sp<SkLights> fLights;
typedef SkDrawable INHERITED;
@@ -434,6 +475,10 @@ protected:
fDrawable->right();
this->inval(NULL);
return true;
+ case 'o':
+ fDrawable->rotateLight();
+ this->inval(NULL);
+ return true;
default:
break;
}
diff --git a/src/core/SkLightingShader.cpp b/src/core/SkLightingShader.cpp
index cb25944a01..d011712fea 100644
--- a/src/core/SkLightingShader.cpp
+++ b/src/core/SkLightingShader.cpp
@@ -510,8 +510,8 @@ void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y,
xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX +
lightShader.fInvNormRotation.fY * norm.fY;
- xformedNorm.fY = lightShader.fInvNormRotation.fX * norm.fX -
- lightShader.fInvNormRotation.fY * norm.fY;
+ xformedNorm.fY = -lightShader.fInvNormRotation.fY * norm.fX +
+ lightShader.fInvNormRotation.fX * norm.fY;
xformedNorm.fZ = norm.fZ;
SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]);