国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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];

 

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛纳斯县| 奉化市| 囊谦县| 石渠县| 黔西县| 略阳县| 香格里拉县| 凤庆县| 涟水县| 交口县| 陆川县| 新民市| 合山市| 南涧| 苍山县| 齐河县| 留坝县| 河北省| 哈尔滨市| 礼泉县| 临澧县| 南昌市| 通化市| 恭城| 铜川市| 石泉县| 当雄县| 阳信县| 利川市| 泰州市| 乌鲁木齐市| 宁武县| 扶绥县| 云林县| 莒南县| 永川市| 凌云县| 虞城县| 林口县| 讷河市| 新巴尔虎右旗|