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 "SampleCode.h"
9 #include "SkAnimTimer.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPatchUtils.h"
13 #include "SkPerlinNoiseShader2/SkPerlinNoiseShader2.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 SampleView {
63     SkShader* fShader0;
64     SkShader* fShader1;
65     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 public:
PerlinPatchView()75     PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f),
76                         fTexX(100.0), fTexY(50.0), fTexScale(1.0f) {
77         const SkScalar s = 2;
78         // The order of the colors and points is clockwise starting at upper-left corner.
79         //top points
80         fPts[0].set(100 * s, 100 * s);
81         fPts[1].set(150 * s, 50 * s);
82         fPts[2].set(250 * s, 150 * s);
83         fPts[3].set(300 * s, 100 * s);
84         //right points
85         fPts[4].set(275 * s, 150 * s);
86         fPts[5].set(350 * s, 250 * s);
87         //bottom points
88         fPts[6].set(300 * s, 300 * s);
89         fPts[7].set(250 * s, 250 * s);
90         //left points
91         fPts[8].set(150 * s, 350 * s);
92         fPts[9].set(100 * s, 300 * s);
93         fPts[10].set(50 * s, 250 * s);
94         fPts[11].set(150 * s, 150 * s);
95 
96         const SkColor colors[SkPatchUtils::kNumCorners] = {
97             0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF
98         };
99         const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f),
100                                     SkPoint::Make(100.0f, 100.0f) };
101         fShader0 = SkGradientShader::CreateLinear(points,
102                                                   colors,
103                                                   NULL,
104                                                   3,
105                                                   SkShader::kMirror_TileMode,
106                                                   0,
107                                                   NULL);
108     }
109 
~PerlinPatchView()110     virtual ~PerlinPatchView() {
111         SkSafeUnref(fShader0);
112     }
113 protected:
114     // overrides from SkEventSink
onQuery(SkEvent * evt)115     bool onQuery(SkEvent* evt)  override {
116         if (SampleCode::TitleQ(*evt)) {
117             SampleCode::TitleR(evt, "PerlinPatch");
118             return true;
119         }
120         return this->INHERITED::onQuery(evt);
121     }
122 
onAnimate(const SkAnimTimer & timer)123     bool onAnimate(const SkAnimTimer& timer) override {
124         fSeed += 0.005f;
125         return true;
126     }
127 
128 
onDrawContent(SkCanvas * canvas)129     void onDrawContent(SkCanvas* canvas) override {
130         if (!canvas->getTotalMatrix().invert(&fInvMatrix)) {
131             return;
132         }
133 
134         SkPaint paint;
135 
136         SkScalar texWidth = fTexScale * TexWidth;
137         SkScalar texHeight = fTexScale * TexHeight;
138         const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
139             { fTexX - texWidth, fTexY - texHeight},
140             { fTexX + texWidth, fTexY - texHeight},
141             { fTexX + texWidth, fTexY + texHeight},
142             { fTexX - texWidth, fTexY + texHeight}}
143         ;
144 
145         SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kSrc_Mode));
146 
147         SkScalar scaleFreq = 2.0;
148         fShader1 = SkPerlinNoiseShader2::CreateImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4,
149                                                              fSeed);
150         fShaderCompose = SkShader::CreateComposeShader(fShader0, fShader1, nullptr);
151 
152         paint.setShader(fShaderCompose);
153         canvas->drawPatch(fPts, nullptr, texCoords, xfer, paint);
154 
155         draw_control_points(canvas, fPts);
156         SkSafeUnref(fShader1);
157         SkSafeUnref(fShaderCompose);
158     }
159 
160     class PtClick : public Click {
161     public:
162         int fIndex;
PtClick(SkView * view,int index)163         PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
164     };
165 
hittest(const SkPoint & pt,SkScalar x,SkScalar y)166     static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
167         return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
168     }
169 
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)170     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
171         // holding down shift
172         if (1 == modi) {
173             return new PtClick(this, -1);
174         }
175         // holding down ctrl
176         if (2 == modi) {
177             return new PtClick(this, -2);
178         }
179         SkPoint clickPoint = {x, y};
180         fInvMatrix.mapPoints(&clickPoint, 1);
181         for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
182             if (hittest(fPts[i], clickPoint.fX, clickPoint.fY)) {
183                 return new PtClick(this, (int)i);
184             }
185         }
186         return this->INHERITED::onFindClickHandler(x, y, modi);
187     }
188 
onClick(Click * click)189     bool onClick(Click* click) override {
190         PtClick* ptClick = (PtClick*)click;
191         if (ptClick->fIndex >= 0) {
192             fPts[ptClick->fIndex].set(click->fCurr.fX , click->fCurr.fY );
193         } else if (-1 == ptClick->fIndex) {
194             SkScalar xDiff = click->fPrev.fX - click->fCurr.fX;
195             SkScalar yDiff = click->fPrev.fY - click->fCurr.fY;
196             fTexX += xDiff * fTexScale;
197             fTexY += yDiff * fTexScale;
198         } else if (-2 == ptClick->fIndex) {
199             SkScalar yDiff = click->fCurr.fY - click->fPrev.fY;
200             fTexScale += yDiff / 10.0f;
201             fTexScale = SkTMax(0.1f, SkTMin(20.f, fTexScale));
202         }
203         this->inval(nullptr);
204         return true;
205     }
206 
207 private:
208     typedef SampleView INHERITED;
209 };
210 
211 DEF_SAMPLE( return new PerlinPatchView(); )
212 
213