Skip to content

Instantly share code, notes, and snippets.

@dzeikei
Created February 26, 2013 02:12
Show Gist options
  • Save dzeikei/5035233 to your computer and use it in GitHub Desktop.
Save dzeikei/5035233 to your computer and use it in GitHub Desktop.
GPUImageSwirlFilter
/*
* 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment