2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// OpenGLView.m
|
|
|
|
// ElectrEm
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OpenGLView.h"
|
|
|
|
@import CoreVideo;
|
2015-07-24 00:45:07 +00:00
|
|
|
#import <OpenGL/gl.h>
|
2015-07-17 00:40:46 +00:00
|
|
|
|
|
|
|
@implementation CSOpenGLView {
|
|
|
|
CVDisplayLinkRef displayLink;
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
- (void)prepareOpenGL
|
2015-07-17 00:40:46 +00:00
|
|
|
{
|
2015-07-17 01:16:21 +00:00
|
|
|
// Synchronize buffer swaps with vertical refresh rate
|
|
|
|
GLint swapInt = 1;
|
|
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Create a display link capable of being used with all active displays
|
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
|
|
|
|
|
|
|
|
// Set the renderer output callback function
|
2015-07-24 00:45:07 +00:00
|
|
|
CVDisplayLinkSetOutputCallback(displayLink, DisplayLinkCallback, (__bridge void * __nullable)(self));
|
2015-07-17 01:16:21 +00:00
|
|
|
|
|
|
|
// Set the display link for the current renderer
|
|
|
|
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
|
|
|
|
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
|
|
|
|
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
|
|
|
|
|
|
|
|
// Activate the display link
|
|
|
|
CVDisplayLinkStart(displayLink);
|
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
|
2015-07-17 01:16:21 +00:00
|
|
|
{
|
2015-07-17 02:14:40 +00:00
|
|
|
CSOpenGLView *view = (__bridge CSOpenGLView *)displayLinkContext;
|
|
|
|
[view.delegate openGLView:view didUpdateToTime:*now];
|
2015-07-17 01:16:21 +00:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// Release the display link
|
|
|
|
CVDisplayLinkRelease(displayLink);
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 02:51:53 +00:00
|
|
|
- (void)reshape
|
|
|
|
{
|
|
|
|
[super reshape];
|
|
|
|
|
2015-07-25 03:42:19 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
NSPoint backingSize = {.x = self.bounds.size.width, .y = self.bounds.size.height};
|
|
|
|
NSPoint viewSize = [self convertPointToBacking:backingSize];
|
|
|
|
glViewport(0, 0, viewSize.x, viewSize.y);
|
2015-07-24 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
|
|
|
{
|
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
|
|
|
|
[self.delegate openGLViewDrawView:self];
|
|
|
|
|
|
|
|
glSwapAPPLE();
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:51:53 +00:00
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[] =
|
|
|
|
{
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// When we're using a CoreProfile context, crash if we call a legacy OpenGL function
|
|
|
|
// This will make it much more obvious where and when such a function call is made so
|
|
|
|
// that we can remove such calls.
|
|
|
|
// Without this we'd simply get GL_INVALID_OPERATION error for calling legacy functions
|
|
|
|
// but it would be more difficult to see where that function was called.
|
|
|
|
CGLEnable([context CGLContextObj], kCGLCECrashOnRemovedFunctions);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
self.pixelFormat = pixelFormat;
|
|
|
|
self.openGLContext = context;
|
|
|
|
self.wantsBestResolutionOpenGLSurface = YES;
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|