所有你需要做的就是調用[super initWithFrame:frame]和你想要從你的EAGLView類中獲得的幀。這個例子將在左上角創建一個透明的OpenGL視圖,並將它添加到你的controllers視圖中。
我有下面的代碼在我EAGLView類:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = NO;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO],
kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8,
kEAGLDrawablePropertyColorFormat,
nil];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
}
return self;
}
然後我從我的控制器創建EAGLView這樣的:
theEAGLView = [[EAGLView alloc] initWithFrame:CGRectMake(30.0f, 30.0f,
90.0f, 70.0f)];
theEAGLView.opaque = NO;
[self.view addSubview:theEAGLView];
是實際的繪圖中的代碼我的EAGLView類是從控制器調用,像這樣:
- (void)render {
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, backingWidth, 0.0f, backingHeight, 1000.0f, -1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// this is where we draw our stuff
// ...
// ...
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
這是偉大的 - 謝謝。我仍然困惑,但爲什麼你會這樣做: glViewport(0,0,backingWidth,backingHeight); glOrthof(0.0f,backingWidth,0.0f,backingHeight,1000.0f,-1000.0f); 當backingWidth和height設置爲320x480,並且您的繪圖表面(EAGLView)現在不再是全屏時。 glViewport()和EAGLView的CGRect之間的關係是什麼?或者是完全武斷的? 同樣在glOrtho()調用中,你是不是想要從0到EAGLView中的最大寬度,而不是0到320? 感謝您的幫助! – yozzozo 2009-10-15 16:45:40
EAGLView的backingWidth和backingHeight將根據初始化EAGLView時使用的CGRect的大小來設置。所以在這個例子中它們都是30。 – 2009-10-19 10:17:01