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