Created
February 25, 2015 10:16
-
-
Save virasio/7e17b4bc38dbcfb84f7c to your computer and use it in GitHub Desktop.
Mirror filter for GPUImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GPUImageMirrorFilter.h | |
// | |
#import <GPUImage/GPUImageFilter.h> | |
@interface GPUImageMirrorFilter : GPUImageFilter { | |
GLint verticalMirrorUniform, horizontalMirrorUniform; | |
} | |
@property (nonatomic) CGFloat verticalMirror; | |
@property (nonatomic) CGFloat horizontalMirror; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GPUImageMirrorFilter.m | |
// | |
#import "GPUImageMirrorFilter.h" | |
NSString *const kGPUImageMirrorFragmentShaderString = SHADER_STRING | |
( | |
varying lowp vec2 textureCoordinate; | |
uniform sampler2D inputImageTexture; | |
uniform lowp float verticalMirror; | |
uniform lowp float horizontalMirror; | |
void main(void) { | |
mediump vec2 p = textureCoordinate; | |
if (verticalMirror < 0.0) { | |
if (p.x > -verticalMirror) { | |
p.x = -2.0 * verticalMirror - p.x; | |
} | |
} | |
else if (verticalMirror > 0.0) { | |
if (p.x < verticalMirror) { | |
p.x = 2.0 * verticalMirror - p.x; | |
} | |
} | |
if (horizontalMirror < 0.0) { | |
if (p.y > horizontalMirror) { | |
p.y = -2.0 * horizontalMirror - p.y; | |
} | |
} | |
else if (horizontalMirror > 0.0) { | |
if (p.y < horizontalMirror) { | |
p.y = 2.0 * horizontalMirror - p.y; | |
} | |
} | |
if (p.x > 1.0 || p.x < 0.0 || p.y > 1.0 || p.y < 0.0) { | |
gl_FragColor = vec4(0.0); | |
} else { | |
lowp vec4 outputColor = texture2D(inputImageTexture, p); | |
gl_FragColor = outputColor; | |
} | |
} | |
); | |
@implementation GPUImageMirrorFilter | |
- (id)init { | |
if (!(self = [self initWithFragmentShaderFromString:kGPUImageMirrorFragmentShaderString])) { | |
return nil; | |
} | |
return self; | |
} | |
- (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString { | |
if (!(self = [super initWithFragmentShaderFromString:fragmentShaderString])) { | |
return nil; | |
} | |
verticalMirrorUniform = [filterProgram uniformIndex:@"verticalMirror"]; | |
self.verticalMirror = 0; | |
horizontalMirrorUniform = [filterProgram uniformIndex:@"horizontalMirror"]; | |
self.horizontalMirror = 0; | |
return self; | |
} | |
- (void)setVerticalMirror:(CGFloat)newValue { | |
NSAssert(newValue < 1.f && newValue > -1.f, @"Mirror position must be between -1 and 1. Current value: %f", newValue); | |
_verticalMirror = newValue; | |
[self setFloat:_verticalMirror forUniform:verticalMirrorUniform program:filterProgram]; | |
} | |
- (void)setHorizontalMirror:(CGFloat)newValue { | |
NSAssert(newValue < 1.f && newValue > -1.f, @"Mirror position must be between -1 and 1. Current value: %f", newValue); | |
_horizontalMirror = newValue; | |
[self setFloat:_horizontalMirror forUniform:horizontalMirrorUniform program:filterProgram]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment