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

发布于 2024-07-21 00:51:18 字数 1164 浏览 8 评论 0原文

设置用于绘制 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?

What's the minimum boilerplate code required to setup an OpenGL view (with the necessary projections,camera angles etc) for drawing a 2D game?

For example, the minimum required to do Quartz 2D drawing in a custom view (and, say, load a background image) is the following:

#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

Would anything in the boilerplate code be different for Open GS ES on the iPhone as opposed to using Open GL on a Mac?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱本泡沫多脆弱 2024-07-28 00:51:18

在 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

作为替代方案,本文提供了在 iPhone 上创建 OpenGL ES 应用程序的精彩演练。

The easiest way to setup an OpenGL application on the iPhone is to create a "OpenGL ES Application" through Xcode. It generates the boilerplate source code you'll need to get started.

Here is the boilerplate source I am using for an OpenGL iPhone game:

@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

As an alternative, this article provides a nice walkthrough in creating an OpenGL ES application on the iPhone.

恬淡成诗 2024-07-28 00:51:18

与在 Mac 上使用 OpenGL 相比,iPhone 上的 [OpenGL] ES 的样板代码中是否有任何不同?

是的。 在 Mac 上,您可以使用 NSOpenGLView,它是应用程序工具包的一部分。 iPhone 没有 AppKit。

我不能肯定地说更多,因为我不是 iPhone 开发人员,但我猜你会使用 EAGL

Would anything in the boilerplate code be different for [OpenGL] ES on the iPhone as opposed to using Open GL on a Mac?

Yes. On a Mac, you'd use NSOpenGLView, which is part of Application Kit. The iPhone doesn't have AppKit.

I can't say more for sure because I'm not an iPhone developer, but I would guess that you'll use EAGL on the iPhone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文