Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <QCAR/Renderer.h>
- #import <QCAR/VideoBackgroundTextureInfo.h>
- #import <QCAR/Image.h>
- #import "QCARutils.h"
- @interface ImageRecognizerARView : UIView {
- EAGLContext *context_;
- GLuint framebuffer_;
- GLuint renderbuffer_;
- }
- - (void)renderFrameQCAR;
- - (void)renderFrameQCARFixed;
- @end
- @implementation ImageRecognizerARView
- + (Class)layerClass {
- return [CAEAGLLayer class];
- }
- - (id)initWithFrame:(CGRect)frame {
- if ((self = [super initWithFrame:frame])) {
- QCARutils *qUtils = [QCARutils getInstance];
- qUtils.QCARFlags = QCAR::GL_20;
- CAEAGLLayer *layer = (CAEAGLLayer *)self.layer;
- layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
- kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
- nil];
- context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
- } return self;
- }
- - (void)dealloc {
- // This is called on main thread, so we need to store the main thread context, and restore it when we're done
- EAGLContext *mainThreadContext = [EAGLContext currentContext];
- [EAGLContext setCurrentContext:context_];
- if (framebuffer_) {
- glDeleteFramebuffers(1, &framebuffer_);
- glDeleteFramebuffers(1, &renderbuffer_);
- }
- // Restore main thread context
- [EAGLContext setCurrentContext:mainThreadContext];
- [context_ release];
- [super dealloc];
- }
- - (void)createFrameBuffer {
- // This is called on main thread
- EAGLContext *mainThreadContext = [EAGLContext currentContext];
- [EAGLContext setCurrentContext:context_];
- glGenFramebuffers(1, &framebuffer_);
- glGenRenderbuffers(1, &renderbuffer_);
- glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
- glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_);
- [context_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer_);
- // Restore main thread context
- [EAGLContext setCurrentContext:mainThreadContext];
- }
- - (void)renderFrameQCAR {
- // This is called on background thread
- if (!framebuffer_) {
- [self performSelectorOnMainThread:@selector(createFrameBuffer) withObject:nil waitUntilDone:YES];
- }
- [EAGLContext setCurrentContext:context_];
- glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
- glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_);
- //glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 1, 1);
- GLint width, height;
- glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
- glViewport(0, 0, width, height);
- // Skip the first few frames
- static int frameCount = 0;
- if (frameCount < 5) {
- frameCount++;
- return;
- }
- [self renderFrameQCARFixed];
- }
- - (void)renderFrameQCARFixed {
- // Use this method instead of renderFrameQCAR
- // Note that this is also called on a background thread
- QCAR::State state = QCAR::Renderer::getInstance().begin();
- const QCAR::Frame frame = state.getFrame();
- // This should now be safe
- int numImages = frame.getNumImages();
- NSLog(@"numImages %d", numImages);
- QCAR::Renderer::getInstance().end();
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement