forked from tapsquare/GPUImage
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe84406
commit 9bf612f
Showing
9 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
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
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
2 changes: 1 addition & 1 deletion
2
examples/FilterShowcase/FilterShowcase/ShowcaseFilterViewController.h
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import "GPUImageFilter.h" | ||
|
||
@interface GPUImageSwirlFilter : GPUImageFilter | ||
{ | ||
GLint radiusUniform, centerUniform, angleUniform; | ||
} | ||
|
||
// The center about which to apply the distortion, with a default of (0.5, 0.5) | ||
@property(readwrite, nonatomic) CGPoint center; | ||
// The radius of the distortion, ranging from 0.0 to 1.0, with a default of 0.5 | ||
@property(readwrite, nonatomic) CGFloat radius; | ||
// The amount of distortion to apply, with a minimum of 0.0 and a default of 1.0 | ||
@property(readwrite, nonatomic) CGFloat angle; | ||
|
||
@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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#import "GPUImageSwirlFilter.h" | ||
|
||
// Adapted from the shader example here: http://www.geeks3d.com/20110428/shader-library-swirl-post-processing-filter-in-glsl/ | ||
|
||
NSString *const kGPUImageSwirlFragmentShaderString = SHADER_STRING | ||
( | ||
varying highp vec2 textureCoordinate; | ||
|
||
uniform sampler2D inputImageTexture; | ||
|
||
uniform highp vec2 center; | ||
uniform highp float radius; | ||
uniform highp float angle; | ||
|
||
void main() | ||
{ | ||
highp vec2 textureCoordinateToUse = textureCoordinate; | ||
highp float dist = distance(center, textureCoordinate); | ||
textureCoordinateToUse -= center; | ||
if (dist < radius) | ||
{ | ||
highp float percent = (radius - dist) / radius; | ||
// highp float theta = percent * angle; | ||
highp float theta = percent * percent * angle * 8.0; | ||
highp float s = sin(theta); | ||
highp float c = cos(theta); | ||
textureCoordinateToUse = vec2(dot(textureCoordinateToUse, vec2(c, -s)), dot(textureCoordinateToUse, vec2(s, c))); | ||
} | ||
textureCoordinateToUse += center; | ||
|
||
gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse ); | ||
|
||
} | ||
); | ||
|
||
@implementation GPUImageSwirlFilter | ||
|
||
#pragma mark - | ||
#pragma mark Initialization and teardown | ||
|
||
- (id)init; | ||
{ | ||
if (!(self = [super initWithFragmentShaderFromString:kGPUImageSwirlFragmentShaderString])) | ||
{ | ||
return nil; | ||
} | ||
|
||
radiusUniform = [filterProgram uniformIndex:@"radius"]; | ||
angleUniform = [filterProgram uniformIndex:@"angle"]; | ||
centerUniform = [filterProgram uniformIndex:@"center"]; | ||
|
||
self.radius = 0.5; | ||
self.angle = 1.0; | ||
self.center = CGPointMake(0.5, 0.5); | ||
|
||
return self; | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark Accessors | ||
|
||
@synthesize center = _center; | ||
@synthesize radius = _radius; | ||
@synthesize angle = _angle; | ||
|
||
- (void)setRadius:(CGFloat)newValue; | ||
{ | ||
_radius = newValue; | ||
|
||
[GPUImageOpenGLESContext useImageProcessingContext]; | ||
[filterProgram use]; | ||
glUniform1f(radiusUniform, _radius); | ||
} | ||
|
||
- (void)setAngle:(CGFloat)newValue; | ||
{ | ||
_angle = newValue; | ||
|
||
[GPUImageOpenGLESContext useImageProcessingContext]; | ||
[filterProgram use]; | ||
glUniform1f(angleUniform, _angle); | ||
} | ||
|
||
- (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 |