1

I am trying out OpenGL 3.2 using a Cocoa window and an NSOpenGLView. However I can't get the NSOpenGLView to draw a red color. I just get a white window. Here is my code (inside subclass of NSOpenGLView):

-(void)awakeFromNib{

NSOpenGLPixelFormatAttribute attrs[] =
{
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFADepthSize, 24,
    // Must specify the 3.2 Core Profile to use OpenGL 3.2
    NSOpenGLPFAOpenGLProfile,
    NSOpenGLProfileVersion3_2Core,
    0
};

NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];

if (!pf)
{
    NSLog(@"No OpenGL pixel format");
}


NSOpenGLContext *context = [[[NSOpenGLContext alloc]initWithFormat:pf shareContext:nil]autorelease];

[self setPixelFormat:pf];
[self setOpenGLContext:context];


 }


 - (id)initWithFrame:(NSRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code here.
}

return self;
 }

 - (void)drawRect:(NSRect)dirtyRect
 {

glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

 }

The code should simply clear the view to red. No errors or warnings -- just a white window.

genpfault
  • 51,148
  • 11
  • 85
  • 139
foobar5512
  • 2,470
  • 5
  • 36
  • 52
  • Try to insert some NSLog to `drawRect:`. May be it doesn't get called at all? May be you should tell the OpenGL view to redraw itself? – cody Jul 23 '13 at 06:18
  • I've added an NSLog() to drawRect: and it does end up getting called, so I'm still not sure what's broken. – foobar5512 Jul 23 '13 at 14:56

2 Answers2

1

I needed to swap the buffers using glSwapAPPLE() after my call to glClear()

I read somewhere that NSOpenGLViews don't need to be swapped because it is done automatically. After my experience I can say that is completely untrue.

foobar5512
  • 2,470
  • 5
  • 36
  • 52
  • I've managed to do this in the past with simply flushing the context. It is neccessary in the end of your draw code for opengl to draw anything. I don't see you using any flush or swap method in the end of your drawrect code , hence why adding glswapapple worked. Try adding glFlush(); it should work as well. – apoiat Jul 25 '13 at 10:33
  • Is your view set up to use double-buffering? Then from my understanding you need to swap. Otherwise you won't. – cacau Aug 26 '15 at 11:13
0

You should use CGLFlushDrawable(). Like blow:

CGLFlushDrawable([[self openGLContext] CGLContextObj]);
BrianChen
  • 183
  • 10