Hi Forum,
I use Objective-C with CVDisplayLink to create the Loop.
But I get an empty window with the following code:
#import "OpenGLView.h"
#import "ShaderManager.h"
@interface OpenGLView()
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
@end
static const GLfloat vertexBufferData[] =
{
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0
};
@implementation OpenGLView
- (void)prepareOpenGL
{
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *) self);
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
CVDisplayLinkStart(displayLink);
// Init GL
NSRect rect = self.bounds;
glClearColor(0.0, 0.0, 0.0, 1.0);
glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
// Init Buffers
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
// Init Shader
manager = [[ShaderManager alloc] init];
if (![manager createShader:@"MyShader" vertexShader:[[NSBundle mainBundle] pathForResource:@"SimpleVertexShader" ofType:@"glsl"] fragmentShader:[[NSBundle mainBundle]pathForResource:@"SimpleFragmentShader" ofType:@"glsl"]])
{
NSLog(@"Fehler beim Shader laden.");
}
}
- (void)reshape
{
NSRect rect = self.bounds;
glViewport(0, 0, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
gluPerspective(45.0, rect.size.width / rect.size.height, 1.0, 5.0);
}
- (void)renderScene
{
[[self openGLContext] makeCurrentContext];
CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
// Render - Start
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[manager useShader:@"MyShader"];
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
// Render - Ende
CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]);
CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]);
}
static CVReturn MyDisplayLinkCallback(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
Yes the Shader are loaded correctly.