Hi Forum,
i try to set the Pixel Format for my NSOpenGLView.
But I only get a white screen.
#import <Cocoa/Cocoa.h>
#import <OpenGL/gl.h>
#import <OpenGL/OpenGL.h>
#import <QuartzCore/CVDisplayLink.h>
@interface OpenGLView : NSOpenGLView
{
CVDisplayLinkRef dlDisplayLink;
}
#import "OpenGLView.h"
@interface OpenGLView()
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
@end
@implementation OpenGLView
- (void) awakeFromNib
{
NSOpenGLPixelFormatAttribute pfaAttributes[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFADepthSize, 24, NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, 0};
@autoreleasepool
{
NSOpenGLPixelFormat *pfPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pfaAttributes];
NSOpenGLContext* glcContext = [[NSOpenGLContext alloc] initWithFormat:pfPixelFormat shareContext:nil];
[self setPixelFormat:pfPixelFormat];
[self setOpenGLContext:glcContext];
}
}
- (void)prepareOpenGL
{
[super prepareOpenGL];
[[self openGLContext] makeCurrentContext];
GLint iSwap = 1;
[[self openGLContext] setValues:&iSwap forParameter:NSOpenGLCPSwapInterval];
glClearColor(0.0, 0.0, 1.0, 1.0); // Bild blau
glViewport(0, 0, (GLsizei) self.bounds.size.width, (GLsizei) self.bounds.size.height);
[[self openGLContext] setValues:&iSwap forParameter:NSOpenGLCPSwapInterval];
CVDisplayLinkCreateWithActiveCGDisplays(&dlDisplayLink);
CVDisplayLinkSetOutputCallback(dlDisplayLink, &DisplayLinkCallback, (__bridge void *) self);
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(dlDisplayLink, cglContext, cglPixelFormat);
CVDisplayLinkStart(dlDisplayLink);
}
- (void)reshape
{
[super reshape];
CGLLockContext([[self openGLContext] CGLContextObj]);
glViewport(0, 0, (GLsizei) self.bounds.size.width, (GLsizei) self.bounds.size.height);
CGLUnlockContext([[self openGLContext] CGLContextObj]);
}
- (void)renderScene
{
[[self openGLContext] makeCurrentContext];
CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
// Render - Start
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
// Render - Ende
CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
}
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void *displayLinkContext)
{
return [(__bridge OpenGLView *)displayLinkContext getFrameForTime:outputTime];
}
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
{
@autoreleasepool
{
[self renderScene];
}
return kCVReturnSuccess;
}
@end
The screen should be blue. glCloarColor(0.0, 0.0, 1.0, 1.0)