2009-05-23 4 views
0

2Dゲームを描くためにOpenGLビュー(必要な投影、カメラアングルなど)を設定するために必要な最小限のボイラープレートコードは何ですか?OpenGL 2D Viewのボイラープレートコードは何ですか?

例えば、カスタムビューの描画石英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 

ボイラープレートのコードは、Mac上でOpen GLを使用するのとは異なり、iPhone上のOpen GS ESとは何か異なりますか?

+0

ああ、ゲームライブラリ、cocos2Dなどのための推奨はしないでください。 OpenGLだけで答えが欲しい。 – Hejazzman

答えて

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

MacでOpen GLを使用するのではなく、iPhoneの[OpenGL] ESでは何か違いはありますか?

はい。 Macでは、Application Kitの一部であるNSOpenGLViewを使用します。 iPhoneにAppKitはありません。

私はiPhoneの開発者ではないので、もっと言い表せませんが、iPhoneでEAGLを使用すると思います。