所有你需要做的就是调用[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