Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Unified Diff: samplecode/SampleShadowing.cpp

Issue 2198933002: Making a sample for shadow maps for more intensive development (Closed) Base URL: https://skia.googlesource.com/skia@shadow-gm
Patch Set: undo change Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: samplecode/SampleShadowing.cpp
diff --git a/samplecode/SampleShadowing.cpp b/samplecode/SampleShadowing.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d35c9d0742a5bf7ae8f88f17f96f3c07873a0384
--- /dev/null
+++ b/samplecode/SampleShadowing.cpp
@@ -0,0 +1,304 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SampleCode.h"
robertphillips 2016/08/02 16:05:06 Do we need SkDrawFilter.h ?
vjiaoblack 2016/08/03 14:04:21 Done.
+#include "SkDrawFilter.h"
+#include "SkNormalSource.h"
robertphillips 2016/08/02 16:05:06 Same for SkPathEffect.h
vjiaoblack 2016/08/03 14:04:21 Done.
+#include "SkPathEffect.h"
+#include "SkPictureRecorder.h"
+#include "SkSurface.h"
+#include "SkShadowPaintFilterCanvas.h"
+#include "SkShadowShader.h"
+
+#ifdef SK_EXPERIMENTAL_SHADOWING
+
+constexpr int NUM_TEST_RECTS = 3;
+
+static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth,
+ sk_sp<SkImage> diffuse,
+ sk_sp<SkLights> lights) {
+
+ sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode);
+
+ sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode);
+
+ return SkShadowShader::Make(std::move(povDepthShader),
+ std::move(diffuseShader),
+ std::move(lights),
+ diffuse->width(), diffuse->height());
+}
+
+
+class IndexClick : public SkView::Click {
+ int fIndex;
+public:
robertphillips 2016/08/02 16:05:06 add INHERITED and use it here
vjiaoblack 2016/08/03 14:04:21 Done.
+ IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
+
+ static int GetIndex(SkView::Click* click) {
+ return ((IndexClick*)click)->fIndex;
+ }
+};
+
+class ShadowingView : public SampleView {
+ enum {
+ kZoom = 96
+ };
+
+public:
+ ShadowingView() {
+
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ SkLights::Builder builder;
+ builder.add(SkLights::Light(SkColor3f::Make(0.1f, 0.2f, 0.3f),
+ SkVector3::Make(0.27f, 0.07f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.3f, 0.2f, 0.1f),
+ SkVector3::Make(0.03f, 0.27f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.15f, 0.1f),
+ SkVector3::Make(0.21f, 0.21f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.1f, 0.15f, 0.2f),
+ SkVector3::Make(0.09f, 0.09f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.3f, 0.3f, 0.3f)));
+ this->fLights = builder.finish();
+
robertphillips 2016/08/02 16:05:07 don't need this-> on member variables
vjiaoblack 2016/08/03 14:04:21 Done.
+ this->fColors[0] = 0xFFEE8888;
+ this->fDepths[0] = 80;
+ this->fRects[0] = SkRect::MakeLTRB(200,150,350,300);
+
+ this->fColors[1] = 0xFF88EE88;
+ this->fDepths[1] = 160;
+ this->fRects[1] = SkRect::MakeLTRB(150,200,300,350);
+
+ this->fColors[2] = 0xFF8888EE;
+ this->fDepths[2] = 240;
+ this->fRects[2] = SkRect::MakeLTRB(100,100,250,250);
+ }
+
+protected:
+ static const int kWidth = 400;
+ static const int kHeight = 400;
+ SkRect fRects[NUM_TEST_RECTS];
+ int fDepths[NUM_TEST_RECTS];
+ SkColor fColors[NUM_TEST_RECTS];
+ int fSelectedRect = -1;
+ bool fMoveLight = false;
+
+ bool onQuery(SkEvent *evt) override {
+ if (SampleCode::TitleQ(*evt)) {
+ SampleCode::TitleR(evt, "shadowing");
+ return true;
+ }
+
+ SkUnichar uni;
+ if (SampleCode::CharQ(*evt, &uni)) {
+ switch (uni) {
+ case 'L':
+ fMoveLight = !fMoveLight;
+ break;
+ default:
+ break;
+ }
+ }
+ return this->INHERITED::onQuery(evt);
+ }
+
robertphillips 2016/08/02 16:05:06 This is file static naming. You either need to mak
vjiaoblack 2016/08/03 14:04:21 Done.
+ sk_sp<SkPicture> make_test_picture(int width, int height) {
+ SkPictureRecorder recorder;
+
+ // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory)
+ SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height));
+
+ SkASSERT(canvas->getTotalMatrix().isIdentity());
+ SkPaint paint;
+ paint.setColor(SK_ColorGRAY);
+
+ // LONG RANGE TODO: tag occluders
+ // LONG RANGE TODO: track number of IDs we need (hopefully less than 256)
+ // and determinate the mapping from z to id
+
+ // universal receiver, "ground"
+ canvas->drawRect(SkRect::MakeIWH(width, height), paint);
+
+ for (int i = 0; i < NUM_TEST_RECTS; i++) {
+ paint.setColor(fColors[i]);
+ if (i == 0) {
+ canvas->translateZ(fDepths[0]);
+ } else {
+ canvas->translateZ(fDepths[i] - fDepths[i-1]);
+ }
+ canvas->drawRect(fRects[i], paint);
+ }
+
+ return recorder.finishRecordingAsPicture();
+ }
+
robertphillips 2016/08/02 16:05:07 SkTMin ?
vjiaoblack 2016/08/03 14:04:21 Done.
+#define MIN(x,y) ((x) > (y) ? (y) : (x))
+ void onDrawContent(SkCanvas *canvas) override {
+ // This picture stores the picture of the scene.
+ // It's used to generate the depth maps.
robertphillips 2016/08/02 16:05:06 Do you need to recreate this every time ?
vjiaoblack 2016/08/03 14:04:21 Done.
+ sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight));
+
robertphillips 2016/08/02 16:05:07 Don't you only need to recreate the shadow map if
vjiaoblack 2016/08/03 14:04:21 Done.
+ for (int i = 0; i < fLights->numLights(); ++i) {
+ // skip over ambient lights; they don't cast shadows
+ if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) {
+ continue;
+ }
+
+ // TODO: maybe add a kDepth_8_SkColorType when vertices have depth
+
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
+ // Create a new surface (that matches the backend of canvas)
+ // for each shadow map
+ sk_sp<SkSurface> surf(canvas->makeSurface(info));
+
+ // Wrap another SPFCanvas around the surface
+ sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
+ sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
+
+ // set the depth map canvas to have the light we're drawing.
+ SkLights::Builder builder;
+ builder.add(fLights->light(i));
+ sk_sp<SkLights> curLight = builder.finish();
+
+ depthMapCanvas->setLights(std::move(curLight));
+ depthMapCanvas->drawPicture(pic);
+
+ fLights->light(i).setShadowMap(surf->makeImageSnapshot());
+ }
+
+ sk_sp<SkImage> povDepthMap;
+ sk_sp<SkImage> diffuseMap;
+
+ // TODO: pass the depth to the shader in vertices, or uniforms
+ // so we don't have to render depth and color separately
+
+ // povDepthMap
+ {
+ SkLights::Builder builder;
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+ SkVector3::Make(0.0f, 0.0f, 1.0f)));
+ sk_sp<SkLights> povLight = builder.finish();
+
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
+ // Create a new surface (that matches the backend of canvas)
+ // to create the povDepthMap
+ sk_sp<SkSurface> surf(canvas->makeSurface(info));
+
+ // Wrap another SPFCanvas around the surface
+ sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
+ sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
+
+ // set the depth map canvas to have the light as the user's POV
+ depthMapCanvas->setLights(std::move(povLight));
+
+ depthMapCanvas->drawPicture(pic);
+
+ povDepthMap = surf->makeImageSnapshot();
+ }
+
+ // diffuseMap
+ {
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
+ sk_sp<SkSurface> surf(canvas->makeSurface(info));
+ surf->getCanvas()->drawPicture(pic);
+
+ diffuseMap = surf->makeImageSnapshot();
+ }
+
+ SkPaint paint;
+ paint.setShader(make_shadow_shader(std::move(povDepthMap),
+ std::move(diffuseMap),
+ fLights));
+
+ canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
+ }
+
+ SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
+ int index = 0;
+
+ return new IndexClick(this, index);
+ }
+
+ bool onClick(Click *click) override {
+ SkScalar x = click->fCurr.fX;
+ SkScalar y = click->fCurr.fY;
+
+ SkScalar dx = x - click->fPrev.fX;
+ SkScalar dy = y - click->fPrev.fY;
+
+ if (fMoveLight) {
robertphillips 2016/08/02 16:05:07 Rebuild the object here ...
vjiaoblack 2016/08/03 14:04:21 Done.
+ fLights->light(0).setDir(SkVector3::Make( 0.12f + (200.0f - x) / 400.0f,
+ -0.08f + (200.0f - y) / 400.0f, 1.0f));
+ fLights->light(1).setDir(SkVector3::Make(-0.12f + (200.0f - x) / 400.0f,
+ 0.12f + (200.0f - y) / 400.0f, 1.0f));
+ fLights->light(2).setDir(SkVector3::Make( 0.06f + (200.0f - x) / 400.0f,
+ 0.06f + (200.0f - y) / 400.0f, 1.0f));
+ fLights->light(3).setDir(SkVector3::Make(-0.06f + (200.0f - x) / 400.0f,
+ -0.06f + (200.0f - y) / 400.0f, 1.0f));
+ return true;
+ }
+
+ if (click->fState == Click::State::kUp_State) {
+ fSelectedRect = -1;
+ return true;
+ }
+
+ if (fSelectedRect > -1) {
+ SkScalar x = click->fCurr.fX;
+ SkScalar y = click->fCurr.fY;
+
+ SkScalar dx = x - click->fPrev.fX;
+ SkScalar dy = y - click->fPrev.fY;
+
+ fRects[fSelectedRect].fLeft += dx;
+ fRects[fSelectedRect].fRight += dx;
+ fRects[fSelectedRect].fTop += dy;
+ fRects[fSelectedRect].fBottom += dy;
+
+ return true;
+ }
+
+ // assume last elements are highest
+ for (int i = NUM_TEST_RECTS - 1; i >= 0; i--) {
+ if (fRects[i].contains(SkRect::MakeXYWH(x, y, 1, 1))) {
+ fSelectedRect = i;
+ fRects[i].fLeft += dx;
+ fRects[i].fRight += dx;
+ fRects[i].fTop += dy;
+ fRects[i].fBottom += dy;
+
+ break;
+ }
+ }
+
+ return true;
+ }
+
+
+private:
+ sk_sp<SkLights> fLights;
+
+ typedef SampleView INHERITED;
+};
+
+
+//////////////////////////////////////////////////////////////////////////////
+static SkView* MyFactory() { return new ShadowingView; }
+static SkViewRegister reg(MyFactory);
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698