-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLOpenGLView.m
More file actions
240 lines (185 loc) · 5.79 KB
/
MLOpenGLView.m
File metadata and controls
240 lines (185 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#import "MLOpenGLView.h"
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
@interface MLOpenGLView (Private)
- (void)removeObserverIfNecessary;
- (void)surfaceNeedsUpdate:(NSNotification *)notification;
@end
@implementation MLOpenGLView
+ (NSOpenGLPixelFormat *)defaultPixelFormat
{
NSOpenGLPixelFormatAttribute attribs[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFANoRecovery,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) 32,
NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) 8,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) 32,
NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) 8,
(NSOpenGLPixelFormatAttribute) 0
};
return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs] autorelease];
}
- (void)dealloc
{
[self removeObserverIfNecessary];
[self clearOpenGLContext];
[pixelFormat release];
[super dealloc];
}
- (void)removeObserverIfNecessary
{
if (addedObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewGlobalFrameDidChangeNotification object:self];
addedObserver = NO;
}
}
- (void)clearOpenGLContext
{
if (openGLContext) {
if ([openGLContext view] == self)
[openGLContext clearDrawable];
[openGLContext release];
openGLContext = nil;
}
sharingOpenGLContext = nil;
}
- (void)setOpenGLContext:(NSOpenGLContext *)context
{
[self clearOpenGLContext];
openGLContext = [context retain];
[self allocateGState];
if (! addedObserver) {
addedObserver = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(surfaceNeedsUpdate:) name:NSViewGlobalFrameDidChangeNotification object:self];
}
}
- (NSOpenGLContext *)openGLContext
{
return openGLContext;
}
- (NSOpenGLContext *)openGLContextCreateIfNecessary
{
if (! openGLContext) {
if (! [self createOpenGLContext])
return nil;
[openGLContext makeCurrentContext];
}
return openGLContext;
}
- (NSOpenGLContext *)createOpenGLContext
{
return [self createOpenGLContextShareContext:nil];
}
- (NSOpenGLContext *)createOpenGLContextShareContext:(NSOpenGLContext *)shareContext
{
[self clearOpenGLContext];
NSOpenGLPixelFormat *usePixelFormat = [self pixelFormat];
if (! usePixelFormat)
return nil;
NSOpenGLContext *newOpenGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:shareContext];
if (! newOpenGLContext)
return nil;
[self setOpenGLContext:newOpenGLContext];
sharingOpenGLContext = shareContext;
[newOpenGLContext release]; // We've already retained it by calling setOpenGLContext.
return newOpenGLContext;
}
- (NSOpenGLContext *)createPixelFormatAndOpenGLContextWithProperties:(const MLOpenGLViewProperties *)properties shareContext:(NSOpenGLContext *)shareContext
{
int msaa = properties->msaa;
if (msaa < 1)
msaa = 1;
for (; msaa; --msaa) {
static const NSOpenGLPixelFormatAttribute endMarker = (NSOpenGLPixelFormatAttribute) 0;
NSOpenGLPixelFormatAttribute attribs[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFANoRecovery,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) properties->colourBits,
NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) properties->alphaBits,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) properties->depthBits,
NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) properties->stencilBits,
msaa > 1 ? NSOpenGLPFAMultisample : endMarker,
NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute) 1,
NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute) msaa,
endMarker
};
NSOpenGLPixelFormat *newPixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs] autorelease];
if (! newPixelFormat)
continue;
[self setPixelFormat:newPixelFormat];
NSOpenGLContext *newContext = [self createOpenGLContextShareContext:shareContext];
if (newContext)
return newContext;
}
return nil;
}
- (void)setPixelFormat:(NSOpenGLPixelFormat *)newPixelFormat
{
[newPixelFormat retain];
[pixelFormat release];
pixelFormat = newPixelFormat;
}
- (NSOpenGLPixelFormat *)pixelFormat
{
if (! pixelFormat)
return [self createPixelFormat];
return pixelFormat;
}
- (NSOpenGLPixelFormat *)createPixelFormat
{
pixelFormat = [[[self class] defaultPixelFormat] retain];
return pixelFormat;
}
- (void)getPixelFormatProperties:(MLOpenGLViewProperties *)properties
{
GLint gotMSAA = 0;
[pixelFormat getValues:&gotMSAA forAttribute:NSOpenGLPFASamples forVirtualScreen:0];
properties->msaa = (int) gotMSAA;
GLint gotColour;
[pixelFormat getValues:&gotColour forAttribute:NSOpenGLPFAColorSize forVirtualScreen:0];
properties->colourBits = (int) gotColour;
GLint gotAlpha;
[pixelFormat getValues:&gotAlpha forAttribute:NSOpenGLPFAColorSize forVirtualScreen:0];
properties->alphaBits = (int) gotAlpha;
GLint gotDepth;
[pixelFormat getValues:&gotDepth forAttribute:NSOpenGLPFADepthSize forVirtualScreen:0];
properties->depthBits = (int) gotDepth;
GLint gotStencil;
[pixelFormat getValues:&gotStencil forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
properties->stencilBits = (int) gotStencil;
}
- (void)lockFocus
{
NSOpenGLContext *context = [self openGLContextCreateIfNecessary];
[super lockFocus];
if ([context view] != self)
[context setView:self];
[context makeCurrentContext];
}
- (void)surfaceNeedsUpdate:(NSNotification *)notification
{
[self update];
}
- (void)update
{
if ([openGLContext view] == self)
[openGLContext update];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSRect bounds = [self bounds];
[[self openGLContextCreateIfNecessary] makeCurrentContext];
glViewport(0, 0, (GLint) bounds.size.width, (GLint) bounds.size.height);
// If you get orange then you know MLOpenGLView's drawRect is being called.
glClearColor(1.0f, 0.5f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
[self flushBuffer];
}
- (void)flushBuffer
{
glFlush();
[[self openGLContext] flushBuffer];
}
@end