OpenGLES2DView.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:2k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  OpenGLES2DView.m
  3. //  GLFun
  4. //
  5. //  Created by Jeff LaMarche on 8/5/08.
  6. //  Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "OpenGLES2DView.h"
  9. @implementation OpenGLES2DView
  10. + (Class) layerClass
  11. {
  12. return [CAEAGLLayer class];
  13. }
  14. #pragma mark -
  15. - (BOOL)createFramebuffer {
  16. glGenFramebuffersOES(1, &viewFramebuffer);
  17. glGenRenderbuffersOES(1, &viewRenderbuffer);
  18. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  19. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  20. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
  21. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  22. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  23. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  24. if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  25. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  26. return NO;
  27. }
  28. return YES;
  29. }
  30. - (id)initWithCoder:(NSCoder*)coder
  31. {
  32. if((self = [super initWithCoder:coder])) {
  33. // Get the layer
  34. CAEAGLLayer *eaglLayer = (CAEAGLLayer*) self.layer;
  35. eaglLayer.opaque = YES;
  36. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  37. [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat, nil];
  38. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  39. if(!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer]) {
  40. [self release];
  41. return nil;
  42. }
  43. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  44. glViewport(0, 0, backingWidth, backingHeight);
  45. glMatrixMode(GL_PROJECTION);
  46. glLoadIdentity();
  47. glOrthof(0, self.frame.size.width, 0, self.frame.size.height, -1, 1);
  48. glMatrixMode(GL_MODELVIEW);
  49. glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. glEnableClientState (GL_VERTEX_ARRAY);
  52. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  53. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  54. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  55. glEnable(GL_TEXTURE_2D);
  56. glEnableClientState(GL_VERTEX_ARRAY);
  57. }
  58. return self;
  59. }
  60. - (void)dealloc {
  61. [super dealloc];
  62. }
  63. @end