title: rendering in OpenGL using CAOpenGLLayer
body: <p>I have a general question regarding how <code>CAOpenGLLayer</code> works in Mac OS X.  Is it possible to draw to <strong>OpenGL</strong> in a place other than in the <code>-[CAOpenGLLayer drawInCGLContext:]</code> method of <code>CAOpenGLLayer</code>?</p>
  
  <p>I'm using Apple's example code <a href="http://developer.apple.com/library/mac/#samplecode/CALayerEssentials/Introduction/Intro.html" rel="nofollow"><strong>CALayerEssentials</strong></a>
  and just moved the code that draws a rectangle from <code>drawInCGLContext</code> to <code>redrawGLContent</code>.  The <code>redrawGLContent</code> method is called each time the user clicks a button on the OpenGL Window.<br>
  When the drawing code is in the <code>drawInCGLContext</code>, it draws as expected, but when I move the code under <code>redrawGLContent</code>, nothing is drawn to the OpenGL Window.  I'm trying to understand how it works.</p>
  
  <pre><code>-(IBAction)redrawGLContent:(id)sender
  {
  
  // This part wass added, but does not draw the rectangle
  
      float timeInterval=0;
      GLfloat rotate = timeInterval * 60.0; // 60 degrees per second!
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glRotatef(rotate, 0.0, 0.0, 1.0);
      glBegin(GL_QUADS);
          glColor3f(1.0, 0.0, 0.0);
          glVertex2f(-0.5, -0.5);
          glVertex2f(-0.5,  0.5);
          glVertex2f( 0.5,  0.5);
          glVertex2f( 0.5, -0.5);
      glEnd();
      glPopMatrix();
  // up to here
  
      // Just tell the layer to display itself and it will redraw
      [hostCAOpenGLLayer.layer setNeedsDisplay];
  }
  
  -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
  {
  
      // Set the current context to the one given to us.
      CGLSetCurrentContext(glContext);
  
  
  /* This part was removed.  When code is here the quad is displayed 
  
      GLfloat rotate = timeInterval * 60.0; // 60 degrees per second!
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glRotatef(rotate, 0.0, 0.0, 1.0);
      glBegin(GL_QUADS);
          glColor3f(1.0, 0.0, 0.0);
          glVertex2f(-0.5, -0.5);
          glVertex2f(-0.5,  0.5);
          glVertex2f( 0.5,  0.5);
          glVertex2f( 0.5, -0.5);
      glEnd();
      glPopMatrix();
  */    
      // Call super to finalize the drawing. By default all it does is call glFlush().
      [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
  }
  </code></pre>