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

Side by Side Diff: samplecode/SampleShadowing.cpp

Issue 2246463004: Added distance attenuation and diffuse shading to PointLights (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Cleaned up code 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkPictureRecorder.h" 9 #include "SkPictureRecorder.h"
10 #include "SkShadowPaintFilterCanvas.h" 10 #include "SkShadowPaintFilterCanvas.h"
11 #include "SkShadowShader.h" 11 #include "SkShadowShader.h"
12 #include "SkSurface.h" 12 #include "SkSurface.h"
13 13
14 #ifdef SK_EXPERIMENTAL_SHADOWING 14 #ifdef SK_EXPERIMENTAL_SHADOWING
15 15
16 class ShadowingView : public SampleView { 16 class ShadowingView : public SampleView {
17 public: 17 public:
18 ShadowingView() { 18 ShadowingView() {
19 19
20 this->setBGColor(0xFFCCCCCC); 20 this->setBGColor(0xFFCCCCCC);
21 SkLights::Builder builder; 21 SkLights::Builder builder;
22 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.3f, 0.4f), 22 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.3f, 0.5f, 0.7f) ,
23 SkVector3::Make(0.2f, 0.05f , 1.0f))); 23 SkVector3::Make(150.0f, 150.0f, 3 00.0f)));
24 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.4f, 0.3f, 0.2f), 24 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.7f, 0.5f, 0.3f) ,
25 SkVector3::Make(0.05f, 0.2f , 1.0f))); 25 SkVector3::Make(250.0f, 250.0f, 3 00.0f)));
26 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4 f)));
27 fLights = builder.finish(); 26 fLights = builder.finish();
28 27
29 fTestRects[0].fColor = 0xFFEE8888; 28 fTestRects[0].fColor = 0xFFEE8888;
30 fTestRects[0].fDepth = 80; 29 fTestRects[0].fDepth = 80;
31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); 30 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300);
32 31
33 fTestRects[1].fColor = 0xFF88EE88; 32 fTestRects[1].fColor = 0xFF88EE88;
34 fTestRects[1].fDepth = 160; 33 fTestRects[1].fDepth = 160;
35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); 34 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350);
36 35
37 fTestRects[2].fColor = 0xFF8888EE; 36 fTestRects[2].fColor = 0xFF8888EE;
38 fTestRects[2].fDepth = 240; 37 fTestRects[2].fDepth = 240;
39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); 38 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250);
40 39
41 fSceneChanged = true; 40 fSceneChanged = true;
42 fLightsChanged = true; 41 fLightsChanged = true;
43 42
44 fSelectedRect = -1; 43 fSelectedRect = -1;
45 fMoveLight = false; 44 fMoveLight = false;
46 45
46 fLightDepth = 300.0f;
47 fClearShadowMaps = false; 47 fClearShadowMaps = false;
48 } 48 }
49 49
50 protected: 50 protected:
51 bool onQuery(SkEvent *evt) override { 51 bool onQuery(SkEvent *evt) override {
52 if (SampleCode::TitleQ(*evt)) { 52 if (SampleCode::TitleQ(*evt)) {
53 SampleCode::TitleR(evt, "shadowing"); 53 SampleCode::TitleR(evt, "shadowing");
54 return true; 54 return true;
55 } 55 }
56 56
57 SkUnichar uni; 57 SkUnichar uni;
58 if (SampleCode::CharQ(*evt, &uni)) { 58 if (SampleCode::CharQ(*evt, &uni)) {
59 switch (uni) { 59 switch (uni) {
60 case 'L': 60 case 'L':
61 fMoveLight = !fMoveLight; 61 fMoveLight = !fMoveLight;
62 break; 62 break;
63 case 'd': 63 case 'd':
64 // Raster generated shadow maps have their origin in the UL corner 64 // Raster generated shadow maps have their origin in the UL corner
65 // GPU shadow maps can have an arbitrary origin. 65 // GPU shadow maps can have an arbitrary origin.
66 // We override the 'd' keypress so that when the device is c ycled, 66 // We override the 'd' keypress so that when the device is c ycled,
67 // the shadow maps will be re-generated according to the new backend. 67 // the shadow maps will be re-generated according to the new backend.
68 fClearShadowMaps = true; 68 fClearShadowMaps = true;
69 break; 69 break;
70 case 'q':
71 fLightDepth += 5.0f;
72 fMoveLight = true;
73 break;
74 case 'w':
75 fLightDepth -= 5.0f;
76 fMoveLight = true;
77 break;
70 default: 78 default:
71 break; 79 break;
72 } 80 }
73 } 81 }
74 return this->INHERITED::onQuery(evt); 82 return this->INHERITED::onQuery(evt);
75 } 83 }
76 84
77 sk_sp<SkPicture> makeTestPicture(int width, int height) { 85 sk_sp<SkPicture> makeTestPicture(int width, int height) {
78 SkPictureRecorder recorder; 86 SkPictureRecorder recorder;
79 87
(...skipping 30 matching lines...) Expand all
110 } 118 }
111 119
112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { 120 if (fSceneChanged || fLightsChanged || fClearShadowMaps) {
113 for (int i = 0; i < fLights->numLights(); i++) { 121 for (int i = 0; i < fLights->numLights(); i++) {
114 fLights->light(i).setShadowMap(nullptr); 122 fLights->light(i).setShadowMap(nullptr);
115 } 123 }
116 fSceneChanged = false; 124 fSceneChanged = false;
117 fLightsChanged = false; 125 fLightsChanged = false;
118 fClearShadowMaps = false; 126 fClearShadowMaps = false;
119 127
120 canvas->setLights(fLights);
121 canvas->drawShadowedPicture(fPicture, nullptr, nullptr);
122 } 128 }
129 canvas->setLights(fLights);
130 canvas->drawShadowedPicture(fPicture, nullptr, nullptr);
123 } 131 }
124 132
125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 133 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
126 return new SkView::Click(this); 134 return new SkView::Click(this);
127 } 135 }
128 136
129 bool onClick(Click *click) override { 137 bool onClick(Click *click) override {
130 SkScalar x = click->fCurr.fX; 138 SkScalar x = click->fCurr.fX;
131 SkScalar y = click->fCurr.fY; 139 SkScalar y = click->fCurr.fY;
132 140
133 SkScalar dx = x - click->fPrev.fX; 141 SkScalar dx = x - click->fPrev.fX;
134 SkScalar dy = y - click->fPrev.fY; 142 SkScalar dy = y - click->fPrev.fY;
135 143
136 if (fMoveLight) { 144 if (fMoveLight) {
137 if (dx != 0 || dy != 0) { 145 if (dx != 0 || dy != 0) {
138 float recipX = 1.0f / kWidth; 146 SkLights::Builder builder;
139 float recipY = 1.0f / kHeight; 147 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.3f, 0.5 f, 0.7f),
148 SkVector3::Make(x - 50,
149 350 - y,
150 fLightDep th)));
140 151
141 SkLights::Builder builder; 152 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.7f, 0.5 f, 0.3f),
142 builder.add(SkLights::Light::MakeDirectional( 153 SkVector3::Make(x + 50,
143 SkColor3f::Make(0.2f, 0.3f, 0.4f), 154 450 - y,
144 SkVector3::Make(0.2f + (200.0f - x) * recipX, 155 fLightDep th)));
145 0.05f + (200.0f - y) * recipY,
146 1.0f)));
147 builder.add(SkLights::Light::MakeDirectional(
148 SkColor3f::Make(0.4f, 0.3f, 0.2f),
149 SkVector3::Make(0.05f + (200.0f - x) * recipX,
150 0.2f + (200.0f - y) * recipY,
151 1.0f)));
152 builder.add(SkLights::Light::MakeAmbient(
153 SkColor3f::Make(0.4f, 0.4f, 0.4f)));
154 fLights = builder.finish(); 156 fLights = builder.finish();
155 157
156 fLightsChanged = true; 158 fLightsChanged = true;
157 this->inval(nullptr); 159 this->inval(nullptr);
158 } 160 }
159 return true; 161 return true;
160 } 162 }
161 163
162 if (click->fState == Click::State::kUp_State) { 164 if (click->fState == Click::State::kUp_State) {
163 fSelectedRect = -1; 165 fSelectedRect = -1;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } fTestRects[kNumTestRects]; 203 } fTestRects[kNumTestRects];
202 204
203 int fSelectedRect; 205 int fSelectedRect;
204 bool fMoveLight; 206 bool fMoveLight;
205 207
206 sk_sp<SkPicture> fPicture; 208 sk_sp<SkPicture> fPicture;
207 209
208 bool fSceneChanged; 210 bool fSceneChanged;
209 bool fLightsChanged; 211 bool fLightsChanged;
210 212
213 SkScalar fLightDepth;
214
211 sk_sp<SkLights> fLights; 215 sk_sp<SkLights> fLights;
212 216
213 typedef SampleView INHERITED; 217 typedef SampleView INHERITED;
214 }; 218 };
215 219
216 ////////////////////////////////////////////////////////////////////////////// 220 //////////////////////////////////////////////////////////////////////////////
217 static SkView* MyFactory() { return new ShadowingView; } 221 static SkView* MyFactory() { return new ShadowingView; }
218 static SkViewRegister reg(MyFactory); 222 static SkViewRegister reg(MyFactory);
219 223
220 #endif 224 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698