2015-07-17 00:40:46 +00:00
|
|
|
//
|
2015-07-26 19:25:11 +00:00
|
|
|
// CSCathodeRayView.m
|
|
|
|
// CLK
|
2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#import "CSCathodeRayView.h"
|
2015-07-17 00:40:46 +00:00
|
|
|
@import CoreVideo;
|
2015-07-26 19:13:46 +00:00
|
|
|
#import <OpenGL/gl3.h>
|
|
|
|
#import <OpenGL/gl3ext.h>
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-08-02 18:25:21 +00:00
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
@implementation CSCathodeRayView {
|
2015-07-17 00:40:46 +00:00
|
|
|
CVDisplayLinkRef displayLink;
|
2015-07-26 19:13:46 +00:00
|
|
|
|
|
|
|
GLuint _vertexShader, _fragmentShader;
|
|
|
|
GLuint _shaderProgram;
|
|
|
|
GLuint _arrayBuffer, _vertexArray;
|
|
|
|
GLint _positionAttribute;
|
2015-07-26 19:25:11 +00:00
|
|
|
GLint _textureCoordinatesAttribute;
|
2015-08-02 18:25:21 +00:00
|
|
|
GLint _lateralAttribute;
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
GLuint _textureName;
|
2015-07-28 00:58:51 +00:00
|
|
|
CRTSize _textureSize;
|
2015-07-31 21:47:10 +00:00
|
|
|
|
|
|
|
CRTFrame *_crtFrame;
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
|
|
|
// get the shader ready, set the clear colour
|
2015-07-26 19:25:11 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-07-26 19:55:19 +00:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
2015-07-26 19:13:46 +00:00
|
|
|
[self prepareShader];
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Activate the display link
|
|
|
|
CVDisplayLinkStart(displayLink);
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
2015-07-31 21:47:10 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
2015-07-17 01:16:21 +00:00
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-27 03:50:43 +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-26 19:13:46 +00:00
|
|
|
CSCathodeRayView *view = (__bridge CSCathodeRayView *)displayLinkContext;
|
2015-07-17 02:14:40 +00:00
|
|
|
[view.delegate openGLView:view didUpdateToTime:*now];
|
2015-07-17 01:16:21 +00:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
2015-07-28 01:15:10 +00:00
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
CVDisplayLinkStop(displayLink);
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// Release the display link
|
|
|
|
CVDisplayLinkRelease(displayLink);
|
2015-07-28 01:15:10 +00:00
|
|
|
|
|
|
|
// Release OpenGL buffers
|
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
glDeleteBuffers(1, &_arrayBuffer);
|
|
|
|
glDeleteVertexArrays(1, &_vertexArray);
|
|
|
|
glDeleteTextures(1, &_textureName);
|
|
|
|
glDeleteProgram(_shaderProgram);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[] =
|
|
|
|
{
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
2015-07-27 20:57:11 +00:00
|
|
|
NSOpenGLPFASampleBuffers, 1,
|
|
|
|
NSOpenGLPFASamples, 16,
|
2015-07-24 02:51:53 +00:00
|
|
|
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-31 21:47:10 +00:00
|
|
|
|
|
|
|
- (BOOL)pushFrame:(CRTFrame * __nonnull)crtFrame
|
2015-07-26 19:13:46 +00:00
|
|
|
{
|
2015-07-31 22:04:33 +00:00
|
|
|
BOOL hadFrame = _crtFrame ? YES : NO;
|
2015-07-26 19:13:46 +00:00
|
|
|
_crtFrame = crtFrame;
|
|
|
|
[self setNeedsDisplay:YES];
|
2015-07-26 19:55:19 +00:00
|
|
|
|
2015-07-27 03:50:43 +00:00
|
|
|
if(crtFrame)
|
|
|
|
{
|
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-08-02 18:25:21 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, _crtFrame->number_of_runs * sizeof(GLushort) * kCRTSizeOfVertex * 6, _crtFrame->runs, GL_DYNAMIC_DRAW);
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _textureName);
|
2015-07-28 00:58:51 +00:00
|
|
|
|
|
|
|
if(_textureSize.width != _crtFrame->size.width || _textureSize.height != _crtFrame->size.height)
|
|
|
|
{
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _crtFrame->size.width, _crtFrame->size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, _crtFrame->buffers[0].data);
|
|
|
|
_textureSize = _crtFrame->size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _crtFrame->size.width, _crtFrame->dirty_size.height, GL_RGBA, GL_UNSIGNED_BYTE, _crtFrame->buffers[0].data);
|
2015-07-27 03:50:43 +00:00
|
|
|
}
|
2015-07-31 21:47:10 +00:00
|
|
|
|
2015-07-31 22:04:33 +00:00
|
|
|
return hadFrame;
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Frame output
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
// the main job of the vertex shader is just to map from an input area of [0,1]x[0,1], with the origin in the
|
|
|
|
// top left to OpenGL's [-1,1]x[-1,1] with the origin in the lower left, and to convert input data coordinates
|
|
|
|
// from integral to floating point.
|
2015-07-26 19:13:46 +00:00
|
|
|
const char *vertexShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec2 position;\n"
|
2015-07-26 19:25:11 +00:00
|
|
|
"in vec2 srcCoordinates;\n"
|
2015-08-02 18:32:29 +00:00
|
|
|
"in float lateral;\n"
|
2015-07-26 19:13:46 +00:00
|
|
|
"\n"
|
2015-07-26 19:25:11 +00:00
|
|
|
"out vec2 srcCoordinatesVarying;\n"
|
2015-08-02 18:32:29 +00:00
|
|
|
"out float lateralVarying;\n"
|
2015-07-26 19:13:46 +00:00
|
|
|
"\n"
|
|
|
|
"void main (void)\n"
|
|
|
|
"{\n"
|
2015-08-02 18:32:29 +00:00
|
|
|
"srcCoordinatesVarying = vec2(srcCoordinates.x / 512.0, (srcCoordinates.y + 0.5) / 512.0);\n"
|
|
|
|
"lateralVarying = lateral * 2.09435310266667 + 0.52359877566668;"
|
2015-07-27 20:44:47 +00:00
|
|
|
"gl_Position = vec4(position.x * 2.0 - 1.0, 1.0 - position.y * 2.0 + position.x / 131.0, 0.0, 1.0);\n"
|
2015-07-26 19:13:46 +00:00
|
|
|
"}\n";
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
// TODO: this should be factored out and be per project
|
2015-07-26 19:13:46 +00:00
|
|
|
const char *fragmentShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
2015-07-26 19:25:11 +00:00
|
|
|
"in vec2 srcCoordinatesVarying;\n"
|
2015-08-02 18:32:29 +00:00
|
|
|
"in float lateralVarying;"
|
2015-07-26 19:13:46 +00:00
|
|
|
"out vec4 fragColour;\n"
|
2015-07-27 03:50:43 +00:00
|
|
|
"uniform sampler2D texID;\n"
|
2015-07-26 19:13:46 +00:00
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
2015-08-02 18:32:29 +00:00
|
|
|
"fragColour = texture(texID, srcCoordinatesVarying) * vec4(1.0, 1.0, 1.0, sin(lateralVarying));\n"
|
2015-07-26 19:13:46 +00:00
|
|
|
"}\n";
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
- (void)logErrorForObject:(GLuint)object
|
|
|
|
{
|
|
|
|
GLint logLength;
|
|
|
|
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &logLength);
|
|
|
|
if (logLength > 0) {
|
|
|
|
GLchar *log = (GLchar *)malloc(logLength);
|
|
|
|
glGetShaderInfoLog(object, logLength, &logLength, log);
|
|
|
|
NSLog(@"Compile log:\n%s", log);
|
|
|
|
free(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
- (GLuint)compileShader:(const char *)source type:(GLenum)type
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
[self logErrorForObject:shader];
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prepareShader
|
|
|
|
{
|
|
|
|
_shaderProgram = glCreateProgram();
|
|
|
|
_vertexShader = [self compileShader:vertexShader type:GL_VERTEX_SHADER];
|
|
|
|
_fragmentShader = [self compileShader:fragmentShader type:GL_FRAGMENT_SHADER];
|
|
|
|
|
|
|
|
glAttachShader(_shaderProgram, _vertexShader);
|
|
|
|
glAttachShader(_shaderProgram, _fragmentShader);
|
|
|
|
glLinkProgram(_shaderProgram);
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
[self logErrorForObject:_shaderProgram];
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
glGenVertexArrays(1, &_vertexArray);
|
|
|
|
glBindVertexArray(_vertexArray);
|
|
|
|
glGenBuffers(1, &_arrayBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _arrayBuffer);
|
|
|
|
|
|
|
|
glUseProgram(_shaderProgram);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
_positionAttribute = glGetAttribLocation(_shaderProgram, "position");
|
2015-07-26 19:25:11 +00:00
|
|
|
_textureCoordinatesAttribute = glGetAttribLocation(_shaderProgram, "srcCoordinates");
|
2015-08-02 18:25:21 +00:00
|
|
|
_lateralAttribute = glGetAttribLocation(_shaderProgram, "lateral");
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
glEnableVertexAttribArray(_positionAttribute);
|
2015-07-26 19:25:11 +00:00
|
|
|
glEnableVertexAttribArray(_textureCoordinatesAttribute);
|
2015-08-02 18:25:21 +00:00
|
|
|
glEnableVertexAttribArray(_lateralAttribute);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-08-02 18:25:21 +00:00
|
|
|
const GLsizei vertexStride = kCRTSizeOfVertex * sizeof(GLushort);
|
|
|
|
glVertexAttribPointer(_positionAttribute, 2, GL_UNSIGNED_SHORT, GL_TRUE, vertexStride, (void *)(kCRTVertexOffsetOfPosition * sizeof(GLushort)));
|
|
|
|
glVertexAttribPointer(_textureCoordinatesAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)(kCRTVertexOffsetOfTexCoord * sizeof(GLushort)));
|
|
|
|
glVertexAttribPointer(_lateralAttribute, 1, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)(kCRTVertexOffsetOfLateral * sizeof(GLushort)));
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
glGenTextures(1, &_textureName);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _textureName);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
|
|
|
{
|
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
if (_crtFrame)
|
|
|
|
{
|
2015-07-27 03:50:43 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, _crtFrame->number_of_runs*6);
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glSwapAPPLE();
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|