Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created July 27, 2013 12:14
Show Gist options
  • Save rsaunders100/6094708 to your computer and use it in GitHub Desktop.
Save rsaunders100/6094708 to your computer and use it in GitHub Desktop.
Core image rendering on a EAGLContext
// View controller is a subclass of GLKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
self.ciContext = [CIContext
contextWithEAGLContext:self.eaglContext
options: @{kCIContextWorkingColorSpace:[NSNull null]} ];
GLKView *view = (GLKView *)self.view;
view.context = self.eaglContext;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[EAGLContext setCurrentContext:self.eaglContext];
}
- (void) viewDidAppear:(BOOL)animated
{
[self updateScreen];
}
- (void) updateScreen
{
NSURL * testImageURL = [[NSBundle mainBundle] URLForResource:@"Image" withExtension:@"png"];
NSAssert(nil != testImageURL, @"Image not found");
CIImage * image = [CIImage imageWithContentsOfURL:testImageURL
options:@{ kCIImageColorSpace:[NSNull null] }];
CIFilter * filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:@0.8 forKey:@"InputIntensity"];
CIImage * result = [filter valueForKey:kCIOutputImageKey];
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
[self.ciContext drawImage:result inRect:[[UIScreen mainScreen] applicationFrame]
fromRect:[result extent]];
GLuint render_buffer = 0;
glBindRenderbuffer(GL_RENDERBUFFER, render_buffer);
[self.eaglContext presentRenderbuffer:GL_RENDERBUFFER];
}
@theoknock
Copy link

Doesn't work. No errors; just a blank screen. In Interface Builder, I created a GLKViewController with a GLKView (did not create a subview; just using the one that comes with the view controller). Here's the code:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <GLKit/GLKit.h>
#import <Foundation/Foundation.h>
#import <CoreImage/CoreImage.h>


@interface ViewController : GLKViewController <GLKViewControllerDelegate, GLKViewDelegate>


@end

//
//  ViewController.m
//  CoreImageGLKViewOpenGL3Test
//
//  Created by James Alan Bush on 2/5/16.
//  Copyright © 2016 James Alan Bush. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () {

}

@property (strong, nonatomic) IBOutlet GLKView *glkView;

@property (strong, nonatomic) EAGLContext *eaglContext;
@property (strong, nonatomic) CIContext *ciContext;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    self.ciContext = [CIContext
                      contextWithEAGLContext:self.eaglContext
                      options: @{kCIContextWorkingColorSpace:[NSNull null]} ];

    //GLKView *view = (GLKView *)self.view;
    self.glkView.context = self.eaglContext;
    self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    [EAGLContext setCurrentContext:self.eaglContext];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self updateScreen];
    NSLog(@"viewDidAppear");
}

- (void)glkView:(GLKView *)view
     drawInRect:(CGRect)rect {
    //NSLog(@"drawInRect");
}

- (void)glkViewControllerUpdate:(GLKViewController *)controller {
    NSLog(@"glkViewControllerUpdate");

}

- (void)updateScreen
{
    NSLog(@"updateScreen");
    NSURL * testImageURL = [[NSBundle mainBundle] URLForResource:@"IMG_1520" withExtension:@"png"];
    NSAssert(nil != testImageURL, @"Image not found");

    CIImage * image = [CIImage imageWithContentsOfURL:testImageURL
                                              options:@{ kCIImageColorSpace:[NSNull null] }];

    CIFilter * filter = [CIFilter filterWithName:@"CISepiaTone"];

    [filter setValue:image forKey:kCIInputImageKey];
    [filter setValue:@0.8 forKey:@"InputIntensity"];

    CIImage * result = [filter valueForKey:kCIOutputImageKey];

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    [self.ciContext drawImage:result inRect:[[UIScreen mainScreen] bounds]
                     fromRect:[result extent]];

    GLuint render_buffer = 0;
    glBindRenderbuffer(GL_RENDERBUFFER, render_buffer);
    [self.eaglContext prejentrenderbuffer:GL_RENDERBUFFER];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment