Todo lo que necesita hacer es llamar a [super initWithFrame: frame] con el marco que desee de su clase EAGLView cuando lo inicie. Este ejemplo creará una vista transparente de OpenGL en la esquina superior izquierda y la agregará a su vista de controladores.
Tengo el siguiente código en mi clase 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;
}
Y luego creo el EAGLView de mi controlador de la siguiente manera:
theEAGLView = [[EAGLView alloc] initWithFrame:CGRectMake(30.0f, 30.0f,
90.0f, 70.0f)];
theEAGLView.opaque = NO;
[self.view addSubview:theEAGLView];
El código que hace el dibujo real en mi clase EAGLView se llama desde el controlador y va más o menos así:
- (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];
}
Esto es genial, gracias. Todavía estoy confundido sobre por qué harías esto: glViewport (0, 0, backingWidth, backingHeight); glOrthof (0.0f, backingWidth, 0.0f, backingHeight, 1000.0f, -1000.0f); Cuando backingWidth y height están configurados en 320x480, y su superficie de dibujo (el EAGLView) ya no es pantalla completa. ¿Cuál es la relación entre glViewport() y el CGRect de EAGLView? ¿O es completamente arbitrario? También en la llamada glOrtho(), ¿no le gustaría simplemente ir de 0 a su ancho máximo en EAGLView, en lugar de 0 a 320? ¡Gracias por la ayuda! – yozzozo
El ancho de respaldo y la altura de respaldo del EAGLView se establecerán a partir del tamaño del CGRect utilizado al inicializar el EAGLView. Entonces en este ejemplo ambos serán 30. –