-
Notifications
You must be signed in to change notification settings - Fork 2
/
v002MasterPluginInterface.m
201 lines (160 loc) · 5.05 KB
/
v002MasterPluginInterface.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
//
// v002MasterPluginInterface.m
// v002Blurs
//
// Created by vade on 4/7/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "v002MasterPluginInterface.h"
#import <OpenGL/CGLMacro.h>
@implementation v002_PLUGIN_CLASS_NAME_REPLACE_ME
@synthesize shaderUniformBlock;
@synthesize pluginShaderName;
- (void) finalize
{
self.pluginShaderName = nil;
[super finalize];
}
- (void)dealloc
{
self.pluginShaderName = nil;
// Just in case stopExecution wasn't called:
[pluginShader release];
[pluginFBO release];
[super dealloc];
}
- (v002FBO*) pluginFBO
{
return pluginFBO;
}
- (v002Shader*) pluginShader
{
return pluginShader;
}
@end
@implementation v002_PLUGIN_CLASS_NAME_REPLACE_ME (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
CGLContextObj cgl_ctx = [context CGLContextObj];
// shader loading
if([pluginShaderName length]) // do we have a name? if not dont bother.
{
NSBundle *pluginBundle =[NSBundle bundleForClass:[self class]];
pluginShader = [[v002Shader alloc] initWithShadersInBundle:pluginBundle withName:self.pluginShaderName forContext:cgl_ctx];
if(pluginShader == nil)
{
[context logMessage:@"Cannot compile GLSL shader."];
return NO;
}
}
pluginFBO = [[v002FBO alloc] initWithContext:cgl_ctx];
if(pluginFBO == nil)
{
[context logMessage:@"Cannot create FBO"];
return NO;
}
return YES;
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
[pluginShader release];
pluginShader = nil;
[pluginFBO release];
pluginFBO = nil;
}
#pragma mark - Helper Methods
- (BOOL) boundImageIsFloatingPoint:(id<QCPlugInInputImageSource>)image inContext:(CGLContextObj)cgl_ctx;
{
// Deduce the bit depth of the input image, so we can appropriately output a lossless image
GLint result;
glGetTexLevelParameteriv([image textureTarget], 0, GL_TEXTURE_INTERNAL_FORMAT, &result);
BOOL useFloat = (result == GL_RGBA32F_ARB) ? YES : NO;
return useFloat;
}
- (NSString*) pixelFormatIfUsingFloat:(BOOL)useFloat;
{
#if __BIG_ENDIAN__
#define v002QCPluginPixelFormat QCPlugInPixelFormatARGB8
#else
#define v002QCPluginPixelFormat QCPlugInPixelFormatBGRA8
#endif
return (useFloat) ? QCPlugInPixelFormatRGBAf : v002QCPluginPixelFormat;
}
- (GLuint) singleImageRenderWithContext:(CGLContextObj)cgl_ctx image:(id<QCPlugInInputImageSource>)image useFloat:(BOOL)useFloat
{
GLsizei width = [image imageBounds].size.width;
GLsizei height = [image imageBounds].size.height;
[pluginFBO pushAttributes:cgl_ctx];
glEnable(GL_TEXTURE_RECTANGLE_EXT);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, tex);
if(useFloat)
{
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA32F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
}
else
{
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
[pluginFBO pushFBO:cgl_ctx];
[pluginFBO attachFBO:cgl_ctx withTexture:tex width:width height:height];
glColor4f(1.0, 1.0, 1.0, 1.0);
glEnable([image textureTarget]);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, [image textureName]);
glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// do not need blending if we use black border for alpha and replace env mode, saves a buffer wipe
// we can do this since our image draws over the complete surface of the FBO, no pixel goes untouched.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glDisable(GL_BLEND);
// old alpha compositing code for reference
// glClearColor(0.0, 0.0, 0.0, 0.0);
// glClear(GL_COLOR_BUFFER_BIT);
// glEnable(GL_BLEND);
// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// bind our shader program
glUseProgramObjectARB([pluginShader programObject]);
// setup our shaders!
if(self.shaderUniformBlock)
{
self.shaderUniformBlock(cgl_ctx, self, image);
}
else
{
// some error or some shit
}
// move to VA for rendering
GLfloat tex_coords[] =
{
1.0,1.0,
0.0,1.0,
0.0,0.0,
1.0,0.0
};
GLfloat verts[] =
{
width,height,
0.0,height,
0.0,0.0,
width,0.0
};
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(2, GL_FLOAT, 0, tex_coords );
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, verts );
glDrawArrays( GL_TRIANGLE_FAN, 0, 4 ); // TODO: GL_QUADS or GL_TRIANGLE_FAN?
if(useFloat)
{
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_TRUE);
}
// disable shader program
glUseProgramObjectARB(NULL);
[pluginFBO detachFBO:cgl_ctx];
[pluginFBO popFBO:cgl_ctx];
[pluginFBO popAttributes:cgl_ctx];
return tex;
}
@end