我有一個關於在Mac OS X中如何CAOpenGLLayer
作品是否有可能提請的OpenGL比在CAOpenGLLayer
的-[CAOpenGLLayer drawInCGLContext:]
方法以外的地方一般性的問題?呈現在OpenGL中使用CAOpenGLLayer
我正在使用Apple的示例代碼CALayerEssentials ,並將代碼從drawInCGLContext
移至redrawGLContent
。每次用戶單擊OpenGL窗口上的按鈕時,都會調用redrawGLContent
方法。
當繪圖代碼在drawInCGLContext
中時,它按預期繪製,但是當我將代碼移動到redrawGLContent
下時,沒有任何東西繪製到OpenGL窗口。我試圖瞭解它是如何工作的。
-(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];
}
我注意到你沒有在實現redrawGLContent時調用CGLSetCurrentContext。你確定當被調用的當前上下文與傳遞給drawInCGLContext的上下文相同嗎? – BinaryStar 2011-06-10 21:20:03