2009-05-23 21 views
0

建立OpenGL視圖(必要的投影,攝像機角度等)用於繪製2D遊戲所需的最低樣板代碼是多少?什麼是OpenGL 2D視圖的最小樣板代碼?

例如,做石英2D中的自定義視圖繪圖(和,比方說,加載一個背景圖像)所需的最小如下:

#import <Cocoa/Cocoa.h> 

@interface MyView : NSView { 
} 

@end 

= = =

#import "MyView.h" 

@implementation MyView 

- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    return self; 
} 

- (void)drawRect:(NSRect)rect { 
    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; 
    CGRect frame = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef url = CFBundleCopyResourceURL(mainBundle, CFSTR("background"), CFSTR("png"), NULL); 
    CGDataProviderRef provider = CGDataProviderCreateWithURL (url); 
    CGImageRef image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault); 
    CGDataProviderRelease (provider); 
    CGContextDrawImage (myContext, frame, image); 
    CGImageRelease (image); 

    //rest of drawing code here... 
} 

@end 

樣板代碼中的任何內容與iPhone上的Open GS ES不同,而不是在Mac上使用Open GL?

+0

哦,請不要推薦遊戲庫,cocos2D等人。我只想要一個基於OpenGL的答案。 – Hejazzman 2009-05-23 00:40:51

回答

1

在iPhone上設置OpenGL應用程序的最簡單方法是通過Xcode創建一個「OpenGL ES應用程序」。它會生成您需要開始使用的樣板源代碼。

這裏是我使用一個OpenGL iPhone遊戲的樣板來源:

@implementation EAGLView 

@synthesize context; 

// You must implement this method 
+ (Class)layerClass { 
    return [CAEAGLLayer class]; 
} 

//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: 
- (id)initWithCoder:(NSCoder*)coder { 

    if ((self = [super initWithCoder:coder])) { 
     // Get the layer 
     CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 

     eaglLayer.opaque = YES; 
     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; 
     } 
    } 
    return self; 
} 

- (void)drawView 
{ 
    [EAGLContext setCurrentContext:context]; 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glViewport(0, 0, ScreenWidth, ScreenHeight); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    // Setup the coordinate system to use 0,0 as the lower left corner 
    // and 320,480 as the upper right corner of the screen (in portrait mode). 
    glOrthof(0.0f, ScreenWidth, 0.0f, ScreenHeight, -1.0f, 1.0f); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Here is where you draw everything in your world. 
    DrawWorld(); 

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 


- (void)layoutSubviews { 
    [EAGLContext setCurrentContext:context]; 
    [self destroyFramebuffer]; 
    [self createFramebuffer]; 
    [self drawView]; 
} 


- (BOOL)createFramebuffer { 

    glGenFramebuffersOES(1, &viewFramebuffer); 
    glGenRenderbuffersOES(1, &viewRenderbuffer); 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; 
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); 

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &ScreenWidth); 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &ScreenHeight); 

    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; 
} 

- (void)dealloc { 

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

    [context release]; 
    [super dealloc]; 
} 

@end 

作爲替代,this article創建iPhone上的OpenGL ES的應用程序提供了一個很好的演練。

0

對於iPhone上的[OpenGL] ES,與在Mac上使用Open GL相比,樣板代碼中的任何內容都會有所不同?

是的。在Mac上,您可以使用NSOpenGLView,它是Application Kit的一部分。 iPhone沒有AppKit。

我不能肯定地說更多,因爲我不是iPhone開發者,但我想你會在iPhone上使用EAGL