Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created September 24, 2010 00:35
Show Gist options
  • Save DylanLukes/594675 to your computer and use it in GitHub Desktop.
Save DylanLukes/594675 to your computer and use it in GitHub Desktop.
//
// CNWorldView+Drawing.m
// Concorde
//
// Created by Dylan Lukes on 9/22/10.
// Copyright 2010 Dylan Lukes. All rights reserved.
//
#import "CNWorldView+Drawing.h"
#import "Common.h"
#import <OpenGL/gl.h>
#import <GLUT/GLUT.h>
@implementation CNWorldView (Drawing)
- (void)setupOpenGL{
GLfloat width = [self bounds].size.width;
GLfloat height = [self bounds].size.height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2*(width/height), 2*(width/height),
-2.0f, 2.0f,
-2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
- (CVReturn)drawWithGL{
static const Vertex3D vertices[] = {
{-1.0f, 0.0f, -1.0f},
{-1.0f, 0.0f, 1.0f},
{ 1.0f, 0.0f, -1.0f},
{ 1.0f, 0.0f, 1.0f},
};
[[self openGLContext] makeCurrentContext];
// Reset matrices
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Perform isometric axis rotations
glRotatef(ISOANGLE, 1.0f, 0.0f, 0.0f); // around X 45°
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f); // around Y 45° + rotation offset
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(0.41, 0.66, 0.25, 1.0);
glVertexPointer(3, GL_FLOAT, 0, vertices);
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
drawAxes();
glColor4f(0.41, 0.66, 0.25, 1.0);
glutWireCube(1);
glDisableClientState(GL_VERTEX_ARRAY);
glSwapAPPLE();
return kCVReturnSuccess;
}
@end
//
// CNWorldView.m
// Concorde
//
// Created by Dylan Lukes on 9/21/10.
// Copyright 2010 Dylan Lukes. All rights reserved.
//
#import "CNWorldView.h"
#import "CNWorldView+Drawing.h"
#import "Common.h"
@interface CNWorldView (PrivateMethods)
static CVReturn CNDisplayLinkCallback(CVDisplayLinkRef, const CVTimeStamp*, const CVTimeStamp*,
CVOptionFlags, CVOptionFlags*, void* displayLinkContext);
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
@end
@implementation CNWorldView
- (void)prepareOpenGL{
// Syncronixe buffer swaps with v-refresh rate
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
// Create a display link
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
// Set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &CNDisplayLinkCallback, self);
// Set the display link for the current rendered
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
// Activate the display link
CVDisplayLinkStart(displayLink);
[self setupOpenGL];
NSLog(@"%f %f", ISOANGLE, M_PI);
}
static CVReturn CNDisplayLinkCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags* flagsOut,
void* displayLinkContext){
CVReturn result = [(CNWorldView*)displayLinkContext getFrameForTime:outputTime];
return result;
}
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime{
return [self drawWithGL];
}
- (void)mouseDown:(NSEvent *)theEvent{
NSLog(@"Mouse click!");
}
- (void)dealloc{
CVDisplayLinkRelease(displayLink);
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment