Permalink
Browse files

PolarPixellate Shader based on Vid by Toneburst

  • Loading branch information...
1 parent e1a5ff4 commit 0a9e627a803e37ae315b27ff93def9222de5ffa4 @fattjake fattjake committed Mar 30, 2012
Showing with 118 additions and 0 deletions.
  1. +17 −0 framework/Source/GPUImagePolarPixellateFilter.h
  2. +101 −0 framework/Source/GPUImagePolarPixellateFilter.m
@@ -0,0 +1,17 @@
+//
+// GPUImagePolarPixellateFilter.h
+
+
+#import "GPUImageFilter.h"
+
+@interface GPUImagePolarPixellateFilter : GPUImageFilter {
+ GLint centerUniform, pixelSizeUniform;
+}
+
+// The center about which to apply the distortion, with a default of (0.5, 0.5)
+@property(readwrite, nonatomic) CGPoint center;
+// The amount of distortion to apply, from -2.0 to 2.0, with a default of 0.5
+@property(readwrite, nonatomic) CGPoint pixelSize;
+
+
+@end
@@ -0,0 +1,101 @@
+//
+// GPUImagePolarPixellateFilter.m
+// @fattjake based on vid by toneburst
+
+#import "GPUImagePolarPixellateFilter.h"
+
+NSString *const kGPUImagePolarPixellateFragmentShaderString = SHADER_STRING
+(
+ varying highp vec2 textureCoordinate;
+
+ uniform sampler2D inputImageTexture;
+
+ uniform highp vec2 center;
+ uniform highp vec2 pixelSize;
+
+
+ void main()
+ {
+ highp vec2 normCoord = 2.0 * textureCoordinate - 1.0;
+ highp vec2 normCenter = 2.0 * center - 1.0;
+
+ normCoord -= normCenter;
+
+ highp float r = length(normCoord); // to polar coords
+ highp float phi = atan(normCoord.y, normCoord.x); // to polar coords
+
+ r = r - mod(r, pixelSize.x) + 0.03;
+ phi = phi - mod(phi, pixelSize.y);
+
+ normCoord.x = r * cos(phi);
+ normCoord.y = r * sin(phi);
+
+ normCoord += normCenter;
+
+ mediump vec2 textureCoordinateToUse = normCoord / 2.0 + 0.5;
+
+ gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse );
+
+ }
+ );
+
+
+@implementation GPUImagePolarPixellateFilter
+
+@synthesize center = _center;
+
+@synthesize pixelSize = _pixelSize;
+
+#pragma mark -
+#pragma mark Initialization and teardown
+
+- (id)init;
+{
+ if (!(self = [super initWithFragmentShaderFromString:kGPUImagePolarPixellateFragmentShaderString]))
+ {
+ return nil;
+ }
+
+ pixelSizeUniform = [filterProgram uniformIndex:@"pixelSize"];
+ centerUniform = [filterProgram uniformIndex:@"center"];
+
+
+ self.pixelSize = CGPointMake(0.05, 0.05);
+ self.center = CGPointMake(0.5, 0.5);
+
+ return self;
+}
+
+#pragma mark -
+#pragma mark Accessors
+
+
+- (void)setPixelSize:(CGPoint)pixelSize
+{
+ _pixelSize = pixelSize;
+
+ [GPUImageOpenGLESContext useImageProcessingContext];
+ [filterProgram use];
+ GLfloat pixelS[2];
+ pixelS[0] = _pixelSize.x;
+ pixelS[1] = _pixelSize.y;
+ glUniform2fv(pixelSizeUniform, 1, pixelS);
+}
+
+- (void)setCenter:(CGPoint)newValue;
+{
+ _center = newValue;
+
+ [GPUImageOpenGLESContext useImageProcessingContext];
+ [filterProgram use];
+
+ GLfloat centerPosition[2];
+ centerPosition[0] = _center.x;
+ centerPosition[1] = _center.y;
+
+ glUniform2fv(centerUniform, 1, centerPosition);
+}
+
+
+
+@end

0 comments on commit 0a9e627

Please sign in to comment.