Skip to content

Commit

Permalink
StretchDistortion
Browse files Browse the repository at this point in the history
  • Loading branch information
fattjake committed Mar 30, 2012
1 parent 0a9e627 commit d66cd6e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions framework/Source/GPUImageStretchDistortionFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// GPUImageStrechFilter.h
// Face Esplode


#import "GPUImageFilter.h"

@interface GPUImageStretchDistortionFilter : GPUImageFilter {
GLint centerUniform;
}

// The center about which to apply the distortion, with a default of (0.5, 0.5)
@property(readwrite, nonatomic) CGPoint center;

@end
78 changes: 78 additions & 0 deletions framework/Source/GPUImageStretchDistortionFilter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// GPUImageStrechFilter.m

#import "GPUImageStretchDistortionFilter.h"

NSString *const kGPUImageStretchDistortionFragmentShaderString = SHADER_STRING
(
varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

uniform highp vec2 center;

void main()
{
highp vec2 normCoord = 2.0 * textureCoordinate - 1.0;
highp vec2 normCenter = 2.0 * center - 1.0;

normCoord -= normCenter;
mediump vec2 s = sign(normCoord);
normCoord = abs(normCoord);
normCoord = 0.5 * normCoord + 0.5 * smoothstep(0.25, 0.5, normCoord) * normCoord;
normCoord = s * normCoord;

normCoord += normCenter;

mediump vec2 textureCoordinateToUse = normCoord / 2.0 + 0.5;


gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse );

}
);


@implementation GPUImageStretchDistortionFilter

@synthesize center = _center;

#pragma mark -
#pragma mark Initialization and teardown

- (id)init;
{
if (!(self = [super initWithFragmentShaderFromString:kGPUImageStretchDistortionFragmentShaderString]))
{
return nil;
}

centerUniform = [filterProgram uniformIndex:@"center"];



self.center = CGPointMake(0.5, 0.5);

return self;
}

#pragma mark -
#pragma mark Accessors


- (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 d66cd6e

Please sign in to comment.