Skip to content

Instantly share code, notes, and snippets.

@dzeikei
Created February 26, 2013 02:12
Show Gist options
  • Select an option

  • Save dzeikei/5035233 to your computer and use it in GitHub Desktop.

Select an option

Save dzeikei/5035233 to your computer and use it in GitHub Desktop.

Revisions

  1. dzeikei created this gist Feb 26, 2013.
    91 changes: 91 additions & 0 deletions GPUImageSwirlFilter.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    /*
    * Copyright (C) 2012 CyberAgent
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

    package jp.co.cyberagent.android.gpuimage;

    import android.graphics.PointF;
    import android.opengl.GLES20;

    public class GPUImageSwirlFilter extends GPUImageFilter {
    public static final String SWIRL_FRAGMENT_SHADER = "" +
    "varying highp vec2 textureCoordinate;\n" +
    "uniform sampler2D inputImageTexture;\n" +
    "uniform highp vec2 center;\n" +
    "uniform highp float radius;\n" +
    "uniform highp float angle;\n" +
    "void main()\n" +
    "{\n" +
    " highp vec2 textureCoordinateToUse = textureCoordinate;\n" +
    " highp float dist = distance(center, textureCoordinate);\n" +
    " if (dist < radius)\n" +
    " {\n" +
    " textureCoordinateToUse -= center;\n" +
    " highp float percent = (radius - dist) / radius;\n" +
    " highp float theta = percent * percent * angle * 8.0;\n" +
    " highp float s = sin(theta);\n" +
    " highp float c = cos(theta);\n" +
    " textureCoordinateToUse = vec2(dot(textureCoordinateToUse, vec2(c, -s)), dot(textureCoordinateToUse, vec2(s, c)));\n" +
    " textureCoordinateToUse += center;\n" +
    " }\n" +
    " gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse);\n" +
    "}";

    private int mCenterLocation;
    private int mRadiusLocation;
    private int mAngleLocation;

    private float mRadius;
    private float mAngle;
    private PointF mCenter;

    public GPUImageSwirlFilter() {
    super(NO_FILTER_VERTEX_SHADER, SWIRL_FRAGMENT_SHADER);
    }

    @Override
    public void onInit() {
    super.onInit();

    mCenterLocation = GLES20.glGetUniformLocation(getProgram(), "center");
    mRadiusLocation = GLES20.glGetUniformLocation(getProgram(), "radius");
    mAngleLocation = GLES20.glGetUniformLocation(getProgram(), "angle");
    }

    @Override
    public void onInitialized() {
    super.onInitialized();

    setCenter(new PointF(0.5f, 0.5f));
    setRadius(0.5f);
    setAngle(1.0f);
    }

    public void setCenter(final PointF center) {
    mCenter = center;
    setPoint(mCenterLocation, mCenter);
    }

    public void setRadius(final float radius) {
    mRadius = radius;

    setFloat(mRadiusLocation, mRadius);
    }

    public void setAngle(final float angle) {
    mAngle = angle;
    setFloat(mAngleLocation, mAngle);
    }
    }