Advertisement
Guest User

Untitled

a guest
Apr 29th, 2012
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <QCAR/Renderer.h>
  2. #import <QCAR/VideoBackgroundTextureInfo.h>
  3. #import <QCAR/Image.h>
  4. #import "QCARutils.h"
  5.  
  6. @interface ImageRecognizerARView : UIView {
  7.     EAGLContext *context_;
  8.     GLuint framebuffer_;
  9.     GLuint renderbuffer_;
  10. }
  11.  
  12. - (void)renderFrameQCAR;
  13. - (void)renderFrameQCARFixed;
  14.  
  15. @end
  16.  
  17.  
  18. @implementation ImageRecognizerARView
  19.  
  20. + (Class)layerClass {
  21.     return [CAEAGLLayer class];
  22. }
  23.  
  24. - (id)initWithFrame:(CGRect)frame {
  25.     if ((self = [super initWithFrame:frame])) {
  26.         QCARutils *qUtils = [QCARutils getInstance];
  27.         qUtils.QCARFlags = QCAR::GL_20;
  28.        
  29.         CAEAGLLayer *layer = (CAEAGLLayer *)self.layer;
  30.         layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  31.                                         kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
  32.                                         nil];
  33.        
  34.         context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  35.     } return self;
  36. }
  37.  
  38. - (void)dealloc {
  39.    
  40.     // This is called on main thread, so we need to store the main thread context, and restore it when we're done
  41.    
  42.     EAGLContext *mainThreadContext = [EAGLContext currentContext];
  43.     [EAGLContext setCurrentContext:context_];
  44.    
  45.     if (framebuffer_) {
  46.         glDeleteFramebuffers(1, &framebuffer_);
  47.         glDeleteFramebuffers(1, &renderbuffer_);
  48.     }
  49.    
  50.     // Restore main thread context
  51.     [EAGLContext setCurrentContext:mainThreadContext];
  52.    
  53.     [context_ release];
  54.    
  55.     [super dealloc];
  56. }
  57.  
  58. - (void)createFrameBuffer {
  59.    
  60.     // This is called on main thread
  61.    
  62.     EAGLContext *mainThreadContext = [EAGLContext currentContext];
  63.     [EAGLContext setCurrentContext:context_];
  64.    
  65.     glGenFramebuffers(1, &framebuffer_);
  66.     glGenRenderbuffers(1, &renderbuffer_);
  67.    
  68.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
  69.     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_);
  70.    
  71.     [context_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  72.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer_);
  73.    
  74.     // Restore main thread context
  75.     [EAGLContext setCurrentContext:mainThreadContext];
  76. }
  77.  
  78. - (void)renderFrameQCAR {
  79.  
  80.     // This is called on background thread
  81.    
  82.     if (!framebuffer_) {
  83.         [self performSelectorOnMainThread:@selector(createFrameBuffer) withObject:nil waitUntilDone:YES];
  84.     }
  85.    
  86.     [EAGLContext setCurrentContext:context_];
  87.    
  88.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
  89.     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_);
  90.  
  91.     //glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 1, 1);
  92.    
  93.     GLint width, height;
  94.     glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  95.     glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  96.     glViewport(0, 0, width, height);
  97.    
  98.      // Skip the first few frames
  99.     static int frameCount = 0;
  100.     if (frameCount < 5) {
  101.         frameCount++;
  102.         return;
  103.     }
  104.    
  105.     [self renderFrameQCARFixed];
  106.    
  107. }
  108.  
  109. - (void)renderFrameQCARFixed {
  110.  
  111.     // Use this method instead of renderFrameQCAR
  112.     // Note that this is also called on a background thread
  113.  
  114.     QCAR::State state = QCAR::Renderer::getInstance().begin();
  115.    
  116.     const QCAR::Frame frame = state.getFrame();
  117.    
  118.     // This should now be safe
  119.     int numImages = frame.getNumImages();
  120.     NSLog(@"numImages %d", numImages);
  121.    
  122.     QCAR::Renderer::getInstance().end();
  123. }
  124.  
  125. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement