• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "Sample.h"
9 #include "SkAnimTimer.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPatchUtils.h"
13 #include "SkPerlinNoiseShader.h"
14 
draw_control_points(SkCanvas * canvas,const SkPoint cubics[12])15 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
16     //draw control points
17     SkPaint paint;
18     SkPoint bottom[SkPatchUtils::kNumPtsCubic];
19     SkPatchUtils::GetBottomCubic(cubics, bottom);
20     SkPoint top[SkPatchUtils::kNumPtsCubic];
21     SkPatchUtils::GetTopCubic(cubics, top);
22     SkPoint left[SkPatchUtils::kNumPtsCubic];
23     SkPatchUtils::GetLeftCubic(cubics, left);
24     SkPoint right[SkPatchUtils::kNumPtsCubic];
25     SkPatchUtils::GetRightCubic(cubics, right);
26 
27     paint.setColor(SK_ColorBLACK);
28     paint.setStrokeWidth(0.5f);
29     SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
30     canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
31     canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
32     canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
33     canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
34     canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
35 
36     canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
37     canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
38     canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
39 
40     paint.setStrokeWidth(2);
41 
42     paint.setColor(SK_ColorRED);
43     canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
44 
45     paint.setColor(SK_ColorBLUE);
46     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
47 
48     paint.setColor(SK_ColorCYAN);
49     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
50 
51     paint.setColor(SK_ColorYELLOW);
52     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
53 
54     paint.setColor(SK_ColorGREEN);
55     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
56 }
57 
58 // These are actually half the total width and hieghts
59 const SkScalar TexWidth = 100.0f;
60 const SkScalar TexHeight = 100.0f;
61 
62 class PerlinPatchView : public Sample {
63     sk_sp<SkShader> fShader0;
64     sk_sp<SkShader> fShader1;
65     sk_sp<SkShader> fShaderCompose;
66     SkScalar fXFreq;
67     SkScalar fYFreq;
68     SkScalar fSeed;
69     SkPoint  fPts[SkPatchUtils::kNumCtrlPts];
70     SkScalar fTexX;
71     SkScalar fTexY;
72     SkScalar fTexScale;
73     SkMatrix fInvMatrix;
74     bool     fShowGrid = false;
75 
76 public:
PerlinPatchView()77     PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f),
78                         fTexX(100.0), fTexY(50.0), fTexScale(1.0f) {
79         const SkScalar s = 2;
80         // The order of the colors and points is clockwise starting at upper-left corner.
81         //top points
82         fPts[0].set(100 * s, 100 * s);
83         fPts[1].set(150 * s, 50 * s);
84         fPts[2].set(250 * s, 150 * s);
85         fPts[3].set(300 * s, 100 * s);
86         //right points
87         fPts[4].set(275 * s, 150 * s);
88         fPts[5].set(350 * s, 250 * s);
89         //bottom points
90         fPts[6].set(300 * s, 300 * s);
91         fPts[7].set(250 * s, 250 * s);
92         //left points
93         fPts[8].set(150 * s, 350 * s);
94         fPts[9].set(100 * s, 300 * s);
95         fPts[10].set(50 * s, 250 * s);
96         fPts[11].set(150 * s, 150 * s);
97 
98         const SkColor colors[SkPatchUtils::kNumCorners] = {
99             0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF
100         };
101         const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f),
102                                     SkPoint::Make(100.0f, 100.0f) };
103         fShader0 = SkGradientShader::MakeLinear(points,
104                                                   colors,
105                                                   nullptr,
106                                                   3,
107                                                   SkShader::kMirror_TileMode,
108                                                   0,
109                                                   nullptr);
110     }
111 
112 protected:
onQuery(Sample::Event * evt)113     bool onQuery(Sample::Event* evt)  override {
114         if (Sample::TitleQ(*evt)) {
115             Sample::TitleR(evt, "PerlinPatch");
116             return true;
117         }
118         SkUnichar uni;
119         if (Sample::CharQ(*evt, &uni)) {
120             switch (uni) {
121                 case 'g': fShowGrid = !fShowGrid; return true;
122                 default: break;
123             }
124         }
125         return this->INHERITED::onQuery(evt);
126     }
127 
onAnimate(const SkAnimTimer & timer)128     bool onAnimate(const SkAnimTimer& timer) override {
129         fSeed += 0.005f;
130         return true;
131     }
132 
133 
onDrawContent(SkCanvas * canvas)134     void onDrawContent(SkCanvas* canvas) override {
135         if (!canvas->getTotalMatrix().invert(&fInvMatrix)) {
136             return;
137         }
138 
139         SkPaint paint;
140 
141         SkScalar texWidth = fTexScale * TexWidth;
142         SkScalar texHeight = fTexScale * TexHeight;
143         const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
144             { fTexX - texWidth, fTexY - texHeight},
145             { fTexX + texWidth, fTexY - texHeight},
146             { fTexX + texWidth, fTexY + texHeight},
147             { fTexX - texWidth, fTexY + texHeight}}
148         ;
149 
150         SkScalar scaleFreq = 2.0;
151         fShader1 = SkPerlinNoiseShader::MakeImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4,
152                                                              fSeed);
153         fShaderCompose = SkShader::MakeComposeShader(fShader0, fShader1, SkBlendMode::kSrcOver);
154 
155         paint.setShader(fShaderCompose);
156 
157         const SkPoint* tex = texCoords;
158         if (fShowGrid) {
159             tex = nullptr;
160         }
161         canvas->drawPatch(fPts, nullptr, tex, SkBlendMode::kSrc, paint);
162 
163         draw_control_points(canvas, fPts);
164     }
165 
166     class PtClick : public Click {
167     public:
168         int fIndex;
PtClick(Sample * view,int index)169         PtClick(Sample* view, int index) : Click(view), fIndex(index) {}
170     };
171 
hittest(const SkPoint & pt,SkScalar x,SkScalar y)172     static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
173         return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
174     }
175 
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)176     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
177         // holding down shift
178         if (1 == modi) {
179             return new PtClick(this, -1);
180         }
181         // holding down ctrl
182         if (2 == modi) {
183             return new PtClick(this, -2);
184         }
185         SkPoint clickPoint = {x, y};
186         fInvMatrix.mapPoints(&clickPoint, 1);
187         for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
188             if (hittest(fPts[i], clickPoint.fX, clickPoint.fY)) {
189                 return new PtClick(this, (int)i);
190             }
191         }
192         return this->INHERITED::onFindClickHandler(x, y, modi);
193     }
194 
onClick(Click * click)195     bool onClick(Click* click) override {
196         PtClick* ptClick = (PtClick*)click;
197         if (ptClick->fIndex >= 0) {
198             fPts[ptClick->fIndex].set(click->fCurr.fX , click->fCurr.fY );
199         } else if (-1 == ptClick->fIndex) {
200             SkScalar xDiff = click->fPrev.fX - click->fCurr.fX;
201             SkScalar yDiff = click->fPrev.fY - click->fCurr.fY;
202             fTexX += xDiff * fTexScale;
203             fTexY += yDiff * fTexScale;
204         } else if (-2 == ptClick->fIndex) {
205             SkScalar yDiff = click->fCurr.fY - click->fPrev.fY;
206             fTexScale += yDiff / 10.0f;
207             fTexScale = SkTMax(0.1f, SkTMin(20.f, fTexScale));
208         }
209         return true;
210     }
211 
212 private:
213     typedef Sample INHERITED;
214 };
215 
216 DEF_SAMPLE( return new PerlinPatchView(); )
217