Advertisement
programmerbro

MyOpenGLView.m

Dec 23rd, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "MyOpenGLView.h"
  2.  
  3. @implementation MyOpenGLView
  4.  
  5. - (id)initWithFrame: (NSRect)frameRect pixelFormat: (NSOpenGLPixelFormat*)format;
  6. {
  7.     self = [super initWithFrame:frameRect];
  8.     if (self) {
  9.         _pixelFormat = [format retain];
  10.        
  11.         [[NSNotificationCenter defaultCenter] addObserver: self
  12.                                                  selector: @selector(_surfaceNeedsUpdate:)
  13.                                                      name: NSViewGlobalFrameDidChangeNotification
  14.                                                    object: self];
  15.     }
  16.    
  17.     return self;
  18. }
  19.  
  20. - (NSOpenGLContext*) openGLContext
  21. {
  22.     return _openGLContext;
  23. }
  24.  
  25. - (void) _surfaceNeedsUpdate: (NSNotificationCenter*)notification
  26. {
  27.     [self update];
  28. }
  29.  
  30. - (void) lockFocus
  31. {
  32.     NSOpenGLContext* context = [self openGLContext];
  33.    
  34.     [super lockFocus];
  35.     if ([context view] != self)
  36.     {
  37.         [context setView: self];
  38.     }
  39.     [context makeCurrentContext];
  40. }
  41.  
  42. - (void) drawRect
  43. {
  44.     NSOpenGLContext* context = [self openGLContext];
  45.  
  46.     [context makeCurrentContext];
  47.    
  48.     glClearColor(0, 0, 0, 0);
  49.     glClear(GL_COLOR_BUFFER_BIT);
  50.     glColor3f(1.0f, 0.85f, 0.35f);
  51.     glBegin(GL_TRIANGLES);
  52.     {
  53.         glVertex3f(  0.0,  0.6, 0.0);
  54.         glVertex3f( -0.2, -0.3, 0.0);
  55.         glVertex3f(  0.2, -0.3 ,0.0);
  56.     }
  57.     glEnd();
  58.     glFlush();
  59.    
  60.     [context flushBuffer];
  61. }
  62.  
  63. - (void) viewDidMoveToWindow
  64. {
  65.     NSOpenGLContext* context = [self openGLContext];
  66.  
  67.     [super viewDidMoveToWindow];
  68.     if ([self window] == nil)
  69.         [context clearDrawable];
  70. }
  71.  
  72. - (void) update
  73. {
  74.     [self drawRect];
  75. }
  76.  
  77. - (NSOpenGLPixelFormat*) pixelFormat
  78. {
  79.     return _pixelFormat;
  80. }
  81.  
  82. - (void)dealloc
  83. {
  84.     [super dealloc];
  85. }
  86.  
  87. - (void)drawRect:(NSRect)dirtyRect
  88. {
  89.     // Drawing code here.
  90. }
  91.  
  92. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement