GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

How to load a complex shader to a custom filter?

Open Khazmovsky opened this issue 12 years ago • 0 comments

Hi~Brad Larson,thanks for your excellent work! Now I have a complex shader,the fragment shader looks as follow:

static NSString *const kIFAmaroShaderString = SHADER_STRING ( precision lowp float;

varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; //blowout; uniform sampler2D inputImageTexture3; //overlay; uniform sampler2D inputImageTexture4; //map

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 = texture2D(inputImageTexture4, vec2(texel.r, .16666)).r;
 mapped.g = texture2D(inputImageTexture4, vec2(texel.g, .5)).g;
 mapped.b = texture2D(inputImageTexture4, vec2(texel.b, .83333)).b;
 mapped.a = 1.0;

 gl_FragColor = mapped;

} );

I use a subclass of GPUImageFilter ,it looks like this:

in .h:

import "GPUImageFilter.h"

@interface XTFilter : GPUImageFilter { GLint filterInputTextureUniform2; GLint filterInputTextureUniform3; GLint filterInputTextureUniform4; GLuint filterSourceTexture2; GLuint filterSourceTexture3; GLuint filterSourceTexture4; }

@end

in .m: @implementation XTFilter { CMTime newFrameTime; }

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

    runSynchronouslyOnVideoProcessingQueue(^{ [GPUImageContext useImageProcessingContext];

    filterSourceTexture2 = [filterProgram uniformIndex:@"inputImageTexture2"];
    filterSourceTexture3 = [filterProgram uniformIndex:@"inputImageTexture3"];
    filterSourceTexture4 = [filterProgram uniformIndex:@"inputImageTexture4"];
    
    [filterProgram use];
    glEnableVertexAttribArray(filterPositionAttribute);
    glEnableVertexAttribArray(filterTextureCoordinateAttribute);
    

    });

    return self; }

  • (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates sourceTexture:(GLuint)sourceTexture { if (self.preventRendering) { return; }

    [GPUImageContext useImageProcessingContext]; [self setFilterFBO]; [self setUniformsForProgramAtIndex:0];

    glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT);

    glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, sourceTexture); glUniform1i(filterInputTextureUniform, 2);

    if (filterSourceTexture2 != 0) { glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, filterSourceTexture2);

    glUniform1i(filterInputTextureUniform2, 3);
    

    }

    if (filterSourceTexture3 != 0) { glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, filterSourceTexture3); glUniform1i(filterInputTextureUniform3, 4); }

    if (filterSourceTexture4 != 0) { glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_2D, filterSourceTexture4); glUniform1i(filterInputTextureUniform4, 5); }

    glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices); glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    for (id currentTarget in targets) { if (currentTarget != self.targetToIgnoreForUpdates) { NSInteger indexOfObject = [targets indexOfObject:currentTarget]; NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];

        if ([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage)
        {
            [self setInputTextureForTarget:currentTarget atIndex:textureIndex];
        }
    
        [currentTarget setInputSize:[self outputFrameSize] atIndex:textureIndex];
        [currentTarget newFrameReadyAtTime:newFrameTime atIndex:textureIndex];
    }
    

    } }

  • (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex { newFrameTime = frameTime; [super newFrameReadyAtTime:frameTime atIndex:textureIndex]; }

  • (void)setInputTexture:(GLuint)newInputTexture atIndex:(NSInteger)textureIndex; { if (textureIndex == 0) { filterSourceTexture = newInputTexture; } else if (filterSourceTexture2 == 0) { filterSourceTexture2 = newInputTexture; } else if (filterSourceTexture3 == 0) { filterSourceTexture3 = newInputTexture; } else if (filterSourceTexture4 == 0) { filterSourceTexture4 = newInputTexture; } }

@end

now in view did load:

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _video = [[GPUImageMovie alloc] initWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"]]; _filter = [[XTFilter alloc] init];

    GPUImagePicture *pic1 = [[GPUImagePicture alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"blackboard1024" ofType:@"png"]]]; GPUImagePicture *pic2 = [[GPUImagePicture alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"overlayMap" ofType:@"png"]]]; GPUImagePicture *pic3 = [[GPUImagePicture alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"amaroMap" ofType:@"png"]]]; [pic1 processImage]; [pic2 processImage]; [pic3 processImage];

    [_video addTarget:_filter]; [pic1 addTarget:_filter]; [pic2 addTarget:_filter]; [pic3 addTarget:_filter];

    _imageView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)]; [self.view addSubview:_imageView]; [_filter addTarget:_imageView]; [_video startProcessing]; }

it's not working....

i use this shader by using GLKit ,it could work,but in GLKit, i don't know how to add a still image into a video~~~

could you figure me where i have took mistakes...

thank you very much.

Khazmovsky avatar Apr 23 '14 11:04 Khazmovsky