2012-05-17 147 views
3

我試圖在筆尖文件中使用其中一個視圖作爲GLView從筆尖的視圖中製作GLView

在我的iphoneDisplay.nib文件中,我有一個視圖設置爲GLView(代碼在底部)的自定義類,並在我的應用程序委託中設置了插座。該視圖我設置更改爲黑色背景顏色和NSLogs顯示它正在從GLView和我的GLViewController繪圖,但我沒有看到任何:/。任何人都知道我做錯了什麼?

我的應用程序委託:

#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]]; 
    // Override point for customization after application launch. 
    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 

GLView(從傑夫·拉馬什樣板代碼):

// 
// GLView.m 
// Part6Project 
// 
// Created by jeff on 5/31/09. 
// Copyright Jeff LaMarche 2009. All rights reserved. 
// 

#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]; 
     // scale value should be 1.0 on 3G and 3GS, and 2.0 on iPhone 4. 
     self.contentMode = UIViewContentModeScaleToFill; 
    } 

    // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color. 
    eaglLayer.opaque = YES; 
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, 
            kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, 
            nil]; 

    // Create our EAGLContext, and if successful make it current and create our framebuffer. 
    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 
    if(!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer]) 
    { 
     [self release]; 
     return nil; 
    } 

    // Default the animation interval to 1/60th of a second. 
    animationInterval = 1.0/kRenderingFrequency; 
    return self; 
} 

-(GLViewController *)controller 
{ 
    return controller; 
} 

-(void)setController:(GLViewController *)d 
{ 
    controller = d; 
    controllerSetup = ![controller respondsToSelector:@selector(setupView:)]; 
} 

// If our view is resized, we'll be asked to layout subviews. 
// This is the perfect opportunity to also update the framebuffer so that it is 
// the same size as our display area. 
-(void)layoutSubviews 
{ 
    [EAGLContext setCurrentContext:context]; 
    [self destroyFramebuffer]; 
    [self createFramebuffer]; 
    [self drawView]; 
} 

- (BOOL)createFramebuffer 
{ 
    NSLog(@"about to make FB"); 
    // Generate IDs for a framebuffer object and a color renderbuffer 
    glGenFramebuffersOES(1, &viewFramebuffer); 
    glGenRenderbuffersOES(1, &viewRenderbuffer); 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer) 
    // allowing us to draw into a buffer that will later be rendered to screen whereever the layer is (which corresponds with our view). 
    [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); 

    // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer. 
    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; 
} 

// Clean up any buffers we have allocated. 
- (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]; 
    } 
} 

// Updates the OpenGL view when the timer fires 
- (void)drawView 
{ 
    NSLog(@"draw"); 
    // Make sure that you are drawing to the current context 
    [EAGLContext setCurrentContext:context]; 

    // If our drawing delegate needs to have the view setup, then call -setupView: and flag that it won't need to be called again. 
    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); 

} 

// Stop animating and release resources when they are no longer needed. 
- (void)dealloc 
{ 
    [self stopAnimation]; 

    if([EAGLContext currentContext] == context) 
    { 
     [EAGLContext setCurrentContext:nil]; 
    } 

    [context release]; 
    context = nil; 

    [super dealloc]; 
} 

@end 
+0

顯示視圖控制器的「drawView:」方法 –

回答

0

這裏是我想你的代碼運行

self.viewController = [[GLViewController alloc] initWithNibName:@"iPhoneDisplay" bundle:nil]"; 

該讀視圖控制器並實例化(或按需調度實例,而不是)當然),您在NIB文件中定義的GLView。

self.window.rootViewController = self.viewController; 

執行,此時GLView你在你的NIB文件中定義被添加到視圖層次結構。然後,您創建一個全新的GLView,它永遠不會添加到視圖層次結構中,然後將該視圖設置爲動畫。

glViewFromNib = [[GLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
glViewFromNib.controller = self.window.rootViewController; 
glViewFromNib.animationInterval = 1.0/kRenderingFrequency; 
[glViewFromNib startAnimation]; 

你應該使用一個IBOutlet獲得上載有您的NIB的GLView一個手柄,並啓動動畫。 (或者如果要在代碼中創建GLView,則在分配rootViewController之前執行類似於[self.viewController.view addSubView:glViewCreatedInCode]的操作)