-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv002FBOGlitchPlugIn.m
425 lines (339 loc) · 13.8 KB
/
v002FBOGlitchPlugIn.m
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// v002FBOGLSLTemplatePlugIn.m
// v002FBOGLSLTemplate
//
// Created by vade on 6/30/08.
// Copyright (c) 2008 __MyCompanyName__. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "v002FBOGlitchPlugIn.h"
#define kQCPlugIn_Name @"v002 Glitch FBO"
#define kQCPlugIn_Description @"Creates Glitches by rendering a Frame Buffer Object without properly clearing/flushing, resulting in capturing video memory"
#define kRGBTextureTypes [NSArray arrayWithObjects: [NSNumber numberWithInt:GL_INTENSITY], [NSNumber numberWithInt:GL_INTENSITY4], [NSNumber numberWithInt:GL_INTENSITY8], [NSNumber numberWithInt:GL_INTENSITY12], [NSNumber numberWithInt:GL_INTENSITY16], [NSNumber numberWithInt:GL_R3_G3_B2], [NSNumber numberWithInt:GL_RGB4], [NSNumber numberWithInt:GL_RGB5], [NSNumber numberWithInt:GL_RGB8], [NSNumber numberWithInt:GL_RGB10], [NSNumber numberWithInt:GL_RGB12], [NSNumber numberWithInt:GL_RGB16], [NSNumber numberWithInt:GL_RGB16F_ARB], [NSNumber numberWithInt:GL_RGBA2], [NSNumber numberWithInt:GL_RGBA4], [NSNumber numberWithInt:GL_RGB5_A1], [NSNumber numberWithInt:GL_RGBA8], [NSNumber numberWithInt:GL_RGB10_A2], [NSNumber numberWithInt:GL_RGBA12], [NSNumber numberWithInt:GL_RGBA16], [NSNumber numberWithInt:GL_RGBA16F_ARB], nil]
#define kRGBTextureTypesString [NSArray arrayWithObjects:@"GL_INTENSITY", @"GL_INTENSITY4", @"GL_INTENSITY8", @"GL_INTENSITY12", @"GL_INTENSITY16",@"GL_R3_G3_B2" @"GL_RGB4", @"GL_RGB5", @"GL_RGB8",@"GL_RGB10", @"GL_RGB12", @"GL_RGB16", @"GL_RGB16F_ARB", @"GL_RGBA2", @"GL_RGBA4", @"GL_RGB5_A1", @"GL_RGBA8", @"GL_RGB10_A2", @"GL_RGBA12", @"GL_RGBA16", @"GL_RGBA16F_ARB", nil]
#pragma mark -
#pragma mark Static Functions
static void _TextureReleaseCallback(CGLContextObj cgl_ctx, GLuint name, void* info)
{
glDeleteTextures(1, &name);
}
// our render setup
//static GLuint renderToFBO(CGLContextObj cgl_ctx, NSUInteger pixelsWide, NSUInteger pixelsHigh, NSRect bounds)
static GLuint fboGlitchCreate(GLuint frameBuffer,CGLContextObj cgl_ctx, NSUInteger pixelsWide, NSUInteger pixelsHigh, NSRect bounds, BOOL newRandomValues, int rgbType)
{
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glPushAttrib(GL_ALL_ATTRIB_BITS);
GLint previousFBO, previousReadFBO, previousDrawFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &previousFBO);
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &previousReadFBO);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &previousDrawFBO);
static int i = 0;
static int lastRandom = 0;
// int format;
int type;
NSUInteger rand;
if(newRandomValues)
{
// NSLog(@"new rand value");
srandom(i % 10000);
rand = random();
lastRandom = rand;
++i;
// type = (rgba) ? (rand % ([kRGBATextureTypes count])) : (rand % ([kRGBTextureTypes count]));
type = rand % [kRGBTextureTypes count];
// format = (rgba) ? (rand % ([kRGBAPackedPixelInternalFormats count])) : (rand % ([kRGBPackedPixelInternalFormats count]));
}
else
{
// rand = lastRandom;
type = rgbType;
}
GLsizei width = bounds.size.width, height = bounds.size.height;
GLuint name;
GLenum status;
GLenum rgbOrRGBA;
// pick GL_RGB vs GL_RGBA
if (type > 4)
rgbOrRGBA = GL_LUMINANCE;
else if(type > 12)
rgbOrRGBA = GL_RGBA;
else
rgbOrRGBA = GL_RGB;
// Create texture to render into
glGenTextures(1, &name);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, name);
glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, [[kRGBTextureTypes objectAtIndex:type] intValue] , width, height, 0, rgbOrRGBA, GL_UNSIGNED_BYTE, NULL);
// bind our FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
// attach our just created texture
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, name, 0);
// Assume FBOs JUST WORK, because we checked on startExecution
// status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
// if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
// return to parent FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, previousFBO);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, previousReadFBO);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, previousDrawFBO);
// Check for OpenGL errors
status = glGetError();
if(status)
{
NSLog(@"OpenGL error %04X", status);
glDeleteTextures(1, &name);
name = 0;
}
glPopAttrib();
glPopClientAttrib();
return name;
}
@implementation v002FBOGlitchPlugIn
/*
Here you need to declare the input / output properties as dynamic as Quartz Composer will handle their implementation
@dynamic inputFoo, outputBar;
*/
@dynamic inputWidth, inputHeight, inputNewRandomValues, inputInternalTypeRGB, inputNumberOfFBOs, outputImage;
+ (NSDictionary*) attributes
{
/*
Return a dictionary of attributes describing the plug-in (QCPlugInAttributeNameKey, QCPlugInAttributeDescriptionKey...).
*/
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
if([key isEqualToString:@"inputNumberOfFBOs"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"FBO Count", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"inputWidth"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Width", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"inputHeight"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Height", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"inputNewRandomValues"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Random", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputInternalTypeRGB"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Texture Format", QCPortAttributeNameKey,
[NSArray arrayWithArray:kRGBTextureTypesString], QCPortAttributeMenuItemsKey,
[NSNumber numberWithUnsignedInteger:0.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:[kRGBTextureTypesString count] - 1], QCPortAttributeMaximumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"outputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
return nil;
}
+ (NSArray*) sortedPropertyPortKeys
{
return [NSArray arrayWithObjects:@"inputNumberOfFBOs", @"inputWidth", @"inputHeight", @"inputInternalTypeRGB", @"inputNewRandomValues", nil];
}
+ (QCPlugInExecutionMode) executionMode
{
/*
Return the execution mode of the plug-in: kQCPlugInExecutionModeProvider, kQCPlugInExecutionModeProcessor, or kQCPlugInExecutionModeConsumer.
*/
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
/*
Return the time dependency mode of the plug-in: kQCPlugInTimeModeNone, kQCPlugInTimeModeIdle or kQCPlugInTimeModeTimeBase.
*/
return kQCPlugInTimeModeIdle;
}
- (id) init
{
self = [super init];
if(self)
{
fboArray = [[NSMutableArray alloc] init];
cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
currentFBO = 0;
/*
Allocate any permanent resource required by the plug-in.
*/
}
return self;
}
- (void) finalize
{
/*
Release any non garbage collected resources created in -init.
*/
[super finalize];
}
- (void) dealloc
{
/*
Release any resources created in -init.
*/
[fboArray release];
CGColorSpaceRelease(cspace);
[super dealloc];
}
@end
@implementation v002FBOGlitchPlugIn (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
currentFBO = 0;
// work around lack of GLMacro.h for now
CGLContextObj cgl_ctx = [context CGLContextObj];
//#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
// CGLGetParameter(ctx, kCGLCPSupportSeparateAddressSpace, &supportsSeparateAddressSpace);
// GLint supportsSeparateAddressSpace = 0;
// CGLSetParameter(cgl_ctx, kCGLCPSupportSeparateAddressSpace, &supportsSeparateAddressSpace);
CGLDisable(cgl_ctx, kCGLCPSupportSeparateAddressSpace);
//#endif
// build up and destroy an FBO. If it works, we are good to go and dont do any other slow error checking for our main rendering,
// if we cant make the FBO, fail by returning NO.
GLuint name;
GLenum status;
GLuint tempFBO;
// state saving for currently bound FBO
GLint previousFBO, previousReadFBO, previousDrawFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &previousFBO);
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &previousReadFBO);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &previousDrawFBO);
glGenTextures(1, &name);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, name);
glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create temporary FBO to render in texture
glGenFramebuffersEXT(1, &tempFBO);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, tempFBO);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, name, 0);
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
NSLog(@"Cannot create FBO");
// return to parent FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, previousFBO);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, previousReadFBO);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, previousDrawFBO);
glDeleteFramebuffersEXT(1, &tempFBO);
glDeleteTextures(1, &name);
return NO;
}
// cleanup
// return to parent FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, previousFBO);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, previousReadFBO);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, previousDrawFBO);
glDeleteTextures(1, &name);
glDeleteFramebuffersEXT(1, &tempFBO);
return YES;
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
CGLContextObj cgl_ctx = [context CGLContextObj];
if([self didValueForInputKeyChange:@"inputNumberOfFBOs"])
{
if(self.inputNumberOfFBOs <= 0)
self.inputNumberOfFBOs = 1;
// lock access to fboArray to prevent dumb shit from happening.
@synchronized(fboArray)
{
// reset our counter
currentFBO = 0;
// remove all old FBOs
for(NSNumber* fbo in fboArray)
{
GLuint tempFBOiD = [fbo unsignedIntValue];
glDeleteFramebuffersEXT(1, &tempFBOiD);
}
[fboArray removeAllObjects];
// create a set of new FBOs to use based on the number we need
for(int i = 0; i < self.inputNumberOfFBOs; i++)
{
GLuint tempFBOiD;
GLenum status;
glGenFramebuffersEXT(1, &tempFBOiD);
// check to make sure this worked...
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
// yo dawg, I put a buffer in your buffer, so you can glitch when you glitch...
[fboArray addObject:[NSNumber numberWithUnsignedInt:tempFBOiD]];
}
else
{
glDeleteFramebuffersEXT(1, &tempFBOiD);
}
}
}
}
NSUInteger width = self.inputWidth;
NSUInteger height = self.inputHeight;
NSRect bounds = NSMakeRect(0.0, 0.0, width, height);
GLuint finalOutput;
id provider = nil;
// make sure fboArray is locked or dumb shit will happen
@synchronized(fboArray)
{
// set our current FBO we want for this frame
currentFBO++;
if([fboArray count])
{
if(currentFBO % [fboArray count] == 0)
currentFBO = 0;
//NSLog(@"using frameBuffer: %i", currentFBO);
finalOutput = fboGlitchCreate([[fboArray objectAtIndex:currentFBO] unsignedIntValue] ,cgl_ctx, width, height, bounds, self.inputNewRandomValues, self.inputInternalTypeRGB);
glFlushRenderAPPLE();
}
}
if(finalOutput != 0)
{
// output our final image as a QCPluginOutputImageProvider using the QCPluginContext convinience method. No need to go through the trouble of making our own conforming object.
provider = [context outputImageProviderFromTextureWithPixelFormat:QCPlugInPixelFormatBGRA8 pixelsWide:bounds.size.width pixelsHigh:bounds.size.height name:finalOutput flipped:YES releaseCallback:_TextureReleaseCallback releaseContext:NULL colorSpace:[context colorSpace] shouldColorMatch:YES];
// #if __BIG_ENDIAN__
// provider = [context outputImageProviderFromTextureWithPixelFormat:QCPlugInPixelFormatARGB8 pixelsWide:bounds.size.width pixelsHigh:bounds.size.height name:finalOutput flipped:NO releaseCallback:_TextureReleaseCallback releaseContext:NULL colorSpace:CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear) shouldColorMatch:YES];
// #else
// provider = [context outputImageProviderFromTextureWithPixelFormat:QCPlugInPixelFormatBGRA8 pixelsWide:bounds.size.width pixelsHigh:bounds.size.height name:finalOutput flipped:YES releaseCallback:_TextureReleaseCallback releaseContext:NULL colorSpace:CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear) shouldColorMatch:YES];
if(provider == nil)
{
return NO;
}
}
self.outputImage = provider;
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
CGLContextObj cgl_ctx = [context CGLContextObj];
// remove our GLSL program
@synchronized(fboArray)
{
for(NSNumber* fbo in fboArray)
{
GLuint tempFBOiD = [fbo unsignedIntValue];
glDeleteFramebuffersEXT(1, &tempFBOiD);
}
[fboArray removeAllObjects];
}
glslProgramObject = NULL;
}
@end