首页 > 学院 > 开发设计 > 正文

GPUImage简单滤镜使用(一)

2019-11-14 17:52:53
字体:
来源:转载
供稿:网友

  今天来学习一下一个简单滤镜使用的流程,通过调节亮度滤镜来了解。先将GPUImage库导入到项目中,引入头文件"GPUImage.h"

     一、创建亮度滤镜对象

      GPUImageBrightnessFilter *filter = [[GPUImageBrightnessFilter alloc] init],经过alloc init之后,程序为我们创建了顶点数组以及帧缓冲区,纹理,并绑定为当前使用的对象。

  1.为顶点着色添加属性

   首先我们来看一该滤镜的顶点着色器字符串

 

 attribute vec4 position; attribute vec4 inputTextureCoordinate;  varying vec2 textureCoordinate;  void main() {     gl_Position = position;     textureCoordinate = inputTextureCoordinate.xy; }

 

 

 我们了解到该顶点有2个需要添加的属性position,inputTextureCoordinate.我们需要在程序中添加这2个属性,通过下列方法来添加

 

- (void)initializeAttributes;{    [filterPRogram addAttribute:@"position"];    [filterProgram addAttribute:@"inputTextureCoordinate"];    // Override this, calling back to this super method, in order to add new attributes to your vertex shader}

 

  2.片段着色提供uniform
  
 varying highp vec2 textureCoordinate;  uniform sampler2D inputImageTexture; uniform lowp float brightness;  void main() {     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);          gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w); }  

 

  brightnessUniform = [filterProgram uniformIndex:@"brightness"]

  filterInputTextureUniform = [filterProgram uniformIndex:@"inputImageTexture"]

  3.启用顶点数组

  glEnableVertexAttribArray(filterPositionAttribute);

      glEnableVertexAttribArray(filterTextureCoordinateAttribute)

 4.创建纹理
- (void)generateTexture;{    glActiveTexture(GL_TEXTURE1);    glGenTextures(1, &_texture);    glBindTexture(GL_TEXTURE_2D, _texture);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _textureOptions.minFilter);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _textureOptions.magFilter);    // This is necessary for non-power-of-two textures    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _textureOptions.wrapS);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _textureOptions.wrapT);        // TODO: Handle mipmaps}
 
  5.创建帧缓冲区

      glGenFramebuffers(1, &framebuffer)

  6.帧缓冲绑定纹理

  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);

 

   二、设置亮度值

  filter.brightness = value

 

  三、设置纹理尺寸

    [filter forceProcessingAtSize:image.size]

 

  四、创建GPUImagePicture对象

     GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image]

 

  五、向创建好的GPUImagePicture对象添加target

 

  六,处理图像

  [pic processImage]

 

进行图像渲染并绘制

 

    glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);    glClear(GL_COLOR_BUFFER_BIT);    glActiveTexture(GL_TEXTURE2);    glBindTexture(GL_TEXTURE_2D, [firstInputFramebuffer texture]);        glUniform1i(filterInputTextureUniform, 2);        glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);    glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

 

 

 

 

  七、[filter useNextFrameForImageCapture]

 

   八、获取处理后的图像

  image = [filter imageFromCurrentFramebuffer]

 

    GPUImageBrightnessFilter *filter = [[GPUImageBrightnessFilter alloc] init];    filter.brightness = value;    [filter forceProcessingAtSize:image.size];    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];    [pic addTarget:filter];        [pic processImage];    [filter useNextFrameForImageCapture];    image = [filter imageFromCurrentFramebuffer];

 

  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表