Permalink
Browse files

Moved some thing around

  • Loading branch information...
1 parent 7611d12 commit 3dbd20a7a2f3d05d450e2dc08e7a2dae236486dd @fattjake committed May 17, 2012
@@ -1,19 +0,0 @@
-//
-// GPUImageJFAVoroni.h
-// Face Esplode
-//
-// Created by Jacob Gundersen on 4/27/12.
-// The shaders are mostly taken from UnitZeroOne's WebGL example here:
-// http://unitzeroone.com/blog/2011/03/22/jump-flood-voronoi-for-webgl/
-
-#import "GPUImageFilter.h"
-
-@interface GPUImageJFAVoroni : GPUImageFilter {
- GLuint secondFilterOutputTexture;
- GLuint secondFilterFramebuffer;
- GLint sampleStepUniform;
-}
-
-@property (nonatomic, assign) NSUInteger numPasses;
-
-@end
@@ -1,338 +0,0 @@
-//
-// GPUImageJFAVoroni.m
-// Face Esplode
-//
-// Created by Jacob Gundersen on 4/27/12.
-//
-// adapted from unitzeroone - http://unitzeroone.com/labs/jfavoronoi/
-
-#import "GPUImageJFAVoroni.h"
-#import "GPUImage3x3TextureSamplingFilter.h"
-
-NSString *const kGPUImageJFAVoroniFragmentShaderString = SHADER_STRING
-(
-
- precision highp float;
-
- varying vec2 textureCoordinate;
- varying vec2 leftTextureCoordinate;
- varying vec2 rightTextureCoordinate;
-
- varying vec2 topTextureCoordinate;
- varying vec2 topLeftTextureCoordinate;
- varying vec2 topRightTextureCoordinate;
-
- varying vec2 bottomTextureCoordinate;
- varying vec2 bottomLeftTextureCoordinate;
- varying vec2 bottomRightTextureCoordinate;
-
- uniform sampler2D inputImageTexture;
-
- //varying vec2 textureCoordinate;
- //uniform float sampleStep;
-
- vec2 getCoordFromColor(vec4 color, vec2 size)
-{
- float z = color.z * 256.0;
- float yoff = floor(z / 8.0);
- float xoff = mod(z, 8.0);
- float x = color.x*256.0 + xoff*256.0;
- float y = color.y*256.0 + yoff*256.0;
- return vec2(x,y) / size;
-}
-
- void main(void) {
-
- vec2 sub;
- vec4 dst;
- vec4 local = texture2D(inputImageTexture, textureCoordinate);
- vec4 sam;
- float l;
- float smallestDist;
- if(local.a == 0.0){
-
- smallestDist = dot(1.0,1.0);
- }else{
- sub = getCoordFromColor(local)-textureCoordinate;
- smallestDist = dot(sub,sub);
- }
- dst = local;
-
-
- sam = texture2D(inputImageTexture, topRightTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, topTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, topLeftTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, bottomRightTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, bottomTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, bottomLeftTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, leftTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
-
- sam = texture2D(inputImageTexture, rightTextureCoordinate);
- if(sam.a == 1.0){
- sub = (getCoordFromColor(sam)-uvIn);
- l = dot(sub,sub);
- if(l < smallestDist){
- smallestDist = l;
- dst = sam;
- }
- }
- gl_FragColor = dst;
- }
- );
-
-@implementation GPUImageJFAVoroni
-
-@synthesize numPasses;
-
-- (id)init;
-{
- if (!(self = [super initWithFragmentShaderFromString:kGPUImageJFAVoroniFragmentShaderString]))
- {
-
- NSLog(@"nil returned");
- return nil;
-
- }
-
- sampleStepUniform = [filterProgram uniformIndex:@"sampleStep"];
- NSLog(@"setup filter");
- return self;
-}
-
-#pragma mark -
-#pragma mark Managing the display FBOs
-
-
-- (void)initializeOutputTexture;
-{
- [super initializeOutputTexture];
-
- glGenTextures(1, &secondFilterOutputTexture);
- glBindTexture(GL_TEXTURE_2D, secondFilterOutputTexture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glBindTexture(GL_TEXTURE_2D, 0);
-}
-
-- (void)createFilterFBOofSize:(CGSize)currentFBOSize;
-{
- NSLog(@"log %f", log2(256));
- numPasses = (int)log2(currentFBOSize.width);
-
- if ([GPUImageOpenGLESContext supportsFastTextureUpload] && preparedToCaptureImage)
- {
- preparedToCaptureImage = NO;
- [super createFilterFBOofSize:currentFBOSize];
- preparedToCaptureImage = YES;
- }
- else
- {
- [super createFilterFBOofSize:currentFBOSize];
- }
-
- glGenFramebuffers(1, &secondFilterFramebuffer);
- glBindFramebuffer(GL_FRAMEBUFFER, secondFilterFramebuffer);
-
- if ([GPUImageOpenGLESContext supportsFastTextureUpload] && preparedToCaptureImage)
- {
- CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[[GPUImageOpenGLESContext sharedImageProcessingOpenGLESContext] context], NULL, &filterTextureCache);
- if (err)
- {
- NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d");
- }
-
- // Code originally sourced from http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/
-
- CFDictionaryRef empty; // empty value for attr value.
- CFMutableDictionaryRef attrs;
- empty = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // our empty IOSurface properties dictionary
- attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
- CFDictionarySetValue(attrs, kCVPixelBufferIOSurfacePropertiesKey, empty);
-
- err = CVPixelBufferCreate(kCFAllocatorDefault, (int)currentFBOSize.width, (int)currentFBOSize.height, kCVPixelFormatType_32BGRA, attrs, &renderTarget);
- if (err)
- {
- NSLog(@"FBO size: %f, %f", currentFBOSize.width, currentFBOSize.height);
- NSAssert(NO, @"Error at CVPixelBufferCreate %d");
- }
-
- err = CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
- filterTextureCache, renderTarget,
- NULL, // texture attributes
- GL_TEXTURE_2D,
- GL_RGBA, // opengl format
- (int)currentFBOSize.width,
- (int)currentFBOSize.height,
- GL_BGRA, // native iOS format
- GL_UNSIGNED_BYTE,
- 0,
- &renderTexture);
- if (err)
- {
- NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreateTextureFromImage %d");
- }
-
- CFRelease(attrs);
- CFRelease(empty);
- glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), CVOpenGLESTextureGetName(renderTexture));
- secondFilterOutputTexture = CVOpenGLESTextureGetName(renderTexture);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);
- }
- else
- {
- glBindTexture(GL_TEXTURE_2D, secondFilterOutputTexture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, secondFilterOutputTexture, 0);
- }
- NSLog(@"doe here");
- GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
-
- NSAssert(status == GL_FRAMEBUFFER_COMPLETE, @"Incomplete filter FBO: %d", status);
-
- glBindTexture(GL_TEXTURE_2D, 0);
-}
-
-
- //we may not need these
-- (void)setSecondFilterFBO;
-{
- glBindFramebuffer(GL_FRAMEBUFFER, secondFilterFramebuffer);
- //
- // CGSize currentFBOSize = [self sizeOfFBO];
- // glViewport(0, 0, (int)currentFBOSize.width, (int)currentFBOSize.height);
-}
-
-- (void)setOutputFBO;
-{
- if (numPasses % 2 == 1) {
- [self setSecondFilterFBO];
- } else {
- [self setFilterFBO];
- }
-
-}
-
-
-- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates sourceTexture:(GLuint)sourceTexture;
-{
- // Run the first stage of the two-pass filter
- [GPUImageOpenGLESContext useImageProcessingContext];
- [filterProgram use];
- glUniform1f(sampleStepUniform, 0.5);
-
- [super renderToTextureWithVertices:vertices textureCoordinates:textureCoordinates sourceTexture:sourceTexture];
-
- for (int pass = 1; pass <= numPasses + 1; pass++) {
- //NSLog(@"pass %d", pass);
- if (pass % 2 == 0) {
-
- [GPUImageOpenGLESContext useImageProcessingContext];
- [filterProgram use];
-
- float step = pow(2.0, numPasses - pass) / self.sizeOfFBO.width;
- glUniform1f(sampleStepUniform, step);
-
- [super renderToTextureWithVertices:vertices textureCoordinates:textureCoordinates sourceTexture:secondFilterOutputTexture];
- } else {
- // Run the second stage of the two-pass filter
- [self setSecondFilterFBO];
-
- [filterProgram use];
-
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
-
- glActiveTexture(GL_TEXTURE3);
- glBindTexture(GL_TEXTURE_2D, outputTexture);
-
- glUniform1i(filterInputTextureUniform, 3);
-
- float step = pow(2.0, numPasses - pass) / self.sizeOfFBO.width;
- glUniform1f(sampleStepUniform, step);
-
- if (filterSourceTexture2 != 0)
- {
- glActiveTexture(GL_TEXTURE4);
- glBindTexture(GL_TEXTURE_2D, filterSourceTexture2);
-
- glUniform1i(filterInputTextureUniform2, 4);
- }
-
- glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);
- glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
-
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- }
- }
-}
-
-
-
-@end
@@ -1,13 +0,0 @@
-//
-// GPUVoroniConsumerFilter.h
-// Face Esplode
-//
-// Created by Jacob Gundersen on 4/28/12.
-// Copyright (c) 2012 Interrobang Software LLC. All rights reserved.
-//
-
-#import "GPUImageFilter.h"
-
-@interface GPUImageVoroniConsumerFilter : GPUImageFilter
-
-@end
Oops, something went wrong.

0 comments on commit 3dbd20a

Please sign in to comment.