今天来学习一下一个简单滤镜使用的流程,通过调节亮度滤镜来了解。先将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(, &_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];

  

GPUImage简单滤镜使用(一)的更多相关文章

  1. GPUImage简单滤镜使用(二)

    GPUImage中,提供了许多简单的的常用的滤镜.在上一篇文章讲了如何调节图像的亮度这片文章讲一下如何通过GPUImage调节图像的对比度,饱和度,曝光度,和白平衡(美图秀秀中的色温). 原图像 调整 ...

  2. GPUImage简单滤镜使用之色阶(三)

    色阶是表示图像亮度强弱的指数标准,图像的色彩丰满度和精细度是由色阶决定的.在GPUImage中GPUImageLevelsFilter提供了此功能. GPUImageLevelsFilter定义了修改 ...

  3. GPUImage 自定义滤镜

    GPUImage 自定义滤镜 GPUImage 是一个基于 GPU 图像和视频处理的开源 iOS 框架.由于使用 GPU 来处理图像和视频,所以速度非常快,它的作者 BradLarson 称在 iPh ...

  4. GPUImage处理图片(滤镜)

    GPUImage 是基于 GPU 处理图像的一个开源库, 提供了各种图像处理滤镜,例如调 亮度/饱和度/曝光度/白平衡/锐化等滤镜. 并且支持照相机/摄像机 的实时滤镜. GPUImage采用链式方式 ...

  5. ffmpeg第2篇:简单滤镜与复杂滤镜的区别

    在ffmpeg的滤镜中,有简单滤镜(simple filter)和复杂滤镜(complex filter)两种. 使用简单滤镜时,用-vf选项,使用复杂滤镜时,使用-filter_complex或-l ...

  6. GPUimage实时滤镜的实现

    GPUIMAGE中GPUImageStillCamera可以调用系统相机,并实现实时滤镜,但是我没有找到相机全屏的方法,望知道的说一下 GPUImageStillCamera继承自GPUImageVi ...

  7. 导入GPUImage,实时滤镜相机,GUPImage遇到的问题解决,_OBJC_METACLASS_$_GBGPUImageView in GBGPUImageView.o

    导入方法转自:http://www.cnblogs.com/S2-huai/p/3881349.html.. (原文:http://www.cnblogs.com/YouXianMing/p/3709 ...

  8. 图像处理库GPUImage简单使用

    一.介绍 GPUImage是一个基于OpenGL ES 2.0的开源的图像处理库,作者是Brad Larson.GPUImage将OpenGL ES封装为简洁的Objective-C或Swift接口, ...

  9. GpuImage简单使用

    声明变量 @interface ********** { GPUImageVideoCamera *Camera; GPUImageOutput *Filters; GPUImageView *Cam ...

随机推荐

  1. STM32 UART DMA实现未知数据长度接收

    串口通信是经常使用到的功能,在STM32中UART具有DMA功能,并且收发都可以使用DMA,使用DMA发送基本上大家不会遇到什么问题,因为发送的时候会告知DMA发送的数据长度,DMA按照发送的长度直接 ...

  2. IBDAP-CMSIS-DAP

    IBDAP-CMSIS-DAP Armstart's CMSIS-DAP firmware implementation in gcc and makefile. http://www.armstar ...

  3. STL中经常使用数据结构

    STL中经常使用的数据结构: [1]  stack.queue默认的底层实现为deque结构. [2]  deque:用map管理多个size大小的连续内存块,方便头尾插入. [3]  vector: ...

  4. Revit API封装一个通用函数“过名称找元素”

    感觉这个函数不错.通过这种方式寻找元素经常需要用到. )         {  ];         }         // cannot find it.         return null; ...

  5. 使用java中replaceAll方法替换字符串中的反斜杠

    今天在项目中使用java中replaceAll方法将字符串中的反斜杠("\")替换成空字符串(""),结果出现如下的异常: java.util.regex.Pa ...

  6. AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等

    本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...

  7. 无线通信中FEC 编码原理及评价

    转自:http://blog.csdn.net/wiznet2012/article/details/7492146 大家好,前面我们给大家介绍了无线通信中FEC编码原理(1)和(2),今天继续献上F ...

  8. unity3d 5.6烘焙教程

    unity5.6是今年发布,作为5.x的最后一个版本,有很多烘焙优势,在此总结一些作为5.x系列完结的笔记这个版本在烘焙上的特点就是增加了渐进光照贴图(Progressive Lightmapper) ...

  9. url空格转码的问题

    上知乎在搜索框里输入"1-n 随机数"(中间有空格,然后会看到了这个界面)   最开始我使用的是chrome,发现有脚本报错了,以为是服务器维护了,但再一想,不对啊,刚刚明明是好的 ...

  10. cocos2d-x 3.0rc1 编译cpp-testsproject

    1.进入cocos2d-x的build文件夹 2.打开一个cmd命令行窗体,输入 android-build.py cpp-tests 然后回车