Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: sameshit on Jul 31st, 2011  |  syntax: None  |  size: 3.58 KB  |  views: 144  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
  2. {
  3.         uint32_t w = CGRectGetWidth([self bounds]), h = CGRectGetHeight([self bounds]);
  4.         wvp* my_wvp = [self GetWvp];
  5.         uint8_t *data  = my_wvp->is->GetLastPicture()->bitmap;
  6.         CVPixelBufferRef pixel_buffer;
  7.         CVReturn err;
  8.         GLenum target;
  9.         GLint name;
  10.         GLfloat topLeft[2], topRight[2], bottomRight[2], bottomLeft[2];
  11.         uint8_t *planes[4];
  12.         size_t plane_w[4];
  13.         size_t plane_h[4];
  14.         size_t bytes_per_row[4];
  15.         CVOpenGLTextureRef texture;
  16.        
  17.        
  18.         if (my_wvp->__id == 1)
  19.         {
  20.                
  21.         }
  22.        
  23.         if (!data)
  24.                 return;
  25.                                
  26.         planes[0] = &data[0];
  27.         planes[1] = &data[h*w/2];
  28.         planes[2] = &data[h*w];
  29.         planes[3] = &data[3*h*w/2];
  30.                
  31.         plane_w[0] = w;
  32.         plane_w[1] = w;
  33.         plane_w[2] = w;
  34.         plane_w[3] = w;
  35.        
  36.         plane_h[0] = h;
  37.         plane_h[1] = h;
  38.         plane_h[2] = h;
  39.         plane_h[3] = h;
  40.        
  41.         bytes_per_row[0] = 2*w;
  42.         bytes_per_row[1] = 2*w;
  43.         bytes_per_row[2] = 2*w;
  44.         bytes_per_row[3] = 2*w;
  45.        
  46.        
  47.         CGLContextObj cgl_ctx = ctx; // CGLMacro.h
  48. //      CGLSetCurrentContext(ctx);
  49.        
  50.         glPushAttrib(GL_ALL_ATTRIB_BITS);
  51.         glPushClientAttrib(GL_ALL_ATTRIB_BITS);
  52.        
  53.         CVOpenGLTextureCacheFlush(texture_cache,0);
  54.        
  55.         err = CVPixelBufferCreateWithPlanarBytes(NULL,w,h,kCVPixelFormatType_422YpCbCr8,data,h*w*2,4,(void**)planes,plane_w,plane_h, bytes_per_row,NULL,NULL,NULL,&pixel_buffer);      
  56.         assert(kCVReturnSuccess == err);       
  57.                        
  58.         err = CVOpenGLTextureCacheCreateTextureFromImage(NULL,texture_cache,pixel_buffer,NULL,&texture);
  59.         assert(kCVReturnSuccess == err);               
  60.        
  61.        
  62.     glShadeModel(GL_SMOOTH);                // enable smooth shading
  63.         assert(glGetError() == GL_NO_ERROR);
  64.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   // black background
  65.         assert(glGetError() == GL_NO_ERROR);
  66.     glClearDepth(1.0f);                     // depth buffer setup
  67.         assert(glGetError() == GL_NO_ERROR);
  68.     glEnable(GL_DEPTH_TEST);                // enable depth testing
  69.         assert(glGetError() == GL_NO_ERROR);   
  70.     glDepthFunc(GL_LEQUAL);                 // type of depth test to do
  71.         assert(glGetError() == GL_NO_ERROR);
  72.        
  73.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  74.         assert(glGetError() == GL_NO_ERROR);
  75.    
  76.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  77.         assert(glGetError() == GL_NO_ERROR);   
  78.        
  79.                
  80.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.        
  82.  
  83.        
  84.         glMatrixMode(GL_TEXTURE);
  85.         glLoadIdentity();
  86.                
  87.         glMatrixMode(GL_MODELVIEW);
  88.         glLoadIdentity();
  89.  
  90.                
  91.         target = CVOpenGLTextureGetTarget(texture);
  92.         name   = CVOpenGLTextureGetName(texture);      
  93.        
  94.     CVOpenGLTextureGetCleanTexCoords(texture, bottomLeft, bottomRight, topRight, topLeft);
  95.        
  96.         // get the texture coordinates for the part of the image that should be displayed
  97.         glPushMatrix();        
  98.         assert(glGetError() == GL_NO_ERROR);
  99.                        
  100.         // bind the texture and draw the quad
  101.         glEnable(target);
  102.         assert(glGetError() == GL_NO_ERROR);
  103.        
  104.         glBindTexture(target, name);
  105.         assert(glGetError() == GL_NO_ERROR);   
  106.        
  107.         glBegin(GL_QUADS);
  108.         glTexCoord2fv(bottomLeft);  glVertex2i(-1, -1);
  109.         glTexCoord2fv(topLeft);     glVertex2i(-1,  1);
  110.         glTexCoord2fv(topRight);    glVertex2i( 1,  1);
  111.         glTexCoord2fv(bottomRight); glVertex2i( 1, -1);
  112.         glEnd();
  113.        
  114.         assert(glGetError() == GL_NO_ERROR);
  115.        
  116.        
  117.         glDisable(target);
  118.         assert(glGetError() == GL_NO_ERROR);   
  119.        
  120.         glPopMatrix();
  121.         assert(glGetError() == GL_NO_ERROR);   
  122.                                
  123.         glPopClientAttrib();   
  124.         glPopAttrib();
  125.     [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];
  126.        
  127.         CVPixelBufferRelease(pixel_buffer);    
  128.         CVOpenGLTextureRelease(texture);
  129.                
  130. }
  131. @end