OpenGL 2D View的最小样板代码是什么?

时间:2009-05-23 00:35:26

标签: iphone cocoa opengl graphics boilerplate

为绘制2D游戏设置OpenGL视图(使用必要的投影,摄像机角度等)所需的最小样板代码是什么?

例如,在自定义视图中(例如,加载背景图像)进行Quartz 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吗?

2 个答案:

答案 0 :(得分: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应用程序的一个很好的演练。

答案 1 :(得分:0)

  

对于iPhone上的[OpenGL] ES而言,样板代码中的任何内容都不同,而不是在Mac上使用Open GL吗?

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

我不能说肯定是因为我不是iPhone开发者,但我猜你会在iPhone上使用EAGL