From fd69e99cc8cd89f602ef68e2cf778796806e1ad4 Mon Sep 17 00:00:00 2001 From: ihgazni Date: Sat, 7 Nov 2015 11:51:08 +0800 Subject: [PATCH] 1.add a filter which will generate random points and then generate a picture for JFAVronoi input,roughly done,not well need improvment.2:add two remap filters template to make new effects --- GPUImagePointsGenerator.h | 34 ++++++ GPUImagePointsGenerator.m | 230 ++++++++++++++++++++++++++++++++++++++ GPUImageRemap_1.h | 23 ++++ GPUImageRemap_1.m | 56 ++++++++++ GPUImageRemap_2.h | 21 ++++ GPUImageRemap_2.m | 57 ++++++++++ 6 files changed, 421 insertions(+) create mode 100644 GPUImagePointsGenerator.h create mode 100644 GPUImagePointsGenerator.m create mode 100644 GPUImageRemap_1.h create mode 100644 GPUImageRemap_1.m create mode 100644 GPUImageRemap_2.h create mode 100644 GPUImageRemap_2.m diff --git a/GPUImagePointsGenerator.h b/GPUImagePointsGenerator.h new file mode 100644 index 0000000..7aa5462 --- /dev/null +++ b/GPUImagePointsGenerator.h @@ -0,0 +1,34 @@ +// +// GPUImagePointsGenerator.h +// PhotoFX +// +// Created by dli on 11/3/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + +#ifndef PhotoFX_GPUImagePointsGenerator_h +#define PhotoFX_GPUImagePointsGenerator_h + + +#endif + +#import +#import "GPUImageFilter.h" + +@interface GPUImagePointsGenerator : GPUImageFilter +{ + //GLfloat red; + //GLfloat green; + //GLfloat blue; +} + +- (float)genRandomFloat:(int)upperLimit; + +@property(readwrite, nonatomic) int pointsCount; +@property (nonatomic, readwrite) BOOL useSelfDefinedSize; +@property (nonatomic, readwrite) CGSize sizeInPixels; + + +@end + + diff --git a/GPUImagePointsGenerator.m b/GPUImagePointsGenerator.m new file mode 100644 index 0000000..371734b --- /dev/null +++ b/GPUImagePointsGenerator.m @@ -0,0 +1,230 @@ +// +// GPUImagePointsGenerator.m +// PhotoFX +// +// Created by dli on 11/3/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + + +#import "GPUImagePointsGenerator.h" + +@implementation GPUImagePointsGenerator + +@synthesize pointsCount = _pointsCount; +@synthesize useSelfDefinedSize = _useSelfDefinedSize; +@synthesize sizeInPixels = _sizeInPixels; + +NSString *const kGPUImagePointsGeneratorVertexShaderString = SHADER_STRING +( + + attribute vec4 position; + varying float r; + varying float g; + varying float b; + + + void main() + { + gl_Position = position; + if ( (position.x < 0.5) && (position.y < 0.5) ) { + r = position.x; + g = 0.5 - position.y; + b = 2.0 / 255.0; + } + if ( (position.x < 0.5) && (position.y > 0.5) ) { + r = position.x; + g = 1.0 - position.y; + b = 0.0 ; + } + if ( (position.x > 0.5) && (position.y < 0.5) ) { + r = position.x - 0.5; + g = 0.5 - position.y; + b = 3.0 / 255.0; + } + if ( (position.x > 0.5) && (position.y > 0.5) ) { + r = position.x - 0.5 ; + g = 1.0 - position.y; + b = 1.0 / 255.0; + } + } + + ); + + +#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE +NSString *const kGPUImagePointsGeneratorFragmentShaderString = SHADER_STRING +( + precision lowp float; + varying float r; + varying float g; + varying float b; + + + void main() + { + gl_FragColor = vec4(r,g,b,1.0); + } +); +#else +NSString *const kGPUImagePointsGeneratorFragmentShaderString = SHADER_STRING +( + precision lowp float; + varying float r; + varying float g; + varying float b; + + + void main() + { + gl_FragColor = vec4(r,g,b,1.0); + } + ); +#endif + +#pragma mark - +#pragma mark Initialization and teardown + + + +- (id)init; +{ + if (!(self = [super initWithVertexShaderFromString:kGPUImagePointsGeneratorVertexShaderString fragmentShaderFromString:kGPUImagePointsGeneratorFragmentShaderString])) + { + return nil; + } + + self.pointsCount = 3000; + + return self; +} + +#pragma mark - +#pragma mark Accessors + +- (void)setPointsCount:(int)newValue; +{ + _pointsCount = newValue; + +} + +- (float)genRandomFloat:(int)upperLimit; +{ + int r = arc4random_uniform(upperLimit); + + float y = (float)r / (float)upperLimit; + + if (y > 0.99999) { + y = 1.0; + } + r = arc4random_uniform(2); + if (r==0) { + } else { + y = -y; + } + return(y); +} + + +-(void)setSizeInPixels:(CGSize)sizeInPixels { + _sizeInPixels = sizeInPixels; + + //validate that it's a power of 2 and square + + float width = log2(sizeInPixels.width); + float height = log2(sizeInPixels.height); + + if (width != height) { + NSLog(@"Voronoi point texture must be square"); + return; + } + if (width != floor(width) || height != floor(height)) { + NSLog(@"Voronoi point texture must be a power of 2. Texture size %f, %f", sizeInPixels.width, sizeInPixels.height); + return; + } + + _sizeInPixels.width = pow(2,width); + _sizeInPixels.height = pow(2,height); + +} + + +-(void)setUseSelfDefinedSize:(BOOL)useSelfDefinedSize { + _useSelfDefinedSize = useSelfDefinedSize; + +} + + + +- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates; +{ + GLfloat realVertices[_pointsCount]; + + for (int i = 0; i < _pointsCount; i++) { + float x = [self genRandomFloat:999999]; + realVertices[i] = x; + } + + + + if (self.preventRendering) + { + [firstInputFramebuffer unlock]; + return; + } + + [GPUImageContext setActiveShaderProgram:filterProgram]; + + + if (_useSelfDefinedSize == YES) { + + outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:_sizeInPixels textureOptions:self.outputTextureOptions onlyTexture:NO]; + + + } else { + + outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO]; + + } + + + [outputFramebuffer activateFramebuffer]; + if (usingNextFrameForImageCapture) + { + [outputFramebuffer lock]; + } + + [self setUniformsForProgramAtIndex:0]; + + + + + glClearColor(0.0,0.0,0.0,0.0); + //glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha); + glClear(GL_COLOR_BUFFER_BIT); + + glActiveTexture(GL_TEXTURE2); + + + glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, &realVertices); + + NSLog(@"points counts : %d", _pointsCount); + + glDrawArrays(GL_POINTS, 0, _pointsCount); + + + + [firstInputFramebuffer unlock]; + + if (usingNextFrameForImageCapture) + { + dispatch_semaphore_signal(imageCaptureSemaphore); + } + + + + +} + + +@end diff --git a/GPUImageRemap_1.h b/GPUImageRemap_1.h new file mode 100644 index 0000000..3d09a6a --- /dev/null +++ b/GPUImageRemap_1.h @@ -0,0 +1,23 @@ +// +// GPUImageRemap_1.h +// PhotoFX +// +// Created by dli on 10/24/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + +#ifndef PhotoFX_GPUImageRemap_1_h +#define PhotoFX_GPUImageRemap_1_h + + +#endif + + +#import +#import + +@interface GPUImageRemap_1 : GPUImageThreeInputFilter +{ +} +@end + diff --git a/GPUImageRemap_1.m b/GPUImageRemap_1.m new file mode 100644 index 0000000..105370c --- /dev/null +++ b/GPUImageRemap_1.m @@ -0,0 +1,56 @@ +// +// GPUImageRemap_1.m +// PhotoFX +// +// Created by dli on 10/24/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + +#import "GPUImageRemap_1.h" + + + +NSString *const kGPUImageRemap_1ShaderString = SHADER_STRING +( + precision lowp float; + + varying highp vec2 textureCoordinate; + + uniform sampler2D inputImageTexture; + uniform sampler2D inputImageTexture2; + uniform sampler2D inputImageTexture3; + + + void main() + { + + vec4 texel = texture2D(inputImageTexture, textureCoordinate); + vec3 bbTexel = texture2D(inputImageTexture2, textureCoordinate).rgb; + + texel.r = texture2D(inputImageTexture3, vec2(bbTexel.r, texel.r)).r; + texel.g = texture2D(inputImageTexture3, vec2(bbTexel.g, texel.g)).g; + texel.b = texture2D(inputImageTexture3, vec2(bbTexel.b, texel.b)).b; + + vec4 mapped; + mapped.r = texel.r; + mapped.g = texel.g; + mapped.b = texel.b; + mapped.a = 1.0; + + gl_FragColor = mapped; + } + ); + +@implementation GPUImageRemap_1 + +- (id)init; +{ + if (!(self = [super initWithFragmentShaderFromString:kGPUImageRemap_1ShaderString])) + { + return nil; + } + + return self; +} + +@end diff --git a/GPUImageRemap_2.h b/GPUImageRemap_2.h new file mode 100644 index 0000000..213949d --- /dev/null +++ b/GPUImageRemap_2.h @@ -0,0 +1,21 @@ +// +// GPUImageRemap_2.h +// PhotoFX +// +// Created by dli on 10/24/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + +#ifndef PhotoFX_GPUImageRemap_2_h +#define PhotoFX_GPUImageRemap_2_h + + +#endif + +#import +#import + +@interface GPUImageRemap_2 : GPUImageTwoInputFilter +{ +} +@end diff --git a/GPUImageRemap_2.m b/GPUImageRemap_2.m new file mode 100644 index 0000000..07b8c30 --- /dev/null +++ b/GPUImageRemap_2.m @@ -0,0 +1,57 @@ +// +// GPUImageRemap_2.m +// PhotoFX +// +// Created by dli on 10/24/15. +// Copyright (c) 2015 Mobiletuts. All rights reserved. +// + +#import "GPUImageRemap_2.h" + + +NSString *const kGPUImageRemap_2ShaderString = SHADER_STRING +( + precision lowp float; + + varying highp vec2 textureCoordinate; + + uniform sampler2D inputImageTexture; + uniform sampler2D inputImageTexture2; + + void main() + { + + vec4 texel = texture2D(inputImageTexture, textureCoordinate); + + + vec4 mapped; + mapped.r = texture2D(inputImageTexture2, vec2(texel.r, .16666)).r; + mapped.g = texture2D(inputImageTexture2, vec2(texel.g, .5)).g; + mapped.b = texture2D(inputImageTexture2, vec2(texel.b, .83333)).b; + + + + + mapped.a = 1.0; + + gl_FragColor = mapped; + } + ); + + +@implementation GPUImageRemap_2 + +- (id)init; +{ + if (!(self = [super initWithFragmentShaderFromString:kGPUImageRemap_2ShaderString])) + { + return nil; + } + + return self; +} + +@end + + +