/ QuartzCore / CAWindowOpenGLContext.m
CAWindowOpenGLContext.m
  1  #import <Onyx2D/O2Surface.h>
  2  #import <OpenGL/OpenGL.h>
  3  #import <QuartzCore/CAWindowOpenGLContext.h>
  4  
  5  @implementation CAWindowOpenGLContext
  6  
  7  - initWithCGLContext: (CGLContextObj) cglContext {
  8      _cglContext = CGLRetainContext(cglContext);
  9      return self;
 10  }
 11  
 12  - (void) dealloc {
 13      CGLReleaseContext(_cglContext);
 14      [super dealloc];
 15  }
 16  
 17  - (void) prepareViewportWidth: (int) width height: (int) height {
 18      // prepare
 19      CGLError error;
 20  
 21      if ((error = CGLSetCurrentContext(_cglContext)) != kCGLNoError)
 22          NSLog(@"CGLSetCurrentContext failed with %d in %s %d", error, __FILE__,
 23                __LINE__);
 24  
 25      glEnable(GL_DEPTH_TEST);
 26      glShadeModel(GL_SMOOTH);
 27  
 28      // reshape
 29      glViewport(0, 0, width, height);
 30      glMatrixMode(GL_PROJECTION);
 31      glLoadIdentity();
 32      glOrtho(0, width, 0, height, -1, 1);
 33  }
 34  
 35  - (void) renderSurface: (O2Surface *) surface {
 36      size_t width = O2ImageGetWidth(surface);
 37      size_t height = O2ImageGetHeight(surface);
 38  
 39      // prepare
 40      glEnable(GL_DEPTH_TEST);
 41      glShadeModel(GL_SMOOTH);
 42  
 43      // reshape
 44      glViewport(0, 0, width, height);
 45      glMatrixMode(GL_PROJECTION);
 46      glLoadIdentity();
 47      glOrtho(0, width, 0, height, -1, 1);
 48  
 49      // render
 50      glMatrixMode(GL_MODELVIEW);
 51      glLoadIdentity();
 52  
 53      glClearColor(0, 0, 0, 0);
 54      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 55  
 56      glEnable(GL_TEXTURE_2D);
 57      glEnableClientState(GL_VERTEX_ARRAY);
 58      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 59  
 60      glEnable(GL_BLEND);
 61      glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 62  
 63      width = O2ImageGetWidth(surface);
 64      height = O2ImageGetHeight(surface);
 65  
 66      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA,
 67                   GL_UNSIGNED_INT_8_8_8_8_REV, [surface pixelBytes]);
 68  
 69      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 70      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 71      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 72      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 73      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 74  
 75      GLfloat vertices[4 * 2];
 76      GLfloat texture[4 * 2];
 77  
 78      vertices[0] = 0;
 79      vertices[1] = 0;
 80      vertices[2] = width;
 81      vertices[3] = 0;
 82      vertices[4] = 0;
 83      vertices[5] = height;
 84      vertices[6] = width;
 85      vertices[7] = height;
 86  
 87      texture[0] = 0;
 88      texture[1] = 1;
 89      texture[2] = 1;
 90      texture[3] = 1;
 91      texture[4] = 0;
 92      texture[5] = 0;
 93      texture[6] = 1;
 94      texture[7] = 0;
 95  
 96      glPushMatrix();
 97      //  glTranslatef(width/2,height/2,0);
 98      glTexCoordPointer(2, GL_FLOAT, 0, texture);
 99      glVertexPointer(2, GL_FLOAT, 0, vertices);
100      //  glTranslatef(center.x,center.y,0);
101      //  glRotatef(1,0,0,1);
102      glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
103      glPopMatrix();
104  
105      glFlush();
106  }
107  
108  @end