I am trying to use one of the views in my nib file as GLView.
In my file iphoneDisplay.nib, I have a view defined as a user class GLView(code below) with the output set in my application delet. In the View view, I changed the color of the black background, and NSLogs show that it draws both from GLViewand from mine GLViewController, but I can’t see anything: /. Does anyone know what I'm doing wrong?
My application delegate:
#import "HBAppDelegate.h"
#import "GLViewController.h"
#import "GLView.h"
#import "TestFlight.h"
@implementation HBAppDelegate
@synthesize window=_window;
@synthesize viewController=_controller;
@synthesize glViewFromNib;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[GLViewController alloc] initWithNibName:@"iPhoneDisplay" bundle:nil];
self.window.rootViewController = self.viewController;
glViewFromNib = [[GLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
glViewFromNib.controller = self.window.rootViewController;
glViewFromNib.animationInterval = 1.0 / kRenderingFrequency;
[glViewFromNib startAnimation];
[glViewFromNib setUserInteractionEnabled:false];
[self.window makeKeyAndVisible];
return YES;
}
@end
My GLView(template code by Jeff LaMarche):
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "GLView.h"
#import "GLViewController.h"
@interface GLView (private)
- (id)initGLES;
- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
@end
@implementation GLView
@synthesize animationInterval;
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self != nil)
{
NSLog(@"init with frame");
self = [self initGLES];
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
if((self = [super initWithCoder:coder]))
{
self = [self initGLES];
}
return self;
}
-(id)initGLES
{
NSLog(@"init GLES");
CAEAGLLayer *eaglLayer = (CAEAGLLayer*) self.layer;
if ([self respondsToSelector:@selector(contentScaleFactor)])
{
self.contentScaleFactor = [[UIScreen mainScreen] scale];
self.contentMode = UIViewContentModeScaleToFill;
}
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if(!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer])
{
[self release];
return nil;
}
animationInterval = 1.0 / kRenderingFrequency;
return self;
}
-(GLViewController *)controller
{
return controller;
}
-(void)setController:(GLViewController *)d
{
controller = d;
controllerSetup = ![controller respondsToSelector:@selector(setupView:)];
}
-(void)layoutSubviews
{
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}
- (BOOL)createFramebuffer
{
NSLog(@"about to make FB");
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}
- (void)destroyFramebuffer
{
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
if(depthRenderbuffer)
{
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
}
- (void)startAnimation
{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
NSLog(@"starting animation");
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
NSLog(@"stopping animation");
}
- (void)setAnimationInterval:(NSTimeInterval)interval
{
animationInterval = interval;
if(animationTimer)
{
[self stopAnimation];
[self startAnimation];
}
}
- (void)drawView
{
NSLog(@"draw");
[EAGLContext setCurrentContext:context];
if(!controllerSetup)
{
[controller setupView:self];
controllerSetup = YES;
}
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
[controller drawView:self];
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
GLenum err = glGetError();
if(err)
NSLog(@"%x error", err);
}
- (void)dealloc
{
[self stopAnimation];
if([EAGLContext currentContext] == context)
{
[EAGLContext setCurrentContext:nil];
}
[context release];
context = nil;
[super dealloc];
}
@end